我目前正在使用for循环沿x轴创建4个矩形。当我运行程序时,矩形图像会在消失前显示几毫秒。你能告诉我我必须做什么才能使图像永久显示在屏幕上。
当我创建单独的矩形时,它会显示图像而不会消失。
一个例子会有所帮助。感谢
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();
}
答案 0 :(得分:1)
您的问题是由于您未在每个帧上重置barrack1_xposition
而导致的问题,因此它会越来越高。我最初没有注意到,因为没有RenderClear
所以它似乎很好但实际上并没有。
#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;
}