Search code examples
c++sdlsdl-1.2

Draw image in function


I want an image to be drawn when the space key is pressed. This is my code:

void drawImage()
{
   rect.x = 100;
   rect.y = 100;

   SDL_Surface *image = IMG_Load("image.png");
   SDL_BlitSurface(image, NULL, screen, &rect);
}

Here is it called:

while (SDL_PollEvent(&event))
{
    if (event.type == SDL_QUIT)
    {
        gameRunning = false;
    }
    if (event.type == SDL_KEYDOWN)
    {
        if (event.key.keysym.sym == SDLK_SPACE)
        {
            drawImage();
        }
    }

}

The image isn't drawn when I press the space key. What is wrong?


Solution

  • Flip the screen buffers (SDL_Surfaces).