Keyboard Events vs. Mouse Events

I’m writing a little run and jump just to get familar with SDL, and I’ve
got a little question. I’ve got a loop that looks like this:

while (SDL_PollEvent(&event) > 0) {
printf(“Event\n”);
switch (event.type) {
case SDL_MOUSEMOTION:
walk = (event.motion.x * 3);
break;
case SDL_KEYDOWN:
key = event.key.keysym.sym;
switch (key) {
case SDLK_a:
walk += 3;
break;
}
break;
}
}

Basicly what happens is walk is incremented or decremented and I move the
little scence 1920 pixel forward and back. Walk being the x that I start
with for the background image. Now with the mouse I can move around very
wonderfully, because I can recieve multiple events before the loop
recycles and decides it wants to draw the scene, but even with
SDL_EnableKeyRepeat(1, 1), I can’t get more than one event per cycle. (The
loop is about 30ms long). So either I’ve got to move extremely slow or
have no granularity at slow speeds.

Is there anyone out there that can think of a good solution to my problem?

– Jonathan

Basicly what happens is walk is incremented or decremented and I move the
little scence 1920 pixel forward and back. Walk being the x that I start
with for the background image. Now with the mouse I can move around very
wonderfully, because I can recieve multiple events before the loop
recycles and decides it wants to draw the scene, but even with
SDL_EnableKeyRepeat(1, 1), I can’t get more than one event per cycle. (The
loop is about 30ms long). So either I’ve got to move extremely slow or
have no granularity at slow speeds.

Is there anyone out there that can think of a good solution to my problem?

– Jonathan

Modify a velocity with the event instead, and use that to calculate your
position at each screen update.

ie
walk = (currenttime - lastupdatetime) * velocity

Julian.

Now with the mouse I can move around very
wonderfully, because I can recieve multiple events before the loop
recycles and decides it wants to draw the scene, but even with
SDL_EnableKeyRepeat(1, 1), I can’t get more than one event per cycle. (The
loop is about 30ms long). So either I’ve got to move extremely slow or
have no granularity at slow speeds.

The same situation arose when I tried to patch keyboard-support into
Bills circuslinux (i.e. I failed :wink:

Is there anyone out there that can think of a good solution to my problem?

In my own game, which runs in a 10ms-loop (I believe), I do it similar to
this:

void move() {
keys = SDL_GetKeyState(NULL);
if (keys[SDLK_UP) {
player.y -= 2;
drawplayer();
}
if (keys[SDLK_DOWN) {
player.x += 2;
drawplayer();
}

}

Which is both smooth and fast enough. So I don’t use events for movement at
all, I ask for the KeyState myself as often as I want it. But that requires
that you ask/loop fast enough.

You could also start moving on a keypress-event, and not stop until the
key is un-pressed, which should generate another event.

All the best,
robOn Wed, Mar 15, 2000 at 07:52:21PM -0600, Jonathan Miller wrote: