Reputation: 663
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?
Upvotes: 0
Views: 135
Reputation:
After your line
SDL_BlitSurface(image, NULL, screen, &rect);
Add
SDL_UpdateRect(screen, 0, 0, 0, 0);
Upvotes: 1
Reputation: 192
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
}
Upvotes: -1