#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) { switch (keysym.sym) { case SDLK_ESCAPE: Engine::RunCommand ("quit"); return true; case SDLK_RETURN: GetModel()->SetGameState(GameStateEnterPlayername); return true; case SDLK_h: GetModel()->SetGameState(GameStateShowHighscore); 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 (); // Engine::DrawGLString ( right * 0.5 - 100, 100, "A s t e r o i d s"); Engine::GUI::Label (4, "A s t e r o i d s", right * 0.5 - 100, 180); if (Engine::GUI::Button (1, "Start Game", right * 0.5 - 100, 200, 250, 40)) { GetModel()->SetGameState(GameStateEnterPlayername); } if (Engine::GUI::Button (2, "Highscores", right * 0.5 - 100, 250, 250, 40)) { GetModel()->SetGameState(GameStateShowHighscore); } if (Engine::GUI::Button (3, "Quit", right * 0.5 - 100, 330, 250, 40)) { Engine::RunCommand("quit"); } GetView()->SelectFont("console.ttf"); /* // then we do the drawings 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, "[h] - Show Highscore"); 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(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) { GetController()->EnableTextinput(true); switch (keysym.sym) { case SDLK_ESCAPE: GetModel()->SetGameState(GameStateMainMenu); return true; case SDLK_BACKSPACE: if (mPlayerNameInput.size() > 0) mPlayerNameInput = mPlayerNameInput.substr (0, mPlayerNameInput.size() - 1 ); return true; break; case SDLK_RETURN: // If we just entered a new entry we simply show the highscore table, // otherwise we switch back to the main menu GetModel()->SetPlayerName(mPlayerNameInput); GetController()->EnableTextinput(false); GetModel()->SetGameState(GameStateRunning); return true; default: break; } if (keysym.unicode) { if ((keysym.unicode & 0xFF80) == 0) { mPlayerNameInput += keysym.unicode & 0x7F; return true; } else { Engine::LogWarning ("Input key not supported!"); return false; } } return true; } 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"); 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, "Enter your name: "); std::string name_output (mPlayerNameInput); if (SDL_GetTicks() >> 9 & 1) name_output += "_"; Engine::DrawGLString ( x + 15*8, y, name_output.c_str()); Engine::DrawGLString ( x + 16, y + 16, "Press [Return] to continue."); glPopMatrix (); glMatrixMode (GL_PROJECTION); glPopMatrix (); glMatrixMode (GL_MODELVIEW); }; }