Sign in to follow this  

Game instantly quits.

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

This is my Game.cpp file:

#include "Game.h"

bool quit = false;

// Starts the game
void Game::Start() {

	if ( Game::Init() == 1 ) {
			std::cout << SDL_GetError();
			quit = true;
		}
	
	while( !quit ) {
		if ( Game::Update() == 1 ) {
			std::cout << SDL_GetError();
			quit = true;
		}

		if ( Game::Draw() == 1 ) {
			std::cout << SDL_GetError();
			quit = true;
		}
	}

	SDL_Quit();
}

int Game::Init() {
	SDL_Init( SDL_INIT_EVERYTHING );

	return 0;
}

int Game::Update() {

	while (SDL_PollEvent( &event ) ) {
		if ( event.type == SDL_QUIT ) {
			quit = true;
		}
	}

	return 0;
}

int Game::Draw() {

	return 0;
}

 

Game::Start(); is called from main.cpp, but it quits instantly now. I hope somebody can help!

Thanks.

Share this post


Link to post
Share on other sites

I would have the start function initialize the library and an end function to close the library. I would also have a separate function for your main loop to call Update, Draw, etc.

 

I would also have the bool quit as a member variable to allow for more "dynamic" changing.  In my current project, the core class, GameEngine, is set up as a singleton with the bool quit as a static.  Then my main function calls are called in main within my main loop.

 

Just some design ideas as you progress further.  Cleverly placed print statements also help in debugging something like this wink.png

 

Edit: Forgot to mention that your init(), update(), and draw() functions only return 0 and not 1 which is what your if tests for.

Edited by DeafTV

Share this post


Link to post
Share on other sites
This is a good opportunity for you to get familiar with your IDE's debugger. A debugger can find problems like this very quickly. Just set a breakpoint at the beginning of Game::Start() and keep stepping forward until you see something go wrong.

DeafTV, on 03 Mar 2013 - 10:06, said:
Edit: Forgot to mention that your init(), update(), and draw() functions only return 0 and not 1 which is what your if tests for.

I'd assume that he'd return 1 on failure, but does not yet have failure cases coded, or else has stripped some content in order to ask his question.

Share this post


Link to post
Share on other sites

Try rebuilding, it might magickally fix the problem. Make sure the dlls are in the same folder as the executable too.

Share this post


Link to post
Share on other sites
Turns out the variable quit did not initialize properly, so it got a random value which was always true, because only 0 is false I think.

Share this post


Link to post
Share on other sites

How? In your code, it is declared and initialized to false on the same line.

 

Personally, I use a variable called "running" instead, so the game loop ends when !running.

Share this post


Link to post
Share on other sites
Just a shot in the dark, but could you post the contents of "Game.h"? I'm wondering if there's more than one 'quit' rolling around here.

Share this post


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