Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » if somebody press Esc he go to another function

This thread is locked; no one can reply to it. rss feed Print
if somebody press Esc he go to another function
a b
Member #8,092
December 2006

Is some instruction in c++ or library allegro that if somebody press Esc in function1() or in function2() then he go to important() - because I am doing this now as the example:

1int main()
2{
3function1();
4important();
5return 0;
6}
7 
8 
9 
10void function1()
11{
12readkey();
13 while (!key[KEY_ESC]&&!key[KEY_ENTER])
14 {
15 readkey();
16 }
17 function2();
18}
19 
20void function2()
21{
22if(!key[KEY_ESC])
23{
24......
25readkey();
26}
27 
28while(!key[KEY_ENTER]&&(!key[KEY_ESC]))
29 {
30 readkey();
31 }
32if(!key[KEY_ESC])
33{
34............
35}
36}
37
38 
39 
40void important()
41{
42}

Steve Terry
Member #1,989
March 2002
avatar

Did you compile with -f-important-esc-function in the compiler flags?

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

LennyLen
Member #5,313
December 2004
avatar

Firstly, you don't need to use readkey() to access the key[] array.

And why can't you just use:

if (key[KEY_ESC]) important();

edit:

Steve: Meanie. :P

a b
Member #8,092
December 2006

I must use readkey() - without that my program wrong run, and I wrote bad example - It should be:

1int main()
2{
3important()
4{
5.........
6}
7 
8function1();
9function3();
10return 0;
11}
12 
13 
14 
15void function1()
16{
17readkey(); //programme waits on pressing key
18 while (!key[KEY_ESC]&&!key[KEY_ENTER])
19 {
20 readkey(); //programme waits on pressing GOOD key (ESC or ENTER)
21 }
22 function2();
23}
24 
25void function2()
26{
27if(!key[KEY_ESC]) //do this only when key wasn't ESC
28{
29......
30readkey(); //programme waits on pressing key
31}
32 
33while(!key[KEY_ENTER]&&(!key[KEY_ESC]))
34 {
35 readkey(); //programme waits on pressing key
36 }
37if(!key[KEY_ESC]) //do this only when key wasn't ESC
38{
39............
40}
41}
42
43 
44 
45void function3();
46{
47}

If somebody press "Esc" - then function1() should will finish and should will go to the start point - important()

LennyLen
Member #5,313
December 2004
avatar

Quote:

I must use readkey() - without that my program wrong run

If you need to use readkey() without checking what it returns, then your program probably isn't written properly. The code you have posted makes very little sense, so it's hard to tell exactly what you're trying to achieve.

But what you probably need in function1() is:

if (key[KEY_ESC]) {
    esc_was_pressed = 1;
    return;
}

And then in main():

if (esc_was_pressed) important();

Another alternative is just having the following in function1():

if (key[KEY_ESC]) {
    important();
    return;
}

Without knowing more about the program flow or what you're trying to achieve, I don't know which would be better.

Albin Engström
Member #8,110
December 2006
avatar

Albin found: "code of horrors"

a b
Member #8,092
December 2006

if (key[KEY_ESC]) {
    important();
    return;
}

after that program go to meni and exit - I think this is fault "return" in above code :/

LennyLen
Member #5,313
December 2004
avatar

Then post some REAL code that actually makes sense!!!

And please start using function names that describes what the function does. I have absolutely no idea what the hell it is you are trying to do, which makes it near-impossible to help you.

a b
Member #8,092
December 2006

I can't give you all my code :) I give you a little part of my code - I had to change names to english because first they were in polish because I am Pole and I deleted 99% my code so maybe something is wrong :) :

1 
2int main()
3{
4 inicjalization();
5 if (menu()==0)
6 { return 0; }
7
8else
9{
10 start_game();
11 end();
12 return 0;
13}
14
15}
16END_OF_MAIN()
17 
18 
19//int a=50
20 
21int menu()
22{
23while(a!=0)
24{
25clear_to_color(bufer,makecol(255,255,255));
26blit(meni,bufor,0,0,0,0,meni->w,meni->h);
27
28 masked_blit(frame,bufer, 0,0, 80,table[index],frame->w,frame->h);
29 
30 blit(bufer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
31 readkey();
32
33 if (key[KEY_DOWN]&&index!=3)
34 {
35
36 index++;
37 }
38 else if (key[KEY_DOWN]&&index==3)
39 {
40
41 index=0;
42 }
43 else if (key[KEY_UP]&&index!=0)
44 {
45
46 index--;
47 }
48 else if (key[KEY_UP]&&index==0)
49 {
50
51 index=3;
52 }
53 else if(key[KEY_ENTER]&&index==3){return 0;}
54 else if(key[KEY_ENTER]&&index==0){return 1;}
55 else if(key[KEY_ENTER]&&index==1){instructions();}
56 else if(key[KEY_ENTER]&&index==2){authors();}
57
58
59}
60}
61 
62void start_game()
63{
64
65 while (......)
66 {
67
68
69 readkey(); // oczekuje na wcisniecie klawisza
70 if (key[KEY_DOWN])
71 {
72 ........
73 function();
74 }
75 else if (key[KEY_UP])
76 {
77 .............
78 function();
79 }
80 else if(key[KEY_ESC]){break;} // but here I want that if somebody press ESC this function
81 //finish and we go AGAIN to function menu() - menu of my game
82
83 }
84}

LennyLen
Member #5,313
December 2004
avatar

If I was going to implement a menu, I'd first define an enumeration to declare the states the program can be in. eg:

enum { MENU, GAME, INSTRUCTIONS, AUTHORS, EXIT };

Then I'd add a loop that checks the state. eg:

1int state = MENU;
2 
3while (state != EXIT) {
4 
5 switch (state) {
6
7 case MENU: run_menu();
8 break;
9 case GAME: run_game();
10 break;
11 case INSTRUCTIONS: show_instructions();
12 break;
13 case AUTHORS: show_authors();
14 
15 }
16 
17}

Then, in your menu function, I'd change the state depending on what the user selects. eg:

1[if (key[KEY_ENTER]) {
2 
3 switch (index) {
4
5 case 0: state = GAME;
6 break;
7 case 1: state = INSTRUCTIONS;
8 break;
9 case 2: state = AUTHORS;
10 break;
11 case 3: state = EXIT;
12 
13 }
14 
15 return;
16 
17}


The state will also need to be set to MENU when the run_game(), show_instruction(), and show_authors() functions exit.

a b
Member #8,092
December 2006

not, I want that when somebody press ESC then he go to menu (first player choose option from menu for example "new game", and then he can play, but when he then press ESC he go to menu) and you only change fragment of my code which doing that same thing

LennyLen
Member #5,313
December 2004
avatar

I'm not going to write your program for you! That was just an EXAMPLE of another way of doing something that I EXPECTED YOU to be able to modify to your needs.

On that note: I quit. You're on your own.

a b
Member #8,092
December 2006

And I don't want that you write code for me, my code was only example so you can't write for me. You idea is wrong.

Evert
Member #794
November 2000
avatar

You should not mix input from the key[] array and readkey(), they serve different purposes.
Rather than calling readkey() and then checking the key[] array, you should use the return value of readkey(), eg if ((readkey() >> 8) == KEY_ESC). Be sure to check the manual.

a b
Member #8,092
December 2006

hm........... and everywhere: if ((readkey() >> 8)== KEY_ENTER)&&index!=3) this not compile and it is longer - this : if (key[KEY_DOWN]&&index!=3) is better and shorter

Evert
Member #794
November 2000
avatar

Quote:

this not compile

Count your closing parentheses...

Quote:

this [...] is better and shorter

It is shorter (but the readkey() version can be written more elegantly) and actually worse, depending on the situation.
Do you want to respond to the player having pressed the key? If yes, use readkey().
Do you want to respond to the key being held down right now? Then use key[].
Do not use readkey() in the second situation and do not use key[] in the first. Somewhere, sometime something will go wrong somehow.

a b
Member #8,092
December 2006

I must use readkey() and key[] because ONLY then program good run. Without readkey at present showed option in menu when I press "->" it jumps over about several of position to lower part and it should about 1 position. And when I have readkey() without key[] - when I pressing another keys than "->" and "<-" postions change and I can't go to game when I am on 1. position in menu and press ENTER (1. position in my menu is "new game"). And when I use readkey() and key[] - EVERYTHING IS PERFECT:
for example - this runs very well because I have readkey() and key[] :

1 readkey();
2 if (key[KEY_DOWN]&&index!=3)
3 {
4
5 index++;
6 }
7 else if (key[KEY_DOWN]&&index==3)
8 {
9
10 index=0;
11 }
12 else if (key[KEY_UP]&&index!=0)
13 {
14
15 index--;
16 }
17 else if (key[KEY_UP]&&index==0)
18 {
19
20 index=3;
21 }
22 else if(key[KEY_ENTER]&&index==3){return 0;}
23 else if(key[KEY_ENTER]&&index==0){return 1;}
24 else if(key[KEY_ENTER]&&index==1){instructions();}
25 else if(key[KEY_ENTER]&&index==2){authors();}

Evert
Member #794
November 2000
avatar

Did you even bother to read (and try to understand) what I wrote?

Quote:

I must use readkey() and key[] because ONLY then program good run.

You're doing something wrong then. Post a sample of the code that doesn't run properly.

a b
Member #8,092
December 2006

ok Ever so what is wrong - could you take attachment - then you will see that if I delete readkey() program runs wrong - you can press only KEY_UP, KEY_DOWN or ENTER.

1 
2#include <iostream>
3#include <ctime>
4#include <allegro.h>
5using namespace std;
6 
7BITMAP *game = NULL;
8BITMAP *bufor = NULL;
9BITMAP *menu = NULL;
10BITMAP *autors=NULL;
11BITMAP *instructions = NULL;
12BITMAP *frame = NULL;
13 
14 
15int go=1;
16int table[4]={215,295,392,550};
17int index=0;
18 
19int meni();
20void autor();
21void instruction();
22void start();
23void end();
24 
25 
26 
27int main()
28{
29allegro_init();
30 install_keyboard();
31 set_color_depth(32);
32 set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
33 bufor=create_bitmap(SCREEN_W,SCREEN_H);
34 game = load_bitmap("game.bmp",NULL);
35 menu = load_bitmap("menu.bmp",NULL);
36 autors = load_bitmap("autors.bmp",NULL);
37 instructions = load_bitmap("instructions.bmp",NULL);
38 frame = load_bitmap("frame.bmp",NULL);
39
40 if (meni()==0)
41 { return 0; }
42
43
44 end();
45 return 0;
46
47 
48 
49}
50END_OF_MAIN()
51 
52 
53 
54int meni()
55{
56 while(go!=0)
57{
58clear_to_color(bufor,makecol(255,255,255));
59blit(menu,bufor,0,0,0,0,menu->w,menu->h);
60
61 masked_blit(frame,bufor, 0,0, 80,table[index],frame->w,frame->h);
62
63 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
64 readkey();
65
66 
67 if (key[KEY_DOWN]&&index!=3)
68 {
69
70 index++;
71 }
72 else if (key[KEY_DOWN]&&index==3)
73 {
74
75 index=0;
76 }
77 else if (key[KEY_UP]&&index!=0)
78 {
79
80 index--;
81 }
82 else if (key[KEY_UP]&&index==0)
83 {
84
85 index=3;
86 }
87 else if(key[KEY_ENTER]&&index==3){return 0;}
88 else if(key[KEY_ENTER]&&index==0){start();}
89 else if(key[KEY_ENTER]&&index==1){instruction();}
90 else if(key[KEY_ENTER]&&index==2){autor();}
91
92
93}
94}
95 
96 
97 
98void start()
99{
100 clear_to_color(bufor,makecol(255,255,255));
101 blit(game,bufor,0,0,0,0,game->w,game->h);
102 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
103 readkey();
104}
105 
106 
107void instruction()
108{
109 clear_to_color(bufor,makecol(255,255,255));
110 blit(instructions,bufor,0,0,0,0,instructions->w,instructions->h);
111 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
112 readkey();
113}
114 
115 
116void autor()
117{
118 clear_to_color(bufor,makecol(255,255,255));
119 blit(autors,bufor,0,0,0,0,autors->w,autors->h);
120 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
121 readkey();
122}
123 
124 
125 
126 void end()
127{
128 destroy_bitmap(game);
129 destroy_bitmap(menu);
130 destroy_bitmap(frame);
131 destroy_bitmap(instructions);
132 destroy_bitmap(autors);
133 destroy_bitmap(bufor);
134 allegro_exit();
135}

Evert
Member #794
November 2000
avatar

Quote:

ok Ever

Evertt

Quote:

- could you take attachment

No, I can't. I can't open rar archives from this machine.

Make sure that if you do use readkey() instead of key[] you call it only once.

a b
Member #8,092
December 2006

ok - now it is "zip" file :)

As I say without "readkey()" - everything is wrong.

Richard Phipps
Member #1,632
November 2001
avatar

a b: You are just going to have to trust us when we say is it wrong. Listen to the advice you get instead of trying to fight it. :)

a b
Member #8,092
December 2006

I want trust you but when I delete readkey() - everything is wrong :)

tobing
Member #5,213
November 2004
avatar

I had some trouble with key[] and readkey also with my first project. The point is, you have to use allegro's functions for key input the way they are designed, which is a little different from what you do. Other people here can explain this very much better than I can, but look into do_dialog() and similar functions to make your UI. It has its root somewhere in the parallel existence of a keyboard buffer, which is handled by routines like readkey(), and the key[] array, which allows immediate access to the current key states.

For my needs, I'll do my next game with guichan, which is an excellent GUI library, but still in heavy development (working its way to version 0.6).

a b
Member #8,092
December 2006

I understand but why must I use readkey() and key[] in below code (also in attachment) ? Do somebdoy know ? If I use only readkey() or only key[] program runs wrong:

1#include <iostream>
2#include <ctime>
3#include <allegro.h>
4using namespace std;
5 
6BITMAP *game = NULL;
7BITMAP *bufor = NULL;
8BITMAP *menu = NULL;
9BITMAP *autors=NULL;
10BITMAP *instructions = NULL;
11BITMAP *frame = NULL;
12 
13 
14int go=1;
15int table[4]={215,295,392,550};
16int index=0;
17 
18int meni();
19void autor();
20void instruction();
21void start();
22void end();
23 
24 
25 
26int main()
27{
28allegro_init();
29 install_keyboard();
30 set_color_depth(32);
31 set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
32 bufor=create_bitmap(SCREEN_W,SCREEN_H);
33 game = load_bitmap("game.bmp",NULL);
34 menu = load_bitmap("menu.bmp",NULL);
35 autors = load_bitmap("autors.bmp",NULL);
36 instructions = load_bitmap("instructions.bmp",NULL);
37 frame = load_bitmap("frame.bmp",NULL);
38
39 if (meni()==0)
40 { return 0; }
41
42
43 end();
44 return 0;
45
46 
47 
48}
49END_OF_MAIN()
50 
51 
52 
53int meni()
54{
55 while(go!=0)
56{
57clear_to_color(bufor,makecol(255,255,255));
58blit(menu,bufor,0,0,0,0,menu->w,menu->h);
59
60 masked_blit(frame,bufor, 0,0, 80,table[index],frame->w,frame->h);
61
62 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
63 readkey();
64
65 
66 if (key[KEY_DOWN]&&index!=3)
67 {
68
69 index++;
70 }
71 else if (key[KEY_DOWN]&&index==3)
72 {
73
74 index=0;
75 }
76 else if (key[KEY_UP]&&index!=0)
77 {
78
79 index--;
80 }
81 else if (key[KEY_UP]&&index==0)
82 {
83
84 index=3;
85 }
86 else if(key[KEY_ENTER]&&index==3){return 0;}
87 else if(key[KEY_ENTER]&&index==0){start();}
88 else if(key[KEY_ENTER]&&index==1){instruction();}
89 else if(key[KEY_ENTER]&&index==2){autor();}
90
91
92}
93}
94 
95 
96 
97void start()
98{
99 clear_to_color(bufor,makecol(255,255,255));
100 blit(game,bufor,0,0,0,0,game->w,game->h);
101 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
102 readkey();
103}
104 
105 
106void instruction()
107{
108 clear_to_color(bufor,makecol(255,255,255));
109 blit(instructions,bufor,0,0,0,0,instructions->w,instructions->h);
110 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
111 readkey();
112}
113 
114 
115void autor()
116{
117 clear_to_color(bufor,makecol(255,255,255));
118 blit(autors,bufor,0,0,0,0,autors->w,autors->h);
119 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
120 readkey();
121}
122 
123 
124 
125 void end()
126{
127 destroy_bitmap(game);
128 destroy_bitmap(menu);
129 destroy_bitmap(frame);
130 destroy_bitmap(instructions);
131 destroy_bitmap(autors);
132 destroy_bitmap(bufor);
133 allegro_exit();
134}



Go to: