Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glfontengine.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* libsegl - Sebas Extended GL Library
  2. * Collection of Opengl/3D-Math helpers
  3. *
  4. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  5. * http://www.seba-geek.de
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. #include "glfontengine.h"
  23. namespace segl {
  24. bool GLFontEngine::addFont(std::string fontfile, std::string fontname) {
  25. GLFont *tmp = new GLFont(fontfile, 0.685f);
  26. if(!tmp->font->isLoaded()) {
  27. delete(tmp);
  28. std::cerr << fontfile << " konnte als Font nicht geladen werden" << std::endl;
  29. return false;
  30. }
  31. fontpool[fontname] = tmp;
  32. return true;
  33. }
  34. void GLFontEngine::quit() {
  35. for(std::map<std::string, GLFont*>::iterator iter = fontpool.begin(); iter != fontpool.end(); iter++) {
  36. delete(iter->second); // unloaded texture.. der dtor
  37. fontpool.erase(iter);
  38. }
  39. }
  40. void GLFontEngine::prepare2DbyPushingMatrix() {
  41. const SDL_Surface *screen = SDL_GetVideoSurface();
  42. glMatrixMode(GL_PROJECTION);
  43. glPushMatrix();
  44. glLoadIdentity();
  45. glOrtho(0, screen->w, screen->h, 0, -1, 1);
  46. // glOrtho(0, 640, 480, 0, -1, 1);
  47. glMatrixMode(GL_MODELVIEW);
  48. glPushMatrix();
  49. glLoadIdentity();
  50. }
  51. void GLFontEngine::regain3DbyPoppingMatrix() {
  52. glMatrixMode(GL_PROJECTION);
  53. glPopMatrix();
  54. glMatrixMode(GL_MODELVIEW);
  55. glPopMatrix();
  56. }
  57. void GLFontEngine::paintSDLRect(SDL_Rect r) {
  58. glBegin(GL_QUADS);
  59. glTexCoord2f(0.0f, 1.0f);
  60. glVertex2i(r.x, r.y);
  61. glTexCoord2f(0.0f, 0.0f);
  62. glVertex2i(r.x, r.y+r.h);
  63. glTexCoord2f(1.0f, 0.0f);
  64. glVertex2i(r.x+r.w, r.y+r.h);
  65. glTexCoord2f(1.0f, 1.0f);
  66. glVertex2i(r.x+r.w, r.y);
  67. glEnd();
  68. }
  69. GLFontEngine::GLFontEngine() {
  70. init();
  71. fontloaded = fontSelect("");
  72. }
  73. GLFontEngine::GLFontEngine(std::string fontstr) {
  74. init();
  75. fontloaded = fontSelect(fontstr);
  76. }
  77. void GLFontEngine::init() {
  78. // r = g = b = a = 1.0f;
  79. col.set(1.0f, 1.0f, 1.0f);
  80. fsize = 16;
  81. }
  82. void GLFontEngine::setColor(float _r, float _g, float _b, float _a) {
  83. // r = _r;
  84. // g = _g;
  85. // b = _b;
  86. // a = _a;
  87. col.set(_r, _g, _b, _a);
  88. }
  89. void GLFontEngine::setColor(Color c) {
  90. col = c;
  91. }
  92. bool GLFontEngine::fontSelect(std::string fontstr) {
  93. if(fontpool.size()==0) {
  94. fontloaded = false;
  95. return false;
  96. }
  97. fontloaded = true; // fontstr oder fallbackfont
  98. if(fontpool[fontstr])
  99. font = fontpool[fontstr];
  100. else {
  101. //fallbackfont - ersten aus der liste
  102. font = (++fontpool.begin())->second;
  103. // std::cout << "font: " << font << std::endl;
  104. }
  105. return true;
  106. }
  107. void GLFontEngine::renderText(std::string text, SDL_Rect pos) {
  108. renderLine(text, pos);
  109. }
  110. void GLFontEngine::renderLine(std::string text, SDL_Rect pos) {
  111. // glColor4f(r, g, b, a);
  112. glMatrixMode(GL_TEXTURE);
  113. glLoadIdentity();
  114. glMatrixMode(GL_MODELVIEW);
  115. glColorGLC(col);
  116. if(fontloaded) {
  117. font->font->selectTexture();
  118. float step = pos.w / (float)text.length();
  119. float tex_x, tex_y;
  120. float tex_len = (1.0f/16.0f);
  121. for(unsigned int i=0; i<text.length(); i++) {
  122. int ch = text[i];
  123. if(ch<0) {
  124. ch += 256; // char geht wohl in machen f�llen nur von -128 bis +127 *hust*
  125. }
  126. // std::cout << (int)text[i] << " ";
  127. tex_x = ((ch-1)%16 / 16.0f);
  128. tex_y = 1.0f-(floor((ch-1)/16) / 16.0f)-tex_len;
  129. // std::cout << text[i] << " == " << tex_x << ", " << tex_y << std::endl;
  130. glBegin(GL_QUADS);
  131. glTexCoord2f(tex_x, tex_y+tex_len);
  132. glVertex2f(pos.x+i*step, pos.y);
  133. glTexCoord2f(tex_x, tex_y);
  134. glVertex2f(pos.x+i*step, pos.y+pos.h);
  135. glTexCoord2f(tex_x+tex_len, tex_y);
  136. glVertex2f(pos.x+(i+1)*step, pos.y+pos.h);
  137. glTexCoord2f(tex_x+tex_len, tex_y+tex_len);
  138. glVertex2f(pos.x+(i+1)*step, pos.y);
  139. glEnd();
  140. }
  141. // std::cout << std::endl;
  142. } else {
  143. glBegin(GL_QUADS);
  144. glVertex2f(pos.x, pos.y);
  145. glVertex2f(pos.x+pos.w, pos.y);
  146. glVertex2f(pos.x+pos.w, pos.y+pos.h);
  147. glVertex2f(pos.x, pos.y+pos.h);
  148. glEnd();
  149. }
  150. }
  151. void GLFontEngine::renderLine(std::string str, int x, int y, bool center, SDL_Rect *rendered_to) {
  152. glEnable(GL_TEXTURE_2D);
  153. SDL_Rect m = { x, y, getTextWidth(str), fsize};
  154. if(center)
  155. m.x = m.x - m.w/2;
  156. renderLine(str, m);
  157. if(rendered_to)
  158. *rendered_to = m;
  159. return;
  160. }
  161. void GLFontEngine::renderLines(std::string str, int x, int y, bool center, SDL_Rect *rendered_to, int wrap, int paintbackground) {
  162. if(wrap) {
  163. // \n einf�gen, wenns zu gro� ist
  164. for(unsigned int i=0, a=0; i<str.length(); i++, a++) {
  165. if(str[i]!='\n') {
  166. if(a*font->charwidth*fsize>wrap) {
  167. str.insert(i, "\n");
  168. }
  169. } else {
  170. a=0;
  171. }
  172. }
  173. }
  174. if(paintbackground) {
  175. std::cout << "Paint Background implementieren.. ;) " << std::endl;
  176. }
  177. SDL_Rect m;
  178. int strlpos;
  179. int max_width = 0;
  180. int linecount = 0;
  181. std::string rstr;
  182. while(str!="") {
  183. if( (unsigned int)(strlpos = str.find('\n')) != std::string::npos) {
  184. rstr = str.substr(0, strlpos);
  185. str = str.substr(strlpos+1);
  186. } else {
  187. rstr = str;
  188. str = "";
  189. }
  190. renderLine(rstr, x, y+(int)(1.1*linecount*fsize), center, &m);
  191. max_width = std::max(max_width, (int)m.w);
  192. linecount++;
  193. }
  194. if(rendered_to) {
  195. m.w = max_width;
  196. m.y = y;
  197. *rendered_to = m;
  198. }
  199. return;
  200. }
  201. void GLFontEngine::setSize(int s) {
  202. fsize = s;
  203. }
  204. int GLFontEngine::getSize() {
  205. return fsize;
  206. }
  207. void GLFontEngine::getSDLRect(const std::string &str, SDL_Rect *r) {
  208. r->w = getTextWidth(str);
  209. r->h = getSize();
  210. }
  211. int GLFontEngine::getTextWidth(const std::string &moep) {
  212. if(fontloaded)
  213. return getTextWidthbyInt(moep.length());
  214. // return (int)(moep.length()*(font->charwidth*fsize));
  215. else
  216. return 1;
  217. }
  218. int GLFontEngine::getTextWidthbyInt(int length) {
  219. return (int)(length*font->charwidth*fsize);
  220. }
  221. std::map<std::string, GLFont*> GLFontEngine::fontpool;
  222. } // namespace segl