[SDL] Help with keyboard input and sprite movement

Started by
4 comments, last by xDancinpoptartx 11 years, 5 months ago
I'm new to SDL and I was wondering why this keyboard movement wasn't working.
[source lang="cpp"]#include "SDL/SDL.h"
#include <stdbool.h>
//SURFACES/variables
SDL_Surface *background = NULL;
SDL_Surface *buffer = NULL;
SDL_Surface *sprite = NULL;
int y = 250;
int x = 250;
//apply
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
//start
int main( int argc, char* args[] )
{
//init
SDL_Init( SDL_INIT_EVERYTHING );

background = SDL_LoadBMP("bg.bmp");
sprite = SDL_LoadBMP("char.bmp");
SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, SDL_MapRGB(sprite->format, 230, 0, 230));
SDL_Event event;
bool fullscreen = false;
bool done = false;
//loops
if (fullscreen == false )
{
buffer = SDL_SetVideoMode(500, 500, 32, SDL_SWSURFACE);
}
//APPNAME
SDL_WM_SetCaption("Game", NULL);
//apply surface/flips
apply_surface(0, 0, background, buffer);
apply_surface(x, y, sprite, buffer);
SDL_Flip(buffer);
//quit
while(!done)
{
while(SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_QUIT:
{
done = true;
break;
}
case SDL_KEYDOWN:
{
if(event.key.keysym.sym == SDLK_DOWN)
{
y = y+1;
}
}
}
}
}
//END
SDL_FreeSurface(background);
SDL_FreeSurface(sprite);
SDL_Quit();
return 0;
}
[/source]
Advertisement
You need to draw your scene every frame, not just draw it once in it's first state and then update state, but not drawing.
Place your drawing code inside the while loop (don't have experience with SDL so can't really say if within SDL_PollEvent while or (!done) while but I would say you should put your drawing code inside the !done while.

apply_surface(x, y, sprite, buffer);
SDL_Flip(buffer);
// ...
while ( !done )
{
// ...
y = y+1;
// ...
}


Think of it this way: Does changing the amount of sugar in a cake recipe change the sugar content for all cakes baked with the same recipe? Of course not; the cake needs to be re-baked every time its recipe changes.
(First of all, I say sorry for my english)

You just need to draw the scene. Put the apply_surface() function after the SDL_PollEvent while and will work (remember this logic order on games: update logic -> user input -> draw scene to the user).

Another thing is: you want the sprite moving while the key is pressed? or you just want to move him few pixels every time you press the key?
There's 2 ways to do this things:

The way you are doing, you will press the key, then the sprite y will be set to y+1. If you press the key, and keep it pressed, the sprite will not move more than 1 pixel until you press the key again.

The other way is use the function SDL_GetKeyState():


[source lang="cpp"]Uint8* keystate = SDL_GetKeyState(NULL);
if(keystate[SDLK_DOWN])
{
y = y+1;
}[/source]

The SDL_GetKeyState() function get a snapshot of the current keyboard state.
What's the difference? The SDL_KEYDOWN it's an event that just occurs when you press that key, and the way above, it's to check if the current key is pressed, if it's pressed, increments y. (This case will be true until you release the key)
So where do I put the apply_surface because i put it after the poll event like this
[source lang="cpp"]while (!done)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
done = true;
}
Uint8* keystate = SDL_GetKeyState(NULL);
if(keystate[SDLK_DOWN])
{
y = y+1;
}
}
apply_surface(x, y, sprite, screen);
}[/source]
Nvm i figured it out. Thanks for all the help!

This topic is closed to new replies.

Advertisement