问题描述
我正在尝试使用CodeBlocks和SDL编写一个简单的程序,该程序每秒更改一个矩形的高度。到目前为止,我的代码是这样的:
'''
#include <SDL2/SDL.h>
#include <stdio.h>
#include <iostream>
#include <string>
int main(int argv,char** args)
{
SDL_Window *p;
SDL_Renderer *w;
p = SDL_CreateWindow("Temp",SDL_WINDOWPOS_UNDEFINED,800,640,SDL_WINDOW_SHOWN);
w = SDL_CreateRenderer(p,-1,SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(w,255,255);
SDL_Rect Rect = {220,140,200,200};
// int rectX = 220;
// int rectY = 140;
// int width = 200;
// int height = 200;
bool isRunning = true;
SDL_Event event;
int lastTime = 0;
int currentTime = 0;
while(isRunning)
{
// time stuff
currentTime = SDL_GetTicks();
int timeDifference = currentTime - lastTime;
if(currentTime > lastTime + 1000)
{
lastTime = currentTime;
height = 200;
}
// Check if window closes
while ( SDL_PollEvent(&event))
{
if ( event.type == SDL_QUIT)
{
isRunning = false;
}
if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)
{
isRunning = false;
}
}
// rectangle stuff
SDL_SetRenderDrawColor(w,255);
int newHeight = height + timeDifference / 4;
Rect = {220,newHeight};
SDL_RenderFillRect(w,&Rect);
SDL_RenderPresent(w);
SDL_Delay(10);
}
SDL_DestroyRenderer(w);
SDL_DestroyWindow(p);
SDL_Quit();
return 0;
}
'''
每次循环时,以前制作的矩形仍然存在。另外,我尝试了SDL_RenderClear
,但这样做时,整个屏幕都变成红色。我该如何解决?我希望它每次都刷新。
此外,我知道前面的矩形仍然存在,因为我尝试了使用有线矩形,并且这种情况更加明显。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)