我希望在按下空格键时绘制图像。这是我的代码:
void drawImage()
{
rect.x = 100;
rect.y = 100;
SDL_Surface *image = IMG_Load("image.png");
SDL_BlitSurface(image, NULL, screen, &rect);
}
以下是:
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_SPACE)
{
drawImage();
}
}
}
按空格键时不会绘制图像。有什么问题?
答案 0 :(得分:2)
Flip屏幕缓冲区(SDL_Surfaces)。
答案 1 :(得分:1)
行后
SDL_BlitSurface(image, NULL, screen, &rect);
添加
SDL_UpdateRect(screen, 0, 0, 0, 0);
答案 2 :(得分:-1)
函数是否在主循环中绘制而没有按空格键?
bool draw = false;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_SPACE)
{
draw = true;
}
}
}
int main()
{
if(draw){
drawImage();
}
//DO SOME ERROR CHECKING
}