master
Eduardo Bart 13 years ago
parent 21bcbf9a97
commit 8d1281b316

@ -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

@ -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;
}

@ -27,9 +27,11 @@
#include "resources.h"
Fonts g_fonts;
Font *g_defaultFont = NULL;
void Fonts::init()
{
// load all fonts
std::list<std::string> 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;
}

@ -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

@ -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();
}

@ -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);

@ -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(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) {
T w = x2 - x1;
T h = y2 - y1;

@ -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)

Loading…
Cancel
Save