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

Beginning SDL Part 4 - State Manager

#31 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Posted 23 February 2012 - 07:04 PM

Hello, I took up your offer on learning SDL but i'm getting an error no matter what i do. I copied all of your code and i still get a
'MenuState' : cannot instantiate abstract class


Was This Post Helpful? 0
  • +
  • -

#32 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,172
  • Joined: 14-February 08

Posted 24 February 2012 - 08:44 AM

@Tentacle, please post the rest of your code. It would seem that you haven't actually created a state that can be used, you cannot use the GameState class directly because it is an abstract class with pure virtual functions.

Amb154, are you trying to new the MenuState class anywhere? This will not work as the class is a singleton. Please post your code so I can diagnose :)
Was This Post Helpful? 0
  • +
  • -

#33 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Posted 24 February 2012 - 11:05 AM

It's giving me the error right on
#include <stdio.h>

#include "SDL.h"
#include "Game.h"
#include "MenuState.h"

MenuState MenuState::m_MenuState;


That's from MenuState.cpp
And it says that "Object of abstract class type 'MenuState' is not allowed".
I'm trying it with all the code you posted and it's still not working. It might be a problem in game.h/cpp or something else, but i don't see how that could be possible.
Was This Post Helpful? 0
  • +
  • -

#34 s0me0ne  Icon User is offline

  • New D.I.C Head

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

Posted 24 February 2012 - 11:11 AM

View PostAmb154, on 24 February 2012 - 11:05 AM, said:

It's giving me the error right on
#include <stdio.h>

#include "SDL.h"
#include "Game.h"
#include "MenuState.h"

MenuState MenuState::m_MenuState;


That's from MenuState.cpp
And it says that "Object of abstract class type 'MenuState' is not allowed".
I'm trying it with all the code you posted and it's still not working. It might be a problem in game.h/cpp or something else, but i don't see how that could be possible.


Small tip.. indepedent to your error.
Try getting all the classes in one file with a correct order.
Sometimes header and cpp files defining sucks in C++.. i have spent a lot of hours in debugging different errors because of define declaration problems.
Was This Post Helpful? 0
  • +
  • -

#35 Tentacle  Icon User is offline

  • New D.I.C Head

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

Posted 24 February 2012 - 11:48 AM

Okay I will post all my code. But I am just to the point where you say make sure it compiles. Btw. your tutorials are great! I feel like I have learned a lot about OOP in it.

main.cpp
#include "cEngine.h"

int main(int argc, char* argv[])
{
	
	cEngine Engine;
	
	Engine.init( "Game Engine", 640, 480, 32, false );

	while( Engine.running() )
	{

		Engine.handleEvents();
		
		Engine.update();

		Engine.draw();

	}

	Engine.cleanUp();

	return 0;
}


cEngine.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;
}



cEngine.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



cGameState.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


Was This Post Helpful? 0
  • +
  • -

#36 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,172
  • Joined: 14-February 08

Posted 25 February 2012 - 08:57 AM

Yes, you will get an error if you try to run it because at this point you are not actually creating any states. GameState is an abstract base class and so you cannot instantiate it directly. Further into the tutorial you will create some states and set them in the framework.

At the point you don't have any states in the stack so it throws an access violation due to the states list being empty. Keep following the tutorials and this error will go away :)
Was This Post Helpful? 0
  • +
  • -

#37 gamer27lv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-May 12

Posted 11 May 2012 - 07:12 PM

MenuState MenuState::m_MenuState;

Is there something I am doing wrong with this line? I have gone through the tutorial 10 times and I cant get it right. This line shows an error message: undefined reference to MenuState::MenuState()
Was This Post Helpful? 0
  • +
  • -

#38 gamer27lv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-May 12

Posted 12 May 2012 - 03:23 AM

View Postgamer27lv, on 11 May 2012 - 07:12 PM, said:

MenuState MenuState::m_MenuState;

Is there something I am doing wrong with this line? I have gone through the tutorial 10 times and I cant get it right. This line shows an error message: undefined reference to MenuState::MenuState()


**FIXED**

in my menustate.h file, I had the constructor wrong, I had MenuState(); instead of MenuState(){}
Was This Post Helpful? 0
  • +
  • -

#39 gamer27lv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-May 12

Posted 12 May 2012 - 06:49 AM

Hey Thanks a lot for all of this work you are putting in to these tutorials. I just have one question right now... What exactly does the (void ChangeState(Game* game, GameState* state){game->ChangeState(state);} function in the GameState class do?

I can't see it..
Was This Post Helpful? 0
  • +
  • -

#40 gamer27lv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-May 12

Posted 13 May 2012 - 01:13 AM

View Postgamer27lv, on 12 May 2012 - 06:49 AM, said:

Hey Thanks a lot for all of this work you are putting in to these tutorials. I just have one question right now... What exactly does the (void ChangeState(Game* game, GameState* state){game->ChangeState(state);} function in the GameState class do?

I can't see it..


I do kind of see it... but the problem I have with understanding it is that in the handleEvent section of the state classes, where the new states are called... the ChangeState function is only taking in 1 parameter ChangeState(StateLevelOne->Instance() for example. but within the GameState.h file, there is a void ChangeState function taking in 2 parameters, I know this is not a virtual function, so I don't think they are the same.. but where is the one with 2 parameters used?? and why?
Was This Post Helpful? 0
  • +
  • -

#41 sandmaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 12

Posted 06 December 2012 - 03:22 AM

I am having issues with the code, i get the following error when i try and compile it:

menustate.cpp(6): error C2259: 'MenuState' : cannot instantiate abstract class
1>          due to following members:
1>          'void GameState::Cleanup(void)' : is abstract



I have tried and followed the tutorial lots of times, but i dont seem to see where the problem lies.
Was This Post Helpful? 0
  • +
  • -

#42 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,172
  • Joined: 14-February 08

Posted 06 December 2012 - 03:28 AM

Hi, could you post your gamestate.h, menustate.h and menustate.cpp

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

#43 sandmaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 12

Posted 06 December 2012 - 03:33 AM

View Poststayscrisp, on 06 December 2012 - 03:28 AM, said:

Hi, could you post your gamestate.h, menustate.h and menustate.cpp

Thanks :)/>


Sure

Gamestate.h:
#ifndef GAMESTATE_H
#define GAMESTATE_H

#include "Game.h"

class GameState
{
public:
	virtual void Init() = 0;
	virtual void Cleanup() = 0;

	virtual void Pause() = 0;
	virtual void Resume() = 0;

	virtual void HandleEvents(Game* game ) = 0;
	virtual void Update(Game* game) = 0;
	virtual void Draw(Game* game) = 0;

	void ChangeState(Game* game, GameState* state)
	{
		game->ChangeState(state);
	}
	
protected:
	GameState() {}

};

#endif


Menustate.h
#ifndef MENUSTATE_H
#define MENUSTATE_H

#include <SDL.h>
#include "GameState.h"
#include "Sprite.h"

class MenuState : public GameState
{
public:
	void Init();
	void Clean();

	void Pause();
	void Resume();

	void HandleEvents(Game* game);
	void Update(Game* game);
	void Draw(Game* game);

	// implement Singleton Pattern
	static MenuState* Instance()
	{
		return &m_MenuState;
	}

protected:
	MenuState(){}

private:
	static MenuState m_MenuState;
	SDL_Surface* menuSprite;
};


#endif


Menustate.cpp
#include <stdio.h>
#include <SDL.h>
#include "Game.h"
#include "MenuState.h"

MenuState MenuState::m_MenuState;
 
void MenuState::Init()
{
	menuSprite = NULL; // set pointer to NULL;
	menuSprite = Sprite::Load("menusprite.png"); // load menu state image

}

void MenuState::Clean()
{
	printf("MenuState Clean Successful\n");
}

void MenuState::Pause()
{
	printf("MenuState Paused\n");
}

void MenuState::Resume()
{
	printf("MenuState Resumed\n");
}

void MenuState::HandleEvents(Game* game)
{
	SDL_Event event;

	if( SDL_PollEvent(&event))
	{
		switch(event.type)
		{
		case SDL_QUIT: game->Quit(); break;
		}
	}
}

void MenuState::Update(Game* game)
{

}

void MenuState::Draw(Game* game)
{
	Sprite::Draw(game->GetScreen(), menuSprite, 0, 0);
	SDL_Flip(game->GetScreen());
}


Hope u can help me :)
Was This Post Helpful? 0
  • +
  • -

#44 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,172
  • Joined: 14-February 08

Posted 06 December 2012 - 03:44 AM

Ah, it seems there is a problem in my tutorial which I will now fix. The GameState class implements a Cleanup() function whereas the other states use a Clean() function. the original GameState function needs to be named Clean() along with the rest of the states if they are not already.

The error above is the compiler telling you that it has an abstract function that needs to be implemented in all derived classes. This one was simply named wrong :)
Was This Post Helpful? 0
  • +
  • -

#45 sandmaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 12

Posted 06 December 2012 - 03:49 AM

View Poststayscrisp, on 06 December 2012 - 03:44 AM, said:

Ah, it seems there is a problem in my tutorial which I will now fix. The GameState class implements a Cleanup() function whereas the other states use a Clean() function. the original GameState function needs to be named Clean() along with the rest of the states if they are not already.

The error above is the compiler telling you that it has an abstract function that needs to be implemented in all derived classes. This one was simply named wrong :)/>



Ah! that might have solved it, though iam now getting another error, which might not have with this tutorial to do:
MenuState.obj : error LNK2019: unresolved external symbol "public: static struct SDL_Surface * __cdecl Sprite::Load(char *)" (?Load@Sprite@@SAPAUSDL_Surface@@PAD@Z) referenced in function "public: virtual void __thiscall MenuState::Init(void)" (?Init@MenuState@@UAEXXZ)
1>C:\Users\Robin\documents\visual studio 2010\Projects\GEProject\Debug\GEProject.exe : fatal error LNK1120: 1 unresolved externals


I did get lots of external linker errors before when i used visual studio express 2012, but not when i re did the whole code in visual studio express 2010, and havn't recieved some linker errors until now :(.
Was This Post Helpful? 0
  • +
  • -

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