Пропустить фрагмент кода C++ SDL2

Я пишу функцию в C++ с библиотекой SDL2, и я застрял с небольшой проблемой. Функция имеет дело со всем, что связано с графикой. Я хочу использовать его один раз для создания окна, поверхности и т. Д. (Graphics (0,0)), и каждый раз, когда я использую его, он просто обновляет значения x и y и обновляет поверхность окна. Как мне это сделать? Я перепробовал все, что мог придумать, но чтобы обновить поверхность окна, мне нужно снова создать окно. Заранее спасибо.

void graficos(int var1,int var2){
    int x = 0, y = 0; //declare the variables that will determine position of rectangle
    x += var1; y += var2; //declare the variables that will modify x & y
    //delcare graphics and load them
    SDL_Window * window = SDL_CreateWindow("window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
    SDL_Surface * surface = SDL_GetWindowSurface(window);
    SDL_Surface * texture = SDL_LoadBMP("texture.bmp");
    SDL_Rect rectangle = { x, y, 50, 50 };
    //render graphics
    SDL_BlitSurface(texture, NULL, surface, &rectangle);
    //update window
    SDL_UpdateWindowSurface(window);
    }

1 ответ

Решение

Первый Вы можете разбить свою функцию на две части: init а также graficos как это:

void init( SDL_Window * &window, SDL_Surface * &texture ) {
    window = SDL_CreateWindow("window",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        640,
        480,
        0
    );
    texture = SDL_LoadBMP("texture.bmp");
}

void graficos( SDL_Window * window, SDL_Surface * texture, int var1, int var2 ) {
// You can delete these lines and use var1 and var2 directly
    /*    int x = 0, y = 0; //declare the variables that will determine position of rectangle
    x += var1; y += var2; //declare the variables that will modify x & y
    */
    SDL_Surface * surface = SDL_GetWindowSurface(window);
    SDL_Rect rectangle = { var1, var2, 50, 50 };
    //render graphics
    SDL_BlitSurface(texture, NULL, surface, &rectangle);
    //update window
    SDL_UpdateWindowSurface(window);
}

И вот использование:

// somewhere in global place
SDL_Window * window;
SDL_Surface * texture;

// somewhere in initialization of programm
init( window, texture ); // call it only ones
...

graficos( window, texture, newX, newY ); // call it every time you need

Второе Ты можешь сделать window а также texture как static как это:

void graficos(int var1,int var2){
    /*        int x = 0, y = 0; //declare the variables that will determine position of rectangle
    x += var1; y += var2; //declare the variables that will modify x & y
    */
    //delcare graphics and load them
// ---v---
    static SDL_Window * window = SDL_CreateWindow("window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
// ---^---
    SDL_Surface * surface = SDL_GetWindowSurface(window);
// ---v---
    static SDL_Surface * texture = SDL_LoadBMP("texture.bmp");
// ---^---
    SDL_Rect rectangle = { x, y, 50, 50 };
    //render graphics
    SDL_BlitSurface(texture, NULL, surface, &rectangle);
    //update window
    SDL_UpdateWindowSurface(window);
}

Если будет голосование, тогда мой голос будет первым:)

Другие вопросы по тегам