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
Albin Engström
Member #8,110
December 2006
avatar

WHAT THE F*** IS WRONG WITH YOU!?

Seriously, how can a simple program with a really really simple menu take 1016 lines of space? And something even more disturbing: after 99 replies you still haven't learned? listen to people!. you want help but you refuse to listen? that's just stupid. if you don't understand anything that people say then you should not have asked, do you really just want to be a waste of time?.

Please, you almost made me cry, and i haven't cried in years..

Evert
Member #794
November 2000
avatar

Quote:

if you don't understand anything that people say then you should not have asked

Thing is, I can't work out how much of that is due to his poor English. You can't fault people for not speaking English natively.
So let's try to keep it civil, shall we? :)

Quote:

Evert but in my game haven't mistakes

You do, but that wasn't my point.
What you need to take away from that bit of text is mostly from part 5 onward. In particular you should:
1. Make it clear what you're trying to do
2. Make it clear what teh code you have does
3. Make it clear why this is different from what you want it to do
4. Don't expect people to wade through hundreds of lines of source code
5. Think about what people say, ask if you need more help
6. Change your wording if people do not understand your question. Consider posting the question in your own language as well as in your best English; chances are someone will understand it.

I would also strongly recommend that you invest some time in improving your English. Try reading some English texts or books, or watch English movies/television (with subtitles). You'll need a dictionary for reading at first, but I think it's the best way to learn.

a b
Member #8,092
December 2006

it is code which kaukiz suggest:

1#include <iostream>
2#include <allegro.h>
3using namespace std;
4 
5BITMAP *game = NULL;
6BITMAP *bufor = NULL;
7BITMAP *menu = NULL;
8BITMAP *autors=NULL;
9BITMAP *instructions = NULL;
10BITMAP *frame = NULL;
11 
12 
13 
14 
15void meni();
16void autor();
17void instruction();
18void start();
19void end();
20void kaukiz();
21void something();
22 
23 
24bool gameRunning = true;
25int k;
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 
40while(gameRunning)
41{
42meni();
43if(k == KEY_ESC) gameRunning = false;
44}
45end();
46return 0;
47}
48END_OF_MAIN()
49 
50 
51 
52 
53 
54 
55 
56void meni()
57{
58int go=1;
59int table[4]={215,295,392,550};
60int index=0;
61
62 while(go!=0)
63{
64clear_to_color(bufor,makecol(255,255,255));
65blit(menu,bufor,0,0,0,0,menu->w,menu->h);
66masked_blit(frame,bufor, 0,0, 80,table[index],frame->w,frame->h);
67blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
68k= readkey()>>8;
69 if ((k==KEY_DOWN)&&index!=3)
70 {
71
72 index++;
73 }
74 else if ((k==KEY_DOWN)&&index==3)
75 {
76
77 index=0;
78 }
79 else if ((k==KEY_UP)&&index!=0)
80 {
81
82 index--;
83 }
84 else if ((k==KEY_UP)&&index==0)
85 {
86
87 index=3;
88 }
89 else if((k==KEY_ENTER)&&index==0){start();}
90 else if((k==KEY_ENTER)&&index==1){instruction();}
91 else if((k==KEY_ENTER)&&index==2){autor();}
92 else if((k==KEY_ENTER)&&index==3){break;}
93 
94}
95}
96 
97 
98 
99void start()
100{
101 clear_to_color(bufor,makecol(255,255,255));
102 blit(game,bufor,0,0,0,0,game->w,game->h);
103 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
104 k = readkey()>>8;
105 while (k!=KEY_ESC&&k!=KEY_ENTER) //pressing keys as long as you press ESC or Enter
106 {
107 k = readkey()>>8;
108 }
109 kaukiz(); // for you kaukiz :)
110
111}
112 
113 
114 
115void kaukiz()
116{
117if(k!=KEY_ESC) //I must check if somebody don't press ESC in last pressing key
118 {
119 //here is something
120 k = readkey()>>8;
121 something();
122 }
123}
124 
125 
126void something()
127{
128if(!key[KEY_ESC]) //I must check if somebody don't press ESC in last pressing key
129 {
130 //here is something
131
132 }
133}
134 
135 
136 
137void instruction()
138{
139 clear_to_color(bufor,makecol(255,255,255));
140 blit(instructions,bufor,0,0,0,0,instructions->w,instructions->h);
141 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
142 k = readkey()>>8;
143}
144 
145 
146void autor()
147{
148 clear_to_color(bufor,makecol(255,255,255));
149 blit(autors,bufor,0,0,0,0,autors->w,autors->h);
150 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
151 k = readkey()>>8;
152}
153 
154 
155 
156 void end()
157{
158 destroy_bitmap(game);
159 destroy_bitmap(menu);
160 destroy_bitmap(frame);
161 destroy_bitmap(instructions);
162 destroy_bitmap(autors);
163 destroy_bitmap(bufor);
164 allegro_exit();
165}

But it is obvious that this idea kaukiz is bad because if somebody will press ESC program won't exit.

My idea was better in my previous code but it also wasn't perfect.

I think that the best solution will be that in top right corner will "X" - and after click "X" game will exit - but how can I do it ? I know that we must use:
set_gfx_mode(GFX_AUTODETECT_WINDOWED,1024,768,0,0); but if somebody has resolution of monitor: 1024:768 program will wrong run because size window must be smaller than somebody's resolution of monitor - so what should I do if I want that my program runs good in also resolution of monitor 1024:768? And how can I do "X" which exit program.

Richard Phipps
Member #1,632
November 2001
avatar

Evert
Member #794
November 2000
avatar

Try using a different word for "bad" when someone's suggestion does not do what you want it to do.

Quote:

and after click "X" game will exit - but how can I do it ?

http://alleg.sourceforge.net/stabledocs/en/alleg000.html#set_close_button_callback
Note that this is no different in principle from how you should deal with escape exiting the game.

Quote:

but if somebody has resolution of monitor: 1024:768 program will wrong run because size window must be smaller than somebody's resolution of monitor - so what should I do if I want that my program runs good in also resolution of monitor 1024:768?

Run fullscreen, or make sure the window is smaller. See http://alleg.sourceforge.net/stabledocs/en/alleg000.html#get_desktop_resolution.

Here's another suggestion for your reading list: read through Allegro's manaul to get a rough overview of what's available and where you can find it.

GullRaDriel
Member #3,861
September 2003
avatar

What Evert was thinking ( For the closing part )

1#include <allegro.h>
2 
3 
4 
5BITMAP *scrbuf;
6 
7 
8 
9int DONE = 0; /* will take the value 1 if someone wanna exit */
10 
11 
12 
13void close_callback()
14 {
15 DONE = 1; /* when set_close_callback is specified, intead of just quitting your program when hitting the cross in windowed mode or when pressing alt-f4 it will call this function*/
16 }
17
18 
19int main( int argc, char *argv[] )
20{
21 
22 /*system init */
23 allegro_init();
24 install_timer();
25 install_keyboard();
26 install_mouse();
27
28 set_window_title( "Simple ESC test for a b" );
29 
30 set_close_button_callback( close_callback );
31
32 /* grafic init */
33 set_color_depth( 32 );
34 set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640 , 480 , 0 , 0 );
35 scrbuf = create_bitmap( SCREEN_W , SCREEN_H );
36 clear( scrbuf );
37
38 while( !DONE && !key[ KEY_ESC ] ) /* Here is the important part: continue game since someone press ESC or push the cross-buton or alt-f4 */
39 {
40
41 /*
42 Here do some stuf, add things
43 */
44
45 clear( scrbuf );
46 circle( scrbuf , mouse_x , mouse_y , 20 , makecol( 255 , 255 , 255 ) );
47 blit( scrbuf , screen , 0 , 0 , 0 , 0 , scrbuf -> w , scrbuf -> h );
48
49
50 } /* while( ... ) */
51
52 destroy_bitmap( scrbuf );
53
54 allegro_exit();
55
56}END_OF_MAIN()

What does not work with the ESC key, can you test that attached code, a b ?

Have you really tried what all the others have said ?

Test the attached binary to see if this code do what you want.

Follow Evert's last advisement: there are enough examples and documentation in the allegro library distribution to make you understand how it works (it's commonly the case)

_

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

LennyLen
Member #5,313
December 2004
avatar

Quote:

What does not work with the ESC key

His program works exactly as it's suppsoed to (from the code's point of view). The problem is with the way it's written.

Quote:

Have you really tried what all the others have said ?

If by tried you mean "randomly placing the example code you've been given somewhere in your program," then yes.

Kauhiz
Member #4,798
July 2004

Quote:

But it is obvious that this idea kaukiz is bad because if somebody will press ESC program won't exit.

There are so many things wrong with this sentence :-X...
Firstly: It's not my idea. That's what I do and that's what I told you to do, but it's pretty much standard AFAIK; set a flag that signals the game is running, and run the game as long as the flag is true.
Secondly: like I said, it's standard. That means it does work. You're not an exception in this sense. Programming is logical, the same rules apply everywhere. We're not taking guesses at how to do what you want (although we are guessing what exactly you want), but we're telling you how to do it. We've done it before, the advice given and code posted in this thread is all valid.
Finally, the reason nothing happens when you press ESC is that you have an infinite loop. Any code outside that loop will never be executed, hence the game will never end. You say you can't do what I suggested, since you have another function after meni(). Why? Why do you have a function call you never want to be executed? Please tell me why! If you can't answer that, there's absolutely no helping you.

You can't just call function after function and zip from one procedure to the next. It'll look horrible and it won't work. You have to use states. That is: if your game is in the menu state, call the menu function. No loop inside the menu function, just call it once. If the game is still running, it'll loop over and the function will be called again. Same thing for every state your game can be in. LennyLen already provided basic example code for this. If you do that, all the problems you have addressed so far will pretty much be solved. If you refuse to do this, I refuse to help you.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

a b
Member #8,092
December 2006

So you want that my code will be:

1#include <iostream>
2#include <allegro.h>
3using namespace std;
4 
5BITMAP *game = NULL;
6BITMAP *bufor = NULL;
7BITMAP *menu = NULL;
8BITMAP *autors=NULL;
9BITMAP *instructions = NULL;
10BITMAP *frame = NULL;
11 
12 
13 
14 
15void meni();
16void autor();
17void instruction();
18void start();
19void end();
20void kaukiz();
21void something();
22 
23 
24bool gameRunning = true;
25int k;
26int go=1;
27 
28 
29int main()
30{
31allegro_init();
32 install_keyboard();
33 set_color_depth(32);
34 set_gfx_mode(GFX_AUTODETECT_WINDOWED,1024,768,0,0);
35 bufor=create_bitmap(SCREEN_W,SCREEN_H);
36 game = load_bitmap("game.bmp",NULL);
37 menu = load_bitmap("menu.bmp",NULL);
38 autors = load_bitmap("autors.bmp",NULL);
39 instructions = load_bitmap("instructions.bmp",NULL);
40 frame = load_bitmap("frame.bmp",NULL);
41 
42//yours idea :))))))))))))))))
43aa:
44meni();
45 
46 
47while(!key[ KEY_ESC ]&&go==2)
48{
49start();
50kaukiz();
51something();
52}
53while(!key[ KEY_ESC ]&&go==3)
54{
55autor();
56go=1;
57goto aa;
58
59}
60while(!key[ KEY_ESC ]&&go==4)
61{
62instruction();
63go=1;
64goto aa;
65
66}
67end();
68return 0;
69}
70END_OF_MAIN()
71 
72 
73 
74 
75 
76 
77 
78void meni()
79{
80 
81int table[4]={215,295,392,550};
82int index=0;
83
84 while(go!=0&&go!=2&&go!=3&&go!=4)
85{
86clear_to_color(bufor,makecol(255,255,255));
87blit(menu,bufor,0,0,0,0,menu->w,menu->h);
88masked_blit(frame,bufor, 0,0, 80,table[index],frame->w,frame->h);
89blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
90k= readkey()>>8;
91 if ((k==KEY_DOWN)&&index!=3)
92 {
93
94 index++;
95 }
96 else if ((k==KEY_DOWN)&&index==3)
97 {
98
99 index=0;
100 }
101 else if ((k==KEY_UP)&&index!=0)
102 {
103
104 index--;
105 }
106 else if ((k==KEY_UP)&&index==0)
107 {
108
109 index=3;
110 }
111 else if((k==KEY_ENTER)&&index==0){go=2;}
112 else if((k==KEY_ENTER)&&index==1){go=3;}
113 else if((k==KEY_ENTER)&&index==2){go=4;}
114 else if((k==KEY_ENTER)&&index==3){go=0;}
115 else if((k==KEY_ESC)){go=0;}
116 
117}
118}
119 
120 
121 
122void start()
123{
124 clear_to_color(bufor,makecol(255,255,255));
125 blit(game,bufor,0,0,0,0,game->w,game->h);
126 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
127 k = readkey()>>8;
128 while (k!=KEY_ESC&&k!=KEY_ENTER) //pressing keys as long as you press ESC or Enter
129 {
130 k = readkey()>>8;
131 }
132 if(!key[ KEY_ESC ]){go=1;}
133 else {go=0;}
134
135}
136 
137 
138 
139void kaukiz()
140{
141if(key[KEY_ESC]){go=0;} //I must check if somebody don't press ESC in last pressing key
142else
143{
144 //here is something
145 k = readkey()>>8;
146 something();
147}
148
149}
150 
151 
152void something()
153{
154if(key[KEY_ESC]){go=0;} //I must check if somebody don't press ESC in last pressing key
155else
156{
157k = readkey()>>8; //here is something
158}
159
160}
161 
162 
163 
164void instruction()
165{
166 clear_to_color(bufor,makecol(255,255,255));
167 blit(instructions,bufor,0,0,0,0,instructions->w,instructions->h);
168 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
169 k = readkey()>>8;
170 go=2;
171}
172 
173 
174void autor()
175{
176 clear_to_color(bufor,makecol(255,255,255));
177 blit(autors,bufor,0,0,0,0,autors->w,autors->h);
178 blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
179 k = readkey()>>8;
180 go=2;
181}
182 
183 
184 
185 void end()
186{
187 destroy_bitmap(game);
188 destroy_bitmap(menu);
189 destroy_bitmap(frame);
190 destroy_bitmap(instructions);
191 destroy_bitmap(autors);
192 destroy_bitmap(bufor);
193 allegro_exit();
194}

now it is more complicated .................

Evert
Member #794
November 2000
avatar

For the love of *(&#*@# stop using key[] already!
If you don't want to block with readkey(), use keypressed() first to check if there's a key in the buffer.

Look up flow control and loops in your favorite C book and get rid of those goto statements.

tobing
Member #5,213
November 2004
avatar

I would like an option to ignore a thread. For now, I'll just mark it read instead.

Anyway, sometimes it's quite fun to have a peek into this thread here. Never met anybody so impertinent and resistant to advice.

Richard Phipps
Member #1,632
November 2001
avatar

He has no understanding, that's the problem.

Evert
Member #794
November 2000
avatar

Quote:

I would like an option to ignore a thread

Isn't that what "hide thread" is for?

piccolo
Member #3,163
January 2003
avatar

it "sucks" not knowing English. you will not find the right meaning for words like "sucks" in you translators.

send me your project file zip if your using msvc++6 ill fix for you so you can move on

edit its 2007 why are there no browser to bridge language bearers yet(like star track)

wow
-------------------------------
i am who you are not am i

a b
Member #8,092
December 2006

you use too difficult words in english so I can't understand everything and make everything what you say..... I translate my game to english half day but now I translated !! :) I work over this game all the time...
It is link to this - so can you help me that after ESC game will exit because now I have many loops and when I press ESC game go to menu and no exit :/

........

piccolo
Member #3,163
January 2003
avatar

here is one ways to fix

1void start()
2{
3 eye = bone(1,3);
4 seeall();
5 while (go > 0)
6 {
7
8 if(check()==1){break;}
9
10 gogogo:
11 readkey();
12 if (key[KEY_DOWN])
13 {
14 index = index - eye;
15 if ( index < 0 ) { index = index + 10;}
16 go--;
17 eye = bone(1,3);
18 seeall();
19 }
20 else if (key[KEY_UP])
21 {
22 index = index + eye;
23 if ( index > 9 ) { index = index - 10;}
24 go--;
25 eye = bone(1,3);
26 seeall();
27 }
28 else if(key[KEY_ESC]){exitexit();}
29 else {goto gogogo; }
30 }
31}

this is what i change
else if(key[KEY_ESC]){break;} //old

to

else if(key[KEY_ESC]){exitexit();} //new

EDit:
sorry i could not compile it because i do not have that compiler.
is that what you wanted it to do

wow
-------------------------------
i am who you are not am i

Evert
Member #794
November 2000
avatar

If you want to fix anything you can start by not discarding the return values of readkey().

Quote:

you use too difficult words in english so I can't understand everything

Use a dictionary (there seem to be several on-line ones, try http://www.dict.pl/ or http://www.polish-dictionary.com/ - I don't know if they're good), babelfish (seems babelfish doesn't do Polish) or ask!

piccolo
Member #3,163
January 2003
avatar

the problem was the trance of the manual. this is how it went down you was looking for things to do keyboard input. so you found readkey(); and key[]. what you did not trance is that they both are tools to do the same thing in 2 different ways. thats why you combined them.

int val;
val = readkey();

if ((val >> 8) == KEY_ESC){exitexit();} /* by scancode */

same thing

if(key[KEY_ESC]){exitexit();}

wow
-------------------------------
i am who you are not am i

Kauhiz
Member #4,798
July 2004

trance?

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

piccolo
Member #3,163
January 2003
avatar

interpretation

wow
-------------------------------
i am who you are not am i

hexagon
Member #8,207
January 2007
avatar

piccolo, those two statements are not the same thing.
readkey() is a blocking function, while the check in the key array returns the state of a key (non-blocking).

http://www.allegro.cc/manual/api/keyboard-routines/readkey
http://www.allegro.cc/manual/api/keyboard-routines/key

piccolo
Member #3,163
January 2003
avatar

quote: they both are tools to do the same thing in 2 different ways.

wow
-------------------------------
i am who you are not am i

Evert
Member #794
November 2000
avatar

No, they're not. readkey() and key[] serve completely different purposes. Sure, you can use one to emulate the other (sortof, you'll need keypressed() as well), but that's like using a knife to drive a screw. Or a screwdriver to cut your meat.

hexagon
Member #8,207
January 2007
avatar

What do you mean by same thing?

One waits for a key to be pressed and returns which key it is,
the other one returns the state of a key. That's not the same thing.

piccolo
Member #3,163
January 2003
avatar

they are both tools that can be used to do something based on a key press.i gave examples in my post of them doing the same thing.

wow
-------------------------------
i am who you are not am i



Go to: