2008-02-09 13:43:23 +01:00
|
|
|
|
#include "glfontengine.h"
|
|
|
|
|
|
2008-08-10 17:14:54 +02:00
|
|
|
|
namespace segl {
|
2008-02-09 13:43:23 +01:00
|
|
|
|
|
|
|
|
|
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;
|
2008-07-31 00:51:39 +02:00
|
|
|
|
return true;
|
2008-02-09 13:43:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2008-02-15 15:32:34 +01:00
|
|
|
|
col.set(1.0f, 1.0f, 1.0f);
|
2008-02-09 13:43:23 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-10 17:14:54 +02:00
|
|
|
|
void GLFontEngine::setColor(Color c) {
|
2008-02-09 13:43:23 +01:00
|
|
|
|
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
|
2008-03-08 23:03:26 +01:00
|
|
|
|
font = (++fontpool.begin())->second;
|
2008-03-23 18:00:55 +01:00
|
|
|
|
// std::cout << "font: " << font << std::endl;
|
2008-02-09 13:43:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2008-03-08 23:03:26 +01:00
|
|
|
|
|
2008-02-15 15:32:34 +01:00
|
|
|
|
glMatrixMode(GL_TEXTURE);
|
|
|
|
|
glLoadIdentity();
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
|
|
2008-02-09 13:43:23 +01:00
|
|
|
|
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) {
|
2008-07-31 00:51:39 +02:00
|
|
|
|
ch += 256; // char geht wohl in machen f<>llen nur von -128 bis +127 *hust*
|
2008-02-09 13:43:23 +01:00
|
|
|
|
}
|
|
|
|
|
// 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) {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
|
2008-03-23 18:00:55 +01:00
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
|
|
2008-02-09 13:43:23 +01:00
|
|
|
|
SDL_Rect m = { x, y, getTextWidth(str), fsize};
|
2008-03-08 23:03:26 +01:00
|
|
|
|
|
2008-02-09 13:43:23 +01:00
|
|
|
|
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) {
|
2008-07-31 00:51:39 +02:00
|
|
|
|
// \n einf<6E>gen, wenns zu gro<72> ist
|
2008-02-09 13:43:23 +01:00
|
|
|
|
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!="") {
|
|
|
|
|
|
2008-03-31 00:14:35 +02:00
|
|
|
|
if( (unsigned int)(strlpos = str.find('\n')) != std::string::npos) {
|
2008-02-09 13:43:23 +01:00
|
|
|
|
|
|
|
|
|
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) {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
if(fontloaded)
|
2008-03-31 18:48:58 +02:00
|
|
|
|
return getTextWidthbyInt(moep.length());
|
|
|
|
|
// return (int)(moep.length()*(font->charwidth*fsize));
|
2008-03-08 23:03:26 +01:00
|
|
|
|
else
|
|
|
|
|
return 1;
|
2008-02-09 13:43:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-31 13:00:13 +02:00
|
|
|
|
int GLFontEngine::getTextWidthbyInt(int length) {
|
2008-03-31 18:48:58 +02:00
|
|
|
|
return (int)(length*font->charwidth*fsize);
|
2008-03-31 13:00:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-09 13:43:23 +01:00
|
|
|
|
std::map<std::string, GLFont*> GLFontEngine::fontpool;
|
2008-08-10 17:14:54 +02:00
|
|
|
|
|
|
|
|
|
} // namespace segl
|