Jump to content

  • Log In with Google      Sign In   
  • Create Account


[SDL] Help with keyboard input and sprite movement


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Dylan_DE   Members   -  Reputation: 131

Like
0Likes
Like

Posted 03 December 2012 - 01:34 PM

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/variablesSDL_Surface *background = NULL;SDL_Surface *buffer = NULL;SDL_Surface *sprite = NULL;int y = 250;int x = 250;//applyvoid 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);}//startint 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);//quitwhile(!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]

Sponsor:

#2 Bikonja   Members   -  Reputation: 134

Like
1Likes
Like

Posted 03 December 2012 - 01:38 PM

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.

#3 fastcall22   Crossbones+   -  Reputation: 4255

Like
0Likes
Like

Posted 03 December 2012 - 02:45 PM

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.

Edited by fastcall22, 03 December 2012 - 02:45 PM.

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#4 ptrrf   Members   -  Reputation: 378

Like
0Likes
Like

Posted 03 December 2012 - 04:45 PM

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

Edited by ptrrf, 04 December 2012 - 06:09 AM.


#5 Dylan_DE   Members   -  Reputation: 131

Like
0Likes
Like

Posted 05 December 2012 - 10:32 AM

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]

#6 Dylan_DE   Members   -  Reputation: 131

Like
0Likes
Like

Posted 05 December 2012 - 01:35 PM

Nvm i figured it out. Thanks for all the help!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS