#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::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; 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; } 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); 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; icharwidth*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((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) { return (int)(moep.length()*font->charwidth*fsize); } std::map GLFontEngine::fontpool;