Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

elis-cool

Exit without goto?

This topic is 6053 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know of a good way to exit your game from anywhere in your code as you can only return (to the op sys) from the WinMain function, eg without having a goto placed just above the return 0;? as we all know goto''s are bad but in this case I myself dont see anything too bad with them but I''m just seeing what you guys do... CEO Plunder Studios

Share this post


Link to post
Share on other sites
Advertisement
Look up the WinAPI function ExitProcess.
But that won't make it very easy to shutdown all the systems and release your resources, so there are a couple of ways that I use:
+ I can set a Finished variable to true, and then the main game loop will exit just before the next iteration (because it's a "while (!finished)" loop)
+ In my debug system, I store a linked list of required shutdown procedures - whenever I set a system up (like the graphics system or whatever), I add a shutdown procedure to the list. If I want to terminate the application, I call a procedure in my debug system which first goes through the list of shutdown procedures, and then calls ExitProcess to quit the application.
EDIT :: Obviously you don't have to used a linked list - you could hardcode a set of shutdown procedures, but a linked list is good for two reasons:
+ It means I can use the same debug system for many projects (code reusability is always nice).
+ It means that anything that hasn't been setup try to shutdown.

Hope that helps,

John B

[edited by - JohnBSmall on April 20, 2002 6:49:13 PM]

Share this post


Link to post
Share on other sites
The goto statement is not bad. It's just that code that relies on it often looks horrible to others. goto is just an alias for the assembly command jmp, so it executes very quickly (about 1 clock cycle for a nonconditional jump, which is what goto is).

I use it myself, but not to exit my program. I use it to exit a loop from within another loop nested in it, among other things. I use it very rarely, but I still use it.

~CGameProgrammer( );

EDIT: And don't tell me about break; break just exits the current loop. I sometimes use goto like this:

for( A = 0; A < 100; A ++ )
{
for( B = 0; B < 100; B ++ )
{
if( something )
goto ExitA;
}
}

ExitA:


[edited by - CGameProgrammer on April 20, 2002 7:05:33 PM]

Share this post


Link to post
Share on other sites
I have been toying with this idea.

What if we signal we want to end a game by throwing a specfic exception, like EndGameException. We catch the EndGameException in Winmain and return gracefully. Provided the code is exception safe, this should be quite a nice way to quit.

Just a crazy idea.

Share this post


Link to post
Share on other sites
Well, ignoring the fact that Stroustrup for example advises against using exceptions for non-exceptional situations, it''s a possible idea. But you have to make your code exception-safe and forget about globals. On the other side, many small apps exit when the user hits Alt+F4, which logically puts cleanup code into WM_DESTROY. If you use exceptions, you have to move your cleanup code elsewhere, or have a function that is called from both WM_DESTROY hander and the destructor of whatever object your game engine is.

Share this post


Link to post
Share on other sites
Maybe you could have: ExitGame(); which would then do all clean up code eg releasing devices and then throw the ExitApp exeption mentioned above or else do a goto the the end of WinMain()...

CEO Plunder Studios

Share this post


Link to post
Share on other sites
If you enforce RAII idiom (Resource aquisition is initialization) in your project, then there is no cleanup code required.

But it is just a crazy idea developed from thinking too much about exceptions.

Share this post


Link to post
Share on other sites
You can have the game being run within a Finite State Machine that is called every loop in your winmain. Here''s an example of my code.


  
game::SetupGame(hWnd, hInstance);
// Set the game''s control flow to the menu system

Flow::Setup::Menu_();
game::control = Flow::Run::Menu_;
while (1)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
game::time.UpdateTime();
if (false == game::Close_Application)
{
game::input.Refresh(); game::control();
}
#ifdef _DEBUG_WINDOWED
Sleep( 10 ); // forfeit the remainder of our time slice

#endif
}


The only way to exit my program is to call a function I have calleed game::ShutdownGame() (which is my only global namespace function). Also to reduce the cluage in winmain, I have the game::SetupGame() which sets up my sound, graphics, networking, and other things that are assumed to be setup when I enter my main loops. You lose a lot of if/else''s when using function pointers, and the code becomes much nicer to read/update/comment out. ShutdownGame() is the only thing that posts the quit message, and calls a safe clean on every resource I have in the game. This code I copied from a few months ago, so I wouldn''t structure it exactly like this ( Setting my control function ptr manually ).

Anyway FSMs are really cool and you should check it out. The one I''m showing here is just a half-baked example of one. They have done wonders for structuring my code, and making it easy to expand and modify.

- Kevin "BaShildy" King
Game Programmer: DigiPen
www.mpogd.com

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!