set_close_button_callback(3)

πŸ“¦ version 4.4.3.1, in fedora 43, fedora 44

✨ set_close_button_callback(3) ✨

okay bestie, welcome to the Allegro manual πŸŽ€ β€” this one's from the Allegro game programming library and it's all about what happens when someone clicks the lil X on your window πŸͺŸπŸ’–


πŸ’– NAME πŸ’–

set_close_button_callback β€” handles the user clicking on the close button of the window. Allegro game programming library.


🌸 SYNOPSIS 🌸

#include <allegro.h>

int set_close_button_callback(void (*proc)(void));

πŸ¦‹ DESCRIPTION πŸ¦‹

So on platforms that actually have a close button, this routine installs a callback function to handle the close event. Translating that from nerd to bestie: when the user clicks the close button on your program's window, or any equivalent device, the function you specify here gets called. periodt. πŸ’…

🫢 Now listen, this is the important part. This function should not generally try to exit the program or save any data itself. It could literally get called at any time, and there's usually a risk of conflict with the main thread of the program. Not the vibe we're going for. 😭

Instead β€” and this is the move β€” you should set a flag during this function, and then test that flag on a regular basis in the main loop of the program. Set the flag, check the flag, slay. 🌈✨

Pass NULL as the proc argument to this function to disable the close button functionality β€” and heads up, that disabled state is the default. So if you want close-button behaviour, you gotta opt in, bestie. πŸ‘‘

πŸ’€ Also note that Allegro cannot intercept the close button of a DOS box in Windows. Just cannot. Sorry babe.

🌸 And one more thing! The callback you supply is also called under MacOS X when the user hits Command-Q or selects "Quit" from the application menu. Mac users get the same treatment, iconic behaviour honestly. πŸ“

Example time, this is giving main character energy:

volatile int close_button_pressed = FALSE;

void close_button_handler(void)
{
   close_button_pressed = TRUE;
}
END_OF_FUNCTION(close_button_handler)
...

allegro_init();
LOCK_FUNCTION(close_button_handler);
set_close_button_callback(close_button_handler);
...

while (!close_button_pressed)
   do_stuff();

⭐ RETURN VALUE ⭐

Returns zero on success and non-zero on failure (e.g. the feature is not supported by the platform). Zero = ate. Non-zero = the platform said no. πŸ₯²


🧚 SEE ALSO 🧚


k bye, go lock your functions and stay safe out there πŸ’œ This has been the Allegro manual, version 4.4.3, and this page is set_close_button_callback(3) β€” mwah, love u, go handle that close button πŸ’–πŸŽ€πŸ¦‹βœ¨

πŸ“¦ where this page comes from