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
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:
case user hit the arrow:
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