I'm trying to make a Win32/*nix console-based ASCII game. I want to use no libraries whatsoever that aren't standard C++ or on *nix/windows(.h).
I want it to be structured like a game loop. Aka:
while (!WIN_CLOSE_FUNCTION()) {
//Do crap
}
//Do other shutdown crap
return 0;
Can anyone point me to what function this would be? If it is platform dependent, give me one example on Windows and *nix.
Similar question that might help you What happens when you close a c++ console application
The accepted answer is: Closing a c++ console app with the "x" in the top corner throws an CTRL_CLOSE_EVENT which you could catch and process if you set a control handler using the SetConsoleCtrlHandler function.
Useful links:
On *nix:
On Linux and other Unix systems, the console runs as a separate process. As you close the shell, it sends the SIGHUP signal to the currently active process or processes that are not executed in the background. If the programmer does not handle it, the process simply terminates. The same signal is sent if you close the SSH session with a terminal and an active process.
answer provided by @Zyx in the question linked above
There isn't such a function per se, but both Unix and Windows will send a signal (
SIGHUP
under Unix,SIGBREAK
under Windows) to all processes in the process group when the window on which the process group depends is closed. So all you have to do is catch the signal and set a flag, which you test in the loop:If you're doing any input from the console in the loop, you'll have the be prepared for the input to fail (which you should be anyway).
For the Unix/Linux console, there is no such function. The closest you can do is to catch the signal
SIGHUP
which is sent when losing the terminal. However be aware that the things you can do in a signal handler are quite limited. Probably the closest to your loop would be (note: untested code):