• (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

Beginning SDL Part 4 - State Manager

#16 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Posted 17 February 2011 - 03:08 AM

At the time I wrote this tutorial I thought that data structure would be the most appropriate, I would think that the order would not need to be changed in any way. Once you have a state on the stack then you would most likely just push another on top and keep that order until you pop the state off.

What is your reasoning for using a queue? :)
Was This Post Helpful? 0
  • +
  • -

#17 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Posted 18 February 2011 - 11:44 AM

Well I guess it doesn't really matter.
Was This Post Helpful? 0
  • +
  • -

#18 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Posted 19 February 2011 - 03:23 AM

There probably are better ways and more appropriate data structures, it would be cool to know why a queue came to mind for you?

This post has been edited by stayscrisp: 19 February 2011 - 03:23 AM

Was This Post Helpful? 0
  • +
  • -

#19 chtombleson  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 28-September 09

Posted 21 March 2011 - 10:10 PM

Hi,
Great tutorial.
I've been having problems with the virtual function inheritance and it's just causing a chain reaction and wont compile.

It was working pretty good unit the gamestate code. Any ideas on what would be causing this problem?

Help would be greatly appreciated.

Cheers
Was This Post Helpful? 0
  • +
  • -

#20 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Posted 22 March 2011 - 03:28 AM

If you post your code I will take a look, it's hard to diagnose without seeing it :)

Glad you're enjoying the tutorials.
Was This Post Helpful? 0
  • +
  • -

#21 chtombleson  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 28-September 09

Posted 22 March 2011 - 02:41 PM

Cheers man keep up the good work.

My problem was that the some of the functions name were not always staying the same. Which meant it couldn't inherit the game state class properly.

It was the CleanUp virtual function. Were as the derived classes were using Clean not CleanUp.

Good stuff anyway and trying to figure it out was a good workout.
Was This Post Helpful? 0
  • +
  • -

#22 CaptainCombo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-July 11

Posted 11 July 2011 - 01:27 PM

Cleanup() function which is in GameState class have never been used, right? Or is there a wrting mistake about it. I think it would have been Clean().!?
Was This Post Helpful? 0
  • +
  • -

#23 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Posted 12 July 2011 - 03:38 AM

Yes that is a typo, just use the name you feel works.
Was This Post Helpful? 0
  • +
  • -

#24 DoNotWant  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 03-November 11

Posted 03 November 2011 - 06:43 AM

Nice series of SDL tutorials, thanks!

http://lazyfoo.net/a...cle06/index.php
In this article, the guy uses game states to transition between maps.
Is whis what you would recomend doing, or is there any better ways?
Was This Post Helpful? 0
  • +
  • -

#25 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Posted 14 November 2011 - 07:40 AM

I guess this depends on how big your game is going to be. If you are thinking of doing something with different screens on a large map like the first legend of zelda then it may be easier to think of a different system. Possibly an Area class that contains individual maps and keeps track of the players current location so that it transitions correctly. This could be something like a state machine but really that may be overkill :)

Hope that helped some!
Was This Post Helpful? 1
  • +
  • -

#26 DoNotWant  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 03-November 11

Posted 14 November 2011 - 01:34 PM

Thanks!

Found this http://www.sdltutorials.com/sdl-maps
:)
Was This Post Helpful? 0
  • +
  • -

#27 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Posted 15 November 2011 - 02:20 AM

Yes, that tutorial is quite good. Although I would really just take the ideas not the actual code.
Was This Post Helpful? 0
  • +
  • -

#28 s0me0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 05-November 11

Posted 04 December 2011 - 05:01 AM

Excellent tutorial.
I tried making a new state for me but i keep getting error that: PlayState is not declared ,if i ChangeState to it.
Also if i try poping my new state.. the game closes which means its segmentation fault and it cant find the Instance of PlayState or any other State.

I tried fixing 1 thousand times my includes.. then i limited my header files. BUt still nothing.
I' ve got a question about the way GameState and Game is declared. Each one is getting argument of the other one. Is it possible to cause the problem ? The weird is that i got this on my new State only.

Somehow the others communicate but the 3rd one wont..Its like i got a limit in how many states ill make.
Any ideas? [ also tried making new project, removing readding files ]

Thanks.
Was This Post Helpful? 0
  • +
  • -

#29 s0me0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 05-November 11

Posted 04 December 2011 - 06:48 AM

View Posts0me0ne, on 04 December 2011 - 05:01 AM, said:

Excellent tutorial.
I tried making a new state for me but i keep getting error that: PlayState is not declared ,if i ChangeState to it.
......
Thanks.


Well i figured it out. I dont know why, but i added in correct order every Class in one header file.
GameState, Game, MenuState, PlayState, MyState
and now it works.

I'm just mentioning it in case someone has the same problem.

Sorry for double posting, i really cant find the Edit option.
Was This Post Helpful? 0
  • +
  • -

#30 Tentacle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-February 12

Posted 23 February 2012 - 12:14 PM

Hi there,

sorry to bring up this old topic but I have a problem.
The compiler doesn't give me an erro but during the debugging session I get an access violation here (without anymore explanation): states.back()->handleEvents(this);

I will post my code. Hopefully someone will be able to hlep me.

Game.cpp
#include "cEngine.h"
#include "cGameState.h"

cEngine::cEngine()
{
}

void cEngine::init( const char* title, int width, int height, int bpp, bool fullscreen )
{
	int flags = 0;

	//Init SDL
	SDL_Init( SDL_INIT_EVERYTHING );

	//Set the title bar text
	SDL_WM_SetCaption( title, NULL );

	//Set Icon
	SDL_WM_SetIcon( IMG_Load( "img//icon//icon.gif" ), NULL );

	//Check if fullscreen
	if( fullscreen )
	{
		flags = SDL_FULLSCREEN;
	}

	//Create screen surface
	m_pScreen = SDL_SetVideoMode( width, height, bpp, flags );

	m_bFullscreen = fullscreen;

	m_bRunning = true;
}

void cEngine::changeState( cGameState* state )
{
	//Clenup the current state
	if( !states.empty() )
	{
		states.back()->cleanUp();
		states.pop_back();
	}

	//Store and init the new state
	states.push_back( state );
	states.back()->init();
}

void cEngine::pushState(cGameState* state)
{
	// pause current state
	if ( !states.empty() ) {
		states.back()->pause();
	}
	
	// store and init the new state
	states.push_back( state );
	states.back()->init();
}

void cEngine::popState()
{
	// cleanup the current state
	if ( !states.empty() ) {
		states.back()->cleanUp();
		states.pop_back();
	}
	
	// resume previous state
	if ( !states.empty() ) {
		states.back()->resume();
	}
}

bool cEngine::running()
{
	return m_bRunning;
}

void cEngine::handleEvents()
{
	//State handle events
	states.back()->handleEvents(this);
}

void cEngine::update()
{
	// let the state update the game
	states.back()->update(this);
}

void cEngine::draw()
{
	//state draw the screen
	states.back()->draw(this);
}

void cEngine::cleanUp()
{
	while ( !states.empty() ) {
		states.back()->cleanUp();
		states.pop_back();
	}
	
	// shutdown SDL
	SDL_Quit();
}

void cEngine::quit()
{
	m_bRunning = false;
}



GameState.h
#ifndef _CGAMESTATE_H_
#define _CGAMESTATE_H_

#include "cEngine.h"

class cGameState
{
public:

	virtual void init() = 0;

	virtual void cleanUp() = 0;

	virtual void pause() = 0;

	virtual void resume() = 0;

	virtual void handleEvents( cEngine* Engine ) = 0;

	virtual void update( cEngine* Engine ) = 0;

	virtual void draw( cEngine* Engine ) = 0;

	void changeState( cEngine* Engine, cGameState* state )
	{
		Engine->changeState( state );
	}

protected:
	cGameState() {}
};

#endif



Game.h
#ifndef _CENGINE_H_
#define _CENGINE_H_

#include <SDL.h>
#include <string>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include "cSprite.h"
#include "cTimer.h"
#include <vector>
#include <iostream>

class cGameState;

class cEngine
{
public:

	cEngine();

	void init( const char* title, int width, int height, int bpp, bool fullscreen );

	void changeState( cGameState* state );

	void pushState( cGameState* state );

	void popState();

	bool running();

	void handleEvents();

	void update();

	void draw();

	void cleanUp();

	void quit();

private:
	
	//States
	std::vector<cGameState*> states;

	//Runntime variables
	bool m_bRunning;

	bool m_bFullscreen;

	SDL_Surface* m_pScreen;
};

#endif


Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4