-->

Displaying the rectangle using a for loop in SDL2

2019-09-21 22:54发布

问题:

I'm currently using a for loop to create 4 rectangles along the x-axis. When I run the program the image of the rectangle appears for a few millisecond before it disappears. Can you show me what I have to do in order for the image to display on the screen permanently.

When I create individual rectangles it display's the image without vanishing.

An example will be helpful. Thanks

int main(){
int barrack1_xposition = 167,i=1;
int cnt_barrack=0;
SDL_Window *o = NULL;
SDL_Renderer *r = NULL;
SDL_Rect bar1[4];
SDL_Event e;


SDL_Init(SDL_INIT_VIDEO);

o = SDL_CreateWindow("SPACE INVADERS",
                        SDL_WINDOWPOS_UNDEFINED,
                        SDL_WINDOWPOS_UNDEFINED,
                        1024,
                        800,
                        SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

r = SDL_CreateRenderer(o, -1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

while(i)
{
    while(SDL_PollEvent(&e) !=0)
    {
        if(e.type == SDL_QUIT)
            i=0;

   }

    for(cnt_barrack = 0; cnt_barrack < 4; cnt_barrack++)
    {
        bar1[cnt_barrack].x=barrack1_xposition;
        bar1[cnt_barrack].y=250;
        bar1[cnt_barrack].h=50;
        bar1[cnt_barrack].w=50;

        barrack1_xposition += 200;
    }

    SDL_SetRenderDrawColor(r,255,255,255,255);
    SDL_RenderFillRect(r,&bar1[0]);
    SDL_RenderFillRect(r,&bar1[1]);
    SDL_RenderFillRect(r,&bar1[2]);
    SDL_RenderFillRect(r,&bar1[3]);
    SDL_RenderPresent(r);   
}
SDL_DestroyWindow(o);
SDL_DestroyRenderer(r);;
SDL_Quit();
}

回答1:

Your problem caused by the fact you didn't reset barrack1_xposition on each frame, so it keeps going higher and higher. I initially didn't notice that because there was no RenderClear so it seemed to be fine but actually wasn't.

#include "SDL.h"

int main(){
    int barrack1_xposition,i=1;
    int cnt_barrack=0;
    SDL_Window *o = NULL;
    SDL_Renderer *r = NULL;
    SDL_Rect bar1[4];
    SDL_Event e;


    SDL_Init(SDL_INIT_VIDEO);

    o = SDL_CreateWindow("SPACE INVADERS",
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            1024,
            800,
            SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

    r = SDL_CreateRenderer(o, -1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    while(i)
    {
        while(SDL_PollEvent(&e) !=0)
        {
            if(e.type == SDL_QUIT)
                i=0;

        }

        barrack1_xposition=167;

        for(cnt_barrack = 0; cnt_barrack < 4; cnt_barrack++)
        {
            bar1[cnt_barrack].x=barrack1_xposition;
            bar1[cnt_barrack].y=250;
            bar1[cnt_barrack].h=50;
            bar1[cnt_barrack].w=50;

            barrack1_xposition += 200;
        }

        SDL_SetRenderDrawColor(r,0,0,0,0);
        SDL_RenderClear(r);

        SDL_SetRenderDrawColor(r,255,255,255,255);
        SDL_RenderFillRect(r,&bar1[0]);
        SDL_RenderFillRect(r,&bar1[1]);
        SDL_RenderFillRect(r,&bar1[2]);
        SDL_RenderFillRect(r,&bar1[3]);
        SDL_RenderPresent(r);   
    }
    SDL_DestroyWindow(o);
    SDL_DestroyRenderer(r);;
    SDL_Quit();

    return 0;
}