set_close...n_callback(3alleg4)

πŸ’– NAME πŸ’–

set_close_button_callback β€” bestie, this one handles the user clicking on the close button of the window. It's from the Allegro game programming library, and yes it's exactly as iconic as it sounds. πŸŽ€


✨ SYNOPSIS ✨

#include <allegro.h>

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

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so on platforms that actually have a close button (some don't, rude), this routine installs a callback function to handle the close event. Translating that: when the user clicks the close button on your program's window, or pokes any equivalent device that means "close this", the function you specified right here gets called. That's it. That's the whole vibe. πŸ’…

Not me about to warn you about the main character energy of this callback, but like β€” please don't let this function try to exit the program or save any data itself. It could get called at literally any time, and there's usually a risk of conflict with the main thread of the program. 😭

So instead of doing the dramatic exit in the callback: set a flag in this function, and test that flag on a regular basis in the main loop of the program. That's the move. Periodt. 🫢

Pass NULL as the proc argument to this function to disable the close button functionality β€” and fun fact, that's the default state. So if you never call this at all, nothing happens.

Also, heads up: Allegro cannot intercept the close button of a DOS box in Windows. Nope. Can't do it. πŸ™ˆ

And another one β€” the supplied callback is also called under MacOS X when the user hits Command-Q or selects "Quit" from the application menu. So like, keep that in mind, bestie. πŸ“

Example, look at her:

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). So if you get a non-zero back, it's giving "this platform said no". πŸ’€


🌈 SEE ALSO 🌈


Allegro version 4.4.3 β€” that's the source and version this page came from, mwah. Go forth and close those windows responsibly, bestie. πŸ‘‘βœ¨πŸŒΈ