Page 1 of 1

Beginning SDL - Part 2 - The Basic Framework

#1 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,208
  • Joined: 14-February 08

Posted 17 June 2009 - 02:48 AM

*
POPULAR

Hi, this is the second part of my beginning SDL tutorials, in this tutorial we will create a basic framework for our games. The start of this tutorial is more conceptual than core SDL so it can be used in any of your games using C++ no matter which library you are using.

Ok so lets begin, first we will create our Game class, this will contain some of the functions for our game.

Game.h
#ifndef  _GAME_H_
#define _GAME_H_

#include <SDL.h>

class Game
{
	   public:
			
			  Game();
			  
			  void Init();

	   private:
			   
			   bool m_bRunning
};

#endif



Quite basic to start with, we create our constructor and the Initialize function, this will be the first call for the program.

Next lets write our Game.cpp file

#include "Game.h"

// constructor
Game::Game()
{
}

void Game::Init()
{
	 m_bRunning = true;
}



This will be the class that most of our game will run through, so lets write our main.cpp and test it out

#include "Game.h"

int main(int argc, char* argv[])
{
	 Game game;
	  
	  game.Init();
}



Ok that should compile now, it won't do anything at the moment but should compile with no errors.

So that the hub of our game where we will call all of the functions needed to keep the game running, if you recall from the last tutorial I covered the functions needed for a main game loop these were:
Initialize
// while the game is running
Handle Events
Update
Draw
// once the game is done
Clean



we will incorporate the basic framework of these functions now, open up the Game.h file and add the functions.

#ifndef  _GAME_H_
#define _GAME_H_

#include <SDL.h>

class Game
{
	   public:
			
			  Game();
			  
			  bool Init();
			  
			  void HandleEvents();
			   
			  void Update();
			   
			  void Draw();

			  void Clean();

		private:
			   
			  bool  m_bRunning;
};

#endif




Ok now lets go to our Game.cpp file and create the function body's

#include "Game.h"

// constructor
Game::Game()
{
  
}

void Game::Init()
{
	m_bRunning = true;
}

void Game::HandleEvents()
{
}

void Game::Update()
{
}

void Game::Draw()
{
}

void Game::Clean()
{
}



That is the main framework of our game created, Init will load anything we need such as NPC's, player files, map files or sound files. Handle Events will check for key-presses and handle them accordingly. Update will do any movement and time based features and Draw will draw everything to the screen. These functions will be called in a constant loop when the game is running.

Lets write our main.cpp to incorporate these functions

#include "Game.h"

int main(int argc, char* argv[]) 
{
	Game game;
	
	game.Init();
	
	while(game.Running())
	{
		game.HandleEvents();
		game.Update();
		game.Draw();
	}
	
	// cleanup the engine
	game.Clean();
	
	return 0;
}



Ok so now we have the framework down, we can now add some SDL code to create a window once we initialize our game. First we have to think about what we need to do when creating an SDL window, we need to know the height and width of the screen, bpp and whether the window is fullscreen or not.

Re-open the Game.h file and we will incorporate this code, I won't paste the entire file again only the part's we are working on.


// change our init function to incorporate the parameters we will need
public:
void Init(const char* title, int width, int height, 
				  int bpp, bool fullscreen);

// add a pointer to an SDL surface for our screen and also a bool for fullscreen
private:

SDL_Surface* screen;
bool m_bFullscreen;




now open up the Game.cpp file so we can write the body of this function

void Game::Init(const char* title, int width, int height, 
				int bpp, bool fullscreen)
{
	int flags = 0;
	
	// initialize SDL
	SDL_Init(SDL_INIT_EVERYTHING);
	
	// set the title bar text
	SDL_WM_SetCaption(title, title);
	
	if ( fullscreen ) {
		flags = SDL_FULLSCREEN;
	}
	
	// create the screen surface
	screen = SDL_SetVideoMode(width, height, bpp, flags);
	
	m_bFullscreen = fullscreen;
	
	m_bRunning = true;

		// print our success
	printf("Game Initialised Succesfully\n");
}



Now the program creates a window using SDL according to our parameters so lets update our main.cpp to incorporate this revised function

#include "Game.h"

int main(int argc, char* argv[]) 
{
	Game game;
	
	game.Init("test",640,480,32,false);
	
	while(game.Running())
	{
		game.HandleEvents();
		game.Update();
		game.Draw();
	}
	
	// cleanup the engine
	game.Clean();
	
	return 0;
}



That should compile fine but there is one problem we have no way to exit the program, you will most likely have to force it to close using CTRL+ALT+DELETE or ForceQuit. Lets write a way to close this window using SDL so that we can quit it easier.

Open up the Game.h file and we need to update our handle events function, we need to pass in a pointer to our current game.

void HandleEvents(Game* game);



now open up the Game.cpp file and we will add some way of handling events so we can use the SDL_QUIT event. An SDL_QUIT event is when you click the cross button on an SDL window, we earlier wrote a quit function that simply sets our m_bRunning bool to false, this in turn stops the game loop and our Clean function is called. So lets write a way to handle this event

void Game::HandleEvents(Game* game)
{
	SDL_Event event;
	
	if (SDL_PollEvent(&event)) {
		switch (event.type) {
			case SDL_QUIT:
				game->Quit();
				break;
				
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
					case SDLK_ESCAPE:
						game->Quit();
				}
				break;
		}
	}
	
}



There we go, I also added a way to quit using the escape key, this is useful while building and debugging but make sure to remove it once you are finished.

Ok then back to the main.cpp file for a small update.

#include "Game.h"

int main(int argc, char* argv[]) 
{
	Game game;
	
	game.Init("test",640,480,32,false);
	
	while(game.Running())
	{
		game.HandleEvents(&game);
		game.Update();
		game.Draw();
	}
	
	// cleanup the engine
	game.Clean();
	
	return 0;
}



We update the handle events function and pass in our game.

To recap we create a basic framework for our games incorporating the main game loop, we created a way to create a window according to our needs by passing in values needed to our init function and we created a way for SDL to handle events, one of which is our quit function which is called via escape key or by clicking the cross on and SDL window.

Thanks for reading, in part 3 I am hoping to incorporate a state manager or a sprite class any requests?

Happy coding and of course any questions will be answered :^:

Is This A Good Question/Topic? 7
  • +

Replies To: Beginning SDL - Part 2 - The Basic Framework

#2 Guest_J3d1*


Reputation:

Posted 26 June 2009 - 04:34 PM

Very good articles, many thanks to the author! I will wait for continuation :)
Was This Post Helpful? 0

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,208
  • Joined: 14-February 08

Posted 18 August 2009 - 04:15 AM

One little thing to add to this tutorial that was pointed out to me by BlackPhoenix, I have not wrote the function for Game::Running, this function returns the variable bool m_bRunning;

so here it is, its very small so just chuck it into the Game.h file

bool Running() {return m_bRunning;}



Thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,208
  • Joined: 14-February 08

Posted 15 December 2009 - 07:02 PM

2 more things I noticed, I didn't write the Quit() function, so here it is.

Put it in the Game.h file

void Quit() { m_bRunning = false;}



and also on the first Game.h part I wrote the declaration of Init as a bool and the definition as a void, just make it void :)
Was This Post Helpful? 2
  • +
  • -

#5 Severy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 04-November 09

Posted 25 February 2011 - 06:25 AM

very nice article

I just have one question.

Why do you use a pointer to the game while the Quit() function is accesible from the HandleEvents() function without a pointer?
Is there a special reason why you used a pointer? Im fairly new to c++ and the concept of pointers is still pretty difficult for me (but im working on that), so im trying to get a better understanding of why you used a pointer while it basicly is not needed.
Was This Post Helpful? 1
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,208
  • Joined: 14-February 08

Posted 25 February 2011 - 09:21 AM

You make a very good point, lets chalk it up writing these up and coding at the same time. It could also be that I converted this project from another that included the states like part 4 and they have state pointers passed into them. Nice spot :) you don't need the pointer.
Was This Post Helpful? 0
  • +
  • -

#7 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 23
  • View blog
  • Posts: 263
  • Joined: 18-November 07

Posted 25 November 2011 - 08:32 PM

Not trying to be a grammar nazi, but you misspelled "successfully". I'm just saying. Excellent tutorial and I thank you for your help with the game I am going to create. This is exactly what I've been looking for, something a bit more toward a framework and how things should be.

This post has been edited by kiasta: 25 November 2011 - 08:33 PM

Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,208
  • Joined: 14-February 08

Posted 26 November 2011 - 09:43 AM

I could say that you mix up grammar and spelling. Bringing up a spelling mistake would not make you a grammar nazi. :P/>

I'm sure I made more spelling mistakes, you "spelling" nazi :P.
Was This Post Helpful? 0
  • +
  • -

#9 dannydoomno1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-June 13

Posted 26 June 2013 - 11:16 AM

hi :)/> i really like your tutorial, currently its the only one where the given code ACTUALLY works at all, but i am having the trouble with this part of code.
while(game.Running())
	{
		game.HandleEvents();
		game.Update();
		game.Draw();
	}


im not sure why its not working but i get this error.

C:\game\main.cpp|11|error: no matching function for call to 'Game::HandleEvents()'|

a little hint or some help would be hugely appreciated :)/>/> thank you

This post has been edited by stayscrisp: 26 June 2013 - 01:08 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#10 dannydoomno1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-June 13

Posted 26 June 2013 - 11:22 AM

ignore that apparently i missed a step, prob from my laptop spazing out :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1