绘制图像的功能



我希望在按空格键时绘制图像。这是我的代码:

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();
        }
    }
}

当我按空格键时,图像没有绘制。怎么了?

翻转屏幕缓冲区(SDL_Surfaces)

行后

SDL_BlitSurface(image, NULL, screen, &rect);

添加
SDL_UpdateRect(screen, 0, 0, 0, 0);

函数是否在主循环中没有空格键?

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
}

最新更新