I like using Windows (I use Windows XP Home Edition), and although I try to keep the programming here generic, I prefer to program windowed applications. One of the great features Allegro brings to the table for MS Windows programmers is WinMain()/WinProc() hiding. Instead, you get to use main() and all you have to do is add a macro to the end of it to hide them. I like that I don't have to worry about windows-specific code, and I can still have a window by setting the graphics mode with _WINDOWED tagged to the end of the mode type.
I see alot of games made specifically fullscreen only and I wonder why this is. If you can program a fullscreen Windows game, you only need to jump a few hoops to make it windowed. I also like having the option of switching between fullscreen and windowed, and I tend to throw it into my demos as a feature. This tutorial addresses closing a window in an MS Windows environment.
Consider the following code:
#include <allegro.h> int main() { allegro_init(); set_gfx_mode (GFX_AUTODETECT_WINDOWED, 200, 200, 0, 0); textout (screen, font, "Close Me", 0, 0, 15); while (true) { // do nothing } allegro_exit(); return 0; } END_OF_MAIN() |
When compiled properly (see Tutorial 1: Setting Up and Using Allegro with Dev-C++) and run, it will yield a window similar to that below:
If you try to close the program with the close button, you will get the following pop-up:
By clicking No, you go back to the program; only if you click Yes will you exit. I find this annoying. For small demos, I employ an Allegro function called set_window_close_button(), which allows you to enable or disable the close button. Let's add this function to the program:
#include <allegro.h> int main() { allegro_init(); set_gfx_mode (GFX_AUTODETECT_WINDOWED, 200, 200, 0, 0); set_window_close_button (0); // turn off the close button textout (screen, font, "Close Me", 0, 0, 15); while (true) { // do nothing } allegro_exit(); return 0; } END_OF_MAIN() |
Now the program runs like this:
Notice the close button is now grayed out. This is a great (read: quick and dirty) way to get rid of that silly message. We're rid of the message, but I wouldn't mind being able to close my programs with the button and not have to see that message every time. What we can do is use the Allegro function set_window_close_hook(), and redirect the close button's action to what we want it to do. Here is a very simple way to do what I want:
#include <allegro.h> int close_button_clicked = 0; void close_button_function () { close_button_clicked = 1; } int main() { allegro_init(); set_gfx_mode (GFX_AUTODETECT_WINDOWED, 200, 200, 0, 0); set_window_close_hook (close_button_function); textout (screen, font, "Close Me", 0, 0, 15); while (!close_button_clicked) { // wait for the close button to be clicked } allegro_exit(); return 0; } END_OF_MAIN() |
Now the button works as expected. I warn you to heed the message of the pop-up we got rid of: be sure you save any information and free any memory you need to before you exit the program. This is standard coding procedure, and you would do well to do so.
Any questions or information on other windowing environments that relate to this tutorial would be appreciated. Have fun!