Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to reset keystate Allegro5?

This thread is locked; no one can reply to it. rss feed Print
How to reset keystate Allegro5?
Marcin Piwko
Member #15,823
December 2014

1)After I run my program, firstly on the screen appears text message: "Try to catch me!" "press Enter to go.." 2)Then user has to press the Enter button if he wants go to Main Menu. Key state-Enter in this case, is stored in variable called keyState. And because of that, there is a problem when user want to see menu.. I programmed that if frame(used to choose option from menu) is located surrounding start game option (it means: menuPosition=0) and user click the enter button, the game is going to start. There is a problem cuz after user pressed the Enter button (point 1) ) keyState stores Enter and I realized that because of that user cant see the Main Menu but he is transformed automaticly to the game. Can anyone tell me how to erase/reset the keyState after user click the enter button in point 1) or is there any other option to see Main Menu after clicking Enter in point 1) ?

#SelectExpand
1#include<allegro5\allegro5.h> 2#include<allegro5\allegro_native_dialog.h> 3#include<allegro5\allegro_image.h> 4#include<allegro5\allegro_font.h> 5#include<allegro5\allegro_ttf.h> 6#include<allegro5\allegro_primitives.h> 7 8#include<iostream> 9using namespace std; 10 11int main(){ 12 int ScreenWidth = 800, ScreenHeight = 600; 13 if (!al_init()) 14 { 15 al_show_native_message_box(NULL, "ERROR", NULL, "Could not initialize Allegro 5", NULL, NULL); 16 return -1; 17 } 18 19 al_set_new_display_flags(ALLEGRO_WINDOWED); 20 ALLEGRO_DISPLAY *display = al_create_display(ScreenWidth, ScreenHeight);//creating window with dimensions: 800:600 px 21 al_set_window_position(display, 200, 50);//place from left top positioning the frame of display 22 al_set_window_title(display, "Marcin Piwko RLZ"); 23 24 if (!display)//show this message if sth wrong with display 25 { 26 al_show_native_message_box(display, "Sample Title", "Display Settings", "Display window was not created succesfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 27 return -1; 28 } 29 30 //Fonts init 31 al_init_font_addon();//initialization font addon 32 al_init_ttf_addon();//initialization ttf(true type font) addon 33 34 //INTRO Fonts 35 ALLEGRO_FONT *fontOrbionBlack36 = al_load_font("Orbitron Black.ttf", 36, NULL); 36 ALLEGRO_FONT *fontOrbionBlack18 = al_load_font("Orbitron Black.ttf", 18, NULL); 37 38 const float FPS = 60; 39 const float frameFPS = 15;//1) 40 41 enum Direction{ DOWN, LEFT, RIGHT, UP }; 42 43 bool done = false, draw = true, active = false; 44 float x = 10, y = 10; 45 float moveSpeed = 5; 46 int dir = DOWN, 47 sourceX = 32, sourceY = 0; 48 49 al_install_keyboard(); 50 al_init_image_addon(); 51 al_init_primitives_addon(); 52 53 ALLEGRO_COLOR electricBlue = al_map_rgb(44, 117, 255); 54 55 ALLEGRO_BITMAP *player = al_load_bitmap("PlayerImage1.png"); 56 57 ALLEGRO_KEYBOARD_STATE keyState; 58 59 ALLEGRO_TIMER *timer = al_create_timer(3.0 / FPS);//velocity of player movement 60 ALLEGRO_TIMER *frameTimer = al_create_timer(0.5 / frameFPS);//2) 61 ALLEGRO_TIMER *introTimer = al_create_timer(4.0 / FPS); 62 63 64 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 65 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 66 al_register_event_source(event_queue, al_get_timer_event_source(frameTimer)); 67 al_register_event_source(event_queue, al_get_timer_event_source(introTimer)); 68 69 70 al_register_event_source(event_queue, al_get_display_event_source(display)); 71 al_register_event_source(event_queue, al_get_keyboard_event_source()); 72 73 74 int menuXStartPosition = ScreenWidth / 2 - 120; 75 int menuYStartPosition = ScreenHeight / 5 + 36; 76 int menuX = menuXStartPosition, menuY = menuYStartPosition; 77 int moveMenuSpeed = 36; 78 bool activeMenu = false; 79 80 enum loopStates{ showFirstMessage, showMenu, showStartGame, showBestScores, showExit, NUM_LOOPSTATES }; 81 bool keysLoopStates[] = { false, false, false }; 82 enum menuChoosePosiotion{ startGameCh, showBestScoresCh, showExitCh, NUM_MENUCHOOSEPOSITION }; 83 int menuPosition = 0; 84 bool drawMenuState = false; 85 86 int loopTransition = 0; 87 88 al_start_timer(timer); 89 al_start_timer(frameTimer); 90 al_start_timer(introTimer); 91 92 93 while (!done) 94 { 95 ALLEGRO_EVENT events; 96 al_wait_for_event(event_queue, &events); 97 al_get_keyboard_state(&keyState); 98 if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 99 { 100 done = true; 101 } 102 103 else if (loopTransition == showFirstMessage) 104 { 105 al_clear_to_color(al_map_rgb(0, 0, 0)); 106 al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "Try to catch me!"); 107 al_draw_text(fontOrbionBlack18, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2 + 36 + 10, ALLEGRO_ALIGN_CENTRE, "press Enter to go.."); 108 al_draw_text(fontOrbionBlack18, al_map_rgb(44, 117, 255), 0, ScreenHeight - 18, ALLEGRO_ALIGN_LEFT, "made by Marcin Piwko"); 109 al_flip_display(); 110 loopTransition = 1; 111 al_wait_for_event(event_queue, &events); 112 } 113 if (events.type == ALLEGRO_EVENT_TIMER) 114 { 115 116 if (loopTransition == showMenu) 117 { 118 if (events.timer.source == introTimer) 119 { 120 121 if (al_key_down(&keyState, ALLEGRO_KEY_ENTER)) 122 { 123 activeMenu = true; 124 125 } 126 if (activeMenu == true) 127 { 128 129 drawMenuState = true; 130 if (al_key_down(&keyState, ALLEGRO_KEY_DOWN)) 131 { 132 menuY += moveMenuSpeed; 133 menuPosition++; 134 cout << menuPosition << endl; 135 136 } 137 else if (al_key_down(&keyState, ALLEGRO_KEY_UP)) 138 { 139 menuY -= moveMenuSpeed; 140 menuPosition--; 141 cout << menuPosition << endl; 142 } 143 else if (al_key_down(&keyState, ALLEGRO_KEY_ENTER)) 144 { 145 if (menuPosition == startGameCh) 146 { 147 loopTransition = showStartGame; 148 } 149 if (menuPosition == showExitCh) 150 { 151 done = true; 152 } 153 } 154 //********************************************** 155 else 156 drawMenuState = false; 157 } 158 159 160 161 if (drawMenuState){ 162 al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 6, ALLEGRO_ALIGN_CENTRE, "Main Menu"); 163 al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 5 + 36, ALLEGRO_ALIGN_CENTER, "Star game"); 164 al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 5 + 36 * 2 + 4, ALLEGRO_ALIGN_CENTER, "Best Scores"); 165 al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 5 + 36 * 3 + 4, ALLEGRO_ALIGN_CENTER, "Exit"); 166 167 al_draw_rectangle(menuX, menuY, menuX + 250, menuY + 36, electricBlue, 1.0); 168 169 al_flip_display(); 170 al_clear_to_color(al_map_rgb(0, 0, 0)); 171 drawMenuState = true; 172 } 173 } 174 175 } 176 177 178 179 if (loopTransition == showStartGame) 180 { 181 if (events.timer.source == timer) 182 { 183 active = true; 184 if (al_key_down(&keyState, ALLEGRO_KEY_DOWN)) 185 { 186 y += moveSpeed; 187 dir = DOWN; 188 } 189 else if (al_key_down(&keyState, ALLEGRO_KEY_UP)) 190 { 191 y -= moveSpeed; 192 dir = UP; 193 } 194 else if (al_key_down(&keyState, ALLEGRO_KEY_RIGHT)) 195 { 196 x += moveSpeed; 197 dir = RIGHT; 198 } 199 else if (al_key_down(&keyState, ALLEGRO_KEY_LEFT)) 200 { 201 x -= moveSpeed; 202 dir = LEFT; 203 } 204 //********************************************** 205 else 206 active = false; 207 } 208 209 else if (events.timer.source = frameTimer)//6) 210 { 211 if (active == true) 212 sourceX += al_get_bitmap_width(player) / 3; 213 else 214 sourceX = 32; 215 216 if (sourceX >= al_get_bitmap_width(player)) 217 sourceX = 0; 218 219 sourceY = dir; 220 } 221 draw = true; 222 223 224 if (draw){ 225 al_draw_bitmap_region(player, sourceX, sourceY * al_get_bitmap_height(player) / 4, 32, 32, 226 x, y, NULL); 227 al_flip_display(); 228 al_clear_to_color(al_map_rgb(0, 0, 0)); 229 } 230 } 231 } 232 } 233 al_destroy_display(display); 234 al_destroy_timer(timer); 235 al_destroy_bitmap(player); 236 al_destroy_event_queue(event_queue); 237 return 0; 238}

Trent Gamblin
Member #261
April 2000
avatar

Use keyboard events. You've already registered the keyboard event source, so in your loop you can check for key presses with:

if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ENTER) {
    // react to the event here
}

al_key_down will return true if the key is currently pressed, which means for 1/2 a second or so (as long as you hold the key down) it will return 'true'. The ALLEGRO_EVENT_KEY_DOWN event will be fired off only once per key press, and ALLEGRO_EVENT_KEY_UP will be fired off once per key release.

Marcin Piwko
Member #15,823
December 2014

I changed my command for yours and now even game doesnt start when i press enter..
I don't know where do I make a mistake?

#SelectExpand
1al_flip_display(); 2 loopTransition = 1; 3 } 4 if (events.type == ALLEGRO_EVENT_TIMER) 5 { 6 7 if (loopTransition == showMenu) 8 { 9 if (events.timer.source == introTimer) 10 { 11 12 <u>if (events.type == ALLEGRO_EVENT_KEY_DOWN && events.keyboard.keycode == ALLEGRO_KEY_ENTER) </u> 13 { 14 activeMenu = true; 15 16 } 17 if (activeMenu == true)

Trent Gamblin
Member #261
April 2000
avatar

if (event.type == ALLEGRO_EVENT_TIMER) {
    if (event.type == ALLEGRO_EVENT_KEY_DOWN ...) {
    }
}

Do you think the second if will ever be true?

Marcin Piwko
Member #15,823
December 2014

Ok i solve my problem this way, and now it works correctly:

#SelectExpand
1else if (loopTransition == showFirstMessage) 2 { 3 al_clear_to_color(al_map_rgb(0, 0, 0)); 4 al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "Try to catch me!"); 5 al_draw_text(fontOrbionBlack18, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2 + 36 + 10, ALLEGRO_ALIGN_CENTRE, "press Enter to go.."); 6 al_draw_text(fontOrbionBlack18, al_map_rgb(44, 117, 255), 0, ScreenHeight - 18, ALLEGRO_ALIGN_LEFT, "made by Marcin Piwko"); 7 al_flip_display(); 8 if (events.type == ALLEGRO_EVENT_KEY_DOWN && events.keyboard.keycode == ALLEGRO_KEY_ENTER) 9 { 10 loopTransition = 1; 11 al_clear_to_color(al_map_rgb(0, 0, 0)); 12 al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 6, ALLEGRO_ALIGN_CENTRE, "Main Menu"); 13 al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 5 + 36, ALLEGRO_ALIGN_CENTER, "Star game"); 14 al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 5 + 36 * 2 + 4, ALLEGRO_ALIGN_CENTER, "Best Scores"); 15 al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 5 + 36 * 3 + 4, ALLEGRO_ALIGN_CENTER, "Exit"); 16 17 al_draw_rectangle(menuX, menuY, menuX + 250, menuY + 36, electricBlue, 1.0); 18 19 al_flip_display(); 20 al_clear_to_color(al_map_rgb(0, 0, 0)); 21 al_rest(1.0); 22 } 23 24 } 25 if (events.type == ALLEGRO_EVENT_TIMER) 26 { 27 28 if (loopTransition == showMenu) 29 { 30 if (events.timer.source == introTimer) 31 { 32 33 drawMenuState = true; 34 if (al_key_down(&keyState, ALLEGRO_KEY_DOWN)) 35 { 36 menuY += moveMenuSpeed; 37 menuPosition++; 38 cout << menuPosition << endl; 39 40 } 41 else if (al_key_down(&keyState, ALLEGRO_KEY_UP)) 42 { 43 menuY -= moveMenuSpeed; 44 menuPosition--; 45 cout << menuPosition << endl; 46 } 47 else if (al_key_down(&keyState, ALLEGRO_KEY_ENTER)) 48 { 49 if (menuPosition == startGameCh) 50 { 51 loopTransition = showStartGame; 52 } 53 if (menuPosition == showExitCh) 54 { 55 done = true; 56 } 57 } 58 //********************************************** 59 else 60 drawMenuState = false; 61 }

Go to: