This commit is contained in:
Eduardo Bart 2011-04-07 12:41:49 -03:00
parent 21bcbf9a97
commit 8d1281b316
8 changed files with 31 additions and 23 deletions

View File

@ -69,7 +69,6 @@ void Engine::run()
update(ticks, 0); update(ticks, 0);
lastUpdateTicks = ticks; lastUpdateTicks = ticks;
Font *font = g_fonts.getDefault();
Point fpsPos(10,10); Point fpsPos(10,10);
while(!m_stopping) { while(!m_stopping) {
@ -90,7 +89,7 @@ void Engine::run()
// render only when visible // render only when visible
if(Platform::isWindowVisible()) { if(Platform::isWindowVisible()) {
// calculate and fps // calculate and fps
if(m_calculateFps && font) { if(m_calculateFps) {
frameCount++; frameCount++;
if(ticks - lastFpsTicks >= 1000) { if(ticks - lastFpsTicks >= 1000) {
lastFpsTicks = ticks; lastFpsTicks = ticks;
@ -102,8 +101,8 @@ void Engine::run()
render(); render();
// render fps // render fps
if(m_calculateFps && font) { if(m_calculateFps) {
font->renderText(fpsPos, format("FPS: %d", fps)); g_defaultFont->renderText(fpsPos, format("FPS: %d", fps));
} }
// swap buffers // swap buffers

View File

@ -136,25 +136,28 @@ void Font::renderText(const Point& pos, const std::string& text)
// end texture redering // end texture redering
g_graphics._endTextureRender(); g_graphics._endTextureRender();
g_graphics.resetColor(); g_graphics.resetColor();
// debug box
//g_graphics.drawBoundingRect(Rect(pos, calculateTextSize(text)).expanded(1), Color(0xFF00FF00), 1);
} }
Size Font::calculateTextSize(const std::string& text) Size Font::calculateTextSize(const std::string& text)
{ {
int textLenght = text.length(); int textLenght = text.length();
Size size; Size size;
Point currentPos; Point currentPos(0,m_lineHeight);
for(int i = 0; i < textLenght; ++i) { for(int i = 0; i < textLenght; ++i) {
int glyph = (int)text[i]; int glyph = (int)text[i];
if(glyph == (uchar)'\n') { if(glyph == (uchar)'\n') {
currentPos.y += m_lineHeight; currentPos.y += m_lineHeight;
size.expandedTo(currentPos.toSize());
currentPos.x = 0; currentPos.x = 0;
} }
else if(glyph > 32) { else if(glyph >= 32) {
currentPos.x += m_glyphsSize[glyph].width(); currentPos.x += m_glyphsSize[glyph].width();
} }
size = size.expandedTo(currentPos.toSize());
} }
return size; return size;
} }

View File

@ -27,9 +27,11 @@
#include "resources.h" #include "resources.h"
Fonts g_fonts; Fonts g_fonts;
Font *g_defaultFont = NULL;
void Fonts::init() void Fonts::init()
{ {
// load all fonts
std::list<std::string> files = g_resources.getDirectoryFiles("fonts"); std::list<std::string> files = g_resources.getDirectoryFiles("fonts");
foreach(const std::string& file, files) { foreach(const std::string& file, files) {
if(boost::ends_with(file, ".yml")) { if(boost::ends_with(file, ".yml")) {
@ -40,24 +42,22 @@ void Fonts::init()
m_fonts[name] = font; 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) Font* Fonts::get(const std::string& fontName)
{ {
// find font by name
auto it = m_fonts.find(fontName); auto it = m_fonts.find(fontName);
if(it != m_fonts.end()) { if(it != m_fonts.end()) {
return it->second.get(); return it->second.get();
} }
logError("Font \"%s\" not found", fontName.c_str()); logError("Font \"%s\" not found", fontName.c_str());
return NULL; return NULL;
} }
Font *Fonts::getDefault()
{
Font *font = get("tibia-10px-rounded");
if(font) {
return font;
}
logFatal("Default font not found!");
return NULL;
}

View File

@ -39,9 +39,6 @@ public:
/// Get a font by name /// Get a font by name
Font *get(const std::string& fontName); Font *get(const std::string& fontName);
/// Get default font
Font *getDefault();
/// Terminate all fonts /// Terminate all fonts
void terminate() { } void terminate() { }
@ -50,5 +47,6 @@ private:
}; };
extern Fonts g_fonts; extern Fonts g_fonts;
extern Font *g_defaultFont;
#endif // FONTS_H #endif // FONTS_H

View File

@ -185,7 +185,7 @@ void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color)
{ {
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
glColor4ubv(color.rgbaPtr()); setColor(color);
// rect correction for opengl // rect correction for opengl
int right = screenCoords.right() + 1; int right = screenCoords.right() + 1;
@ -201,6 +201,8 @@ void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color)
glEnd(); glEnd();
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
resetColor();
} }
@ -211,7 +213,7 @@ void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, in
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
glColor4ubv(color.rgbaPtr()); setColor(color);
// rect correction for opengl // rect correction for opengl
int right = screenCoords.right()+1; int right = screenCoords.right()+1;
@ -246,4 +248,6 @@ void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, in
glEnd(); glEnd();
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
resetColor();
} }

View File

@ -63,7 +63,7 @@ public:
// high level rendering // high level rendering
void drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& texCoords = Rect()); void drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& texCoords = Rect());
void drawColoredRect(const Rect& screenCoords, const Color& color); 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 // lower level rendering
void _beginTextureRender(const Texture *texture); void _beginTextureRender(const Texture *texture);

View File

@ -98,6 +98,8 @@ public:
inline TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); } inline TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); }
inline TRect<T> translated(const TPoint<T> &p) const { return TRect<T>(TPoint<T>(x1 + p.x(), y1 + p.y()), TPoint<T>(x2 + p.x(), y2 + p.y())); } inline TRect<T> translated(const TPoint<T> &p) const { return TRect<T>(TPoint<T>(x1 + p.x(), y1 + p.y()), TPoint<T>(x2 + p.x(), y2 + p.y())); }
inline TRect<T> expanded(T pixels) { return TRect<T>(TPoint<T>(x1 - pixels, y1 - pixels), TPoint<T>(x2 + pixels, y2 + pixels)); }
inline void moveCenter(const TPoint<T> &p) { inline void moveCenter(const TPoint<T> &p) {
T w = x2 - x1; T w = x2 - x1;
T h = y2 - y1; T h = y2 - y1;

View File

@ -67,7 +67,9 @@ void MenuState::render()
Rect texCoords(0, 0, texCoordsSize); Rect texCoords(0, 0, texCoordsSize);
texCoords.moveBottomRight(texSize.toPoint()); texCoords.moveBottomRight(texSize.toPoint());
g_graphics.drawTexturedRect(Rect(0, 0, screenSize), m_background.get(), texCoords); 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) void MenuState::update(int ticks, int elapsedTicks)