Game instantly quits.

Started by
5 comments, last by Khatharr 11 years, 1 month ago

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.

Advertisement

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.

Fly Safe 7o

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.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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

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.

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.

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.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement