ganlhi
26/07/2005, 13h05
Salut !
J'écris un petit jeu de shoot façon R-Type, grace à SDL.
J'avais une version fonctionelle en C, et j'ai décidé de la porter en C++.
J'utilise le principe des GameStates (http://www.games-creators.org/wiki/Gestion_des_Game_States_en_C_plus_plus) présentés sur le wiki, et je me heurte à un probleme de compilation de la classe CGameEngine, mais que sur mon code, alors que celui donné en exemple compile bien...
gameengine.h
#ifndef _GAMEENGINE_H_
#define _GAMEENGINE_H_
#include "common.h"
#include "configuration.h"
#include "gamestates.h"
class CGameState;
class CGameEngine
{
public:
CGameEngine();
void Init(const char *title);
void Cleanup();
void ChangeState(CGameState* state);
void PushState(CGameState* state);
void PopState();
void HandleEvents();
void Update();
void Draw();
bool Running() { return m_running; }
inline void Quit() { m_running = false; }
// ecran
SDL_Surface* screen;
private:
// pile de game states
vector<CGameState*> states;
// le jeu est-il en cours d'execution ?
bool m_running, m_fullscreen;
// configuration
Configuration config;
};
#endif
gameengine.cpp
#include "gameengine.h"
void CGameEngine::Init(const char *title)
{
Uint32 flags;
// chargement de la configuration
config.load();
// init du module video de SDL
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr,"Init SDL Video : %s\n",SDL_GetError());
exit(ERR_SDL_INIT);
}
// set the title bar text
SDL_WM_SetCaption(title, title);
// recuperation du booleen fullscreen
m_fullscreen = config.getFullscreen();
// reglages du module video de SDL
flags = config.getSdlVideoFlags();
if (m_fullscreen)
{
flags |= SDL_FULLSCREEN;
}
screen = SDL_SetVideoMode(config.getScreenX(),config.getScreenY(),config.getBpp(),flags);
printf("Mode video : %dx%dx%d\n",screen->w,screen->h,screen->format->BitsPerPixel);
// ne pas afficher le curseur de la souris
SDL_ShowCursor(0);
// demarrage
m_running = true;
}
void CGameEngine::Cleanup(void)
{
Uint32 flags;
// cleanup the all states
while (!states.empty())
{
states.back()->Cleanup();
states.pop_back();
}
// switch back to windowed mode so other
// programs won't get accidentally resized
if (m_fullscreen)
{
flags = config.getSdlVideoFlags() & (! SDL_FULLSCREEN);
screen = SDL_SetVideoMode(config.getScreenX(),config.getScreenY(),config.getBpp(),flags);
}
cout << "game engine cleaned !";
SDL_Quit();
}
void CGameEngine::ChangeState(CGameState* state)
{
// cleanup 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 CGameEngine::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 CGameEngine::PopState(void)
{
// cleanup the current state
if ( !states.empty() ) {
states.back()->Cleanup();
states.pop_back();
}
// resume previous state
if ( !states.empty() ) {
states.back()->Resume();
}
}
void CGameEngine::HandleEvents()
{
// let the state handle events
states.back()->HandleEvents(this);
}
void CGameEngine::Update()
{
// let the state update the game
states.back()->Update(this);
}
void CGameEngine::Draw()
{
// let the state draw the screen
states.back()->Draw(this);
}
gamestates.h
#ifndef _GAMESTATES_H_
#define _GAMESTATES_H_
#include "common.h"
class CGameEngine;
#include "gameengine.h"
class CGameState
{
public:
virtual void Init() = 0;
virtual void Cleanup() = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual void HandleEvents(CGameEngine* game) = 0;
virtual void Update(CGameEngine* game) = 0;
virtual void Draw(CGameEngine* game) = 0;
void ChangeState(CGameEngine* game, CGameState* state)
{
game->ChangeState(state);
}
protected:
CGameState() { }
};
#endif
erreur :
ganlhi@osiris shoot $ g++ -c gameengine.cpp `sdl-config --cflags`
In file included from gameengine.h:6,
from gameengine.cpp:1:
gamestates.h: Dans member function « void CGameState::ChangeState(CGameEngine*,
CGameState*) »:
gamestates.h:24: error: `ChangeState' undeclared (first use this function)
gamestates.h:24: error: (Each undeclared identifier is reported only once for
each function it appears in.)
J'écris un petit jeu de shoot façon R-Type, grace à SDL.
J'avais une version fonctionelle en C, et j'ai décidé de la porter en C++.
J'utilise le principe des GameStates (http://www.games-creators.org/wiki/Gestion_des_Game_States_en_C_plus_plus) présentés sur le wiki, et je me heurte à un probleme de compilation de la classe CGameEngine, mais que sur mon code, alors que celui donné en exemple compile bien...
gameengine.h
#ifndef _GAMEENGINE_H_
#define _GAMEENGINE_H_
#include "common.h"
#include "configuration.h"
#include "gamestates.h"
class CGameState;
class CGameEngine
{
public:
CGameEngine();
void Init(const char *title);
void Cleanup();
void ChangeState(CGameState* state);
void PushState(CGameState* state);
void PopState();
void HandleEvents();
void Update();
void Draw();
bool Running() { return m_running; }
inline void Quit() { m_running = false; }
// ecran
SDL_Surface* screen;
private:
// pile de game states
vector<CGameState*> states;
// le jeu est-il en cours d'execution ?
bool m_running, m_fullscreen;
// configuration
Configuration config;
};
#endif
gameengine.cpp
#include "gameengine.h"
void CGameEngine::Init(const char *title)
{
Uint32 flags;
// chargement de la configuration
config.load();
// init du module video de SDL
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr,"Init SDL Video : %s\n",SDL_GetError());
exit(ERR_SDL_INIT);
}
// set the title bar text
SDL_WM_SetCaption(title, title);
// recuperation du booleen fullscreen
m_fullscreen = config.getFullscreen();
// reglages du module video de SDL
flags = config.getSdlVideoFlags();
if (m_fullscreen)
{
flags |= SDL_FULLSCREEN;
}
screen = SDL_SetVideoMode(config.getScreenX(),config.getScreenY(),config.getBpp(),flags);
printf("Mode video : %dx%dx%d\n",screen->w,screen->h,screen->format->BitsPerPixel);
// ne pas afficher le curseur de la souris
SDL_ShowCursor(0);
// demarrage
m_running = true;
}
void CGameEngine::Cleanup(void)
{
Uint32 flags;
// cleanup the all states
while (!states.empty())
{
states.back()->Cleanup();
states.pop_back();
}
// switch back to windowed mode so other
// programs won't get accidentally resized
if (m_fullscreen)
{
flags = config.getSdlVideoFlags() & (! SDL_FULLSCREEN);
screen = SDL_SetVideoMode(config.getScreenX(),config.getScreenY(),config.getBpp(),flags);
}
cout << "game engine cleaned !";
SDL_Quit();
}
void CGameEngine::ChangeState(CGameState* state)
{
// cleanup 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 CGameEngine::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 CGameEngine::PopState(void)
{
// cleanup the current state
if ( !states.empty() ) {
states.back()->Cleanup();
states.pop_back();
}
// resume previous state
if ( !states.empty() ) {
states.back()->Resume();
}
}
void CGameEngine::HandleEvents()
{
// let the state handle events
states.back()->HandleEvents(this);
}
void CGameEngine::Update()
{
// let the state update the game
states.back()->Update(this);
}
void CGameEngine::Draw()
{
// let the state draw the screen
states.back()->Draw(this);
}
gamestates.h
#ifndef _GAMESTATES_H_
#define _GAMESTATES_H_
#include "common.h"
class CGameEngine;
#include "gameengine.h"
class CGameState
{
public:
virtual void Init() = 0;
virtual void Cleanup() = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual void HandleEvents(CGameEngine* game) = 0;
virtual void Update(CGameEngine* game) = 0;
virtual void Draw(CGameEngine* game) = 0;
void ChangeState(CGameEngine* game, CGameState* state)
{
game->ChangeState(state);
}
protected:
CGameState() { }
};
#endif
erreur :
ganlhi@osiris shoot $ g++ -c gameengine.cpp `sdl-config --cflags`
In file included from gameengine.h:6,
from gameengine.cpp:1:
gamestates.h: Dans member function « void CGameState::ChangeState(CGameEngine*,
CGameState*) »:
gamestates.h:24: error: `ChangeState' undeclared (first use this function)
gamestates.h:24: error: (Each undeclared identifier is reported only once for
each function it appears in.)