moved menu states into separate directory

main
Martin Felis (berta) 2010-06-06 01:22:01 +02:00
parent 50089c9755
commit 5b8e85d993
14 changed files with 729 additions and 227 deletions

View File

@ -7,8 +7,8 @@ LIST( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake )
# FIND_PACKAGE (Cal3D REQUIRED) # FIND_PACKAGE (Cal3D REQUIRED)
INCLUDE_DIRECTORIES ( INCLUDE_DIRECTORIES (
src/
engine/ engine/
asteroids/
include/ include/
engine/libraries/mathlib/ engine/libraries/mathlib/
engine/libraries/coll2d/include engine/libraries/coll2d/include
@ -28,6 +28,7 @@ SET ( ASTEROIDS_SOURCES
asteroids/ControllerCommands.cc asteroids/ControllerCommands.cc
asteroids/EntityFactory.cc asteroids/EntityFactory.cc
asteroids/EnumToString.cc asteroids/EnumToString.cc
asteroids/Game.cc
asteroids/main.cc asteroids/main.cc
asteroids/Model.cc asteroids/Model.cc
asteroids/ModelCommands.cc asteroids/ModelCommands.cc
@ -35,7 +36,7 @@ SET ( ASTEROIDS_SOURCES
asteroids/RocketEntity.cc asteroids/RocketEntity.cc
asteroids/ShipEntity.cc asteroids/ShipEntity.cc
asteroids/View.cc asteroids/View.cc
asteroids/MenuOverlay.cc asteroids/UserInterface.cc
) )
ADD_EXECUTABLE ( run_asteroids ${ASTEROIDS_SOURCES} ) ADD_EXECUTABLE ( run_asteroids ${ASTEROIDS_SOURCES} )

22
asteroids/Game.cc Normal file
View File

@ -0,0 +1,22 @@
#include "Engine.h"
#include "Game.h"
#include "Model.h"
#include "View.h"
#include "Controller.h"
namespace asteroids {
Model *GetModel () {
return dynamic_cast<Model*> (Engine::EngineGetModel());
}
View *GetView () {
return dynamic_cast<View*> (Engine::EngineGetView());
}
Controller *GetController () {
return dynamic_cast<Controller*> (Engine::EngineGetController());
}
}

15
asteroids/Game.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef GAME_H
#define GAME_H
namespace asteroids {
class Model;
class View;
class Controller;
Model *GetModel ();
View *GetView ();
Controller *GetController ();
}
#endif /* GAME_H */

View File

@ -1,203 +0,0 @@
#include "OGLFT.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "DrawingsGL.h"
#include "OverlayBase.h"
#include "MenuOverlay.h"
#include "Model.h"
#include "View.h"
#include "Sprite.h"
#include "ShipEntity.h"
#include "Engine.h"
namespace asteroids {
// static float left = 0;
static float right = 0;
// static float top = 0;
static float bottom = 0;
void MenuOverlay::Init () {
if (!mShipSprite.LoadFromPNG("./data/textures/ship.png"))
Engine::LogError ("Could not load ship sprite!");
// setup of the HUD font
mView->LoadFont ("AldotheApache.ttf", 20);
mView->SelectFont ("AldotheApache.ttf");
mView->SetFontJustification (Engine::FontJustificationRight);
assert (mShipSprite.GetWidth() > 1);
mShipSprite.SetScale (0.1);
}
bool MenuOverlay::OnKeyDown (const SDL_keysym &keysym) {
if (mModel->GetGameState() == GameStateLevelComplete) {
switch (keysym.sym) {
case SDLK_RETURN:
mModel->SetGameState(GameStateRunning);
break;
default:
break;
}
return true;
} else if (mModel->GetGameState() == GameStateRunning) {
switch (keysym.sym) {
case SDLK_ESCAPE:
mModel->SetGameState(GameStatePaused);
return true;
default:
break;
}
return false;
} else if (mModel->GetGameState() == GameStateGameOver) {
switch (keysym.sym) {
case SDLK_ESCAPE:
mModel->SetGameState(GameStateMainMenu);
break;
case SDLK_RETURN:
mModel->SetGameState(GameStateMainMenu);
break;
default:
break;
}
return true;
}
else if (mModel->GetGameState() == GameStateMainMenu
|| mModel->GetGameState() == GameStatePaused) {
switch (keysym.sym) {
case SDLK_ESCAPE:
Engine::RunCommand ("quit");
return true;
case SDLK_RETURN:
mModel->SetGameState(GameStateRunning);
return true;
default:
return true;
}
} else if (mModel->GetGameState() == GameStatePlayerDied) {
switch (keysym.sym) {
case SDLK_RETURN:
mModel->SetGameState(GameStateRunning);
break;
default:
break;
}
return true;
}
return false;
}
void MenuOverlay::Draw () {
glClearColor (0.1, 0.1, 0.1, 1.);
right = static_cast<float> (Engine::GetWindowWidth());
bottom = static_cast<float> (Engine::GetWindowHeight());
// we switch to orthographic projection and draw the contents of the 2d
// overlay on top of the previous drawings
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
// first we have to get the size of the current viewport to set up the
// orthographic projection correctly
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluOrtho2D (viewport[0], viewport[2], viewport[3], viewport[1]);
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity ();
mView->SelectFont("console.ttf");
// then we do the drawings
if (mModel->GetGameState() == GameStateRunning) {
glClearColor (0., 0., 0., 1.);
DrawGameRunning();
} else if (mModel->GetGameState() == GameStateGameOver)
DrawGameOverScreen ();
else if (mModel->GetGameState() == GameStateMainMenu
|| mModel->GetGameState() == GameStatePaused)
DrawGameMenu ();
else if (mModel->GetGameState() == GameStateLevelComplete)
DrawGameLevelComplete ();
else if (mModel->GetGameState() == GameStatePlayerDied)
DrawPlayerDied();
glPopMatrix ();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
};
void MenuOverlay::DrawGameRunning() {
right = static_cast<float> (Engine::GetWindowWidth());
bottom = static_cast<float> (Engine::GetWindowHeight());
mView->SelectFont ("AldotheApache.ttf");
std::ostringstream out_stream;
out_stream << mModel->GetPlayerLives() << " x ";
mView->DrawGLString (right - 64, bottom - 20, out_stream.str().c_str());
mShipSprite.DrawAt2D (right - 32 - 10, bottom - 16);
out_stream.str("");
out_stream << mModel->GetPoints();
mView->DrawGLString (right - 10, 40, out_stream.str().c_str());
}
void MenuOverlay::DrawPlayerDied () {
std::ostringstream topbar_stream;
topbar_stream << "You died ...";
Engine::DrawGLString ( right * 0.5 - 40, bottom * 0.5 - 8 - 32, topbar_stream.str().c_str ());
Engine::DrawGLString ( right * 0.5 - 90, bottom * 0.5 + 8, "Press [Return] to continue.");
}
void MenuOverlay::DrawGameOverScreen() {
std::ostringstream topbar_stream;
Engine::DrawGLString ( right * 0.5 - 60, bottom * 0.5 - 8 - 64, "G a m e O v e r");
if (mModel->GetPlayerLives() == 0) {
topbar_stream << "That was pathetic! ";
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8, topbar_stream.str().c_str ());
} else {
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 32, "You earned yourself the");
Engine::DrawGLString ( right * 0.5 - 60, bottom * 0.5 + 0, "You Rock(TM) award.");
Engine::DrawGLString ( right * 0.5 - 70, bottom * 0.5 + 32, "Go tell your friends.");
}
}
void MenuOverlay::DrawGameMenu() {
Engine::DrawGLString ( right * 0.5 - 100, bottom * 0.5 - 8 - 64, "A s t e r o i d s");
Engine::DrawGLString ( right * 0.5 - 100, bottom * 0.5 - 8 - 32, "Main Menu");
if (mModel->GetGameState() == GameStatePaused)
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8 - 16, "[Return] - Resume Game");
else
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8 - 16, "[Return] - Start Game");
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8, "[Escape] - Quit");
}
void MenuOverlay::DrawGameLevelComplete() {
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8 - 16, "Congratulations - You rock!");
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8, "[Return] - Next level ...");
}
}

411
asteroids/UserInterface.cc Normal file
View File

@ -0,0 +1,411 @@
#include "OGLFT.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "DrawingsGL.h"
#include "OverlayBase.h"
#include "UserInterface.h"
#include "Model.h"
#include "View.h"
#include "Sprite.h"
#include "ShipEntity.h"
#include "Engine.h"
#include "Game.h"
namespace asteroids {
// static float left = 0;
static float right = 0;
// static float top = 0;
static float bottom = 0;
void MainMenuOverlay::Init () {
// setup of the HUD font
GetView()->LoadFont ("AldotheApache.ttf", 20);
GetView()->SelectFont ("AldotheApache.ttf");
GetView()->SetFontJustification (Engine::FontJustificationRight);
}
bool MainMenuOverlay::OnKeyDown (const SDL_keysym &keysym) {
switch (keysym.sym) {
case SDLK_ESCAPE:
Engine::RunCommand ("quit");
return true;
case SDLK_RETURN:
GetModel()->SetGameState(GameStateRunning);
return true;
default:
return true;
}
}
void MainMenuOverlay::Draw () {
glClearColor (0.1, 0.1, 0.1, 1.);
right = static_cast<float> (Engine::GetWindowWidth());
bottom = static_cast<float> (Engine::GetWindowHeight());
// we switch to orthographic projection and draw the contents of the 2d
// overlay on top of the previous drawings
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
// first we have to get the size of the current viewport to set up the
// orthographic projection correctly
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluOrtho2D (viewport[0], viewport[2], viewport[3], viewport[1]);
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity ();
GetView()->SelectFont("console.ttf");
// then we do the drawings
Engine::DrawGLString ( right * 0.5 - 100, bottom * 0.5 - 8 - 64, "A s t e r o i d s");
Engine::DrawGLString ( right * 0.5 - 100, bottom * 0.5 - 8 - 32, "Main Menu");
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8 - 16, "[Return] - Start Game");
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8, "[Escape] - Quit");
glPopMatrix ();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
};
/**********************
*
* Game Running
*
**********************/
void GameRunningOverlay::Init () {
if (!mShipSprite.LoadFromPNG("./data/textures/ship.png"))
Engine::LogError ("Could not load ship sprite!");
// setup of the HUD font
GetView()->LoadFont ("AldotheApache.ttf", 20);
GetView()->SelectFont ("AldotheApache.ttf");
GetView()->SetFontJustification (Engine::FontJustificationRight);
assert (mShipSprite.GetWidth() > 1);
mShipSprite.SetScale (0.1);
}
bool GameRunningOverlay::OnKeyDown (const SDL_keysym &keysym) {
switch (keysym.sym) {
case SDLK_ESCAPE:
GetModel()->SetGameState(GameStatePaused);
return true;
default:
break;
}
return false;
}
void GameRunningOverlay::Draw () {
glClearColor (0., 0., 0., 1.);
right = static_cast<float> (Engine::GetWindowWidth());
bottom = static_cast<float> (Engine::GetWindowHeight());
// we switch to orthographic projection and draw the contents of the 2d
// overlay on top of the previous drawings
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
// first we have to get the size of the current viewport to set up the
// orthographic projection correctly
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluOrtho2D (viewport[0], viewport[2], viewport[3], viewport[1]);
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity ();
// then we do the drawings
right = static_cast<float> (Engine::GetWindowWidth());
bottom = static_cast<float> (Engine::GetWindowHeight());
GetView()->SelectFont ("AldotheApache.ttf");
std::ostringstream out_stream;
out_stream << GetModel()->GetPlayerLives() << " x ";
GetView()->DrawGLString (right - 64, bottom - 20, out_stream.str().c_str());
mShipSprite.DrawAt2D (right - 32 - 10, bottom - 16);
out_stream.str("");
out_stream << GetModel()->GetPoints();
GetView()->DrawGLString (right - 10, 40, out_stream.str().c_str());
glPopMatrix ();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
};
/**********************
*
* Game Over
*
**********************/
void GameOverOverlay::Init () {
}
bool GameOverOverlay::OnKeyDown (const SDL_keysym &keysym) {
switch (keysym.sym) {
case SDLK_ESCAPE:
GetModel()->SetGameState(GameStateMainMenu);
break;
case SDLK_RETURN:
GetModel()->SetGameState(GameStateMainMenu);
break;
default:
break;
}
return true;
}
void GameOverOverlay::Draw () {
glClearColor (0., 0., 0., 1.);
right = static_cast<float> (Engine::GetWindowWidth());
bottom = static_cast<float> (Engine::GetWindowHeight());
// we switch to orthographic projection and draw the contents of the 2d
// overlay on top of the previous drawings
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
// first we have to get the size of the current viewport to set up the
// orthographic projection correctly
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluOrtho2D (viewport[0], viewport[2], viewport[3], viewport[1]);
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity ();
GetView()->SelectFont("console.ttf");
// then we do the drawings
std::ostringstream topbar_stream;
Engine::DrawGLString ( right * 0.5 - 60, bottom * 0.5 - 8 - 64, "G a m e O v e r");
if (GetModel()->GetPlayerLives() == 0) {
topbar_stream << "That was pathetic! ";
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8, topbar_stream.str().c_str ());
} else {
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 32, "You earned yourself the");
Engine::DrawGLString ( right * 0.5 - 60, bottom * 0.5 + 0, "You Rock(TM) award.");
Engine::DrawGLString ( right * 0.5 - 70, bottom * 0.5 + 32, "Go tell your friends.");
}
glPopMatrix ();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
};
/**********************
*
* Level Complete
*
**********************/
void LevelCompleteOverlay::Init () {
}
bool LevelCompleteOverlay::OnKeyDown (const SDL_keysym &keysym) {
switch (keysym.sym) {
case SDLK_RETURN:
GetModel()->SetGameState(GameStateRunning);
break;
default:
break;
}
return true;
}
void LevelCompleteOverlay::Draw () {
glClearColor (0., 0., 0., 1.);
right = static_cast<float> (Engine::GetWindowWidth());
bottom = static_cast<float> (Engine::GetWindowHeight());
// we switch to orthographic projection and draw the contents of the 2d
// overlay on top of the previous drawings
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
// first we have to get the size of the current viewport to set up the
// orthographic projection correctly
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluOrtho2D (viewport[0], viewport[2], viewport[3], viewport[1]);
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity ();
GetView()->SelectFont("console.ttf");
// then we do the drawings
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8 - 16, "Congratulations - You rock!");
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8, "[Return] - Next level ...");
glPopMatrix ();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
};
/**********************
*
* Game Paused
*
**********************/
void GamePausedOverlay::Init () {
}
bool GamePausedOverlay::OnKeyDown (const SDL_keysym &keysym) {
switch (keysym.sym) {
case SDLK_ESCAPE:
Engine::RunCommand ("quit");
return true;
case SDLK_RETURN:
GetModel()->SetGameState(GameStateRunning);
return true;
default:
return true;
}
}
void GamePausedOverlay::Draw () {
glClearColor (0.1, 0.1, 0.1, 1.);
right = static_cast<float> (Engine::GetWindowWidth());
bottom = static_cast<float> (Engine::GetWindowHeight());
// we switch to orthographic projection and draw the contents of the 2d
// overlay on top of the previous drawings
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
// first we have to get the size of the current viewport to set up the
// orthographic projection correctly
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluOrtho2D (viewport[0], viewport[2], viewport[3], viewport[1]);
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity ();
GetView()->SelectFont("console.ttf");
// then we do the drawings
Engine::DrawGLString ( right * 0.5 - 100, bottom * 0.5 - 8 - 64, "A s t e r o i d s");
Engine::DrawGLString ( right * 0.5 - 100, bottom * 0.5 - 8 - 32, "Main Menu");
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8 - 16, "[Return] - Resume Game");
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8, "[Escape] - Quit");
glPopMatrix ();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
};
/**********************
*
* Player Died
*
**********************/
void PlayerDiedOverlay::Init () {
}
bool PlayerDiedOverlay::OnKeyDown (const SDL_keysym &keysym) {
switch (keysym.sym) {
case SDLK_RETURN:
GetModel()->SetGameState(GameStateRunning);
break;
default:
break;
}
return true;
}
void PlayerDiedOverlay::Draw () {
glClearColor (0., 0., 0., 1.);
right = static_cast<float> (Engine::GetWindowWidth());
bottom = static_cast<float> (Engine::GetWindowHeight());
// we switch to orthographic projection and draw the contents of the 2d
// overlay on top of the previous drawings
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
// first we have to get the size of the current viewport to set up the
// orthographic projection correctly
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluOrtho2D (viewport[0], viewport[2], viewport[3], viewport[1]);
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity ();
GetView()->SelectFont("console.ttf");
// then we do the drawings
std::ostringstream topbar_stream;
topbar_stream << "You died ...";
Engine::DrawGLString ( right * 0.5 - 40, bottom * 0.5 - 8 - 32, topbar_stream.str().c_str ());
Engine::DrawGLString ( right * 0.5 - 90, bottom * 0.5 + 8, "Press [Return] to continue.");
glPopMatrix ();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
};
}

97
asteroids/UserInterface.h Normal file
View File

@ -0,0 +1,97 @@
#ifndef USERINTERFACE
#define USERINTERFACE
namespace Engine {
class OverlayBase;
}
#include "OverlayBase.h"
#include "Sprite.h"
namespace asteroids {
class Model;
class View;
class MainMenuOverlay : public Engine::OverlayBase {
public:
MainMenuOverlay () {
};
virtual ~MainMenuOverlay() {};
virtual void Init ();
virtual bool OnKeyDown (const SDL_keysym &keysym);
virtual void Draw ();
private:
Engine::Sprite mShipSprite;
};
class GameRunningOverlay : public Engine::OverlayBase {
public:
GameRunningOverlay () {
};
virtual ~GameRunningOverlay() {};
virtual void Init ();
virtual bool OnKeyDown (const SDL_keysym &keysym);
virtual void Draw ();
private:
Engine::Sprite mShipSprite;
};
class GameOverOverlay : public Engine::OverlayBase {
public:
GameOverOverlay () {
};
virtual ~GameOverOverlay() {};
virtual void Init ();
virtual bool OnKeyDown (const SDL_keysym &keysym);
virtual void Draw ();
};
class LevelCompleteOverlay : public Engine::OverlayBase {
public:
LevelCompleteOverlay () {
};
virtual ~LevelCompleteOverlay() {};
virtual void Init ();
virtual bool OnKeyDown (const SDL_keysym &keysym);
virtual void Draw ();
};
class GamePausedOverlay : public Engine::OverlayBase {
public:
GamePausedOverlay () {
};
virtual ~GamePausedOverlay() {};
virtual void Init ();
virtual bool OnKeyDown (const SDL_keysym &keysym);
virtual void Draw ();
};
class PlayerDiedOverlay : public Engine::OverlayBase {
public:
PlayerDiedOverlay () {
};
virtual ~PlayerDiedOverlay() {};
virtual void Init ();
virtual bool OnKeyDown (const SDL_keysym &keysym);
virtual void Draw ();
};
}
#endif /* USERINTERFACE */

View File

@ -1,6 +1,6 @@
#include "View.h" #include "View.h"
#include "CameraBase.h" #include "CameraBase.h"
#include "MenuOverlay.h" #include "UserInterface.h"
#include "SimpleConsoleOverlay.h" #include "SimpleConsoleOverlay.h"
#include "Engine.h" #include "Engine.h"
@ -30,17 +30,29 @@ int View::OnInit (int argc, char* argv[]) {
ViewBase::OnInit (argc, argv); ViewBase::OnInit (argc, argv);
// We want menu // We want menu
mMenuOverlay = boost::shared_ptr<MenuOverlay> (new MenuOverlay); Engine::OverlayBasePtr menu_overlay (new MainMenuOverlay);
mMenuOverlay->SetModel ((Model*) mModel); Engine::OverlayBasePtr game_running_overlay (new GameRunningOverlay);
mMenuOverlay->SetView (this); Engine::OverlayBasePtr game_over_overlay (new GameOverOverlay);
mMenuOverlay->Init(); Engine::OverlayBasePtr level_complete_overlay (new LevelCompleteOverlay);
mOverlayManager.Register (mMenuOverlay, GameStateMainMenu); Engine::OverlayBasePtr game_paused_overlay (new GamePausedOverlay);
Engine::OverlayBasePtr player_died_overlay (new PlayerDiedOverlay);
mOverlayManager.Register (menu_overlay, GameStateMainMenu);
mOverlayManager.Register (game_running_overlay, GameStateRunning);
mOverlayManager.Register (level_complete_overlay, GameStateLevelComplete);
mOverlayManager.Register (game_over_overlay, GameStateGameOver);
mOverlayManager.Register (player_died_overlay, GameStatePlayerDied);
mOverlayManager.Register (game_paused_overlay, GameStatePaused);
mOverlayManager.InitOverlays();
/*
// We want the console // We want the console
mConsoleOverlay = boost::shared_ptr<Engine::SimpleConsoleOverlay> (new Engine::SimpleConsoleOverlay); mConsoleOverlay = boost::shared_ptr<Engine::SimpleConsoleOverlay> (new Engine::SimpleConsoleOverlay);
// We also want to display the log bar // We also want to display the log bar
mConsoleOverlay->SetDrawLogBar (true); mConsoleOverlay->SetDrawLogBar (true);
mOverlayManager.Register (mConsoleOverlay, GameStateMainMenu); mOverlayManager.Register (mConsoleOverlay, GameStateMainMenu);
*/
// This is a simple star field that makes the game so spacy // This is a simple star field that makes the game so spacy
int i; int i;
@ -79,8 +91,6 @@ void View::OnDestroy() {
mBackgroundStars.clear(); mBackgroundStars.clear();
mShipPartsEntityIds.clear(); mShipPartsEntityIds.clear();
mMenuOverlay.reset();
Engine::ViewBase::OnDestroy(); Engine::ViewBase::OnDestroy();
} }

View File

@ -10,7 +10,6 @@
namespace asteroids { namespace asteroids {
class MenuOverlay;
struct ShipEntity; struct ShipEntity;
struct AsteroidEntity; struct AsteroidEntity;
struct RocketEntity; struct RocketEntity;
@ -43,10 +42,8 @@ class View : public Engine::ViewBase {
void DrawRocket (RocketEntity *asteroid); void DrawRocket (RocketEntity *asteroid);
void DrawShipPart (Engine::EntityBase *entity); void DrawShipPart (Engine::EntityBase *entity);
// Engine::OverlayBasePtr mMenuOverlay; // boost::shared_ptr<MenuOverlay> mMenuOverlay;
// Engine::OverlayBasePtr mConsoleOverlay; // boost::shared_ptr<Engine::SimpleConsoleOverlay> mConsoleOverlay;
boost::shared_ptr<MenuOverlay> mMenuOverlay;
boost::shared_ptr<Engine::SimpleConsoleOverlay> mConsoleOverlay;
std::vector<BackgroundStar> mBackgroundStars; std::vector<BackgroundStar> mBackgroundStars;

View File

@ -34,6 +34,14 @@ BEGIN_ENUM(LogLevel)
} }
END_ENUM(LogLevel) END_ENUM(LogLevel)
BEGIN_ENUM(FontJustification)
{
DECL_ENUM_ELEMENT(FontJustificationRight),
DECL_ENUM_ELEMENT(FontJustificationCenter),
DECL_ENUM_ELEMENT(FontJustificationLeft)
}
END_ENUM(FontJustification)
} }
#endif /* _ENGINEENUMS_H */ #endif /* _ENGINEENUMS_H */

128
engine/OverlayBase.cc Normal file
View File

@ -0,0 +1,128 @@
#include "OverlayBase.h"
#include "ModelBase.h"
namespace Engine {
void OverlayManager::InitOverlays() {
std::map<unsigned int, std::list<OverlayBasePtr> >::iterator map_iter = mOverlays.begin();
if (map_iter == mOverlays.end())
return;
while (map_iter != mOverlays.end()) {
std::list<OverlayBasePtr>::iterator list_iter = map_iter->second.begin();
while (list_iter != map_iter->second.end()) {
(*list_iter)->Init ();
list_iter++;
}
map_iter ++;
}
}
void OverlayManager::Draw () {
ModelBase *model = EngineGetModel();
assert (model);
std::map<unsigned int, std::list<OverlayBasePtr> >::iterator map_iter = mOverlays.find(model->mGameState);
if (map_iter == mOverlays.end())
return;
std::list<OverlayBasePtr>::iterator list_iter = map_iter->second.begin();
while (list_iter != map_iter->second.end()) {
(*list_iter)->Draw ();
list_iter++;
}
}
void OverlayManager::Register (OverlayBasePtr overlay, unsigned int game_state) {
LogDebug ("Adding overlay 0x%x state: %u", &overlay, game_state);
mOverlays[game_state].push_back(overlay);
}
/* Input forwarding for the overlays */
bool OverlayManager::SendKeyDown (const SDL_keysym &keysym) {
ModelBase *model = EngineGetModel();
assert (model);
std::map<unsigned int, std::list<OverlayBasePtr> >::iterator map_iter = mOverlays.find(model->mGameState);
if (map_iter == mOverlays.end())
return false;
std::list<OverlayBasePtr>::iterator list_iter = map_iter->second.begin();
while (list_iter != map_iter->second.end()) {
if ((*list_iter)->OnKeyDown (keysym))
return true;
list_iter++;
}
return false;
}
bool OverlayManager::SendKeyUp (const SDL_keysym &keysym) {
ModelBase *model = EngineGetModel();
assert (model);
std::map<unsigned int, std::list<OverlayBasePtr> >::iterator map_iter = mOverlays.find(model->mGameState);
if (map_iter == mOverlays.end())
return false;
std::list<OverlayBasePtr>::iterator list_iter = map_iter->second.begin();
while (list_iter != map_iter->second.end()) {
if ((*list_iter)->OnKeyUp (keysym))
return true;
list_iter++;
}
return false;
}
bool OverlayManager::SendMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) {
ModelBase *model = EngineGetModel();
assert (model);
std::map<unsigned int, std::list<OverlayBasePtr> >::iterator map_iter = mOverlays.find(model->mGameState);
if (map_iter == mOverlays.end())
return false;
std::list<OverlayBasePtr>::iterator list_iter = map_iter->second.begin();
while (list_iter != map_iter->second.end()) {
if ((*list_iter)->OnMouseButtonUp (button, xpos, ypos))
return true;
list_iter++;
}
return false;
}
bool OverlayManager::SendMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) {
ModelBase *model = EngineGetModel();
assert (model);
std::map<unsigned int, std::list<OverlayBasePtr> >::iterator map_iter = mOverlays.find(model->mGameState);
if (map_iter == mOverlays.end())
return false;
std::list<OverlayBasePtr>::iterator list_iter = map_iter->second.begin();
while (list_iter != map_iter->second.end()) {
if ((*list_iter)->OnMouseButtonDown (button, xpos, ypos))
return true;
list_iter++;
}
return false;
}
}

View File

@ -12,12 +12,22 @@ namespace Engine {
class ModelBase; class ModelBase;
/** \brief Base class for user-interfaces /** \brief Base class for user-interfaces
*
* An Overlay is something that is displayed above the game graphics e.g.
* such as a health indicator for the player or the user interface such as
* the menu or the console. The member function OverlayBase::Draw() is used
* for drawing and also receives input such as key press events and mouse
* button events.
*
* The function OverlayBase::Init() is called by the OverlayManager class and
* allows the OverlayBase to load its ressources.
*/ */
class OverlayBase { class OverlayBase {
public: public:
OverlayBase () {}; OverlayBase () {};
virtual ~OverlayBase() {}; virtual ~OverlayBase() {};
/** \brief This function gets called by the OverlayManager class */
virtual void Init() {}; virtual void Init() {};
virtual bool OnKeyDown (const SDL_keysym &keysym) { return false; }; virtual bool OnKeyDown (const SDL_keysym &keysym) { return false; };
@ -25,22 +35,22 @@ class OverlayBase {
virtual bool OnMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) { return false; }; virtual bool OnMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) { return false; };
virtual bool OnMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) { return false; }; virtual bool OnMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) { return false; };
/** \brief Draws the content of the Overlay */
virtual void Draw () = 0; virtual void Draw () = 0;
void _strange_function_for_vtable () {};
}; };
typedef boost::shared_ptr<OverlayBase> OverlayBasePtr; typedef boost::shared_ptr<OverlayBase> OverlayBasePtr;
/** \brief Takes care of all OverlayBase classes and proxies input and drawing with regard to the current game state of ModelBase /** \brief Takes care of all OverlayBase classes and proxies input and drawing with regard to the current game state of ModelBase
* *
* \node You need to set the ModelBase pointer manually by calling OverlayManager::SetModel()! * \note You need to set the ModelBase pointer manually by calling OverlayManager::SetModel()!
*/ */
class OverlayManager { class OverlayManager {
public: public:
/** \brief Calls OverlayBase::Init() for all registered Overlays */ /** \brief Calls OverlayBase::Init() for all registered Overlays */
void InitOverlays(); void InitOverlays();
/** \brief Draws the Overlays associated with the current game state */
void Draw(); void Draw();
void Register (OverlayBasePtr overlay, unsigned int game_state); void Register (OverlayBasePtr overlay, unsigned int game_state);

View File

@ -371,6 +371,14 @@ void SelectFont (const char* font) {
ViewInstance->SelectFont(font); ViewInstance->SelectFont(font);
} }
void SetFontJustification (FontJustification justification) {
if (!ViewInstance) {
LogError ("Cannot select font: View not yet initialized!");
return;
}
ViewInstance->SetFontJustification (justification);
}
unsigned int GetWindowWidth() { unsigned int GetWindowWidth() {
return ViewInstance->GetWindowWidth (); return ViewInstance->GetWindowWidth ();
} }

View File

@ -2,6 +2,7 @@
#define _VIEWBASE_H #define _VIEWBASE_H
#include "Engine.h" #include "Engine.h"
#include "EngineEnums.h"
#include "OverlayBase.h" #include "OverlayBase.h"
// forward declarations for the OGLFT fonts // forward declarations for the OGLFT fonts
@ -11,12 +12,6 @@ namespace OGLFT {
namespace Engine { namespace Engine {
enum FontJustification {
FontJustificationRight = 0,
FontJustificationCenter,
FontJustificationLeft
};
class Module; class Module;
class ModelBase; class ModelBase;
class CameraBase; class CameraBase;

View File

@ -1,12 +1,15 @@
#ifndef _VIEWGLOBAL_H #ifndef _VIEWGLOBAL_H
#define _VIEWGLOBAL_H #define _VIEWGLOBAL_H
#include "EngineEnums.h"
namespace Engine { namespace Engine {
/** \brief Draws the given string at the given position using the current /** \brief Draws the given string at the given position using the current
* OpenGL transformations */ * OpenGL transformations */
void DrawGLString (float x, float y, const char* str); void DrawGLString (float x, float y, const char* str);
void SelectFont (const char* font); void SelectFont (const char* font);
void SetFontJustification (FontJustification justification);
unsigned int GetWindowWidth(); unsigned int GetWindowWidth();
unsigned int GetWindowHeight(); unsigned int GetWindowHeight();