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