From 5b8e85d993885c4948aa3504778c8077639019d6 Mon Sep 17 00:00:00 2001 From: "Martin Felis (berta)" Date: Sun, 6 Jun 2010 01:22:01 +0200 Subject: [PATCH] moved menu states into separate directory --- CMakeLists.txt | 5 +- asteroids/Game.cc | 22 ++ asteroids/Game.h | 15 ++ asteroids/MenuOverlay.cc | 203 ------------------ asteroids/UserInterface.cc | 411 +++++++++++++++++++++++++++++++++++++ asteroids/UserInterface.h | 97 +++++++++ asteroids/View.cc | 26 ++- asteroids/View.h | 7 +- engine/EngineEnums.h | 8 + engine/OverlayBase.cc | 128 ++++++++++++ engine/OverlayBase.h | 16 +- engine/ViewBase.cc | 8 + engine/ViewBase.h | 7 +- engine/ViewBaseGlobal.h | 3 + 14 files changed, 729 insertions(+), 227 deletions(-) create mode 100644 asteroids/Game.cc create mode 100644 asteroids/Game.h delete mode 100644 asteroids/MenuOverlay.cc create mode 100644 asteroids/UserInterface.cc create mode 100644 asteroids/UserInterface.h create mode 100644 engine/OverlayBase.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index cff7bee..564fb0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,8 @@ LIST( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake ) # FIND_PACKAGE (Cal3D REQUIRED) INCLUDE_DIRECTORIES ( - src/ engine/ + asteroids/ include/ engine/libraries/mathlib/ engine/libraries/coll2d/include @@ -28,6 +28,7 @@ SET ( ASTEROIDS_SOURCES asteroids/ControllerCommands.cc asteroids/EntityFactory.cc asteroids/EnumToString.cc + asteroids/Game.cc asteroids/main.cc asteroids/Model.cc asteroids/ModelCommands.cc @@ -35,7 +36,7 @@ SET ( ASTEROIDS_SOURCES asteroids/RocketEntity.cc asteroids/ShipEntity.cc asteroids/View.cc - asteroids/MenuOverlay.cc + asteroids/UserInterface.cc ) ADD_EXECUTABLE ( run_asteroids ${ASTEROIDS_SOURCES} ) diff --git a/asteroids/Game.cc b/asteroids/Game.cc new file mode 100644 index 0000000..0b5122d --- /dev/null +++ b/asteroids/Game.cc @@ -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 (Engine::EngineGetModel()); +} + +View *GetView () { + return dynamic_cast (Engine::EngineGetView()); +} + +Controller *GetController () { + return dynamic_cast (Engine::EngineGetController()); +} + +} diff --git a/asteroids/Game.h b/asteroids/Game.h new file mode 100644 index 0000000..e875cff --- /dev/null +++ b/asteroids/Game.h @@ -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 */ diff --git a/asteroids/MenuOverlay.cc b/asteroids/MenuOverlay.cc deleted file mode 100644 index 2961b44..0000000 --- a/asteroids/MenuOverlay.cc +++ /dev/null @@ -1,203 +0,0 @@ -#include "OGLFT.h" - -#include -#include - -#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 (Engine::GetWindowWidth()); - bottom = static_cast (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 (Engine::GetWindowWidth()); - bottom = static_cast (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 ..."); -} - - -} diff --git a/asteroids/UserInterface.cc b/asteroids/UserInterface.cc new file mode 100644 index 0000000..c303118 --- /dev/null +++ b/asteroids/UserInterface.cc @@ -0,0 +1,411 @@ +#include "OGLFT.h" + +#include +#include + +#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 (Engine::GetWindowWidth()); + bottom = static_cast (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 (Engine::GetWindowWidth()); + bottom = static_cast (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 (Engine::GetWindowWidth()); + bottom = static_cast (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 (Engine::GetWindowWidth()); + bottom = static_cast (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 (Engine::GetWindowWidth()); + bottom = static_cast (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 (Engine::GetWindowWidth()); + bottom = static_cast (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 (Engine::GetWindowWidth()); + bottom = static_cast (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); + +}; + +} diff --git a/asteroids/UserInterface.h b/asteroids/UserInterface.h new file mode 100644 index 0000000..45fe79f --- /dev/null +++ b/asteroids/UserInterface.h @@ -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 */ + diff --git a/asteroids/View.cc b/asteroids/View.cc index 2d772fa..5b2d44f 100644 --- a/asteroids/View.cc +++ b/asteroids/View.cc @@ -1,6 +1,6 @@ #include "View.h" #include "CameraBase.h" -#include "MenuOverlay.h" +#include "UserInterface.h" #include "SimpleConsoleOverlay.h" #include "Engine.h" @@ -30,17 +30,29 @@ int View::OnInit (int argc, char* argv[]) { ViewBase::OnInit (argc, argv); // We want menu - mMenuOverlay = boost::shared_ptr (new MenuOverlay); - mMenuOverlay->SetModel ((Model*) mModel); - mMenuOverlay->SetView (this); - mMenuOverlay->Init(); - mOverlayManager.Register (mMenuOverlay, GameStateMainMenu); + Engine::OverlayBasePtr menu_overlay (new MainMenuOverlay); + Engine::OverlayBasePtr game_running_overlay (new GameRunningOverlay); + Engine::OverlayBasePtr game_over_overlay (new GameOverOverlay); + Engine::OverlayBasePtr level_complete_overlay (new LevelCompleteOverlay); + 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 mConsoleOverlay = boost::shared_ptr (new Engine::SimpleConsoleOverlay); // We also want to display the log bar mConsoleOverlay->SetDrawLogBar (true); mOverlayManager.Register (mConsoleOverlay, GameStateMainMenu); + */ // This is a simple star field that makes the game so spacy int i; @@ -79,8 +91,6 @@ void View::OnDestroy() { mBackgroundStars.clear(); mShipPartsEntityIds.clear(); - mMenuOverlay.reset(); - Engine::ViewBase::OnDestroy(); } diff --git a/asteroids/View.h b/asteroids/View.h index 1a9d8d4..8e6fbd6 100644 --- a/asteroids/View.h +++ b/asteroids/View.h @@ -10,7 +10,6 @@ namespace asteroids { -class MenuOverlay; struct ShipEntity; struct AsteroidEntity; struct RocketEntity; @@ -43,10 +42,8 @@ class View : public Engine::ViewBase { void DrawRocket (RocketEntity *asteroid); void DrawShipPart (Engine::EntityBase *entity); -// Engine::OverlayBasePtr mMenuOverlay; -// Engine::OverlayBasePtr mConsoleOverlay; - boost::shared_ptr mMenuOverlay; - boost::shared_ptr mConsoleOverlay; + // boost::shared_ptr mMenuOverlay; + // boost::shared_ptr mConsoleOverlay; std::vector mBackgroundStars; diff --git a/engine/EngineEnums.h b/engine/EngineEnums.h index e268884..76cb548 100644 --- a/engine/EngineEnums.h +++ b/engine/EngineEnums.h @@ -34,6 +34,14 @@ BEGIN_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 */ diff --git a/engine/OverlayBase.cc b/engine/OverlayBase.cc new file mode 100644 index 0000000..5c592b9 --- /dev/null +++ b/engine/OverlayBase.cc @@ -0,0 +1,128 @@ +#include "OverlayBase.h" +#include "ModelBase.h" + +namespace Engine { + +void OverlayManager::InitOverlays() { + std::map >::iterator map_iter = mOverlays.begin(); + + if (map_iter == mOverlays.end()) + return; + + while (map_iter != mOverlays.end()) { + std::list::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 >::iterator map_iter = mOverlays.find(model->mGameState); + + if (map_iter == mOverlays.end()) + return; + + std::list::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 >::iterator map_iter = mOverlays.find(model->mGameState); + + if (map_iter == mOverlays.end()) + return false; + + std::list::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 >::iterator map_iter = mOverlays.find(model->mGameState); + + if (map_iter == mOverlays.end()) + return false; + + std::list::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 >::iterator map_iter = mOverlays.find(model->mGameState); + + if (map_iter == mOverlays.end()) + return false; + + std::list::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 >::iterator map_iter = mOverlays.find(model->mGameState); + + if (map_iter == mOverlays.end()) + return false; + + std::list::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; +} + +} diff --git a/engine/OverlayBase.h b/engine/OverlayBase.h index 77655d3..2f65eb0 100644 --- a/engine/OverlayBase.h +++ b/engine/OverlayBase.h @@ -12,12 +12,22 @@ namespace Engine { class ModelBase; /** \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 { public: OverlayBase () {}; virtual ~OverlayBase() {}; + /** \brief This function gets called by the OverlayManager class */ virtual void Init() {}; 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 OnMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) { return false; }; + /** \brief Draws the content of the Overlay */ virtual void Draw () = 0; - - void _strange_function_for_vtable () {}; }; typedef boost::shared_ptr OverlayBasePtr; /** \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 { public: /** \brief Calls OverlayBase::Init() for all registered Overlays */ void InitOverlays(); + /** \brief Draws the Overlays associated with the current game state */ void Draw(); void Register (OverlayBasePtr overlay, unsigned int game_state); diff --git a/engine/ViewBase.cc b/engine/ViewBase.cc index da44367..024ff5a 100644 --- a/engine/ViewBase.cc +++ b/engine/ViewBase.cc @@ -371,6 +371,14 @@ void SelectFont (const char* 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() { return ViewInstance->GetWindowWidth (); } diff --git a/engine/ViewBase.h b/engine/ViewBase.h index 53415db..13e8009 100644 --- a/engine/ViewBase.h +++ b/engine/ViewBase.h @@ -2,6 +2,7 @@ #define _VIEWBASE_H #include "Engine.h" +#include "EngineEnums.h" #include "OverlayBase.h" // forward declarations for the OGLFT fonts @@ -11,12 +12,6 @@ namespace OGLFT { namespace Engine { -enum FontJustification { - FontJustificationRight = 0, - FontJustificationCenter, - FontJustificationLeft -}; - class Module; class ModelBase; class CameraBase; diff --git a/engine/ViewBaseGlobal.h b/engine/ViewBaseGlobal.h index b91af06..ae74fe2 100644 --- a/engine/ViewBaseGlobal.h +++ b/engine/ViewBaseGlobal.h @@ -1,12 +1,15 @@ #ifndef _VIEWGLOBAL_H #define _VIEWGLOBAL_H +#include "EngineEnums.h" + namespace Engine { /** \brief Draws the given string at the given position using the current * OpenGL transformations */ void DrawGLString (float x, float y, const char* str); void SelectFont (const char* font); +void SetFontJustification (FontJustification justification); unsigned int GetWindowWidth(); unsigned int GetWindowHeight();