diff --git a/src/framework/engine.cpp b/src/framework/engine.cpp index b3611f23..d04bfc26 100644 --- a/src/framework/engine.cpp +++ b/src/framework/engine.cpp @@ -69,7 +69,6 @@ void Engine::run() update(ticks, 0); lastUpdateTicks = ticks; - Font *font = g_fonts.getDefault(); Point fpsPos(10,10); while(!m_stopping) { @@ -90,7 +89,7 @@ void Engine::run() // render only when visible if(Platform::isWindowVisible()) { // calculate and fps - if(m_calculateFps && font) { + if(m_calculateFps) { frameCount++; if(ticks - lastFpsTicks >= 1000) { lastFpsTicks = ticks; @@ -102,8 +101,8 @@ void Engine::run() render(); // render fps - if(m_calculateFps && font) { - font->renderText(fpsPos, format("FPS: %d", fps)); + if(m_calculateFps) { + g_defaultFont->renderText(fpsPos, format("FPS: %d", fps)); } // swap buffers diff --git a/src/framework/font.cpp b/src/framework/font.cpp index 0503dbfb..0118e40d 100644 --- a/src/framework/font.cpp +++ b/src/framework/font.cpp @@ -136,25 +136,28 @@ void Font::renderText(const Point& pos, const std::string& text) // end texture redering g_graphics._endTextureRender(); g_graphics.resetColor(); + + // debug box + //g_graphics.drawBoundingRect(Rect(pos, calculateTextSize(text)).expanded(1), Color(0xFF00FF00), 1); } Size Font::calculateTextSize(const std::string& text) { int textLenght = text.length(); Size size; - Point currentPos; + Point currentPos(0,m_lineHeight); for(int i = 0; i < textLenght; ++i) { int glyph = (int)text[i]; if(glyph == (uchar)'\n') { currentPos.y += m_lineHeight; - size.expandedTo(currentPos.toSize()); currentPos.x = 0; } - else if(glyph > 32) { + else if(glyph >= 32) { currentPos.x += m_glyphsSize[glyph].width(); } + size = size.expandedTo(currentPos.toSize()); } return size; } diff --git a/src/framework/fonts.cpp b/src/framework/fonts.cpp index 72ebfb1e..09007daa 100644 --- a/src/framework/fonts.cpp +++ b/src/framework/fonts.cpp @@ -27,9 +27,11 @@ #include "resources.h" Fonts g_fonts; +Font *g_defaultFont = NULL; void Fonts::init() { + // load all fonts std::list files = g_resources.getDirectoryFiles("fonts"); foreach(const std::string& file, files) { if(boost::ends_with(file, ".yml")) { @@ -40,24 +42,22 @@ void Fonts::init() m_fonts[name] = font; } } + + // set default font + g_defaultFont = get("tibia-12px-rounded"); + if(!g_defaultFont) + logFatal("Default font not found!"); } Font* Fonts::get(const std::string& fontName) { + // find font by name auto it = m_fonts.find(fontName); if(it != m_fonts.end()) { return it->second.get(); } + logError("Font \"%s\" not found", fontName.c_str()); return NULL; } -Font *Fonts::getDefault() -{ - Font *font = get("tibia-10px-rounded"); - if(font) { - return font; - } - logFatal("Default font not found!"); - return NULL; -} diff --git a/src/framework/fonts.h b/src/framework/fonts.h index 119a00c6..0475d6a9 100644 --- a/src/framework/fonts.h +++ b/src/framework/fonts.h @@ -39,9 +39,6 @@ public: /// Get a font by name Font *get(const std::string& fontName); - /// Get default font - Font *getDefault(); - /// Terminate all fonts void terminate() { } @@ -50,5 +47,6 @@ private: }; extern Fonts g_fonts; +extern Font *g_defaultFont; #endif // FONTS_H diff --git a/src/framework/graphics.cpp b/src/framework/graphics.cpp index 7655874d..dd2013ec 100644 --- a/src/framework/graphics.cpp +++ b/src/framework/graphics.cpp @@ -185,7 +185,7 @@ void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color) { glDisable(GL_TEXTURE_2D); - glColor4ubv(color.rgbaPtr()); + setColor(color); // rect correction for opengl int right = screenCoords.right() + 1; @@ -201,6 +201,8 @@ void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color) glEnd(); glEnable(GL_TEXTURE_2D); + + resetColor(); } @@ -211,7 +213,7 @@ void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, in glDisable(GL_TEXTURE_2D); - glColor4ubv(color.rgbaPtr()); + setColor(color); // rect correction for opengl int right = screenCoords.right()+1; @@ -246,4 +248,6 @@ void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, in glEnd(); glEnable(GL_TEXTURE_2D); + + resetColor(); } diff --git a/src/framework/graphics.h b/src/framework/graphics.h index c192b2a6..2ecc4730 100644 --- a/src/framework/graphics.h +++ b/src/framework/graphics.h @@ -63,7 +63,7 @@ public: // high level rendering void drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& texCoords = Rect()); void drawColoredRect(const Rect& screenCoords, const Color& color); - void drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth); + void drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth = 1); // lower level rendering void _beginTextureRender(const Texture *texture); diff --git a/src/framework/rect.h b/src/framework/rect.h index ab78e222..c4a58be5 100644 --- a/src/framework/rect.h +++ b/src/framework/rect.h @@ -98,6 +98,8 @@ public: inline TRect translated(int x, int y) const { return TRect(TPoint(x1 + x, y1 + y), TPoint(x2 + x, y2 + y)); } inline TRect translated(const TPoint &p) const { return TRect(TPoint(x1 + p.x(), y1 + p.y()), TPoint(x2 + p.x(), y2 + p.y())); } + inline TRect expanded(T pixels) { return TRect(TPoint(x1 - pixels, y1 - pixels), TPoint(x2 + pixels, y2 + pixels)); } + inline void moveCenter(const TPoint &p) { T w = x2 - x1; T h = y2 - y1; diff --git a/src/menustate.cpp b/src/menustate.cpp index 63b139e4..96393132 100644 --- a/src/menustate.cpp +++ b/src/menustate.cpp @@ -67,7 +67,9 @@ void MenuState::render() Rect texCoords(0, 0, texCoordsSize); texCoords.moveBottomRight(texSize.toPoint()); g_graphics.drawTexturedRect(Rect(0, 0, screenSize), m_background.get(), texCoords); - g_fonts.getDefault()->renderText(Point(10,screenSize.height() - 20), "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac orci id quam condimentum semper."); + g_defaultFont->renderText(Point(10,screenSize.height() - 50), "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" + "Sed blandit justo in lectus ornare ultricies.\n" + "Integer faucibus magna quis metus fermentum suscipit."); } void MenuState::update(int ticks, int elapsedTicks)