I wrote a framework code in the second article in this series of tutorials. After testing, although it can be run, there are indeed some bugs, such as failure to correctly execute the game_destory () function, resulting in Memory leakage. So now I have to write a new one and send it here. The new framework is implemented using a simple state machine.
The complete code is as follows:
// Header file # define ALLEGRO_NO_MAGIC_MAIN # include <Windows. h> # include <allegro5/allegro. h> # pragma comment (lib, "allegro-5.0.7-monolith-mt-debug.lib ") // link the database of Allegro ////////////////////////////////// //////////////////////////////////////// /// // function int game_init (); // initialize the game int game_run (); // enter the game loop int game_update (); // The logic processing function int game_render (); // The rendering function int game_destory (); // release the resource int game_msg (); // Message Processing // constant cons T int WIN_WIDTH = 800; // window width const int WIN_HEIGHT = 600; // window height // global variable ALLEGRO_DISPLAY * display; // display the device ALLEGRO_EVENT_QUEUE * queue; // event queue ALLEGRO_EVENT my_event; // event enum GAME_STATE {GAME_INIT, GAME_RUN, GAME_DESTORY, GAME_END}; GAME_STATE FSM = GAME_INIT; //////////////////////////////////////// //////////////////////////////////////// ////////// int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPS TR lpCmdLine, int nShowCmd) {int error = 0; while (true) {switch (FSM) {case GAME_INIT: error = game_init (); if (error! = 0) {FSM = GAME_DESTORY; break;} break; case GAME_RUN: game_run (); break; case GAME_DESTORY: game_destory (); break; case GAME_END: return error ;}} return error;} int game_init () {/_/plug-in Initialization _/_/_/_/_/_/_/_/_/__/_/_/_//_/_ /_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_ /if (! Al_init () return 1; al_install_mouse (); al_install_keyboard (); display = al_create_display (WIN_WIDTH, WIN_HEIGHT); if (! Display) return 2; al_set_window_title (display, "Framework"); // initialize the event queue. The received messages are stored in the queue = al_create_event_queue (); // specify the device message al_register_event_source (queue, queue (); queue (queue, al_get_mouse_event_source (); queue (queue, al_get_display_event_source (display )); // _/load the font _/_/_/_/_/_/_/_/_/__/_/_/_//_/_/ _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ //_ /_/Load image _/_/_/_/_/_/_/_/_/__/_/_/_//_/_/_/_ /_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_///_ /_/load control _/_/_/_/_/_/_/_/_/__/_/_/_//_/_/_/_ /_/FSM = GAME_RUN; return 0;} int game_msg () {al_wait_for_event (queue, & my_event); if (my_event.type = ALLEGRO_EVENT_DISPLAY_CLOSE) return 98; // if (my_event.type = ALLEGRO_EVENT_KEY_CHAR) {if (my_event.keyboard.keycode = ALLEGRO_KEY_ESCAPE) when the window is closed )// When the ESC key is pressed, return 98;} return 0;} int game_update () {return 0;} int game_render () {al_clear_to_color (al_map_rgb (0, 0, 0 )); al_flip_display (); return 0;} int game_run () {double t_now = 0.0; // double t_pre = 0.0; int error = 0; if (! Al_is_event_queue_empty (queue) {// check whether a new event error = game_msg () exists in the correct event; // if (error! = 0 & error! = 98) return error; if (error = 98) {FSM = GAME_DESTORY; return error ;}} else {t_now = al_get_time (); if (t_now-t_pre> = 0.033) {game_update (); game_render (); t_pre = t_now;} else {al_rest (0) ;}} return 0 ;}int game_destory () {FSM = GAME_END; return 0 ;}