Open Source Tomb Raider Engine
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.

GLString.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : Mtk
  5. * Author : Terry 'Mongoose' Hendrix II
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : GLString
  9. * License : No use w/o permission (C) 2002 Mongoose
  10. * Comments: Open GL rendering font/string class
  11. *
  12. *
  13. * This file was generated using Mongoose's C++
  14. * template generator script. <stu7440@westga.edu>
  15. *
  16. *-- History -------------------------------------------------
  17. *
  18. * 2002.01.01:
  19. * Mongoose - Created
  20. =================================================================*/
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #ifdef __APPLE__
  26. #include <OpenGL/gl.h>
  27. #else
  28. #include <GL/gl.h>
  29. #endif
  30. #include "Texture.h"
  31. #include "GLString.h"
  32. #ifdef DEBUG_MEMEORY
  33. # include "memeory_test.h"
  34. #endif
  35. GLString::GLString()
  36. {
  37. _num_string_max = 0;
  38. _num_font_max = 0;
  39. _num_font = 0;
  40. _num_string = 0;
  41. _scale = 1.0;
  42. _font_texture = NULL;
  43. _font_base = NULL;
  44. _string = NULL;
  45. }
  46. GLString::~GLString()
  47. {
  48. unsigned int i;
  49. for (i = 0; i < _num_font; ++i)
  50. {
  51. glDeleteLists(_font_base[i], 256);
  52. }
  53. if (_font_texture)
  54. {
  55. delete [] _font_texture;
  56. }
  57. if (_font_base)
  58. {
  59. delete [] _font_base;
  60. }
  61. if (_string)
  62. {
  63. for (i = 0; i < _num_string; ++i)
  64. {
  65. if (_string[i].text)
  66. {
  67. delete [] _string[i].text;
  68. }
  69. }
  70. delete [] _string;
  71. }
  72. }
  73. void GLString::Init(unsigned int max_strings, unsigned int max_fonts,
  74. int *tex_map)
  75. {
  76. unsigned int i;
  77. if (!max_strings || !max_fonts || !tex_map)
  78. {
  79. return;
  80. }
  81. _num_string_max = max_strings;
  82. _num_font_max = max_fonts;
  83. _font_texture = new int[max_fonts];
  84. _font_base = new int[max_fonts];
  85. _string = new gl_string_t[max_strings];
  86. for (i = 0; i < max_fonts; ++i)
  87. {
  88. _font_texture[i] = tex_map[i];
  89. if (BuildFontList(i) < 0)
  90. {
  91. printf("GLString::Init> BuildFontList failed for %i\n", i);
  92. }
  93. }
  94. }
  95. void GLString::SetChar(unsigned int string, unsigned int pos, char c)
  96. {
  97. gl_string_t *str = GetString(string);
  98. if (str && pos < str->len)
  99. {
  100. str->text[pos] = c;
  101. }
  102. }
  103. unsigned int GLString::GetStringLen(unsigned int string)
  104. {
  105. gl_string_t *str = GetString(string);
  106. if (str)
  107. {
  108. return str->len;
  109. }
  110. return 0;
  111. }
  112. char *GLString::GetBuffer(unsigned int string)
  113. {
  114. gl_string_t *str = GetString(string);
  115. if (str)
  116. {
  117. return str->text;
  118. }
  119. return 0;
  120. }
  121. void GLString::setActive(unsigned int string, bool active)
  122. {
  123. gl_string_t *str;
  124. str = GetString(string);
  125. if (str)
  126. {
  127. str->active = active;
  128. }
  129. }
  130. void GLString::SetString(unsigned int string, char *s, ...)
  131. {
  132. va_list args;
  133. gl_string_t *str;
  134. unsigned int len;
  135. str = GetString(string);
  136. if (s && s[0] && str)
  137. {
  138. str->active = true;
  139. len = strlen(s);
  140. if (len > str->len)
  141. {
  142. len = str->len - 1;
  143. }
  144. va_start(args, s);
  145. vsnprintf(str->text, str->len-2, s, args);
  146. str->text[str->len-1] = 0;
  147. va_end(args);
  148. }
  149. }
  150. void GLString::Scale(float scale)
  151. {
  152. _scale = scale;
  153. }
  154. int GLString::BuildFontList(int index)
  155. {
  156. int i;
  157. float cx;
  158. float cy;
  159. if (_num_font >= _num_font_max || index < 0 || index >= (int)_num_font_max)
  160. {
  161. return -1;
  162. }
  163. _font_base[index] = glGenLists(256);
  164. glBindTexture(GL_TEXTURE_2D, _font_texture[index]);
  165. // Mongoose 2002.01.01, Generate 256 lists per font
  166. // one per symbol
  167. for (i = 0; i < 256; i++)
  168. {
  169. /* X Position Of Current Character */
  170. cx = 1 - (float)(i % 16) / 16.0f;
  171. /* Y Position Of Current Character */
  172. cy = 1 - (float)(i / 16) / 16.0f;
  173. /* Start Building A List */
  174. glNewList(_font_base[index] + (255 - i), GL_COMPILE);
  175. /* Use A Quad For Each Character */
  176. glBegin(GL_QUADS);
  177. /* Texture Coord (Bottom Left) */
  178. glTexCoord2f(cx - 0.0625, cy);
  179. /* Vertex Coord (Bottom Left) */
  180. glVertex2i(0, 0);
  181. /* Texture Coord (Bottom Right) */
  182. glTexCoord2f(cx, cy);
  183. /* Vertex Coord (Bottom Right) */
  184. glVertex2i(16, 0);
  185. /* Texture Coord (Top Right) */
  186. glTexCoord2f(cx, cy - 0.0625f);
  187. /* Vertex Coord (Top Right) */
  188. glVertex2i(16, 16);
  189. /* Texture Coord (Top Left) */
  190. glTexCoord2f(cx - 0.0625f, cy - 0.0625f);
  191. /* Vertex Coord (Top Left) */
  192. glVertex2i(0, 16);
  193. glEnd();
  194. /* Move To The Left Of The Character */
  195. glTranslated(10, 0, 0);
  196. glEndList();
  197. }
  198. return 0;
  199. }
  200. int GLString::glPrintf(int x, int y, int font, char *string, ...)
  201. {
  202. int sz = 60;
  203. int n;
  204. va_list args;
  205. // Mongoose 2002.01.01, Only allow valid strings
  206. // we must assume it's NULL terminated also if it passes...
  207. if (!string || !string[0])
  208. {
  209. return -1;
  210. }
  211. if (_num_string > _num_string_max)
  212. {
  213. return -2;
  214. }
  215. if (font < 0 || font > (int)_num_font_max)
  216. {
  217. return -3;
  218. }
  219. // Mongoose 2002.01.01, Assume no longer than 'sz' wide lines
  220. // on first try
  221. _string[_num_string].text = new char[sz];
  222. // Mongoose 2002.01.03, Setup scale factor
  223. _string[_num_string].scale = _scale;
  224. // Mongoose 2002.01.01, Setup position
  225. _string[_num_string].x = x;
  226. _string[_num_string].y = y;
  227. // Mongoose 2002.01.01, Setup font list base index to use
  228. _string[_num_string].font = font;
  229. va_start(args, string);
  230. // Mongoose 2002.01.01, Get exact size needed if the first try fails
  231. n = vsnprintf(_string[_num_string].text, sz, string, args);
  232. // Mongoose 2002.01.01, Realloc correct amount if truncated
  233. while (1)
  234. {
  235. if (n > -1 && n < sz)
  236. {
  237. break;
  238. }
  239. // Mongoose 2002.01.01, For glibc 2.1
  240. if (n > -1)
  241. {
  242. sz = n + 1;
  243. delete [] _string[_num_string].text;
  244. _string[_num_string].text = new char[sz];
  245. n = vsnprintf(_string[_num_string].text, sz, string, args);
  246. break;
  247. }
  248. else // glibc 2.0
  249. {
  250. sz *= 2;
  251. delete [] _string[_num_string].text;
  252. _string[_num_string].text = new char[sz];
  253. n = vsnprintf(_string[_num_string].text, sz, string, args);
  254. }
  255. }
  256. va_end(args);
  257. // Mongoose 2002.01.04, Remeber string size, for future rebuffering use
  258. _string[_num_string].len = sz;
  259. // Mongoose 2002.01.01, Incement string counter, since we just
  260. // allocated a string
  261. ++_num_string;
  262. return 0;
  263. }
  264. void GLString::Render(int width, int height)
  265. {
  266. unsigned int i;
  267. #ifndef HAVE_SDL_TTF
  268. // Mongoose 2001.12.31, Start the evil font rendering...
  269. glLoadIdentity();
  270. glDisable(GL_DEPTH_TEST);
  271. // Mongoose 2001.12.31, New 'flat' projection
  272. glMatrixMode(GL_PROJECTION);
  273. glPushMatrix();
  274. glLoadIdentity();
  275. glOrtho(0, width, 0, height, -1, 1);
  276. // Mongoose 2001.12.31, New rendering matrix
  277. glMatrixMode(GL_MODELVIEW);
  278. glPushMatrix();
  279. glLoadIdentity();
  280. // Mongoose 2001.12.31, Rasterize strings' text
  281. for (i = 0; i < _num_string; ++i)
  282. {
  283. if (_string[i].active)
  284. {
  285. glPushMatrix();
  286. glBindTexture(GL_TEXTURE_2D, _font_texture[_string[i].font]);
  287. glTranslated(_string[i].x, _string[i].y, 0);
  288. glScaled(_string[i].scale, _string[i].scale, _string[i].scale);
  289. glListBase(_font_base[_string[i].font] - 32);
  290. glCallLists(strlen(_string[i].text), GL_BYTE, _string[i].text);
  291. glPopMatrix();
  292. }
  293. }
  294. // Mongoose 2001.12.31, Restore scene projection
  295. glMatrixMode(GL_PROJECTION);
  296. glPopMatrix();
  297. // Mongoose 2001.12.31, Restore scene matrix
  298. glMatrixMode(GL_MODELVIEW);
  299. glPopMatrix();
  300. glEnable(GL_DEPTH_TEST);
  301. #else
  302. for (i = 0; i < _num_string; ++i)
  303. {
  304. if (_string[i].active)
  305. {
  306. glPrint2d(_string[i].x, _string[i].y,
  307. _string[i].scale,
  308. _string[i].text);
  309. }
  310. }
  311. #endif
  312. }
  313. gl_string_t *GLString::GetString(unsigned int id)
  314. {
  315. if (id < _num_string)
  316. {
  317. return _string + id;
  318. }
  319. return NULL;
  320. }
  321. ////////////////////////////////////////////////////////////
  322. // Test code
  323. ////////////////////////////////////////////////////////////
  324. #ifdef __TEST__
  325. #include <GL/glu.h>
  326. #ifdef HAVE_MTK
  327. # include "Texture.h"
  328. # include "mtk_tga.h"
  329. Texture gTexture;
  330. #else
  331. # error "Requires MTK: Texture and mtk_tga"
  332. #endif
  333. GLString *TEXT;
  334. void swap_buffers();
  335. void event_resize(int width, int height)
  336. {
  337. GLfloat aspect;
  338. glMatrixMode(GL_PROJECTION);
  339. aspect = (GLfloat)width/(GLfloat)height;
  340. // Mongoose 2002.01.01, Setup view volume, with a nice FOV
  341. gluPerspective(40.0, aspect, 1, 2000);
  342. glMatrixMode(GL_MODELVIEW);
  343. }
  344. void event_display(int width, int height)
  345. {
  346. static float x = 0.0, y = 0.0, z = -150.0, r = 0.0;
  347. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  348. glLoadIdentity();
  349. glTranslatef(0.0, 0.0, -20.0);
  350. glRotatef((float)cos(r)*180.0, 0.0, 0.0, 1.0);
  351. r += 0.01;
  352. // Mongoose 2002.01.01, Render color quad
  353. glDisable(GL_TEXTURE_2D);
  354. glBegin(GL_TRIANGLE_STRIP);
  355. glColor3f(1.0, 0.0, 0.0);
  356. glVertex3f(x + 50, y + 50, z);
  357. glColor3f(0.0, 1.0, 0.0);
  358. glVertex3f(x - 50, y + 50, z);
  359. glColor3f(0.0, 0.0, 1.0);
  360. glVertex3f(x + 50, y - 50, z);
  361. glColor3f(0.5, 0.5, 0.5);
  362. glVertex3f(x - 50, y - 50, z);
  363. glEnd();
  364. // Mongoose 2002.01.01, Render text
  365. glDisable(GL_CULL_FACE);
  366. glEnable(GL_BLEND);
  367. glEnable(GL_TEXTURE_2D);
  368. glColor3f(0.1, 0.2, 1.0);
  369. TEXT->Render(width, height);
  370. glFlush();
  371. swap_buffers();
  372. }
  373. #ifdef HAVE_SDL
  374. #include <SDL/SDL.h>
  375. SDL_Surface *SDL_WINDOW = NULL;
  376. void swap_buffers()
  377. {
  378. SDL_GL_SwapBuffers();
  379. }
  380. void shutdown_gl()
  381. {
  382. SDL_Quit();
  383. }
  384. void init_gl(unsigned int width, unsigned int height,
  385. int argc, char *argv[])
  386. {
  387. int i, j;
  388. int id[4];
  389. float s = 1.0;
  390. // Setup GL
  391. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  392. glShadeModel(GL_SMOOTH);
  393. glEnable(GL_DEPTH_TEST);
  394. glDepthFunc(GL_LESS);
  395. glEnable(GL_BLEND);
  396. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  397. event_resize(width, height);
  398. // Mongoose 2002.01.01, Texture setup
  399. gTexture.reset();
  400. gTexture.setFlag(Texture::fUseMipmaps);
  401. gTexture.setMaxTextureCount(32);
  402. if (argc > 1)
  403. {
  404. for (i = 1, j = 0; i < argc; ++i, ++j)
  405. {
  406. if (j < 4)
  407. {
  408. id[j++] = gTexture.loadTGA(argv[i]);
  409. }
  410. }
  411. }
  412. else
  413. {
  414. id[0] = gTexture.loadTGA("data/font-0.tga");
  415. id[1] = gTexture.loadTGA("data/font-1.tga");
  416. id[2] = gTexture.loadTGA("data/font-2.tga");
  417. id[3] = gTexture.loadTGA("data/font-3.tga");
  418. }
  419. printf("%i %i %i %i\n", id[0], id[1], id[2], id[3]);
  420. TEXT->Init(4, 4, id);
  421. i = TEXT->glPrintf((width/2)-12*5, height/2, 0,
  422. "[font %i] GLString Test", id[0]);
  423. if (i)
  424. {
  425. printf("TEXT->glPrintf> ERROR code %i ( 0 means no error )\n", i);
  426. }
  427. i = TEXT->glPrintf((width/2)-10*7, height/2+32, 1,
  428. "[font %i] GLString Test", id[1]);
  429. if (i)
  430. {
  431. printf("TEXT->glPrintf> ERROR code %i ( 0 means no error )\n", i);
  432. }
  433. s = 1.1;
  434. TEXT->Scale(s);
  435. i = TEXT->glPrintf((width/2)-10*7, height/2+64, 1,
  436. "[font %i] Scaled by %.3f", id[1], s);
  437. if (i)
  438. {
  439. printf("TEXT->glPrintf> ERROR code %i ( 0 means no error )\n", i);
  440. }
  441. i = TEXT->glPrintf((width/2)-10*7, height/2-32, 0,
  442. "[font %i] Scaled by %.3f", id[0], s);
  443. if (i)
  444. {
  445. printf("TEXT->glPrintf> ERROR code %i ( 0 means no error )\n", i);
  446. }
  447. }
  448. int main_gl(int argc, char *argv[])
  449. {
  450. SDL_Event event;
  451. unsigned int mkeys, mod, key;
  452. int flags;
  453. unsigned int width = 640;
  454. unsigned int height = 460;
  455. bool fullscreen = false;
  456. char *driver = NULL;
  457. // Setup clean up on exit
  458. atexit(shutdown_gl);
  459. // Get user settings
  460. //event_init(&width, &height, &fullscreen, &driver, argc, argv);
  461. // Create GL context
  462. SDL_Init(SDL_INIT_VIDEO);
  463. if (!driver || !driver[0] || SDL_GL_LoadLibrary(driver) < 0)
  464. {
  465. SDL_ClearError();
  466. // Fallback 1
  467. if (SDL_GL_LoadLibrary("libGL.so") < 0)
  468. {
  469. SDL_ClearError();
  470. // Fallback 2
  471. if (SDL_GL_LoadLibrary("libGL.so.1") < 0)
  472. {
  473. fprintf(stderr, "main_gl> SDL_GL_LoadLibrary failed!\n");
  474. fprintf(stderr, "main_gl> Error is [%s].\n", SDL_GetError());
  475. exit(1);
  476. }
  477. }
  478. }
  479. flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
  480. if (fullscreen)
  481. {
  482. flags |= SDL_FULLSCREEN;
  483. SDL_ShowCursor(SDL_DISABLE);
  484. }
  485. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  486. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  487. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  488. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  489. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  490. SDL_WINDOW = SDL_SetVideoMode(width, height, 16, flags);
  491. SDL_WM_SetCaption("GLString Test", "GLString Test");
  492. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  493. // Init rendering
  494. init_gl(width, height, argc, argv);
  495. for (;;)
  496. {
  497. // Pause for 10-20 ms
  498. SDL_Delay(10);
  499. event_display(width, height);
  500. while (SDL_PollEvent(&event))
  501. {
  502. switch (event.type)
  503. {
  504. case SDL_QUIT:
  505. exit(0);
  506. break;
  507. case SDL_MOUSEMOTION:
  508. break;
  509. case SDL_MOUSEBUTTONDOWN:
  510. case SDL_MOUSEBUTTONUP:
  511. break;
  512. case SDL_KEYDOWN:
  513. mkeys = (unsigned int)SDL_GetModState();
  514. mod = 0;
  515. if (mkeys & KMOD_LSHIFT)
  516. mod |= KMOD_LSHIFT;
  517. if (mkeys & KMOD_RSHIFT)
  518. mod |= KMOD_RSHIFT;
  519. if (mkeys & KMOD_LCTRL)
  520. mod |= KMOD_LCTRL;
  521. if (mkeys & KMOD_RCTRL)
  522. mod |= KMOD_RCTRL;
  523. if (mkeys & KMOD_LALT)
  524. mod |= KMOD_LALT;
  525. if (mkeys & KMOD_RALT)
  526. mod |= KMOD_RALT;
  527. key = event.key.keysym.sym;
  528. switch (key)
  529. {
  530. case 0x1B: // 27d, ESC
  531. exit(0);
  532. break;
  533. }
  534. break;
  535. case SDL_KEYUP:
  536. break;
  537. case SDL_VIDEORESIZE:
  538. event_resize(event.resize.w, event.resize.h);
  539. width = event.resize.w;
  540. height = event.resize.h;
  541. event_display(width, height);
  542. break;
  543. }
  544. }
  545. }
  546. return 0;
  547. }
  548. #else
  549. #error "Requires SDL to create GL Context"
  550. #endif
  551. int GLString::_RegressionTest(int argc, char *argv[])
  552. {
  553. TEXT = this;
  554. main_gl(argc, argv);
  555. return 0;
  556. }
  557. int main(int argc, char *argv[])
  558. {
  559. GLString test;
  560. printf("[GLString class test]\n");
  561. return test._RegressionTest(argc, argv);
  562. }
  563. #endif