Sign in to follow this  
Dennisvb

Game instantly quits.

Recommended Posts

Dennisvb    160

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
DeafTV    1705

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
Khatharr    8812
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
ultramailman    1720

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this