Cannot use SDL2 variables in block area

I program SDL2, but I can not understand the reason for the following. It works:

SDL_Window  *window;
SDL_Surface *screen_surface;
SDL_Surface *picture;

auto initWindow(void)
{…}
auto loadMedia(void)
{…}
auto close(void)
{…}

int main(void)
{
    initWindow();
    loadMedia();
    close();
}

However, it is not:

auto initWindow(SDL_Window *window, SDL_Surface *screen_surface)
{…}
auto loadMedia(SDL_Surface *picture, std::string resource)
{…}
auto close(SDL_Window *window, SDL_Surface *picture)
{…}

int main(void)
{
    SDL_Window  *window;
    SDL_Surface *screen_surface;
    SDL_Surface *picture;
    initWindow(window, screen_surface);
    loadMedia(picture, "resource_file.bmp");
    close(window, picture);
}

The only difference is that I'm taking window, screen_surfaceand pictureout of scope file and put it into the unit (ie, the main function), and instead of referring to global variables within these functions I use parameters. However, when I try to run this, it displays a white screen but does not display any errors. I do not understand what is wrong here.

0
source share
1 answer

: SDL, , , , .

, initWindow window. , , .

, . , :

auto initWindow(SDL_Window *window, SDL_Surface *screen_surface)
{
    window = SDL_GetWindow(); /* or something */
}

int main(void)
{
    SDL_Window  *window;
    SDL_Surface *screen_surface;
    initWindow(window, screen_surface);
    /* some other code that uses 'window' */
}

window initWindow SDL_GetWindow. window main : , main, , undefined. , , SDL_GetWindow. initWindow window, window main.

, ++, initWindow window, :

auto initWindow(SDL_Window *&window, SDL_Surface *&screen_surface)
{
    window = SDL_GetWindow(); /* or something */
}

int main(void)
{
    SDL_Window  *window;
    SDL_Surface *screen_surface;
    initWindow(window, screen_surface);
    /* some other code that uses 'window' */
}

window main ​​ initWindow , , window main, , SDL_GetWindow.

++ , , RAII ( ). ++- SDL, , , , , std::unique_ptr ( std::shared_ptr, , ). .

+3

Source: https://habr.com/ru/post/1629747/


All Articles