improved font handling and HUD
parent
53220eb0ee
commit
710d3e2509
|
@ -8,6 +8,7 @@
|
|||
#include "OverlayBase.h"
|
||||
#include "MenuOverlay.h"
|
||||
#include "Model.h"
|
||||
#include "View.h"
|
||||
#include "Sprite.h"
|
||||
#include "ShipEntity.h"
|
||||
|
||||
|
@ -24,6 +25,11 @@ 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);
|
||||
}
|
||||
|
@ -110,6 +116,8 @@ void MenuOverlay::Draw () {
|
|||
glPushMatrix ();
|
||||
glLoadIdentity ();
|
||||
|
||||
mView->SelectFont("console.ttf");
|
||||
|
||||
// then we do the drawings
|
||||
if (mModel->GetGameState() == GameStateRunning) {
|
||||
glClearColor (0., 0., 0., 1.);
|
||||
|
@ -137,10 +145,17 @@ void MenuOverlay::DrawGameRunning() {
|
|||
right = static_cast<float> (Engine::GetWindowWidth());
|
||||
bottom = static_cast<float> (Engine::GetWindowHeight());
|
||||
|
||||
int i;
|
||||
for (i = 0; i < mModel->GetPlayerLives(); i++) {
|
||||
mShipSprite.DrawAt2D (right - 32 - i*20, bottom - 16);
|
||||
}
|
||||
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 () {
|
||||
|
|
|
@ -11,6 +11,7 @@ class OverlayBase;
|
|||
namespace asteroids {
|
||||
|
||||
class Model;
|
||||
class View;
|
||||
|
||||
class MenuOverlay : public Engine::OverlayBase {
|
||||
public:
|
||||
|
@ -30,9 +31,11 @@ class MenuOverlay : public Engine::OverlayBase {
|
|||
void DrawPlayerDied ();
|
||||
|
||||
void SetModel (Model *model) { mModel = model; };
|
||||
void SetView (View *view) { mView = view; };
|
||||
|
||||
private:
|
||||
Model *mModel;
|
||||
View *mView;
|
||||
Engine::Sprite mShipSprite;
|
||||
};
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include "EntityFactory.h"
|
||||
#include "AsteroidsEvents.h"
|
||||
#include "AsteroidEntity.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
|
@ -56,6 +57,7 @@ void Model::Process () {
|
|||
if (mLastGameState == GameStateMainMenu && mGameState == GameStateRunning) {
|
||||
mPlayerLives = 3;
|
||||
mCurrentLevelIndex = 0;
|
||||
mPoints = 0;
|
||||
DoLoadLevel (mLevelList[mCurrentLevelIndex].c_str());
|
||||
}
|
||||
else if (mLastGameState == GameStateRunning && mGameState == GameStatePlayerDied) {
|
||||
|
@ -75,8 +77,7 @@ void Model::Process () {
|
|||
}
|
||||
}
|
||||
else if (mLastGameState == GameStatePlayerDied && mGameState == GameStateRunning)
|
||||
// DoLoadLevel (mLevelPath);
|
||||
assert (0);
|
||||
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
|
||||
else if (mLastGameState == GameStateRunning && mGameState == GameStateGameOver)
|
||||
ClearEntities();
|
||||
|
||||
|
@ -105,13 +106,14 @@ unsigned int Model::InitLevelList () {
|
|||
Engine::LogError ("Could not init level list: %s is not a directory!");
|
||||
}
|
||||
|
||||
|
||||
boost::filesystem::directory_iterator end_iter;
|
||||
for (boost::filesystem::directory_iterator dir_iter(level_dir);
|
||||
dir_iter != end_iter;
|
||||
++dir_iter) {
|
||||
if (boost::filesystem::is_regular_file (dir_iter->status())) {
|
||||
mLevelList.push_back (std::string(level_dir_name) + dir_iter->path().filename());
|
||||
std::string level_relative_path (level_dir_name);
|
||||
level_relative_path += dir_iter->path().filename();
|
||||
mLevelList.push_back (level_relative_path);
|
||||
Engine::LogDebug ("Found level %s", mLevelList[mLevelList.size()-1].c_str());
|
||||
}
|
||||
}
|
||||
|
@ -248,6 +250,8 @@ void Model::OnKillEntity (const Engine::EntityBase *entity) {
|
|||
|
||||
if (entity_type == GameEntityTypeAsteroid) {
|
||||
unsigned int i;
|
||||
const AsteroidEntity *asteroid = static_cast<const AsteroidEntity*>(entity);
|
||||
mPoints += 150 + asteroid->mSubAsteroidsCount * 75;
|
||||
|
||||
for (i = 0; i < mAsteroids.size(); i++) {
|
||||
if (mAsteroids.at(i) == entity->mId) {
|
||||
|
|
|
@ -21,6 +21,7 @@ class Model : public Engine::ModelBase {
|
|||
GameState GetGameState () { return mGameState; };
|
||||
|
||||
int GetPlayerLives () { return mPlayerLives; };
|
||||
unsigned int GetPoints () { return mPoints; };
|
||||
|
||||
float GetWorldWidth ();
|
||||
float GetWorldHeight ();
|
||||
|
@ -31,6 +32,7 @@ class Model : public Engine::ModelBase {
|
|||
virtual void OnDestroy() {
|
||||
Engine::ModelBase::OnDestroy();
|
||||
mAsteroids.clear();
|
||||
mLevelList.clear();
|
||||
delete mLevelCompleteEventHandler;
|
||||
};
|
||||
virtual void OnRegisterCommands ();
|
||||
|
@ -48,6 +50,7 @@ class Model : public Engine::ModelBase {
|
|||
GameState mLastGameState;
|
||||
|
||||
int mPlayerLives;
|
||||
unsigned int mPoints;
|
||||
std::vector<std::string> mLevelList;
|
||||
unsigned int mCurrentLevelIndex;
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@ int View::OnInit (int argc, char* argv[]) {
|
|||
// We want menu
|
||||
mMenuOverlay = boost::shared_ptr<MenuOverlay> (new MenuOverlay);
|
||||
mMenuOverlay->SetModel ((Model*) mModel);
|
||||
mMenuOverlay->SetView (this);
|
||||
mMenuOverlay->Init();
|
||||
AddOverlay (mMenuOverlay);
|
||||
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,3 @@
|
|||
AldotheApache.ttf from http://ajpaglia.com/
|
||||
SamsTown.ttf from http://www.dafont.com/sams-town.font
|
||||
console.ttf
|
|
@ -75,6 +75,8 @@ bool SimpleConsoleOverlay::OnKeyDown (const SDL_keysym &keysym) {
|
|||
}
|
||||
|
||||
void SimpleConsoleOverlay::Draw () {
|
||||
SelectFont ("console.ttf");
|
||||
|
||||
// we switch to orthographic projection and draw the contents of the 2d
|
||||
// overlay on top of the previous drawings
|
||||
glMatrixMode (GL_PROJECTION);
|
||||
|
|
|
@ -53,18 +53,13 @@ int ViewBase::OnInit (int argc, char* argv[]) {
|
|||
InitGL ();
|
||||
Resize (mWindowWidth, mWindowHeight);
|
||||
|
||||
mConsoleFont = new OGLFT::Monochrome ("./data/fonts/console.ttf", 12);
|
||||
|
||||
if ( mConsoleFont == 0 || !mConsoleFont->isValid() ) {
|
||||
LogError ("Could not load font %s!", "./data/fonts/console.ttf");
|
||||
exit (-1);
|
||||
}
|
||||
LoadFont ("console.ttf");
|
||||
mCurrentFont = mFonts["console.ttf"];
|
||||
SetFontColor (1., 1., 1.);
|
||||
|
||||
OverlayBasePtr console_overlay(new SimpleConsoleOverlay);
|
||||
AddOverlay (console_overlay);
|
||||
|
||||
mConsoleFont->setForegroundColor (1., 1., 1.);
|
||||
|
||||
mDrawAxis = false;
|
||||
|
||||
mDrawGrid = false;
|
||||
|
@ -77,13 +72,16 @@ int ViewBase::OnInit (int argc, char* argv[]) {
|
|||
}
|
||||
|
||||
void ViewBase::OnDestroy () {
|
||||
if (mConsoleFont )
|
||||
delete mConsoleFont;
|
||||
mConsoleFont = NULL;
|
||||
|
||||
while (mOverlays.size() > 0)
|
||||
mOverlays.pop_back();
|
||||
|
||||
std::map<std::string, OGLFT::Monochrome*>::iterator iter;
|
||||
|
||||
for (iter = mFonts.begin(); iter != mFonts.end(); ++iter) {
|
||||
delete iter->second;
|
||||
}
|
||||
mFonts.clear();
|
||||
|
||||
ViewInstance = NULL;
|
||||
|
||||
LogDebug ("View Destroy");
|
||||
|
@ -189,9 +187,63 @@ void ViewBase::Draw () {
|
|||
SDL_GL_SwapBuffers ();
|
||||
}
|
||||
|
||||
/* Fonts */
|
||||
bool ViewBase::LoadFont (const char *font_name, float point_size) {
|
||||
std::string font_path ("./data/fonts/");
|
||||
font_path += font_name;
|
||||
|
||||
LogDebug ("Loading font %s size %f from %s", font_name, point_size, font_path.c_str());
|
||||
|
||||
OGLFT::Monochrome *font = new OGLFT::Monochrome (font_path.c_str(), point_size);
|
||||
if ( font == 0 || !font->isValid() ) {
|
||||
LogError ("Could not load font %s!", font_path.c_str());
|
||||
}
|
||||
font->setForegroundColor(1., 1., 1.);
|
||||
|
||||
mFonts.insert(std::make_pair<std::string, OGLFT::Monochrome*>(font_name, font));
|
||||
}
|
||||
|
||||
void ViewBase::SelectFont (const char *font) {
|
||||
std::map<std::string, OGLFT::Monochrome*>::iterator font_iter;
|
||||
|
||||
font_iter = mFonts.find(font);
|
||||
|
||||
if (font_iter != mFonts.end()) {
|
||||
mCurrentFont = font_iter->second;
|
||||
return;
|
||||
}
|
||||
|
||||
LogDebug ("Font %s not found, trying to load it", font);
|
||||
if (LoadFont (font)) {
|
||||
font_iter = mFonts.find(font);
|
||||
|
||||
assert (mFonts.find(font) != mFonts.end());
|
||||
|
||||
mCurrentFont = font_iter->second;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
LogError("Error trying to load font %s", font);
|
||||
}
|
||||
|
||||
void ViewBase::SetFontJustification (FontJustification justification) {
|
||||
if (justification == FontJustificationRight)
|
||||
mCurrentFont->setHorizontalJustification(OGLFT::Face::RIGHT);
|
||||
else if (justification == FontJustificationCenter)
|
||||
mCurrentFont->setHorizontalJustification(OGLFT::Face::CENTER);
|
||||
else mCurrentFont->setHorizontalJustification(OGLFT::Face::LEFT);
|
||||
|
||||
}
|
||||
|
||||
void ViewBase::SetFontColor (float r, float g, float b) {
|
||||
assert (mCurrentFont);
|
||||
mCurrentFont->setForegroundColor(r, g, b);
|
||||
};
|
||||
|
||||
void ViewBase::DrawGLString (float x, float y, const char* str) {
|
||||
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
|
||||
mConsoleFont->draw (x, y, str);
|
||||
mCurrentFont->draw (x, y, str);
|
||||
}
|
||||
|
||||
void ViewBase::GetCamereEye (float *eye_out) {
|
||||
|
@ -310,6 +362,14 @@ void DrawGLString (float x, float y, const char* str) {
|
|||
ViewInstance->DrawGLString (x, y, str);
|
||||
}
|
||||
|
||||
void SelectFont (const char* font) {
|
||||
if (!ViewInstance) {
|
||||
LogError ("Cannot select font: View not yet initialized!");
|
||||
return;
|
||||
}
|
||||
ViewInstance->SelectFont(font);
|
||||
}
|
||||
|
||||
unsigned int GetWindowWidth() {
|
||||
return ViewInstance->GetWindowWidth ();
|
||||
}
|
||||
|
|
|
@ -11,6 +11,12 @@ namespace OGLFT {
|
|||
|
||||
namespace Engine {
|
||||
|
||||
enum FontJustification {
|
||||
FontJustificationRight = 0,
|
||||
FontJustificationCenter,
|
||||
FontJustificationLeft
|
||||
};
|
||||
|
||||
class Module;
|
||||
class ModelBase;
|
||||
class CameraBase;
|
||||
|
@ -20,6 +26,7 @@ class CameraBase;
|
|||
|
||||
class ViewBase : public Module{
|
||||
public:
|
||||
ViewBase() : mModel(NULL), mCamera(NULL), mCurrentFont(NULL) {}
|
||||
virtual ~ViewBase() {};
|
||||
|
||||
/** \brief Resizes the View */
|
||||
|
@ -28,9 +35,15 @@ class ViewBase : public Module{
|
|||
/** \brief Performs all drawing */
|
||||
virtual void Draw ();
|
||||
|
||||
/* Fonts */
|
||||
bool LoadFont (const char *font_name, float point_size=12);
|
||||
void SelectFont (const char *font_name);
|
||||
void SetFontJustification (FontJustification justification);
|
||||
void SetFontColor (float r, float g, float b);
|
||||
/** \brief Draws a string at the given position using current projection
|
||||
* and modelview matrices */
|
||||
void DrawGLString (float x, float y, const char* str);
|
||||
|
||||
/** \brief Stores the eye poisition in eye_out */
|
||||
void GetCamereEye (float *eye_out);
|
||||
|
||||
|
@ -87,8 +100,11 @@ class ViewBase : public Module{
|
|||
int mGridSizeX;
|
||||
int mGridSizeZ;
|
||||
|
||||
/** \brief Font that is used in the console */
|
||||
OGLFT::Monochrome* mConsoleFont;
|
||||
/** \brief The font that is currently used */
|
||||
OGLFT::Monochrome* mCurrentFont;
|
||||
|
||||
/** \brief Contains all the fonts that are to be used */
|
||||
std::map<std::string, OGLFT::Monochrome*> mFonts;
|
||||
|
||||
friend class Engine;
|
||||
};
|
||||
|
|
|
@ -6,6 +6,7 @@ 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);
|
||||
|
||||
unsigned int GetWindowWidth();
|
||||
unsigned int GetWindowHeight();
|
||||
|
|
Loading…
Reference in New Issue