|
Allegro 5 States |
lroyan
Member #13,669
October 2011
|
Hello! there's something I want to ask. I'm finishing a game in allegro 5, But I'm having trouble with the menus, I want the game to have options and Instructions, but I can't insert the game code in a whole function to call it when the game option is selected. Is there a way to do that? How can I do it? |
jmasterx
Member #11,410
October 2009
|
You're going to have to be more detailed about what you are asking. I, myself, cannot really understand what you are asking. I know it is something related to menus but I'm not sure what else you mean. If you mean that you want to do scene management eg, go from menu to options, you could do it through polymorphic scenes, but I'm not sure how else. Maybe someone else knows a better way. Agui GUI API -> https://github.com/jmasterx/Agui |
Thomas Fjellstrom
Member #476
June 2000
|
Each set of screen functions can have its own event loop, if you want to stick to simple C code. Or you can have one event loop, and call specific scene/screen update and draw functions. (then it would work similarly to how it would normally be done in C++) -- |
bamccaig
Member #7,536
July 2006
|
More or less dispatch the logic and updates to a state-specific class or set of functions. Example of doing it with a couple of simple switch statements: 1int dispatch_update(game_state * state)
2{
3 game_state new_state = GAME_STATE_NONE;
4
5 switch(state)
6 {
7 case GAME_STATE_SPLASH:
8 new_state = update_splash();
9 break;
10 case GAME_STATE_MAINMENU:
11 new_state = update_mainmenu();
12 break;
13 case GAME_STATE_OPTIONSMENU:
14 new_state = update_optionsmenu();
15 break;
16 case GAME_STATE_GAME:
17 new_state = update_game();
18 break;
19 }
20
21 if(new_state == GAME_STATE_NONE)
22 return -1;
23
24 *state = new_state;
25
26 return 0;
27}
28
29int dispatch_draw(const game_state state)
30{
31 switch(state)
32 {
33 case GAME_STATE_SPLASH:
34 return draw_splash();
35 case GAME_STATE_MAINMENU:
36 return draw_mainmenu();
37 case GAME_STATE_OPTIONSMENU:
38 return draw_optionsmenu();
39 case GAME_STATE_GAME:
40 return draw_game();
41 default:
42 return -1;
43 }
44}
(In this made up example, the new game state is returned by the logic update function for each state) You could instead use function pointers and change the state that way. 1int set_game_state(
2 const game_state state,
3 game_state (**update)(void),
4 int (**draw)(void))
5{
6 switch(state)
7 {
8 case GAME_STATE_SPLASH:
9 *update = update_splash;
10 *draw = draw_splash;
11 break;
12 case GAME_STATE_MAINMENU:
13 *update = update_mainmenu;
14 *draw = draw_mainmenu;
15 break;
16 case GAME_STATE_OPTIONSMENU:
17 *update = update_optionsmenu;
18 *draw = draw_optionsmenu;
19 break;
20 case GAME_STATE_GAME:
21 *update = update_game;
22 *draw = draw_game;
23 break;
24 default:
25 return -1;
26 }
27
28 return 0;
29}
30
31...
32
33game_state (*update)(void);
34int (*draw)(void);
35
36set_game_state(GAME_STATE_SPLASH, &update, &draw);
37
38...
39
40game_state new_state = update();
41
42...
43
44draw();
45
46...
47
48if(new_state != GAME_STATE_NONE)
49 set_game_state(new_state, &update, &draw);
And if using C++ you could instead use polymorphic types. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
David Couzelis
Member #10,079
August 2008
|
If I make "each set of screen functions have its own event loop" as Thomas said, what happens in regards to keyboard events? Will every registered event queue receive every keyboard event? |
Thomas Fjellstrom
Member #476
June 2000
|
nope, only one event loop can run at a time. Which one depends on what screen you're in. -- |
jmasterx
Member #11,410
October 2009
|
ex: void current_screen_process_event(ALLEGRO_EVENT* evt) so if the current scene function pointer points to the menu function, the menu will receive all events. The way I have mine is I pass it to the scene, then the scene passes it to the default handler and indicates if it handled the event. This means the default handler can do house keeping like resolution changes. Agui GUI API -> https://github.com/jmasterx/Agui |
|