消失的 SDL 矩形。如何使用新的附加形状更新 window
Dissapearing SDL Rects. How to update window with new additional shapes
我试图在按下按键时用一个新的附加矩形更新 window,但由于 SDL_RenderClear
,它一直在消失。是否建议删除 SDL_RenderClear
?
while (!quit) {
while (SDL_PollEvent( & e) != 0) {
if (e.type == SDL_QUIT)
quit = true;
}
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer); //if i remove this line, the new rectangle will remain there
SDL_Rect fillRect = {
0,
0,
SCREEN_WIDTH / 5 - 5,
SCREEN_HEIGHT / 5 - 5
};
SDL_SetRenderDrawColor(gRenderer, 255, 205, 51, 0xFF);
SDL_RenderFillRect(gRenderer, & fillRect);
switch (e.type) {
case SDL_KEYDOWN:
if (e.key.keysym.sym == SDLK_RIGHT) {
SDL_Rect fillRect = {
0,
200,
SCREEN_WIDTH / 5 - 5,
SCREEN_HEIGHT / 5 - 5
};
SDL_SetRenderDrawColor(gRenderer, 255, 205, 51, 0xFF);
SDL_RenderFillRect(gRenderer, & fillRect);
}
break;
}
SDL_RenderPresent(gRenderer);
}
SDL_RenderClear() 非常好,实际上您应该使用它。您的问题(又名矩形消失)是由您处理输入的方式引起的。 SDL_KEYDOWN 是一个事件,它只发生在您按下某个键的帧上,并且在短暂延迟后按住它。如果在那个确切的帧上按下了键,而不是在之前的任何帧上按下了键,那么您正在做的就是绘制矩形。通过 bool 的解决方案可能如下所示:
bool keyPressed = false;
while( !quit ) {
while( SDL_PollEvent( &e ) != 0 ) {
if( e.type == SDL_QUIT )
quit = true;
}
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer ); //if i remove this line, the new rectangle will remain there
SDL_Rect fillRect = { 0, 0, SCREEN_WIDTH / 5-5, SCREEN_HEIGHT / 5-5 };
SDL_SetRenderDrawColor( gRenderer, 255, 205, 51, 0xFF );
SDL_RenderFillRect( gRenderer, &fillRect );
if(keyPressed){
SDL_Rect fillRect = { 0, 200, SCREEN_WIDTH / 5-5, SCREEN_HEIGHT / 5-5 };
SDL_SetRenderDrawColor( gRenderer, 255, 205, 51, 0xFF );
SDL_RenderFillRect( gRenderer, &fillRect );
}
switch(e.type){
case SDL_KEYDOWN:
if(e.key.keysym.sym==SDLK_RIGHT){
keyPressed = true;
}
break;
}
SDL_RenderPresent( gRenderer );
}
在图形中绝对应该清屏,否则您在之前帧中绘制的形状将保留在那里。
我试图在按下按键时用一个新的附加矩形更新 window,但由于 SDL_RenderClear
,它一直在消失。是否建议删除 SDL_RenderClear
?
while (!quit) {
while (SDL_PollEvent( & e) != 0) {
if (e.type == SDL_QUIT)
quit = true;
}
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer); //if i remove this line, the new rectangle will remain there
SDL_Rect fillRect = {
0,
0,
SCREEN_WIDTH / 5 - 5,
SCREEN_HEIGHT / 5 - 5
};
SDL_SetRenderDrawColor(gRenderer, 255, 205, 51, 0xFF);
SDL_RenderFillRect(gRenderer, & fillRect);
switch (e.type) {
case SDL_KEYDOWN:
if (e.key.keysym.sym == SDLK_RIGHT) {
SDL_Rect fillRect = {
0,
200,
SCREEN_WIDTH / 5 - 5,
SCREEN_HEIGHT / 5 - 5
};
SDL_SetRenderDrawColor(gRenderer, 255, 205, 51, 0xFF);
SDL_RenderFillRect(gRenderer, & fillRect);
}
break;
}
SDL_RenderPresent(gRenderer);
}
SDL_RenderClear() 非常好,实际上您应该使用它。您的问题(又名矩形消失)是由您处理输入的方式引起的。 SDL_KEYDOWN 是一个事件,它只发生在您按下某个键的帧上,并且在短暂延迟后按住它。如果在那个确切的帧上按下了键,而不是在之前的任何帧上按下了键,那么您正在做的就是绘制矩形。通过 bool 的解决方案可能如下所示:
bool keyPressed = false;
while( !quit ) {
while( SDL_PollEvent( &e ) != 0 ) {
if( e.type == SDL_QUIT )
quit = true;
}
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer ); //if i remove this line, the new rectangle will remain there
SDL_Rect fillRect = { 0, 0, SCREEN_WIDTH / 5-5, SCREEN_HEIGHT / 5-5 };
SDL_SetRenderDrawColor( gRenderer, 255, 205, 51, 0xFF );
SDL_RenderFillRect( gRenderer, &fillRect );
if(keyPressed){
SDL_Rect fillRect = { 0, 200, SCREEN_WIDTH / 5-5, SCREEN_HEIGHT / 5-5 };
SDL_SetRenderDrawColor( gRenderer, 255, 205, 51, 0xFF );
SDL_RenderFillRect( gRenderer, &fillRect );
}
switch(e.type){
case SDL_KEYDOWN:
if(e.key.keysym.sym==SDLK_RIGHT){
keyPressed = true;
}
break;
}
SDL_RenderPresent( gRenderer );
}
在图形中绝对应该清屏,否则您在之前帧中绘制的形状将保留在那里。