Jump to content
  • Advertisement
Sign in to follow this  

SDL Question

This topic is 4122 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, i have been learning SDL with Lazy Foo's tutorials and now i am trying to make a simple game where the character can walk around. i have made it so the player moves when one of the arrow keys is pressed but you have to repeatedly press it. So my question is how do you make it so you just hold down the button? this is my code for drawing the player, background: EDIT: forgot to mention applySurface() is the function in the Lazy Foo tutorials for blitting the surfaces to the screen.
    while(quit != true)
        if (SDL_Flip(screen) == -1)
            return 1;
        applySurface(0, 0, background, screen);
        applySurface(playerX, playerY, player, screen);
            if(event.type == SDL_QUIT)
                // Quits the game
                quit = true;
            if(event.type == SDL_KEYDOWN)
                    case SDLK_UP:
                         playerY -= 6;
                    case SDLK_DOWN:
                         playerY += 6;
                    case SDLK_LEFT:
                         playerX -= 6;
                    case SDLK_RIGHT:
                         playerX += 6;
i don't know if this is simple but thanks in advance :D

Share this post

Link to post
Share on other sites
All of this is mentioned in tutorial 16. Don't just download the source code, the tutorials only work if you actually read them

Also read article 4 after you're done. Your game loop needs better structure.

Share this post

Link to post
Share on other sites
simple way of doing it - though not the most efficent

create 3 global ints

int xVel =0;
int yVel =0;
int speed =6;

in your KEYDOWN switch
have RIGHT
set xVel = speed;
set xVel = -speed;

in UP
set yVel = -speed;
set yVel = speed;

create another if
if(event.type == SDL_KEYUP)

use same switch
set the xVel and yVel back to 0

then under your input

move character based on xVel and yVel

pseudo code

//load stuff
//draw stuff
set x and y velocities to speed or -speed based on direction
set x and y velocities to zero based on need

//update character
character.xPosition += xVel;
character.yPosition += yVel;

Share this post

Link to post
Share on other sites
This way has always done the job for me

bool keys[323] = {false}; // among globals, holds keyboard buttons states
bool btns[3] = {false}; // among globals, holds mouse button states
SDL_Event event; // among globals, key to the users hands

void updateinput(); // among prototypes
void handleinput(); // among prototypes

int main (int argc, char*argv[])
init(); // initializes sub/systems
loadobjects(); // loads objs,graphics,fonts,sounds

while (app){ // main loop

ticks1 = SDL_GetTicks();

updateinput(); // the name is self-explainatory
handleinput(); // same here i believe
//renderscene(); and all you need

ticks2 = SDL_GetTicks();
if ((ticks2 - ticks1) < FRAME_TIME)
SDL_Delay(FRAME_TIME - (ticks2 - ticks1));


freeobjects(); // frees space; counter part of loadobjects()
deinit(); // closes sub/systems; counter part of init()

return 0;
void updateinput()
while (SDL_PollEvent(&event)){
if (event.type == SDL_KEYDOWN) // if a button is pressed
keys[event.key.keysym.sym] = true; // store its state
if (event.type == SDL_KEYUP) // and only when released
keys[event.key.keysym.sym] = false;// change it
if (event.type == SDL_MOUSEBUTTONDOWN) // same as above
btns[event.button.button] = true;
if (event.type == SDL_MOUSEBUTTONUP)
btns[event.button.button] = false;

void handleinput()
if(keys[SDLK_LEFT]) buddy_x -= 3;
if(keys[SDLK_DOWN]) buddy_y += 3;
if(keys[SDLK_UP]) buddy_y -= 3;
if(keys[SDLK_RIGHT]) buddy_x += 3;

// a couple ways of quittin
if ( btns[SDL_BUTTON_LEFT] ||
app = false;

if(event.type == SDL_QUIT) app = false ;

Ive put only the input related shivers to save forum bytes, ask for further if needed =)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!