Domanda

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?

È stato utile?

Soluzione

Flip the screen buffers (SDL_Surfaces).

Altri suggerimenti

After your line

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

Add

SDL_UpdateRect(screen, 0, 0, 0, 0);

Does the function draws, in the main loop without the space pressing thing?

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

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top