Jump to content
  • Advertisement
Sign in to follow this  
mattor

quick 'n easy exit question

This topic is 4917 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

Hey what is the best way to immediately shut down my game when a certain key is pressed? exit(1), or what? I've got a few .cpp files linked and I want to pass a quit flag back to int main() that tells it, quit now. The problem with quitting wherever I am when I hit the button is that the right stuff might not be freed up (fonts, SDL, etc). I'm having trouble with this simple issue but I think it will help my understanding of memory and how to exit a program gracefully.

Share this post


Link to post
Share on other sites
Advertisement
I don't know how your code is organized, but you could use PostQuitMessage( <error code> ); and have your shutdown code after the main message loop(assuming you haven't encapsulated any of the functionality of your game). You might want to look at an OOP design of your game if you're worried about freeing all memory at any given exit point.

[edit] By the way, PostQuitMessage will send a WM_QUIT message that should break you out of your message loop (GetMessage returns 0 if it recieves a WM_QUIT message).


[edit2] Forgive me, I assumed you were writting a windows program.

Regards,
jflanglois

[Edited by - jflanglois on March 3, 2005 12:02:02 PM]

Share this post


Link to post
Share on other sites
A quick and dirty method:


void shutdown()
{
// Do shutdown here
}

int main()
{
atexit( shutdown );
}

void otherfunc()
{
exit( 0 ); // If you want to exit 'normally'
exit( /* Anything besides 0 */ 1 ); // If you want to exit with an error
}



This isn't the cleanest method but it works... :P

Hope it helps and good luck!

[edit] This (should) work across platforms as well. Like the previous poster said I would be leaning towards using a more 'structured' approach here.

Share this post


Link to post
Share on other sites
A good way is to keep a flag (quitRequested) and then only loop while that flag is not set. When the flag is set you'll exit the loop and there you can deallocate your resources/etc.

[Edited by - cozman on March 3, 2005 12:20:58 PM]

Share this post


Link to post
Share on other sites

//...
int done = 0;//make our variable for exiting
//...
if (event.type == SDL_QUIT) { done = 1; }
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE) { done = 1; }
}
//...


That's how I did it [smile].

Share this post


Link to post
Share on other sites
does anyone see a reason why I should be crashing before anything even comes up on the screen? i'm trying to switch from using if statements to switch/case and that might hold the problem...here's my code

//Start the game loop
SDL_Event event;
done = 0;

while(done==0)
{
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
shutdown();
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
shutdown();
break;
case SDLK_p:
done=1;
break;
case SDLK_i:
done=1;
break;
default:
break;
}
default:
break;
}
}
}



Now here's the shutdown function:

void shutdown()
{
SDL_Quit();
freeFont(headerfont);
freeFont(maintext);
exit(0);
}

Share this post


Link to post
Share on other sites
I've tried debugging with run to cursor to see where it crashes, but even if I just run to the #includes it says "Access Violation (Segmentation Fault) raised in your program." How do I figure out what I did wrong with this joker?

Share this post


Link to post
Share on other sites
well since no one seems to be posting, i'll add some more info...
I commented out a few things and got it to work now, but I'm still puzzled about the memory deallocation. Why do I need to do it? My program works fine if I don't free the fonts, (in fact it DOESN'T work when I DO free them) so what's the big deal? And if anyone knows SDL, does calling SDL_Quit() free the fonts somehow? Because I AM calling SDL_Quit() (so my conscience is clear on that point) but I don't understand why I need to free it up. So far, so good.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!