move render stuff to graphics

This commit is contained in:
Eduardo Bart 2011-04-07 04:30:32 -03:00
parent 9bd9f4ba53
commit 937070b3e0
6 changed files with 94 additions and 83 deletions

View File

@ -32,11 +32,6 @@ typedef uint32 RGBA;
class Color class Color
{ {
public: public:
enum {
white = 0xFFFFFFFF,
pink = 0xFF00FFFF
};
inline Color() : color(0) { } inline Color() : color(0) { }
inline Color(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) : color(((r & 0xff)<<24) | ((g & 0xff)<<16) | ((b & 0xff)<<8) | (a & 0xff)) { } inline Color(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) : color(((r & 0xff)<<24) | ((g & 0xff)<<16) | ((b & 0xff)<<8) | (a & 0xff)) { }
inline Color(const Color& other) : color(other.color) { } inline Color(const Color& other) : color(other.color) { }

View File

@ -27,18 +27,11 @@
#include "textures.h" #include "textures.h"
#include "graphics.h" #include "graphics.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
Font::Font() : Font::Font() :
m_lineHeight(14), m_lineHeight(14),
m_cursorSize(14), m_cursorSize(14),
m_color(Color::white), m_color(0xFFFFFFFF)
m_firstGlyph(32),
m_numHorizontalGlyphs(16)
{ {
bzero(m_glyphWidths, sizeof(m_glyphWidths));
} }
bool Font::load(const std::string& file) bool Font::load(const std::string& file)
@ -57,6 +50,10 @@ bool Font::load(const std::string& file)
std::istringstream fin(fileContents); std::istringstream fin(fileContents);
std::string textureName; std::string textureName;
int numHorizontalGlyphs;
int firstGlyph;
Size glyphSize;
Size textureSize;
try { try {
YAML::Parser parser(fin); YAML::Parser parser(fin);
@ -67,43 +64,48 @@ bool Font::load(const std::string& file)
doc["line height"] >> m_lineHeight; doc["line height"] >> m_lineHeight;
doc["cursor size"] >> m_cursorSize; doc["cursor size"] >> m_cursorSize;
doc["color"] >> m_color; doc["color"] >> m_color;
doc["first glyph"] >> m_firstGlyph; doc["first glyph"] >> firstGlyph;
doc["image glyph size"] >> glyphSize;
doc["image"] >> textureName; doc["image"] >> textureName;
doc["image glyph size"] >> m_glyphSize;
m_texture = g_textures.get("fonts/" + textureName);
if(!m_texture) {
error("Failed to load image for font \"%s\"", file.c_str());
return false;
}
textureSize = m_texture->getSize();
numHorizontalGlyphs = textureSize.width() / glyphSize.width();
const YAML::Node& widthsNode = doc["glyph widths"]; const YAML::Node& widthsNode = doc["glyph widths"];
for(auto it = widthsNode.begin(); it != widthsNode.end(); ++it) { for(auto it = widthsNode.begin(); it != widthsNode.end(); ++it) {
int id, width; int glyph, glyphWidth;
it.first() >> id; it.first() >> glyph;
it.second() >> width; it.second() >> glyphWidth;
m_glyphWidths[id] = width;
// calculate glyph texture coords
m_glyphsTextureCoords[glyph].setRect(((glyph - firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
((glyph - firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
glyphWidth,
glyphSize.height());
// store glyph size
m_glyphsSize[glyph].setHeight(glyphSize.height());
m_glyphsSize[glyph].setWidth(glyphWidth);
} }
} catch (YAML::ParserException& e) { } catch (YAML::ParserException& e) {
error("Malformed font file \"%s\"", file.c_str()); error("Malformed font file \"%s\"", file.c_str());
return false; return false;
} }
m_texture = g_textures.get("fonts/" + textureName);
m_numHorizontalGlyphs = m_texture->getSize().width() / m_glyphSize.width();
if(!m_texture) {
error("Failed to load image for font \"%s\"", file.c_str());
return false;
}
return true; return true;
} }
void Font::renderText(const Point& pos, const std::string& text) void Font::renderText(const Point& pos, const std::string& text)
{ {
// bind font texture // begin texture rendering
glBindTexture(GL_TEXTURE_2D, m_texture->getTextureId()); g_graphics.setColor(m_color);
g_graphics._beginTextureRender(m_texture.get());
// set font color
glColor4ubv(m_color.rgbaPtr());
// begin render
glBegin(GL_QUADS);
Point currentPos = pos; Point currentPos = pos;
const Size& screenSize = g_graphics.getScreenSize(); const Size& screenSize = g_graphics.getScreenSize();
@ -112,9 +114,7 @@ void Font::renderText(const Point& pos, const std::string& text)
for(int i = 0; i < textLenght; ++i) { for(int i = 0; i < textLenght; ++i) {
int c = (int)text[i]; int c = (int)text[i];
// check if is visible // break rendering if the Y pos is below the screen
if(currentPos.x >= screenSize.width())
continue;
if(currentPos.y >= screenSize.height()) if(currentPos.y >= screenSize.height())
break; break;
@ -123,45 +123,28 @@ void Font::renderText(const Point& pos, const std::string& text)
currentPos.y += m_lineHeight; currentPos.y += m_lineHeight;
currentPos.x = pos.x; currentPos.x = pos.x;
} }
// text eof // render glyph
else if(c == '\0') { else {
break; // render online if is visible on screen
} if(currentPos.x < screenSize.width())
// normal glyph currentPos.x += renderGlyph(currentPos, c);
else if(c >= m_firstGlyph) {
currentPos.x += renderGlyph(currentPos, c);
} }
} }
// end font render // end texture redering
glEnd(); g_graphics._endTextureRender();
g_graphics.resetColor();
} }
int Font::renderGlyph(const Point& pos, int glyph) int Font::renderGlyph(const Point& pos, int glyph)
{ {
// get glyph width // don't render invalid glyphs
int glyphWidth = m_glyphWidths[glyph]; if(glyph < 32)
return 0;
// calculate glyph coords on texture font
const Size& textureSize = m_texture->getSize();
int glyphTexCoordX = ((glyph - m_firstGlyph) % m_numHorizontalGlyphs) * m_glyphSize.width();
int glyphTexCoordY = ((glyph - m_firstGlyph) / m_numHorizontalGlyphs) * m_glyphSize.height();
float textureRight = (float)(glyphTexCoordX + glyphWidth) / textureSize.width();
float textureBottom = (float)(glyphTexCoordY + m_glyphSize.height()) / textureSize.height();
float textureTop = (float)(glyphTexCoordY) / textureSize.height();
float textureLeft = (float)(glyphTexCoordX) / textureSize.width();
// calculate glyph coords on screen
int right = pos.x + glyphWidth;
int bottom = pos.y + m_glyphSize.height();
int top = pos.y;
int left = pos.x;
// render glyph // render glyph
glTexCoord2f(textureLeft, textureTop); glVertex2i(left, top); Rect screenCoords(pos, m_glyphsSize[glyph]);
glTexCoord2f(textureLeft, textureBottom); glVertex2i(left, bottom); g_graphics._drawTexturedRect(screenCoords, m_glyphsTextureCoords[glyph], m_texture->getSize());
glTexCoord2f(textureRight, textureBottom); glVertex2i(right, bottom);
glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
return glyphWidth; return m_glyphsSize[glyph].width();
} }

View File

@ -28,6 +28,7 @@
#include "prerequisites.h" #include "prerequisites.h"
#include "color.h" #include "color.h"
#include "texture.h" #include "texture.h"
#include "rect.h"
class Font class Font
{ {
@ -67,11 +68,8 @@ private:
int m_cursorSize; int m_cursorSize;
Color m_color; Color m_color;
TexturePtr m_texture; TexturePtr m_texture;
Size m_textureSize; Rect m_glyphsTextureCoords[256];
Size m_glyphSize; Size m_glyphsSize[256];
int m_firstGlyph;
int m_glyphWidths[256];
int m_numHorizontalGlyphs;
}; };
#endif // FONT_H #endif // FONT_H

View File

@ -125,7 +125,37 @@ void Graphics::endRender()
} }
void Graphics::setColor(const Color& color)
{
glColor4ubv(color.rgbaPtr());
}
void Graphics::resetColor()
{
glColor4ub(0xFF, 0xFF, 0xFF, 0xFF);
}
void Graphics::_beginTextureRender(const Texture *texture)
{
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
glBegin(GL_QUADS);
}
void Graphics::_endTextureRender()
{
glEnd();
}
void Graphics::drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& textureCoords) void Graphics::drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& textureCoords)
{
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
glBegin(GL_QUADS);
_drawTexturedRect(screenCoords, textureCoords, texture->getSize());
glEnd();
}
void Graphics::_drawTexturedRect(const Rect& screenCoords, const Rect& textureCoords, const Size& textureSize)
{ {
// rect correction for opengl // rect correction for opengl
int right = screenCoords.right() + 1; int right = screenCoords.right() + 1;
@ -139,20 +169,16 @@ void Graphics::drawTexturedRect(const Rect& screenCoords, const Texture *texture
float textureLeft = 1.0f; float textureLeft = 1.0f;
if(!textureCoords.isEmpty()) { if(!textureCoords.isEmpty()) {
const Size& textureSize = texture->getSize();
textureRight = (float)(textureCoords.right() + 1) / textureSize.width(); textureRight = (float)(textureCoords.right() + 1) / textureSize.width();
textureBottom = (float)(textureCoords.bottom() + 1) / textureSize.height(); textureBottom = (float)(textureCoords.bottom() + 1) / textureSize.height();
textureTop = (float)textureCoords.top() / textureSize.height(); textureTop = (float)textureCoords.top() / textureSize.height();
textureLeft = (float)textureCoords.left() / textureSize.width(); textureLeft = (float)textureCoords.left() / textureSize.width();
} }
glBindTexture(GL_TEXTURE_2D, texture->getTextureId()); glTexCoord2f(textureLeft, textureTop); glVertex2i(left, top);
glBegin(GL_QUADS); glTexCoord2f(textureLeft, textureBottom); glVertex2i(left, bottom);
glTexCoord2f(textureLeft, textureTop); glVertex2i(left, top); glTexCoord2f(textureRight, textureBottom); glVertex2i(right, bottom);
glTexCoord2f(textureLeft, textureBottom); glVertex2i(left, bottom); glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
glTexCoord2f(textureRight, textureBottom); glVertex2i(right, bottom);
glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
glEnd();
} }
void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color) void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color)

View File

@ -57,10 +57,19 @@ public:
const Size& getScreenSize() const { return m_screenSize; } const Size& getScreenSize() const { return m_screenSize; }
void setColor(const Color& color);
void resetColor();
// 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);
// lower level rendering
void _beginTextureRender(const Texture *texture);
void _drawTexturedRect(const Rect& screenCoords, const Rect& textureCoords, const Size& textureSize);
void _endTextureRender();
private: private:
Size m_screenSize; Size m_screenSize;
}; };

View File

@ -44,7 +44,7 @@ public:
inline TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { } inline TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { }
inline TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { } inline TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { }
inline TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { } inline TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { }
inline TRect(const TPoint<T>& topLeft, const TSize<T>& size) : x1(topLeft.x), y1(topLeft.y), x2(x+size.width()-1), y2(y+size.height()-1) { } inline TRect(const TPoint<T>& topLeft, const TSize<T>& size) : x1(topLeft.x), y1(topLeft.y), x2(x1+size.width()-1), y2(y1+size.height()-1) { }
inline bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; } inline bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; }
inline bool isEmpty() const { return x1 > x2 || y1 > y2; } inline bool isEmpty() const { return x1 > x2 || y1 > y2; }