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

a b, use a boolean value to tell whenever a key has been pressed or not, and when pressing a key check if a key has already been pressed and if, to not execute the action again, i think this is your problem right? :)

I did not have time to check this, just understand the keyhasbeenpressed part. :).

bool keyhasbeenpressed; << this.

int meni()
{
while(go!=0)
{
clear_to_color(bufor,makecol(255,255,255));
blit(menu,bufor,0,0,0,0,menu->w,menu->h);

masked_blit(frame,bufor, 0,0, 80,table[index],frame->w,frame->h);

blit(bufor,screen,0,0,0,0,SCREEN_W,SCREEN_H);
readkey(); << remove this.

if (key[KEY_DOWN]&&index!=3&&keyhasbeenpressed==false)
{
keyhasbeenpressed=true;
index++;
}
else if (key[KEY_DOWN]&&index==3&&keyhasbeenpressed==false)
{
keyhasbeenpressed=true;
index=0;
}
else if (key[KEY_UP]&&index!=0&&keyhasbeenpressed==false)
{
keyhasbeenpressed=true;
index--;
}
else if (key[KEY_UP]&&index==0&&keyhasbeenpressed==false)
{
keyhasbeenpressed=true;
index=3;
}
else if(key[KEY_ENTER]&&index==3&&keyhasbeenpressed==false)
{return 0;keyhasbeenpressed=true;}
else if(key[KEY_ENTER]&&index==0&&keyhasbeenpressed==false)
{start();keyhasbeenpressed=true;}
else if(key[KEY_ENTER]&&index==1&&keyhasbeenpressed==false)
{instruction();keyhasbeenpressed=true;}
else if(key[KEY_ENTER]&&index==2&&keyhasbeenpressed==false)
{autor();keyhasbeenpressed=true;}

if(!key[KEY_UP]&&!key[KEY_ENTER]&&!key[KEY_DOWN])keyhasbeenpressed=false;
}
}

tobing
Member #5,213
November 2004
avatar

Use allegro dialogs to do your menu. They have all this UP DOWN stuff already implemented, so you don't need to worry about all that. You also get your callback functions to react on button presses, and you can control your menu with keyboard and mouse. Don't try to re-do all this, it's already contained in allegro.

a b
Member #8,092
December 2006

heheh no :)

This is my simple menu and frame- when you press KEY_DOWn/UP frame is going down/up, and if you press ENTER you go on another map, but without readkey() it is going strange:
http://img146.imageshack.us/img146/2143/menusx6.png
http://img462.imageshack.us/img462/1453/bvu8.jpg

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

i can't really tell what's so wrong with the pictures as i don't know whats right... but are you sure that you think what i posted was wrong??

Evert
Member #794
November 2000
avatar

Here's the proper way of doing the code you posted with readkey() alone (which is what you should be using instead of key[]):

   int k = readkey()>>8;
 
   if (k==KEY_DOWN) {
      ...
   } else if (k == KEY_UP]) {
      ...
   } else if (k == KEY_ENTER]) {
      ...
   }

As others have said, using a DIALOG is probably a better way to do this.

a b
Member #8,092
December 2006

Evert now it is good running - thanks - but why in readkey()>>8; is ">>8" ????

And what is "dialog" - I don't know english very well so mayby I don't understand - in my dictionary dialog=conversation ?

Evert
Member #794
November 2000
avatar

Quote:

but why in readkey()>>8; is ">>8"

Read the Allegro manual entry for readkey(). Check your favourite C book for information on the bit-shift operators << and >>.

Quote:

And what is "dialog"

Normally short for "dialog box", the things that pop up on your computer screen where you press "Ok" or "Cancel". In this specific case a dialog object, which is part of Allegro's GUI system. Again, see the manual.

a b
Member #8,092
December 2006

If I good remember:
a= b >> 3

a 0000 1111
b 0000 0001

but "readkey()>>8" - is strange........

and how can we good write this:

readkey();
        while (!key[KEY_ESC]&&!key[KEY_ENTER])
        {
          readkey();
        }

???

Richard Phipps
Member #1,632
November 2001
avatar

Read the manual for the keyboard section.. please.

a b
Member #8,092
December 2006

nooooooo - your idea with readkey()>>8 is bad :( I can't declare int k = readkey()>>8; as a global variable because frame wrong moves:
it is also in attachment

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

I can't write readkey()>>8 in each place in my program because it is going wrong:

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

Everything is wrong, previously how I wrote readkey() and key[] - everything was okey, it was no problems !! I am angry - sorry, but I am angry.......
Manual don't like me.......

LennyLen
Member #5,313
December 2004
avatar

X-G said:

Posted on 12-12-2006 3:17 AM

Guys, this is a lost cause. Just... do something better.

Oh how right he was.

a b
Member #8,092
December 2006

LennyLen if you are so intelligent, say me why readkey()>>8 in two programs UP wrong runs.

tobing
Member #5,213
November 2004
avatar

Do as Evert (and others) said. READ THE MANUAL.

a b
Member #8,092
December 2006

I readed but it isn't simply when I can't good english........

Richard Phipps
Member #1,632
November 2001
avatar

If you can't understand the english well enough then ok. But if you can, then you don't seem to be trying to understand or think as to why it is not working based on what you read.

Kauhiz
Member #4,798
July 2004

edit: nvm

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

a b
Member #8,092
December 2006

Now I use English translator 3 but it isn't very good.

I gave more parentheses, because I thought that this can their fault :)

Richard Phipps
Member #1,632
November 2001
avatar

Do it like this:

k = readkey() >> 8;

if ((k == KEY_DOWN) && index!=3)
         {
               
               index++;         
         } 
if ((k == KEY_DOWN) && index == 3)
         {
               
               index=0;         
         }

Etc..

When you call readkey it waits for a key to be pressed, if you call it again it waits for another key to be pressed. So you only want to call it once, then check which key was pressed and deal with all those conditions.

Does that help?

a b
Member #8,092
December 2006

We have in menu:

"new game" index 0

"instructions" index 1

"autor" index 2

"end game" index 3

Richard your idea is also wrong - this see that your idea shoud be good but isn't - very strange :(

the problem is that I can't do int k = readkey()>>8; global variable

Richard Phipps
Member #1,632
November 2001
avatar

I'm going to recommend that you look at my Game Creation Articles and try to understand how to build a working game. Most of them have more code than English and I think they will help you once you understand them.

http://www.reflectedgames.com/create.html

a b
Member #8,092
December 2006

Richard but I don't understand why I can't do int k = readkey()>>8; global variable

Richard Phipps
Member #1,632
November 2001
avatar

Think about it..

You can't call a function like that. And even if you could, it would only work ONCE.

a b
Member #8,092
December 2006

yyyy ????? I have this program in attachment .... and I do not know about what already completely it walks

Richard Phipps
Member #1,632
November 2001
avatar

if you put k = readkey() >> 8 outside of any functions, then how are you going to call it? It is not called automatically, and so k will not be correct.

You need to call it each time you want to read 1 new key.

Kauhiz
Member #4,798
July 2004

You're asking why you can't you do int k = readkey()>>8;? Seriously? I don't think bad English or the manual are the problems here...

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



Go to: