improved font handling and HUD

This commit is contained in:
2010-05-02 22:39:49 +02:00
parent 53220eb0ee
commit 710d3e2509
11 changed files with 132 additions and 24 deletions
+2
View File
@@ -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);
+74 -14
View File
@@ -52,19 +52,14 @@ 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 ();
}
+18 -2
View File
@@ -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;
};
+1
View File
@@ -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();