123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- /* 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<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(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; 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�gen, wenns zu gro� 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;
-
- } // namespace segl
|