Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: display a green ball  (Read 4239 times)

0 Members and 1 Guest are viewing this topic.

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
display a green ball
« on: August 10, 2013, 03:21:51 am »
Hello,
I'm kinda stuck with the tutorial example in the C::B installation. I want to reproduce it but with CSFML.
Here's the code:

#include <SFML/Graphics.h>

int main()
{
    sfContextSettings * settings;
    settings = malloc(sizeof(sfContextSettings));
    settings->antialiasingLevel = 8;
    sfVideoMode mode;
    mode.height = 200;
    mode.width = 200;
    sfRenderWindow * renderwindow;
    sfCircleShape * shape;
    renderwindow = sfRenderWindow_create(mode, "SFML works!", sfDefaultStyle ,settings);
    shape = sfCircleShape_create();
    sfCircleShape_setFillColor(shape, sfGreen);

    while (sfRenderWindow_isOpen(renderwindow))
    {
        sfEvent * event;
        event = malloc(sizeof(sfEvent));
        while (sfRenderWindow_pollEvent(renderwindow, event))
        {
            if (event == sfEvtClosed)
            {
                sfRenderWindow_close(renderwindow);
            }
        }
        //sfRenderStates * state;
        //state = malloc(sizeof(sfRenderStates));
        //state->blendMode = sfBlendNone;
        //state->shader = NULL;
        //state->texture = NULL.
        //state->transform = NULL;

        sfRenderWindow_clear(renderwindow, sfWhite);
        sfRenderWindow_drawCircleShape(renderwindow, shape, state);
        sfRenderWindow_display(renderwindow);
    }

    return 0;
}

The problem is that I do not know how to display a circle shape without having to make sfRenderSettings because I do not need shaders, textures, blendness or transform.
Also can you tell me if something is wrong in my code? it compiles and runs but crashes (make a black window, even if I change the sfBlack to sfWhite/Green/... ).
Thanks in advance.
« Last Edit: August 10, 2013, 12:12:24 pm by Engineer »

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: display a green ball
« Reply #1 on: August 10, 2013, 04:17:48 am »
First off, you should add code tags to make code more readable for people on the forums.

To answer your question, you can actually just pass NULL instead of a "default" sfRenderStates. This tells the SFML inside CSFML to use a default sf::RenderStates. And you can do the same with sfConvexSettings too when you create your window, by the way.

        sfRenderWindow_drawCircleShape(renderwindow, shape, NULL);

And just so you know, instead of allocating memory manually for a pointer, I would just create the object normally and then pass the address of said object into the function that wants it. For example,

 
while (sfRenderWindow_isOpen(renderwindow))
    {
        sfEvent event;
        while (sfRenderWindow_pollEvent(renderwindow, &event))
        {
             //get events
        }

        //do stuff
  }

Otherwise, you'll need to free that memory manually too. Since your current code doesn't, you've got leaks!
DSFML - SFML for the D Programming Language.

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: display a green ball
« Reply #2 on: August 10, 2013, 12:43:21 pm »
Yes, that's true, I forgot to free() and destroy()...
Here is the last version of the program as you recommend, with passing the addresses of objects

       

    #include <SFML/Graphics.h>

int main()
{
    sfVideoMode mode = {200, 200, 32};
    sfRenderWindow * renderwindow;
    sfCircleShape * shape;
    shape = sfCircleShape_create();
    sfCircleShape_setRadius(shape, 100.f);
    renderwindow = sfRenderWindow_create(mode, "SFML works!", sfDefaultStyle ,NULL);
    sfCircleShape_setFillColor(shape, sfGreen);

    while (sfRenderWindow_isOpen(renderwindow))
    {
        sfEvent event;
        while (sfRenderWindow_pollEvent(renderwindow, &event))
        {
            if (event.type == sfEvtClosed)
            {
                sfRenderWindow_close(renderwindow);
            }
        }

        sfRenderWindow_clear(renderwindow, sfBlack);
        sfRenderWindow_drawCircleShape(renderwindow, shape, NULL);
        sfRenderWindow_display(renderwindow);

    }

    sfCircleShape_destroy(shape);
    sfRenderWindow_destroy(renderwindow);

    return 0;
}
 

The program compiles with no warnings or errors but crashes. :'(

EDIT: Resolved it, there was some problem with the sfVideoMode. Enjoy. I'm sure it might help some people :)
« Last Edit: August 10, 2013, 01:35:22 pm by Engineer »

 

anything