Problems with input? why am i moving so fast when i shouldt be?

hi guys,

ive just started work on a pac-man clone, and im doing a tile-based system.
ive never worked with this kind of stuff before, and as usual theres not
much help for people using SDL on gamedev (thank god for you guys!)

anyway, heres the problem: i have 32x32 tiles set up and the player himself
is a tile. right now im working on just setting up a background and the
player and having him walk around colliding with walls. well everything is
working good, except, for some WEIRD reason, when i press a directional key
(to move the player), he doesnt just move the amount of spaces he is
supposed to. its almost as if i press the key once and the computer presses
it 5 times for me. if im suposed to move 1 space to the left, i move 5, in
my case i have to move 32 spaces at a time, so when i press a key i pretty
much jump across the screen. i cant figure out WHY tho. im using the EXACT
code i used in my pong clone(NOT tile based), so i dont see what the problem
could be. i added an SDL_Delay(1000) after the player.xPos +=32 etc lines,
AND THIS WORKS! BUT, this is a crappy fix. whats going to happen when i have
enemies moving around the screen and stuff? cant have everything delaying
for a second each frame. another thing that works, is putting ALL of my
input code into a while ( SDL_PollEvent(&event) ), but if i do this, the
player cant hold down the keys.(and besides, i didnt have to do that in my
last game, why does it matter now?) i dont know WHY this is happening, but
could someone maybe help me? here the part of my code which takes input and
moves the player. im using a map array thats 19 by [25] (x) (for
800x600 screen using 32x32 tiles)

void Move_Pac()
{

Uint8* keys; //we start a pointer that points to the keys being pressed

SDL_Event event; //create an event holder

while ( SDL_PollEvent(&event) ) //looks for events
{
	if ( event.type == SDL_QUIT )  {  data.done = 1;  } //if the user pressed 

the X on the screeen quit

  if ( event.type == SDL_KEYDOWN ) //if the user pressed a key then.....
  {
	  if ( event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == 

SDLK_q ) { data.done = 1; } //if they pressed escape or q then quit

  }

}


//SDL_PumpEvents();

keys = SDL_GetKeyState(NULL);

//first we move the player then the enemy



if ( keys[SDLK_UP])
{
	if (map[(player.yPos - 32)/32][player.xPos/32] == OPEN_SPACE)
		player.yPos-=32;

}

if ( keys[SDLK_DOWN])
{
	if (map[(player.yPos + 32)/32][player.xPos/32] == OPEN_SPACE)
		player.yPos+=32;

}

if (keys[SDLK_LEFT])
{
	if(map[player.yPos/32][(player.xPos - 32)/32] == OPEN_SPACE)
		player.xPos-=32;

}

if (keys[SDLK_RIGHT])
{
	if (map[player.yPos/32][(player.xPos + 32)/32] == OPEN_SPACE)
		player.xPos+=32;

}

}

please note - if i put this entire function into the while (
SDL_PollEvent(&event) ), this problem will not happen. but if i do this, the
player cant hold down on the keys. they have to release a key and press it
again to move. i just dont understand why this is happening. also if i add
an SDL_Delay(1000), (at the end of each one of these if statements), this
also fixes it, but for some reason if i take out the delay, its as im
pressing the key 4 or 5 times instead of once. if you want more source to
look at (the whole “game” so far is only around 140 lines or so) please let
me know. i appreciate it. thank you everyone!!!_________________________________________________________________
One-click access to Hotmail from any Web page ? download MSN Toolbar now!
http://clk.atdmt.com/AVE/go/onm00200413ave/direct/01/

hi there.

Have you tried to activate keyrepeat? Maybe it is
SDL_KeyRepeat(1) before your message processign loop;

Hope it helps,

J. Inacio
Free Software Development Coordinator
GNUFrb - Faculdade Ruy Barbosa
Salvador - Bahia - Brasil

— Graveyard Filla
schrieb: > hi guys,>

ive just started work on a pac-man clone, and im
doing a tile-based system.
ive never worked with this kind of stuff before, and
as usual theres not
much help for people using SDL on gamedev (thank god
for you guys!)

anyway, heres the problem: i have 32x32 tiles set up
and the player himself
is a tile. right now im working on just setting up a
background and the
player and having him walk around colliding with
walls. well everything is
working good, except, for some WEIRD reason, when i
press a directional key
(to move the player), he doesnt just move the amount
of spaces he is
supposed to. its almost as if i press the key once
and the computer presses
it 5 times for me. if im suposed to move 1 space to
the left, i move 5, in
my case i have to move 32 spaces at a time, so when
i press a key i pretty
much jump across the screen. i cant figure out WHY
tho. im using the EXACT
code i used in my pong clone(NOT tile based), so i
dont see what the problem
could be. i added an SDL_Delay(1000) after the
player.xPos +=32 etc lines,
AND THIS WORKS! BUT, this is a crappy fix. whats
going to happen when i have
enemies moving around the screen and stuff? cant
have everything delaying
for a second each frame. another thing that works,
is putting ALL of my
input code into a while ( SDL_PollEvent(&event) ),
but if i do this, the
player cant hold down the keys.(and besides, i didnt
have to do that in my
last game, why does it matter now?) i dont know WHY
this is happening, but
could someone maybe help me? here the part of my
code which takes input and
moves the player. im using a map array thats 19
by [25] (x) (for
800x600 screen using 32x32 tiles)

void Move_Pac()
{

Uint8* keys; //we start a pointer that points to
the keys being pressed

SDL_Event event; //create an event holder

while ( SDL_PollEvent(&event) ) //looks for

events
{
if ( event.type == SDL_QUIT ) { data.done = 1;
} //if the user pressed
the X on the screeen quit

  if ( event.type == SDL_KEYDOWN ) //if the user

pressed a key then…
{
if ( event.key.keysym.sym == SDLK_ESCAPE ||
event.key.keysym.sym ==
SDLK_q ) { data.done = 1; } //if they pressed escape
or q then quit

}

}

//SDL_PumpEvents();

keys = SDL_GetKeyState(NULL);

//first we move the player then the enemy

if ( keys[SDLK_UP])
{
if (map[(player.yPos - 32)/32][player.xPos/32] ==
OPEN_SPACE)
player.yPos-=32;

}

if ( keys[SDLK_DOWN])
{
if (map[(player.yPos + 32)/32][player.xPos/32] ==
OPEN_SPACE)
player.yPos+=32;

}

if (keys[SDLK_LEFT])
{
if(map[player.yPos/32][(player.xPos - 32)/32] ==
OPEN_SPACE)
player.xPos-=32;

}

if (keys[SDLK_RIGHT])
{
if (map[player.yPos/32][(player.xPos + 32)/32] ==
OPEN_SPACE)
player.xPos+=32;

}

}

please note - if i put this entire function into the
while (
SDL_PollEvent(&event) ), this problem will not
happen. but if i do this, the
player cant hold down on the keys. they have to
release a key and press it
again to move. i just dont understand why this is
happening. also if i add
an SDL_Delay(1000), (at the end of each one of
these if statements), this
also fixes it, but for some reason if i take out the
delay, its as im
pressing the key 4 or 5 times instead of once. if
you want more source to
look at (the whole “game” so far is only around 140
lines or so) please let
me know. i appreciate it. thank you everyone!!!


One-click access to Hotmail from any Web page ?
download MSN Toolbar now!

http://clk.atdmt.com/AVE/go/onm00200413ave/direct/01/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Mit sch?nen Gr??en von Yahoo! Mail - http://mail.yahoo.de

i think you’re just getting a far too high framerate and therefore your
game is running very fast.

try limiting the fps by using a simple fps limiter:

while(game runs){
time = SDL_GetTicks()

{
put everything you do now in here
}
while((SDL_GetTicks()-framestart) < WAITTIME); //keep framerate constant
at 1000/WAITTIME fps
}

where WAITTIME = 1000/wantedfps

eg: if you want 62 fps your waittime is 1000/62 = 16 (milliseconds)

if you want to take a look at some source code you could try my tutorial
series on jumpnrun development with sdl

of take a look at the crappy source of my game super mario war:

Graveyard Filla wrote:> hi guys,

ive just started work on a pac-man clone, and im doing a tile-based
system. ive never worked with this kind of stuff before, and as usual
theres not much help for people using SDL on gamedev (thank god for
you guys!)

anyway, heres the problem: i have 32x32 tiles set up and the player
himself is a tile. right now im working on just setting up a
background and the player and having him walk around colliding with
walls. well everything is working good, except, for some WEIRD reason,
when i press a directional key (to move the player), he doesnt just
move the amount of spaces he is supposed to. its almost as if i press
the key once and the computer presses it 5 times for me. if im suposed
to move 1 space to the left, i move 5, in my case i have to move 32
spaces at a time, so when i press a key i pretty much jump across the
screen. i cant figure out WHY tho. im using the EXACT code i used in
my pong clone(NOT tile based), so i dont see what the problem could
be. i added an SDL_Delay(1000) after the player.xPos +=32 etc lines,
AND THIS WORKS! BUT, this is a crappy fix. whats going to happen when
i have enemies moving around the screen and stuff? cant have
everything delaying for a second each frame. another thing that works,
is putting ALL of my input code into a while ( SDL_PollEvent(&event)
), but if i do this, the player cant hold down the keys.(and besides,
i didnt have to do that in my last game, why does it matter now?) i
dont know WHY this is happening, but could someone maybe help me? here
the part of my code which takes input and moves the player. im using a
map array thats 19 by [25] (x) (for 800x600 screen using 32x32
tiles)

void Move_Pac()
{

Uint8* keys; //we start a pointer that points to the keys being pressed

SDL_Event event; //create an event holder

while ( SDL_PollEvent(&event) ) //looks for events
{
if ( event.type == SDL_QUIT ) { data.done = 1; } //if the user pressed
the X on the screeen quit

if ( event.type == SDL_KEYDOWN ) //if the user pressed a key then…
{
if ( event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym ==
SDLK_q ) { data.done = 1; } //if they pressed escape or q then quit

}

}

//SDL_PumpEvents();

keys = SDL_GetKeyState(NULL);

//first we move the player then the enemy

if ( keys[SDLK_UP])
{
if (map[(player.yPos - 32)/32][player.xPos/32] == OPEN_SPACE)
player.yPos-=32;

}

if ( keys[SDLK_DOWN])
{
if (map[(player.yPos + 32)/32][player.xPos/32] == OPEN_SPACE)
player.yPos+=32;

}

if (keys[SDLK_LEFT])
{
if(map[player.yPos/32][(player.xPos - 32)/32] == OPEN_SPACE)
player.xPos-=32;

}

if (keys[SDLK_RIGHT])
{
if (map[player.yPos/32][(player.xPos + 32)/32] == OPEN_SPACE)
player.xPos+=32;

}

}

please note - if i put this entire function into the while (
SDL_PollEvent(&event) ), this problem will not happen. but if i do
this, the player cant hold down on the keys. they have to release a
key and press it again to move. i just dont understand why this is
happening. also if i add an SDL_Delay(1000), (at the end of each one
of these if statements), this also fixes it, but for some reason if i
take out the delay, its as im pressing the key 4 or 5 times instead of
once. if you want more source to look at (the whole “game” so far is
only around 140 lines or so) please let me know. i appreciate it.
thank you everyone!!!


One-click access to Hotmail from any Web page ? download MSN Toolbar
now! http://clk.atdmt.com/AVE/go/onm00200413ave/direct/01/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

On Tue, 02 Mar 2004 07:00:51 +0000, “Graveyard Filla”
said:

hi guys,

ive just started work on a pac-man clone, and im doing a tile-based
system.
ive never worked with this kind of stuff before, and as usual theres not
much help for people using SDL on gamedev (thank god for you guys!)

anyway, heres the problem: i have 32x32 tiles set up and the player
himself
is a tile. right now im working on just setting up a background and the
player and having him walk around colliding with walls. well everything
is
working good, except, for some WEIRD reason, when i press a directional
key
(to move the player), he doesnt just move the amount of spaces he is
supposed to. its almost as if i press the key once and the computer
presses
it 5 times for me.

It sounds like you’re moving once per frame, whenever the key is pressed
down. You probably don’t want to do that - this means that if you get 60
fps you’ll move sixty spaces if the key is held down for a second. The
reason your old game worked was because it was moving one pixel every
frame the key was held down. You’ve probably got 800x600 pixels on the
screen, but only 32x32 tiles, so the granularity is of a different order.

Two simple answers:

  • Move once per keypress. So instead of moving every frame when the key
    is held down, move only when the key is initially pressed. (This would
    mean catching keyboard events differently, probably, and would also mean
    you’d have to press 10 times to move 10 tiles.)
    OR
  • Have an internal counter for each key. So instead of moving every
    frame when a key is held down, each frame a key is held down you
    increment a counter. When the counter gets to a threshold level, you
    move. To do this correctly, you’d need to have the threshold value
    somehow related to the number of frames per second, so that you get a
    similar rate of movement depending on the frame rate.

(The second is probably the better one.)

Hope this helps,

Dave.–
Dave Slutzkin
Melbourne, Australia
@Dave_Slutzkin