In my program there are a lot of computations. If the user presses 'q' the program must quit. If the user presses 'r' some stuff must be drawed and then pause before the compuations continue.
Here is my not-working code:
void run()
{
while(1)
{
do_sim();
if(!user_input()) return;
}
}
void do_sim()
{
}
bool user_input()
{
if(keypressed())
{
char c=readkey()&0xff;
switch(c)
{
case 'q':return false;
case 'r':/*here I draw some things*/break;
default:break;
}
wait();//here I display everything on the screen
}
return true;
}
void wait()
{
while(!key[KEY_ESC])
blit(buffer,screen,0,0,0,0,1024,768);
}
If I was you, I would clean up your code at first. Separate your drawing functions from your key input functions. Maybe the problem solves itself after that ...
[EDIT]
Indentation would help a lot, too.
Please use code tags next time. That's pretty difficult to read as is.
Anyways, I would suggest you change your input to be more like this:
1 | // input |
2 | while( keypressed() ) |
3 | { // while there are keypresses left in the buffer... |
4 | switch (readkey() >> 8) |
5 | { // readkey automatically has that pause and keyboard |
6 | // repeat...which is handy when used to toggle things |
7 | case KEY_Q: |
8 | bQuitVar = true; // set the quit variable. |
9 | // when the quit variable is true, our main loop |
10 | // should break lose and exit the program. |
11 | break; |
12 | |
13 | case KEY_R: |
14 | do_pause(); |
15 | break; |
16 | |
17 | // whatever else keyboard input you want that |
18 | // follows the keypress and repeat input type |
19 | } |
20 | } |
21 | |
22 | |
23 | // pause function |
24 | void do_pause() |
25 | { |
26 | // notice we display the buffer BEFORE our pause while-loop. |
27 | // if the appearance of the screen will not change during pause |
28 | // then there is no need to constantly push the buffer onto screen. |
29 | |
30 | /* put the "Other" stuff you said you wanted to draw here... */ |
31 | |
32 | textout_ex(buffer, font, "Game paused. Press R to continue.", 16, 16, 0xFFFFFF, -1); |
33 | blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); |
34 | |
35 | while( 1 ) |
36 | { |
37 | // so we aren't doing 19 billion repetetions a second... |
38 | // and wasting processor time |
39 | rest(1); |
40 | |
41 | // update keyboard information |
42 | if( keyboard_needs_poll() ) |
43 | poll_keyboard(); |
44 | |
45 | // clear keyboard input and |
46 | // break from our infanite loop |
47 | if( key[KEY_R] ) { |
48 | clear_keybuf(); |
49 | break; } |
50 | } |
51 | } |
I haven't actually tested this code, but it should work. I've written similar code to this for doing pause screens (with embedded menus even), so the idea behind it works, though there might be a bug here or there. I hope this helps you in some way.