Public Group

# Basic foundation

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

## Recommended Posts

I'm a beginner in C++ game programming, and this is the first time I'm using classes in a graphical game. I'm unsure if the base I have has bad practices or problems. All my code compiles fine, but I have no idea why this SDL program will not exit when I press ESC! I've clearly included that in my code: main.cpp
#include <iostream>
#include "SDL/SDL.h"
#include "Error.h"
#include "Game.h"
#include "Renderer.h"
#include "EventHandler.h"
#include "Timer.h"
#include "FrameManager.h"

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define BITS_PER_PIXEL 32

int main(int argc, char *args[])
{
Game game;
game.init(SCREEN_WIDTH, SCREEN_HEIGHT, BITS_PER_PIXEL);

while (1)
{
game.handleEvents();
game.render();
}

error("Game exited unexpectedly");
cleanUp();

return 0;
}


Game.h
#ifndef GAME_H
#define GAME_H
#include <string>
#include "SDL/SDL.h"
#include "EventHandler.h"
#include "Renderer.h"

extern void cleanUp();

class Game
{
private:
EventHandler events;
Renderer renderer;
public:
Game();
~Game();
bool init(int SCREEN_WIDTH, int SCREEN_HEIGHT, int BITS_PER_PIXEL);
void handleEvents();
void render();
};

#endif


Game.cpp
#include "SDL/SDL.h"
#include "Game.h"
#include "Error.h"

Game::Game()
{
}

Game::~Game()
{
cleanUp();
}

bool Game::init(int SCREEN_WIDTH = 800, int SCREEN_HEIGHT = 600, int BITS_PER_PIXEL = 32)
{
// Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
error("Unable to initialize SDL.");
return true;
}

renderer.init(SCREEN_WIDTH, SCREEN_HEIGHT, BITS_PER_PIXEL);

// Set window title
SDL_WM_SetCaption("SDLPong", NULL);

return false;
}

void Game::handleEvents()
{
events.handleEvents();
}

void Game::render()
{
renderer.render();
}

extern void cleanUp()
{
// Quit SDL
SDL_Quit();
exit(0);
}


Input.h
#ifndef INPUT_H
#define INPUT_H
#include "SDL/SDL.h"

class KeyboardManager
{
private:
Uint8 *keys;
public:
KeyboardManager();
void updateKeys();
void handleKeys();
};

#endif


Input.cpp
#include "SDL/SDL.h"
#include "Game.h"
#include "Input.h"

KeyboardManager::KeyboardManager()
{
keys = SDL_GetKeyState(NULL);
}

void KeyboardManager::updateKeys()
{
keys = SDL_GetKeyState(NULL);
}

void KeyboardManager::handleKeys()
{
if (keys[SDLK_ESCAPE])
{
cleanUp();
}
}


EventHandler.h
#ifndef EVENTHANDLER_H
#define EVENTHANDLER_H
#include <queue>
#include "SDL/SDL.h"
#include "Input.h"

class EventHandler
{
private:
std::queue<SDL_Event> events;
KeyboardManager keyboard;
public:
void handleEvents();
};

#endif


EventHandler.cpp
#include "EventHandler.h"

void EventHandler::handleEvents()
{
// Handle keyboard input
keyboard.updateKeys();
keyboard.handleKeys();
}


I've uploaded the whole base at filefront. Any help would be greatly appreciated! Thanks in advance! :D [Edited by - Sanctux on January 9, 2008 9:20:51 PM]

##### Share on other sites
Thanks for taking your time to reply. I've edited my previous post.

I know it's a hassle to look download and analyze someone else's code, especially when there are a lot of files. I would extremely appreciate it if someone took the time to help me. ^-^

##### Share on other sites
Hey, this post interested me since it's an example of code separation. I've been following the Lazy Foo tutorials, and though they are awesome there really aren't examples of how to go about separating a larger game (he always puts input,logic, and rendering right in the objects, which I'm not sure is good practice or not). So though I'm fairly new I decided to give your code a check since I'm usually able to debug things. I managed to get it working, but I can't answer if it's a good implementation for a game or not, since I don't know.

Here's what I changed to get it to work:

In Main:

#include "Game.h"#include "Renderer.h"#include "EventHandler.h"#include "Timer.h"#include "FrameManager.h"#define SCREEN_WIDTH 800#define SCREEN_HEIGHT 600#define BITS_PER_PIXEL 32//Declare a variable called quitGame that will be a global, so it is in another fileextern bool quitGame;int main(int argc, char *args[]){	Game game;	game.init(SCREEN_WIDTH, SCREEN_HEIGHT, BITS_PER_PIXEL);//Before you had it set up as an infinite loop, so there was no way that it could get out of this loop, even if cleanUp is called it doesn't break this loop	while (!quitGame)    {		game.handleEvents();		game.render();        	}//I had to take out the error checking stuff to get it to work//It could definitely still be in the game somehow but I'm not sure how     cleanUp();	return 0;}

I made a file called globals.cpp, all it has is:
bool quitGame=false;

In Game.cpp I also changed the part where you had cleanup as extern in there. Since it's implemented in the file I didn't think that it had to be extern, only files that need to call it without it in the file have to say extern I think (I could be wrong here). I also took out the exit(0) for now since it's called through main anyways with the way I had it set up.

here's what cleanup() in Game.cpp looks like now:
void cleanUp(){	// Quit SDL	SDL_Quit();	}

Another problem existed in your input class, here's what it looks like now:

#include "Game.h"#include "Input.h"#include <iostream>extern bool quitGame;KeyboardManager::KeyboardManager(){	keys = SDL_GetKeyState(NULL);}void KeyboardManager::updateKeys(){    //I think that GetKeyState can only be done after all of the events    //have been handled, this basicly runs through all the events and    //does nothing.    SDL_Event event;    while( SDL_PollEvent( &event ) ){    }	keys = SDL_GetKeyState(NULL);}void KeyboardManager::handleKeys(){	if (keys[SDLK_ESCAPE])	{        //Set the global flag quitGame to true so main will exit		quitGame=true;	}}

Another problem existed in your renderer class, I got it to work by commenting out the Delay part, here's renderer.cpp now:

#include "SDL/SDL.h"#include "Renderer.h"Renderer::~Renderer(){	SDL_FreeSurface(screen);}void Renderer::init(int SCREEN_WIDTH, int SCREEN_HEIGHT, int BITS_PER_PIXEL){	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BITS_PER_PIXEL, SDL_SWSURFACE);}void Renderer::applySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination){	SDL_Rect offset;	offset.x = x;	offset.y = y;	SDL_BlitSurface(source, NULL, destination, &offset);}void Renderer::render(){	// Render objects when needed	while (!objectsToRender.empty())	{		applySurface(objectsToRender.front()->x, objectsToRender.front()->y, objectsToRender.front()->image, screen);		objectsToRender.pop();		if (SDL_Flip(screen) == -1)		{		}	}	// Delay rest of frame	//frameManager.delay();}

The problem I think is that frameManager.delay() is messed up since timer.getTicks() won't work because the timer is never started. IE timer.run() is never called.

I think those are all the changes I had to make to get it runnable, if you can do those changes and it still doesn't work it means I've omitted something. If that's the case then either play with it to get it to work, or ask me to upload all of the files.

I'm fairly new so I can't really say a lot about the implementation structure, if someone with more knowledge could take a look at this setup and say whether or not it's a good setup that would be great.

Also, does anyone know of good tutorials for how to setup a game? Specifically I'd like to see more examples of separating input, logic and rendering.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
13
5. 5

• 14
• 10
• 23
• 9
• 57
• ### Forum Statistics

• Total Topics
632639
• Total Posts
3007606
• ### Who's Online (See full list)

There are no registered users currently online

×