diff --git a/CMakeLists.txt b/CMakeLists.txt index 564fb0e..1451d55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,6 @@ SET ( ASTEROIDS_SOURCES asteroids/RocketEntity.cc asteroids/ShipEntity.cc asteroids/View.cc - asteroids/UserInterface.cc ) ADD_EXECUTABLE ( run_asteroids ${ASTEROIDS_SOURCES} ) diff --git a/asteroids/UserInterface.cc b/asteroids/UserInterface.cc deleted file mode 100644 index 2f78fd1..0000000 --- a/asteroids/UserInterface.cc +++ /dev/null @@ -1,628 +0,0 @@ -#include "OGLFT.h" - -#include -#include - -#include "DrawingsGL.h" - -#include "OverlayBase.h" -#include "UserInterface.h" -#include "Model.h" -#include "View.h" -#include "Controller.h" -#include "Sprite.h" -#include "ShipEntity.h" - -#include "Engine.h" -#include "Game.h" - -#include "IMGUIControls.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) { - if (keysym.sym == SDLK_ESCAPE) - Engine::RunCommand("quit"); - return false; -} - -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 (); - - Engine::GUI::Label (4, "A s t e r o i d s", right * 0.5 - 100, 180); - - if (Engine::GUI::Button (1, "New Game", right * 0.5 - 100, 200, 250, 40)) { - GetModel()->SetGameState(GameStateEnterPlayername); - GetController()->uistate.hotitem = 0; - } - - if (Engine::GUI::Button (2, "Highscores", right * 0.5 - 100, 250, 250, 40)) { - GetModel()->SetGameState(GameStateShowHighscore); - GetController()->uistate.hotitem = 0; - } - - if (Engine::GUI::Button (3, "Quit", right * 0.5 - 100, 330, 250, 40)) { - Engine::RunCommand("quit"); - } - - GetView()->SelectFont("console.ttf"); - - 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(GameStateShowHighscore); - break; - case SDLK_RETURN: - GetModel()->SetGameState(GameStateShowHighscore); - 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); - -}; - -/********************** - * - * Highscore - * - **********************/ - -void HighscoreOverlay::Init () { -} - -bool HighscoreOverlay::OnKeyDown (const SDL_keysym &keysym) { - switch (keysym.sym) { - case SDLK_ESCAPE: - case SDLK_RETURN: - GetModel()->SetGameState(GameStateMainMenu); - - return true; - default: - break; - } - return true; -} - -void HighscoreOverlay::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"); - float x = right * 0.5 - 100; - float y = bottom * 0.5 - 8 - 128; - - // then we do the drawings - Engine::DrawGLString ( x, y, "A s t e r o i d s"); - y += 30; - Engine::DrawGLString ( x, y, "Highscore"); - - y += 30; - std::list::iterator highscore_iter = GetModel()->mHighscoreList.begin(); - - GetView()->SetFontJustification(Engine::FontJustificationLeft); - - unsigned int entry_length = 32; - - unsigned int i = 0; - while (highscore_iter != GetModel()->mHighscoreList.end()) { - // compute the number of digits - unsigned int digits = 1; - unsigned int temp_val = highscore_iter->points; - while (temp_val > 0) { - temp_val /= 10; - digits ++; - } - - // output of the name - std::ostringstream out_stream; - out_stream << highscore_iter->name; - - // add dots to fill the line - temp_val = entry_length - out_stream.str().size() - digits; - for (temp_val; temp_val > 0; temp_val--) { - out_stream << "."; - } - out_stream << highscore_iter->points; - - // Check whether we have to highlight an entry (such as when entering - // the name) - if (GetModel()->mNewestHighscoreEntryIndex < GetModel()->mHighscoreList.size() - && GetModel()->mNewestHighscoreEntryIndex == i) { - GetView()->SetFontColor (224./255., 200/255., 0.); - Engine::DrawGLString ( x, y, out_stream.str().c_str()); - GetView()->SetFontColor (1., 1., 1.); - } else { - Engine::DrawGLString ( x, y, out_stream.str().c_str()); - } - - // go down one line - y += 16; - - i++; - highscore_iter++; - } - - Engine::DrawGLString ( x + 16, y + 16, "Press [Return] to continue."); - - - glPopMatrix (); - - glMatrixMode (GL_PROJECTION); - glPopMatrix (); - - glMatrixMode (GL_MODELVIEW); - -}; - -/********************** - * - * EnterPlayername - * - **********************/ - -void EnterPlayernameOverlay::Init () { - mPlayerNameInput = "Starkiller"; -} - -bool EnterPlayernameOverlay::OnKeyDown (const SDL_keysym &keysym) { -/* if (keysym.sym == SDLK_ESCAPE) { - GetModel()->SetGameState(GameStateMainMenu); - GetController()->uistate.hotitem = 0; - return true; - } - */ - - return false; -} - -void EnterPlayernameOverlay::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"); - - // If ESC, we want to go back to the main menu (warning: OpenGL cleanup has - // still to be performed! - if (Engine::GUI::CheckKeyPress(SDLK_ESCAPE)) { - GetModel()->SetGameState(GameStateMainMenu); - glPopMatrix (); - - glMatrixMode (GL_PROJECTION); - glPopMatrix (); - - glMatrixMode (GL_MODELVIEW); - return; - } - - Engine::GUI::Label (4, "A s t e r o i d s", right * 0.5 - 100, 180); - - // Make sure we have UNICODE processing enabled! - GetController()->EnableTextinput(true); - - // Enter your name - std::string player_name = GetModel()->GetPlayerName(); - - Engine::GUI::Label (1, "Enter your name: ", right * 0.5 - 100, 250); - - if (Engine::GUI::LineEdit (2, right * 0.5 + 20, 238, player_name, 16)) { - GetModel()->SetPlayerName(player_name); - } - - // some test - static std::string otherstring("25blabla"); - - Engine::GUI::Label (6, "Enter your age : ", right * 0.5 - 100, 280); - - if (Engine::GUI::LineEdit (7, right * 0.5 + 20, 268, otherstring, 16)) { -// GetModel()->SetPlayerName(player_name); - } - - - if (Engine::GUI::Button (3, "Start Game", right - 150 - 20, 500, 150, 40)) { - GetModel()->SetGameState(GameStateRunning); - } - - if (Engine::GUI::Button (5, "Back", 20, 500, 150, 40)) { - GetModel()->SetGameState(GameStateMainMenu); - GetController()->uistate.hotitem = 0; - } - - glPopMatrix (); - - glMatrixMode (GL_PROJECTION); - glPopMatrix (); - - glMatrixMode (GL_MODELVIEW); - -}; - -} diff --git a/asteroids/UserInterface.h b/asteroids/UserInterface.h deleted file mode 100644 index f375bba..0000000 --- a/asteroids/UserInterface.h +++ /dev/null @@ -1,126 +0,0 @@ -#ifndef USERINTERFACE -#define USERINTERFACE - -namespace Engine { -class OverlayBase; -} - -#include "OverlayBase.h" -#include "Sprite.h" -#include "IMGUIControls.h" -#include - -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 (); -}; - -class HighscoreOverlay : public Engine::OverlayBase { - public: - HighscoreOverlay () { - }; - virtual ~HighscoreOverlay() {}; - - virtual void Init (); - - virtual bool OnKeyDown (const SDL_keysym &keysym); - virtual void Draw (); -}; - -class EnterPlayernameOverlay : public Engine::OverlayBase { - public: - EnterPlayernameOverlay () { - }; - virtual ~EnterPlayernameOverlay() {}; - - virtual void Init (); - - virtual bool OnKeyDown (const SDL_keysym &keysym); - virtual void Draw (); - - private: - std::string mPlayerNameInput; -}; - -} - -#endif /* USERINTERFACE */ - diff --git a/asteroids/View.cc b/asteroids/View.cc index 137a366..b9a0556 100644 --- a/asteroids/View.cc +++ b/asteroids/View.cc @@ -1,19 +1,22 @@ #include "View.h" #include "CameraBase.h" -#include "UserInterface.h" #include "SimpleConsoleOverlay.h" +#include "IMGUIControls.h" #include "Engine.h" #include "Physics.h" #include "Model.h" +#include "Controller.h" #include "EventBase.h" +#include "Game.h" #include "ShipEntity.h" #include "AsteroidEntity.h" #include "AsteroidsEvents.h" #include "RocketEntity.h" #include +#include // #define DRAW_BOUNDARIES @@ -29,27 +32,6 @@ namespace asteroids { int View::OnInit (int argc, char* argv[]) { ViewBase::OnInit (argc, argv); - // We want menu - 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); - Engine::OverlayBasePtr highscore_overlay (new HighscoreOverlay); - Engine::OverlayBasePtr enterplayername_overlay (new EnterPlayernameOverlay); - - 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.Register (highscore_overlay, GameStateShowHighscore); - mOverlayManager.Register (enterplayername_overlay, GameStateEnterPlayername); - - mOverlayManager.InitOverlays(); - /* // We want the console mConsoleOverlay = boost::shared_ptr (new Engine::SimpleConsoleOverlay); @@ -58,6 +40,8 @@ int View::OnInit (int argc, char* argv[]) { mOverlayManager.Register (mConsoleOverlay, GameStateMainMenu); */ + LoadFont("AldotheApache.ttf", 20); + // This is a simple star field that makes the game so spacy int i; for (i = 0; i < 200; i++) { @@ -69,6 +53,9 @@ int View::OnInit (int argc, char* argv[]) { mBackgroundStars.push_back (star); } + mGUIShipSprite.LoadFromPNG("./data/textures/ship.png"); + mGUIShipSprite.SetScale (0.1); + mAsteroidSprite.LoadFromPNG ("./data/textures/asteroid.png"); mShipSprite.LoadFromPNG ("./data/textures/ship.png"); @@ -197,6 +184,29 @@ void View::DrawStars() { } +void View::Draw() { + PreDraw(); + + // Actual Drawing + UpdateCamera (); + + if (mDrawGrid) + DrawGrid (); + + /* + if (mDrawAxis) + DrawAxis (); + */ + + DrawWorld (); + + DrawUi (); +// mOverlayManager.Draw(); + + // Perform post-Draw actions + PostDraw(); +} + void View::DrawWorld() { std::map::iterator entity_iterator; @@ -309,9 +319,260 @@ void View::DrawWorld() { glPopMatrix (); } } +} + +/* + * Userinterface + */ +void View::DrawUi () { + glClearColor (0.1, 0.1, 0.1, 1.); + + screen_right = static_cast (Engine::GetWindowWidth()); + screen_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 (); + + unsigned int game_state = GetModel()->GetGameState(); + + SelectFont("console.ttf"); + SetFontJustification (Engine::FontJustificationLeft); + GetController()->EnableTextinput(true); + + switch (game_state) { + case GameStateMainMenu: + DrawUiMainMenu(); + break; + case GameStateRunning: + DrawUiGameRunning(); + break; + case GameStatePaused: + DrawUiGamePaused(); + break; + case GameStatePlayerDied: + DrawUiPlayerDied(); + break; + case GameStateLevelComplete: + DrawUiLevelComplete(); + break; + case GameStateShowHighscore: + DrawUiHighscore(); + break; + case GameStateEnterPlayername: + DrawUiEnterPlayername(); + break; + case GameStateGameOver: + DrawUiGameOver(); + break; + default: + Engine::LogWarning ("Trying to draw unknown GameState: %s (%d)", + GetStringGameState (game_state), game_state); + break; + } + + glPopMatrix (); + + glMatrixMode (GL_PROJECTION); + glPopMatrix (); + + glMatrixMode (GL_MODELVIEW); } +void View::DrawUiMainMenu() { + Engine::GUI::Label (4, "A s t e r o i d s", screen_right * 0.5 - 100, 180); + + if (Engine::GUI::Button (1, "New Game", screen_right * 0.5 - 100, 200, 250, 40)) { + GetModel()->SetGameState(GameStateEnterPlayername); + } + + if (Engine::GUI::Button (2, "Highscores", screen_right * 0.5 - 100, 250, 250, 40)) { + GetModel()->SetGameState(GameStateShowHighscore); + } + + if (Engine::GUI::Button (3, "Quit", screen_right * 0.5 - 100, 330, 250, 40)) { + Engine::RunCommand("quit"); + } +} + +void View::DrawUiGameRunning() { + // We choose a different font and also draw it aligned to the right as this + // looks nicer with the points + SelectFont ("AldotheApache.ttf"); + SetFontJustification (Engine::FontJustificationRight); + + std::ostringstream out_stream; + out_stream << GetModel()->GetPlayerLives() << " x "; + DrawGLString (screen_right - 64, screen_bottom - 20, out_stream.str().c_str()); + + mGUIShipSprite.DrawAt2D (screen_right - 32 - 10, screen_bottom - 16); + + out_stream.str(""); + out_stream << GetModel()->GetPoints(); + DrawGLString (screen_right - 30, 40, out_stream.str().c_str()); + + // revert the font justification + SetFontJustification (Engine::FontJustificationLeft); + + if (Engine::GUI::CheckKeyPress(SDLK_ESCAPE)) + GetModel()->SetGameState(GameStatePaused); +} + +void View::DrawUiGameOver() { + Engine::GUI::Label (4, "G a m e O v e r", screen_right * 0.5 - 100, 180); + + if (GetModel()->GetPlayerLives() == 0) { + Engine::GUI::Label(5, "That was pathetic!", screen_right * 0.5 - 80, screen_bottom * 0.5 - 8); + } else { + Engine::GUI::Label (6, "You earned yourself the", + screen_right * 0.5 - 80, + screen_bottom * 0.5 - 32); + Engine::GUI::Label (7, "You Rock(TM) award.", + screen_right * 0.5 - 60, + screen_bottom * 0.5 + 0); + Engine::GUI::Label (8, "Go tell your friends.", + screen_right * 0.5 - 70, + screen_bottom * 0.5 + 32); + } + + if (Engine::GUI::Button (2, "Continue...", + screen_right * 0.5 - 100, + screen_bottom * 0.5 + 60, 250, 40)) { + GetModel()->SetGameState(GameStateShowHighscore); + } +} + +void View::DrawUiLevelComplete() { + Engine::GUI::Label (4, "Level Complete!", screen_right * 0.5 - 100, 180); + + Engine::GUI::Label (6, "Congratulations - You rock!", screen_right * 0.5 - 80, screen_bottom * 0.5 -16); + + if(Engine::GUI::Button (1, "Next level ...", screen_right * 0.5 - 80, screen_bottom * 0.5 -16, 250, 40)) { + GetModel()->SetGameState(GameStateRunning); + } +} + +void View::DrawUiGamePaused() { + Engine::GUI::Label (4, "- P a u s e d -", screen_right * 0.5 - 100, 180); + + if (Engine::GUI::Button (1, "Resume Game", screen_right * 0.5 - 100, 200, 250, 40)) { + GetModel()->SetGameState(GameStateRunning); + } + + if (Engine::GUI::Button (2, "Abort Game", screen_right * 0.5 - 100, 250, 250, 40)) { + GetModel()->SetGameState(GameStateMainMenu); + } + + if (Engine::GUI::Button (3, "Quit", screen_right * 0.5 - 100, 330, 250, 40)) { + Engine::RunCommand("quit"); + } +} + +void View::DrawUiPlayerDied() { + Engine::GUI::Label (4, "You died ...", screen_right * 0.5 - 40, screen_bottom * 0.5 - 8 - 32); + + if (Engine::GUI::Button (1, "Continue", screen_right * 0.5 - 100, 200, 250, 40)) { + GetModel()->SetGameState(GameStateRunning); + } +} + +void View::DrawUiHighscore() { + float x = screen_right * 0.5 - 100; + float y = screen_bottom * 0.5 - 8 - 128; + + // then we do the drawings + Engine::DrawGLString ( x, y, "A s t e r o i d s"); + y += 30; + Engine::DrawGLString ( x, y, "Highscore"); + + y += 30; + std::list::iterator highscore_iter = GetModel()->mHighscoreList.begin(); + + SetFontJustification(Engine::FontJustificationLeft); + + unsigned int entry_length = 32; + + unsigned int i = 0; + while (highscore_iter != GetModel()->mHighscoreList.end()) { + // compute the number of digits + unsigned int digits = 1; + unsigned int temp_val = highscore_iter->points; + while (temp_val > 0) { + temp_val /= 10; + digits ++; + } + + // output of the name + std::ostringstream out_stream; + out_stream << highscore_iter->name; + + // add dots to fill the line + temp_val = entry_length - out_stream.str().size() - digits; + for (temp_val; temp_val > 0; temp_val--) { + out_stream << "."; + } + out_stream << highscore_iter->points; + + // Check whether we have to highlight an entry (such as when entering + // the name) + if (GetModel()->mNewestHighscoreEntryIndex < GetModel()->mHighscoreList.size() + && GetModel()->mNewestHighscoreEntryIndex == i) { + SetFontColor (224./255., 200/255., 0.); + Engine::DrawGLString ( x, y, out_stream.str().c_str()); + SetFontColor (1., 1., 1.); + } else { + Engine::DrawGLString ( x, y, out_stream.str().c_str()); + } + + // go down one line + y += 16; + + i++; + highscore_iter++; + } + + if (Engine::GUI::Button (1, "Back to MainMenu", x + 16, y + 16, 250, 40)) { + GetModel()->SetGameState(GameStateMainMenu); + } +} + +void View::DrawUiEnterPlayername() { + Engine::GUI::Label (4, "A s t e r o i d s", screen_right * 0.5 - 100, 180); + + // Enter your name + std::string player_name = GetModel()->GetPlayerName(); + + Engine::GUI::Label (1, "Enter your name: ", screen_right * 0.5 - 100, 250); + + if (Engine::GUI::LineEdit (2, screen_right * 0.5 + 20, 238, player_name, 16)) { + GetModel()->SetPlayerName(player_name); + } + + if (Engine::GUI::Button (3, "Start Game", screen_right - 150 - 20, 500, 150, 40)) { + GetModel()->SetGameState(GameStateRunning); + } + + if (Engine::GUI::Button (5, "Back", 20, 500, 150, 40)) { + GetModel()->SetGameState(GameStateMainMenu); + } +} + +/* + * Entities + */ void View::DrawEntity (Engine::EntityBase *entity) { if (entity->mType == GameEntityTypeAsteroid) DrawAsteroid ((AsteroidEntity*) entity); diff --git a/asteroids/View.h b/asteroids/View.h index c49238a..2a9ea61 100644 --- a/asteroids/View.h +++ b/asteroids/View.h @@ -33,6 +33,18 @@ class View : public Engine::ViewBase { virtual void UpdateCamera (); private: + void DrawUi(); + + void DrawUiMainMenu(); + void DrawUiGameRunning(); + void DrawUiGameOver(); + void DrawUiLevelComplete(); + void DrawUiGamePaused(); + void DrawUiPlayerDied(); + void DrawUiHighscore(); + void DrawUiEnterPlayername(); + + virtual void Draw (); virtual void DrawWorld (); void DrawStars (); @@ -49,10 +61,17 @@ class View : public Engine::ViewBase { std::vector mShipPartsEntityIds; + // \todo [high] add Resource Manager! + Engine::Sprite mGUIShipSprite; Engine::Sprite mAsteroidSprite; Engine::Sprite mShipSprite; Engine::Sprite mShipThrustSprite; Engine::Sprite mShipPartsSprite; + + float screen_left; + float screen_right; + float screen_top; + float screen_bottom; virtual bool OnReceiveEvent (const Engine::EventBasePtr &event); }; diff --git a/engine/ViewBase.cc b/engine/ViewBase.cc index c399dd5..f04524d 100644 --- a/engine/ViewBase.cc +++ b/engine/ViewBase.cc @@ -62,8 +62,6 @@ int ViewBase::OnInit (int argc, char* argv[]) { mOverlayManager.Register (console_overlay, 0); //AddOverlay (console_overlay); - mDrawAxis = false; - mDrawGrid = false; mGridSizeX = 8; mGridSizeZ = 8; @@ -178,9 +176,6 @@ void ViewBase::Draw () { if (mDrawGrid) DrawGrid (); - if (mDrawAxis) - DrawAxis (); - DrawWorld (); mOverlayManager.Draw(); diff --git a/engine/ViewBase.h b/engine/ViewBase.h index 9b0ef64..8dddd5f 100644 --- a/engine/ViewBase.h +++ b/engine/ViewBase.h @@ -76,6 +76,7 @@ class ViewBase : public Module{ bool SendMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos); */ + private: protected: /** \brief Initializes the system */ virtual int OnInit (int argc, char* argv[]); @@ -84,13 +85,16 @@ class ViewBase : public Module{ /** \brief Updates the camera for further drawing */ virtual void UpdateCamera (); - /** \brief Draws a grid of 16 x 16 tiles */ - void DrawGrid (); /** \brief Draws the level and all the visible Entities */ virtual void DrawWorld (); /** \brief Draws orthographic overlay*/ void DrawOverlay2D (); + /** \brief Draws a grid of 16 x 16 tiles */ + void DrawGrid (); + bool mDrawGrid; + + ModelBase *mModel; CameraBase *mCamera; @@ -105,8 +109,6 @@ class ViewBase : public Module{ /** \brief Stores the current frame rate */ int mFrameRate; - bool mDrawAxis; - bool mDrawGrid; int mGridSizeX; int mGridSizeZ;