libsegl/glfontengine.cpp

250 lines
5.3 KiB
C++
Raw Blame History

#include "glfontengine.h"
bool GLFontEngine::addFont(std::string fontfile, std::string fontname) {
GLFont *tmp = new GLFont(fontfile, 0.685f);
if(!tmp->font->isLoaded()) {
delete(tmp);
std::cerr << fontfile << " konnte als Font nicht geladen werden" << std::endl;
return false;
}
fontpool[fontname] = tmp;
return false;
}
void GLFontEngine::quit() {
for(std::map<std::string, GLFont*>::iterator iter = fontpool.begin(); iter != fontpool.end(); iter++) {
delete(iter->second); // unloaded texture.. der dtor
fontpool.erase(iter);
}
}
void GLFontEngine::prepare2DbyPushingMatrix() {
const SDL_Surface *screen = SDL_GetVideoSurface();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, screen->w, screen->h, 0, -1, 1);
// glOrtho(0, 640, 480, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}
void GLFontEngine::regain3DbyPoppingMatrix() {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
void GLFontEngine::paintSDLRect(SDL_Rect r) {
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex2i(r.x, r.y);
glTexCoord2f(0.0f, 0.0f);
glVertex2i(r.x, r.y+r.h);
glTexCoord2f(1.0f, 0.0f);
glVertex2i(r.x+r.w, r.y+r.h);
glTexCoord2f(1.0f, 1.0f);
glVertex2i(r.x+r.w, r.y);
glEnd();
}
GLFontEngine::GLFontEngine() {
init();
fontloaded = fontSelect("");
}
GLFontEngine::GLFontEngine(std::string fontstr) {
init();
fontloaded = fontSelect(fontstr);
}
void GLFontEngine::init() {
// r = g = b = a = 1.0f;
col.set(1.0f, 1.0f, 1.0f);
fsize = 16;
}
void GLFontEngine::setColor(float _r, float _g, float _b, float _a) {
// r = _r;
// g = _g;
// b = _b;
// a = _a;
col.set(_r, _g, _b, _a);
}
void GLFontEngine::setColor(GLColor c) {
col = c;
}
bool GLFontEngine::fontSelect(std::string fontstr) {
if(fontpool.size()==0) {
fontloaded = false;
return false;
}
fontloaded = true; // fontstr oder fallbackfont
if(fontpool[fontstr])
font = fontpool[fontstr];
else {
//fallbackfont - ersten aus der liste
font = (++fontpool.begin())->second;
// std::cout << "font: " << font << std::endl;
}
return true;
}
void GLFontEngine::renderText(std::string text, SDL_Rect pos) {
renderLine(text, pos);
}
void GLFontEngine::renderLine(std::string text, SDL_Rect pos) {
// glColor4f(r, g, b, a);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glColorGLC(col);
if(fontloaded) {
font->font->selectTexture();
float step = pos.w / (float)text.length();
float tex_x, tex_y;
float tex_len = (1.0f/16.0f);
for(unsigned int i=0; i<text.length(); i++) {
int ch = text[i];
if(ch<0) {
ch += 256; // char geht wohl in machen f<>llen nur von -128 bis +127 *hust*
}
// std::cout << (int)text[i] << " ";
tex_x = ((ch-1)%16 / 16.0f);
tex_y = 1.0f-(floor((ch-1)/16) / 16.0f)-tex_len;
// std::cout << text[i] << " == " << tex_x << ", " << tex_y << std::endl;
glBegin(GL_QUADS);
glTexCoord2f(tex_x, tex_y+tex_len);
glVertex2f(pos.x+i*step, pos.y);
glTexCoord2f(tex_x, tex_y);
glVertex2f(pos.x+i*step, pos.y+pos.h);
glTexCoord2f(tex_x+tex_len, tex_y);
glVertex2f(pos.x+(i+1)*step, pos.y+pos.h);
glTexCoord2f(tex_x+tex_len, tex_y+tex_len);
glVertex2f(pos.x+(i+1)*step, pos.y);
glEnd();
}
// std::cout << std::endl;
} else {
glBegin(GL_QUADS);
glVertex2f(pos.x, pos.y);
glVertex2f(pos.x+pos.w, pos.y);
glVertex2f(pos.x+pos.w, pos.y+pos.h);
glVertex2f(pos.x, pos.y+pos.h);
glEnd();
}
}
void GLFontEngine::renderLine(std::string str, int x, int y, bool center, SDL_Rect *rendered_to) {
glEnable(GL_TEXTURE_2D);
SDL_Rect m = { x, y, getTextWidth(str), fsize};
if(center)
m.x = m.x - m.w/2;
renderLine(str, m);
if(rendered_to)
*rendered_to = m;
return;
}
void GLFontEngine::renderLines(std::string str, int x, int y, bool center, SDL_Rect *rendered_to, int wrap, int paintbackground) {
if(wrap) {
// \n einf<6E>gen, wenns zu gro<72> ist
for(unsigned int i=0, a=0; i<str.length(); i++, a++) {
if(str[i]!='\n') {
if(a*font->charwidth*fsize>wrap) {
str.insert(i, "\n");
}
} else {
a=0;
}
}
}
if(paintbackground) {
std::cout << "Paint Background implementieren.. ;) " << std::endl;
}
SDL_Rect m;
int strlpos;
int max_width = 0;
int linecount = 0;
std::string rstr;
while(str!="") {
if( (unsigned int)(strlpos = str.find('\n')) != std::string::npos) {
rstr = str.substr(0, strlpos);
str = str.substr(strlpos+1);
} else {
rstr = str;
str = "";
}
renderLine(rstr, x, y+(int)(1.1*linecount*fsize), center, &m);
max_width = std::max(max_width, (int)m.w);
linecount++;
}
if(rendered_to) {
m.w = max_width;
m.y = y;
*rendered_to = m;
}
return;
}
void GLFontEngine::setSize(int s) {
fsize = s;
}
int GLFontEngine::getSize() {
return fsize;
}
void GLFontEngine::getSDLRect(const std::string &str, SDL_Rect *r) {
r->w = getTextWidth(str);
r->h = getSize();
}
int GLFontEngine::getTextWidth(const std::string &moep) {
if(fontloaded)
return getTextWidthbyInt(moep.length());
// return (int)(moep.length()*(font->charwidth*fsize));
else
return 1;
}
int GLFontEngine::getTextWidthbyInt(int length) {
return (int)(length*font->charwidth*fsize);
}
std::map<std::string, GLFont*> GLFontEngine::fontpool;