/* libsegl - Sebas Extended GL Library * Collection of Opengl/3D-Math helpers * * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de * http://www.seba-geek.de * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "glfontengine.h" namespace segl { 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 true; } 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; 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(Color 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; 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( (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 GLFontEngine::fontpool; } // namespace segl