Event thread, keyboard input, help!

Hi!

I’m working on a breakout clone, and I’m having some troubles with
keyboard input and the way that the threads run (more on that in a sec).

Currently, the program works like this: start an event thread, then (in
the main thread) go into the drawing loop. I chose to use a separate
thread for events because I want user input to be processed independently
of the drawing thread. For example, with a lot of input (key held
down?) and a million (probably less, though…) sprites all being churned
out, there would be user input events missed or framerate
reduction. Before anybody submits any suggestions, let me say that I
don’t really want to lock the framerate, and my code is structured
accordingly.

Anyway, the heart of the matter:

Mouse input works. The event thread picks up events, updates object_next
with a new x coordinate from the mouse. The next time the drawing loop
comes around, it sees that object_next != object, and updates it’s
object. All well and good. But I want the user to be able to hold down
the arrow keys to move the pod (remember, Breakout clone). When I do
this, the pod SLOWLY creeps across the screen, 1 pixel at a time, whereas
if I just increment the pod’s coords each drawing loop, it zooms
across. So the problem seems to lie in the event handling/threading.

My event loop looks something like:

while(SDL_WaitEvent(&event){
switch(event.type)
case user hit the arrow:
etc…
}

When run, I’d TAP the arrow key, and the pod would shoot like 20
pixels! So to further debug, I had each thread print out a message for
each iteration of its loop. What I found was that I’d get like 30-40
iterations of the game loop, then an event loop. Intuitively, I’d think
that since the game loop entails more work, it’d take longer, and the
events would be processed in a timely manner. What’s going on here!

Furthermore, I’ve fiddled with keyboard repeat rates and such, and can’t
seem to get this to work.

I’m sorry I’m not quite being as clear as I could be about my problem. I
can’t post any code (cablem modem on monday, though!) right now, perhaps
in the future if needed.

Can anybody tell me what’s wrong with my threads/game
design/algorithms? Or just give me a general method for implementing what
I want? (User presses arrows, pod slides around nicely) Perhaps I’m
unclear as to how these threads actually operate (but I don’t think
so). Also, is it customary (for pros, for example) to isolate the event
loop from the drawing loop to maximize framerate?

thanks a lot for any help, I want to get on to FUN parts of my project
again!!!

Lyle

Lyle Hanson wrote:

When run, I’d TAP the arrow key, and the pod would shoot like 20
pixels! So to further debug, I had each thread print out a message for
each iteration of its loop. What I found was that I’d get like 30-40
iterations of the game loop, then an event loop. Intuitively, I’d think
that since the game loop entails more work, it’d take longer, and the
events would be processed in a timely manner. What’s going on here!

Threads operate by slicing the CPU time into small blocks, and
allocating
those blocks to your threads and to other processes. A common timeslice
value is 10 milliseconds, and it’s not hard to believe that you could
get 30 updates during that time, providing that you are using hardware
accelerated video drivers.

Furthermore, I’ve fiddled with keyboard repeat rates and such, and can’t
seem to get this to work.

no need if you are watching keyup/keydown events.

I’m sorry I’m not quite being as clear as I could be about my problem. I
can’t post any code (cablem modem on monday, though!) right now, perhaps
in the future if needed.

Can anybody tell me what’s wrong with my threads/game
design/algorithms? Or just give me a general method for implementing what
I want? (User presses arrows, pod slides around nicely) Perhaps I’m
unclear as to how these threads actually operate (but I don’t think
so). Also, is it customary (for pros, for example) to isolate the event
loop from the drawing loop to maximize framerate?

You need to scale the movement to match the elapsed time. For example,
you could use floating point values for the position, and multiply
the change by the elapsed time in seconds. So if you want a movement
rate of 100 pixels per second, and you measured that 10 milliseconds
has elapsed, then you add 100 * 0.010 and add that to the position.

Bear in mind that you can’t just measure the timing once and put a
constant in the code. The time will vary, and you should measure it
each time through the loop, using SDL_GetTicks, for example.

All that aside, on a single CPU machine you probably won’t gain any
advantage from using threads. You could just as easily poll for events
once each time through the display loop, and react then. You would
most likely get better responsiveness that way, again, assuming its
running on a single CPU machine.

For a dual CPU machine threads are still not a clear win, since (I’m
assuming linux here) the X server is a seperate process.>

thanks a lot for any help, I want to get on to FUN parts of my project
again!!!

Lyle