fysxasteroids/asteroids/MenuOverlay.cc

204 lines
5.4 KiB
C++
Raw Normal View History

2010-04-05 23:38:59 +02:00
#include "OGLFT.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "DrawingsGL.h"
#include "OverlayBase.h"
#include "MenuOverlay.h"
#include "Model.h"
2010-05-02 22:39:49 +02:00
#include "View.h"
2010-04-05 23:38:59 +02:00
#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!");
2010-05-02 22:39:49 +02:00
// setup of the HUD font
mView->LoadFont ("AldotheApache.ttf", 20);
mView->SelectFont ("AldotheApache.ttf");
mView->SetFontJustification (Engine::FontJustificationRight);
2010-04-05 23:38:59 +02:00
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 ();
2010-05-02 22:39:49 +02:00
mView->SelectFont("console.ttf");
2010-04-05 23:38:59 +02:00
// 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());
2010-05-02 22:39:49 +02:00
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());
2010-04-05 23:38:59 +02:00
}
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.");
2010-04-05 23:38:59 +02:00
}
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.");
}
2010-04-05 23:38:59 +02:00
}
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 ...");
}
}