|
Keyboard not responding to Input |
Reaper
Member #8,737
June 2007
|
Hello Everyone, I'm new here. I'm currently programming a program for a College assignment and I've run into a problem - they program isn't responding the the user input. Everything else is working fine, but when the user presses a key, nothing happens. I think this has got something to do with the order that I call various functions (e.g. moveplayer() and setupgame()) in my main() function, but I'm not really too sure. If anyone could take a look at my code and make any suggestions it would be very much appreciated. Thanks.
|
gnolam
Member #2,030
March 2002
|
movePlayer() will never be called. When you exit the first loop, key[ESC] will still be set, and the second loop will be skipped. That's your main problem. As soon as you've fixed that, however, you'll discover why mixing logic and drawing is a bad idea... Quote: install_int_ex(increment_speed_counter, BPS_TO_TIMER(1)); //should this be 1 or 60? That should be however many logic updates you want per second. There are more errors and bad practices in there (like not destroying bitmaps you create), but that's as much as I'm willing to correct until the first two items are fixed: move movePlayer() into the actual timed loop, and separate your logic and drawing (see the FAQ for a hint on how it should look) and I'll tell you the rest. -- |
Reaper
Member #8,737
June 2007
|
Thank you very much, gnolam. I have moved my movePlayer() function into the main loop and it now looks like this:
My keyboard now works and everything else. However, the sprite will only move once every second. So I changed the install_int_ex(increment_speed_counter, BPS_TO_TIMER(1)) value from 1 to a higher number (10 and 60). Now the input is working perfectly correctly, but the timer in the top corner is now not displayed at all! Do you have any idea what could be causing this and how to fix it? Also, I realize that my code really is horrible. At this stage though, all I am trying to do is to get something actually working (undoubtedly terrible practice I'm sure) and go back and fix it up later. Or perhaps though, it would be better to start from scratch again and actually do a decent job of my code this time? Once again, thanks for your help. |
Kris Asick
Member #1,424
July 2001
|
You're drawing your buffer bitmap too many times per frame. You only ever want to draw it to the screen once per loop. The way your code is above, to achieve this, simply remove all your calls to draw_sprite(), acquire_screen() and release_screen(), make your blit call at the end of the loop encompass the entire screen (640 x 480) and you should be fine. Also, move your clear command to the top of your loop before you process speed_counter. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
Reaper
Member #8,737
June 2007
|
Thank you for your help, but I'm not sure that I fully understand. Is this what you were meaning?
The above code makes the screen flash once every second between a black screen with the counter on it, and the normal game screen. Everything else works but is their any way that I can stop this flashing? Thanks. |
HardTranceFan
Member #7,317
June 2006
|
No. Keep the clear(bmpBackBuffer) where it was. Your movePlayer() should only do that - move the player. It should have no draw_sprite() or blit() calls in it at all. This is so that you separate your game logic from your drawing. You should have another function drawPlayer(BITMAP*) that draws the player to the screen. You want to call this function after you've cleared the buffer.
-- |
Kris Asick
Member #1,424
July 2001
|
I just noticed you have TWO buffers... You only need one buffer, and all drawing should happen on it. When you're finished drawing everything, you simply blit the entire buffer to the screen. Thus to fix your code as it is without any major changes, change all references to "buffer" to "bmpBackBuffer". And HardTranceFan's advice is good, you should separate your rendering code from everything else so that it can all be grouped together, but until you do, leave the clear(bmpBackBuffer) command in its new spot. Worry about making it work first, then separate the rendering from everything else. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
Reaper
Member #8,737
June 2007
|
Thank you all for all your assistance. Kris Asick, I think the having two buffers was causing a lot of the problems, as it is now working much better. Now I need to go about fixing up my code :-) - thank you for your ideas on how to begin this. Thank you again. |
|