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.3KB

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