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

Author Topic: sfRenderWindow as function argument  (Read 4804 times)

0 Members and 1 Guest are viewing this topic.

c-jay

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
sfRenderWindow as function argument
« on: April 26, 2016, 12:20:14 pm »
Hi,

this works fine:

#include <SFML/Graphics.h>
#define SCREENWIDTH 1000
#define SCREENHEIGHT 1000
#define BITSPERPIXEL 32

sfRenderWindow* sf_init()
{
        sfVideoMode mode =
                        { SCREENWIDTH, SCREENHEIGHT, BITSPERPIXEL };

        /* Create the main window */
        return sfRenderWindow_create(mode, "SFML window", sfResize | sfClose, NULL);

}

void main(){
       sfRenderWindow* window = sf_init();
}
 
But why doesn't work this:

#include <SFML/Graphics.h>
#define SCREENWIDTH 1000
#define SCREENHEIGHT 1000
#define BITSPERPIXEL 32

void sf_init(sfRenderWindow *window)
{
        sfVideoMode mode =
                        { SCREENWIDTH, SCREENHEIGHT, BITSPERPIXEL };

        /* Create the main window */
        window = sfRenderWindow_create(mode, "SFML window", sfResize | sfClose, NULL);

}
void main(){
        sfRenderWindow* window;
        sf_init(window);
}
 

Any ideas into where the problem lies? Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sfRenderWindow as function argument
« Reply #1 on: April 26, 2016, 12:29:18 pm »
You modify a local variable. To change the original pointer and not a copy of it, you must pass a pointer to it.

void sf_init(sfRenderWindow **window)
{
    sfVideoMode mode =
            { SCREENWIDTH, SCREENHEIGHT, BITSPERPIXEL };

    /* Create the main window */
    *window = sfRenderWindow_create(mode, "SFML window", sfResize | sfClose, NULL);

}
void main(){
    sfRenderWindow* window;
    sf_init(&window);
}

This is basic C stuff, nothing to do with SFML... ;)
Laurent Gomila - SFML developer

c-jay

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: sfRenderWindow as function argument
« Reply #2 on: April 26, 2016, 03:26:14 pm »
Okay, thank you very much for open my mind! :D