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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "glfontengine.h"
  2. bool GLFontEngine::addFont(std::string fontfile, std::string fontname) {
  3. GLFont *tmp = new GLFont(fontfile, 0.685f);
  4. if(!tmp->font->isLoaded()) {
  5. delete(tmp);
  6. std::cerr << fontfile << " konnte als Font nicht geladen werden" << std::endl;
  7. return false;
  8. }
  9. fontpool[fontname] = tmp;
  10. return false;
  11. }
  12. void GLFontEngine::quit() {
  13. for(std::map<std::string, GLFont*>::iterator iter = fontpool.begin(); iter != fontpool.end(); iter++) {
  14. delete(iter->second); // unloaded texture.. der dtor
  15. fontpool.erase(iter);
  16. }
  17. }
  18. void GLFontEngine::prepare2DbyPushingMatrix() {
  19. const SDL_Surface *screen = SDL_GetVideoSurface();
  20. glMatrixMode(GL_PROJECTION);
  21. glPushMatrix();
  22. glLoadIdentity();
  23. glOrtho(0, screen->w, screen->h, 0, -1, 1);
  24. // glOrtho(0, 640, 480, 0, -1, 1);
  25. glMatrixMode(GL_MODELVIEW);
  26. glPushMatrix();
  27. glLoadIdentity();
  28. }
  29. void GLFontEngine::regain3DbyPoppingMatrix() {
  30. glMatrixMode(GL_PROJECTION);
  31. glPopMatrix();
  32. glMatrixMode(GL_MODELVIEW);
  33. glPopMatrix();
  34. }
  35. void GLFontEngine::paintSDLRect(SDL_Rect r) {
  36. glBegin(GL_QUADS);
  37. glTexCoord2f(0.0f, 1.0f);
  38. glVertex2i(r.x, r.y);
  39. glTexCoord2f(0.0f, 0.0f);
  40. glVertex2i(r.x, r.y+r.h);
  41. glTexCoord2f(1.0f, 0.0f);
  42. glVertex2i(r.x+r.w, r.y+r.h);
  43. glTexCoord2f(1.0f, 1.0f);
  44. glVertex2i(r.x+r.w, r.y);
  45. glEnd();
  46. }
  47. GLFontEngine::GLFontEngine() {
  48. init();
  49. fontloaded = fontSelect("");
  50. }
  51. GLFontEngine::GLFontEngine(std::string fontstr) {
  52. init();
  53. fontloaded = fontSelect(fontstr);
  54. }
  55. void GLFontEngine::init() {
  56. // r = g = b = a = 1.0f;
  57. col.set(1.0f, 1.0f, 1.0f);
  58. fsize = 16;
  59. }
  60. void GLFontEngine::setColor(float _r, float _g, float _b, float _a) {
  61. // r = _r;
  62. // g = _g;
  63. // b = _b;
  64. // a = _a;
  65. col.set(_r, _g, _b, _a);
  66. }
  67. void GLFontEngine::setColor(GLColor c) {
  68. col = c;
  69. }
  70. bool GLFontEngine::fontSelect(std::string fontstr) {
  71. if(fontpool.size()==0) {
  72. fontloaded = false;
  73. return false;
  74. }
  75. fontloaded = true; // fontstr oder fallbackfont
  76. if(fontpool[fontstr])
  77. font = fontpool[fontstr];
  78. else {
  79. //fallbackfont - ersten aus der liste
  80. font = (fontpool.begin())->second;
  81. }
  82. return true;
  83. }
  84. void GLFontEngine::renderText(std::string text, SDL_Rect pos) {
  85. renderLine(text, pos);
  86. }
  87. void GLFontEngine::renderLine(std::string text, SDL_Rect pos) {
  88. // glColor4f(r, g, b, a);
  89. glMatrixMode(GL_TEXTURE);
  90. glLoadIdentity();
  91. glMatrixMode(GL_MODELVIEW);
  92. glColorGLC(col);
  93. if(fontloaded) {
  94. font->font->selectTexture();
  95. float step = pos.w / (float)text.length();
  96. float tex_x, tex_y;
  97. float tex_len = (1.0f/16.0f);
  98. for(unsigned int i=0; i<text.length(); i++) {
  99. int ch = text[i];
  100. if(ch<0) {
  101. ch += 256; // char geht wohl in machen fällen nur von -128 bis +127
  102. }
  103. // std::cout << (int)text[i] << " ";
  104. tex_x = ((ch-1)%16 / 16.0f);
  105. tex_y = 1.0f-(floor((ch-1)/16) / 16.0f)-tex_len;
  106. // std::cout << text[i] << " == " << tex_x << ", " << tex_y << std::endl;
  107. glBegin(GL_QUADS);
  108. glTexCoord2f(tex_x, tex_y+tex_len);
  109. glVertex2f(pos.x+i*step, pos.y);
  110. glTexCoord2f(tex_x, tex_y);
  111. glVertex2f(pos.x+i*step, pos.y+pos.h);
  112. glTexCoord2f(tex_x+tex_len, tex_y);
  113. glVertex2f(pos.x+(i+1)*step, pos.y+pos.h);
  114. glTexCoord2f(tex_x+tex_len, tex_y+tex_len);
  115. glVertex2f(pos.x+(i+1)*step, pos.y);
  116. glEnd();
  117. }
  118. // std::cout << std::endl;
  119. } else {
  120. glBegin(GL_QUADS);
  121. glVertex2f(pos.x, pos.y);
  122. glVertex2f(pos.x+pos.w, pos.y);
  123. glVertex2f(pos.x+pos.w, pos.y+pos.h);
  124. glVertex2f(pos.x, pos.y+pos.h);
  125. glEnd();
  126. }
  127. }
  128. void GLFontEngine::renderLine(std::string str, int x, int y, bool center, SDL_Rect *rendered_to) {
  129. SDL_Rect m = { x, y, getTextWidth(str), fsize};
  130. if(center)
  131. m.x = m.x - m.w/2;
  132. renderLine(str, m);
  133. if(rendered_to)
  134. *rendered_to = m;
  135. return;
  136. }
  137. void GLFontEngine::renderLines(std::string str, int x, int y, bool center, SDL_Rect *rendered_to, int wrap, int paintbackground) {
  138. if(wrap) {
  139. // \n einfügen, wenns zu groß ist
  140. for(unsigned int i=0, a=0; i<str.length(); i++, a++) {
  141. if(str[i]!='\n') {
  142. if(a*font->charwidth*fsize>wrap) {
  143. str.insert(i, "\n");
  144. }
  145. } else {
  146. a=0;
  147. }
  148. }
  149. }
  150. if(paintbackground) {
  151. std::cout << "Paint Background implementieren.. ;) " << std::endl;
  152. }
  153. SDL_Rect m;
  154. int strlpos;
  155. int max_width = 0;
  156. int linecount = 0;
  157. std::string rstr;
  158. while(str!="") {
  159. if((strlpos = str.find('\n'))!=std::string::npos) {
  160. rstr = str.substr(0, strlpos);
  161. str = str.substr(strlpos+1);
  162. } else {
  163. rstr = str;
  164. str = "";
  165. }
  166. renderLine(rstr, x, y+(int)(1.1*linecount*fsize), center, &m);
  167. max_width = std::max(max_width, (int)m.w);
  168. linecount++;
  169. }
  170. if(rendered_to) {
  171. m.w = max_width;
  172. m.y = y;
  173. *rendered_to = m;
  174. }
  175. return;
  176. }
  177. void GLFontEngine::setSize(int s) {
  178. fsize = s;
  179. }
  180. int GLFontEngine::getSize() {
  181. return fsize;
  182. }
  183. void GLFontEngine::getSDLRect(const std::string &str, SDL_Rect *r) {
  184. r->w = getTextWidth(str);
  185. r->h = getSize();
  186. }
  187. int GLFontEngine::getTextWidth(const std::string &moep) {
  188. return (int)(moep.length()*font->charwidth*fsize);
  189. }
  190. std::map<std::string, GLFont*> GLFontEngine::fontpool;