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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*!
  2. * \file GLString.cpp
  3. * \brief Open GL rendering font/string Unit Test
  4. *
  5. * \author Mongoose
  6. */
  7. #include <math.h>
  8. #ifdef __APPLE__
  9. #include <OpenGL/glu.h>
  10. #else
  11. #include <GL/glu.h>
  12. #endif
  13. #ifdef HAVE_MTK
  14. #include <Texture.h>
  15. #include <mtk_tga.h>
  16. Texture gTexture;
  17. #else
  18. #error "Requires MTK: Texture and mtk_tga"
  19. #endif
  20. #include <GLString.h>
  21. GLString *TEXT;
  22. void swap_buffers();
  23. void event_resize(int width, int height)
  24. {
  25. GLfloat aspect;
  26. glMatrixMode(GL_PROJECTION);
  27. aspect = (GLfloat)width/(GLfloat)height;
  28. // Mongoose 2002.01.01, Setup view volume, with a nice FOV
  29. gluPerspective(40.0, aspect, 1, 2000);
  30. glMatrixMode(GL_MODELVIEW);
  31. }
  32. void event_display(int width, int height)
  33. {
  34. static float x = 0.0, y = 0.0, z = -150.0, r = 0.0;
  35. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  36. glLoadIdentity();
  37. glTranslatef(0.0, 0.0, -20.0);
  38. glRotatef((float)cos(r)*180.0, 0.0, 0.0, 1.0);
  39. r += 0.01;
  40. // Mongoose 2002.01.01, Render color quad
  41. glDisable(GL_TEXTURE_2D);
  42. glBegin(GL_TRIANGLE_STRIP);
  43. glColor3f(1.0, 0.0, 0.0);
  44. glVertex3f(x + 50, y + 50, z);
  45. glColor3f(0.0, 1.0, 0.0);
  46. glVertex3f(x - 50, y + 50, z);
  47. glColor3f(0.0, 0.0, 1.0);
  48. glVertex3f(x + 50, y - 50, z);
  49. glColor3f(0.5, 0.5, 0.5);
  50. glVertex3f(x - 50, y - 50, z);
  51. glEnd();
  52. // Mongoose 2002.01.01, Render text
  53. glDisable(GL_CULL_FACE);
  54. glEnable(GL_BLEND);
  55. glEnable(GL_TEXTURE_2D);
  56. glColor3f(0.1, 0.2, 1.0);
  57. TEXT->Render(width, height);
  58. glFlush();
  59. swap_buffers();
  60. }
  61. #ifdef HAVE_SDL
  62. #include <SDL/SDL.h>
  63. SDL_Surface *SDL_WINDOW = NULL;
  64. void swap_buffers()
  65. {
  66. SDL_GL_SwapBuffers();
  67. }
  68. void shutdown_gl()
  69. {
  70. SDL_Quit();
  71. }
  72. void init_gl(unsigned int width, unsigned int height,
  73. int argc, char *argv[])
  74. {
  75. int i, j;
  76. int id[4];
  77. float s = 1.0;
  78. // Setup GL
  79. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  80. glShadeModel(GL_SMOOTH);
  81. glEnable(GL_DEPTH_TEST);
  82. glDepthFunc(GL_LESS);
  83. glEnable(GL_BLEND);
  84. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  85. event_resize(width, height);
  86. // Mongoose 2002.01.01, Texture setup
  87. gTexture.reset();
  88. gTexture.setFlag(Texture::fUseMipmaps);
  89. gTexture.setMaxTextureCount(32);
  90. if (argc > 1)
  91. {
  92. for (i = 1, j = 0; i < argc; ++i, ++j)
  93. {
  94. if (j < 4)
  95. {
  96. id[j++] = gTexture.loadTGA(argv[i]);
  97. }
  98. }
  99. }
  100. else
  101. {
  102. id[0] = gTexture.loadTGA("data/font-0.tga");
  103. id[1] = gTexture.loadTGA("data/font-0.tga");
  104. id[2] = gTexture.loadTGA("data/font-0.tga");
  105. id[3] = gTexture.loadTGA("data/font-0.tga");
  106. }
  107. printf("%i %i %i %i\n", id[0], id[1], id[2], id[3]);
  108. TEXT->Init(4, 4, id);
  109. i = TEXT->glPrintf((width/2)-12*5, height/2, 0,
  110. "[font %i] GLString Test", id[0]);
  111. if (i)
  112. {
  113. printf("TEXT->glPrintf> ERROR code %i ( 0 means no error )\n", i);
  114. }
  115. i = TEXT->glPrintf((width/2)-10*7, height/2+32, 1,
  116. "[font %i] GLString Test", id[1]);
  117. if (i)
  118. {
  119. printf("TEXT->glPrintf> ERROR code %i ( 0 means no error )\n", i);
  120. }
  121. s = 1.1;
  122. TEXT->Scale(s);
  123. i = TEXT->glPrintf((width/2)-10*7, height/2+64, 1,
  124. "[font %i] Scaled by %.3f", id[1], s);
  125. if (i)
  126. {
  127. printf("TEXT->glPrintf> ERROR code %i ( 0 means no error )\n", i);
  128. }
  129. i = TEXT->glPrintf((width/2)-10*7, height/2-32, 0,
  130. "[font %i] Scaled by %.3f", id[0], s);
  131. if (i)
  132. {
  133. printf("TEXT->glPrintf> ERROR code %i ( 0 means no error )\n", i);
  134. }
  135. }
  136. int main_gl(int argc, char *argv[])
  137. {
  138. SDL_Event event;
  139. unsigned int mkeys, mod, key;
  140. int flags;
  141. unsigned int width = 640;
  142. unsigned int height = 460;
  143. bool fullscreen = false;
  144. char *driver = NULL;
  145. // Setup clean up on exit
  146. atexit(shutdown_gl);
  147. // Get user settings
  148. //event_init(&width, &height, &fullscreen, &driver, argc, argv);
  149. // Create GL context
  150. SDL_Init(SDL_INIT_VIDEO);
  151. #ifndef __APPLE__
  152. if (!driver || !driver[0] || SDL_GL_LoadLibrary(driver) < 0)
  153. {
  154. SDL_ClearError();
  155. // Fallback 1
  156. if (SDL_GL_LoadLibrary("libGL.so") < 0)
  157. {
  158. SDL_ClearError();
  159. // Fallback 2
  160. if (SDL_GL_LoadLibrary("libGL.so.1") < 0)
  161. {
  162. fprintf(stderr, "main_gl> SDL_GL_LoadLibrary failed!\n");
  163. fprintf(stderr, "main_gl> Error is [%s].\n", SDL_GetError());
  164. exit(1);
  165. }
  166. }
  167. }
  168. #endif
  169. flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
  170. if (fullscreen)
  171. {
  172. flags |= SDL_FULLSCREEN;
  173. SDL_ShowCursor(SDL_DISABLE);
  174. }
  175. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  176. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  177. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  178. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  179. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  180. SDL_WINDOW = SDL_SetVideoMode(width, height, 16, flags);
  181. SDL_WM_SetCaption("GLString Test", "GLString Test");
  182. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  183. // Init rendering
  184. init_gl(width, height, argc, argv);
  185. for (;;)
  186. {
  187. // Pause for 10-20 ms
  188. SDL_Delay(10);
  189. event_display(width, height);
  190. while (SDL_PollEvent(&event))
  191. {
  192. switch (event.type)
  193. {
  194. case SDL_QUIT:
  195. exit(0);
  196. break;
  197. case SDL_MOUSEMOTION:
  198. break;
  199. case SDL_MOUSEBUTTONDOWN:
  200. case SDL_MOUSEBUTTONUP:
  201. break;
  202. case SDL_KEYDOWN:
  203. mkeys = (unsigned int)SDL_GetModState();
  204. mod = 0;
  205. if (mkeys & KMOD_LSHIFT)
  206. mod |= KMOD_LSHIFT;
  207. if (mkeys & KMOD_RSHIFT)
  208. mod |= KMOD_RSHIFT;
  209. if (mkeys & KMOD_LCTRL)
  210. mod |= KMOD_LCTRL;
  211. if (mkeys & KMOD_RCTRL)
  212. mod |= KMOD_RCTRL;
  213. if (mkeys & KMOD_LALT)
  214. mod |= KMOD_LALT;
  215. if (mkeys & KMOD_RALT)
  216. mod |= KMOD_RALT;
  217. key = event.key.keysym.sym;
  218. switch (key)
  219. {
  220. case 0x1B: // 27d, ESC
  221. exit(0);
  222. break;
  223. }
  224. break;
  225. case SDL_KEYUP:
  226. break;
  227. case SDL_VIDEORESIZE:
  228. event_resize(event.resize.w, event.resize.h);
  229. width = event.resize.w;
  230. height = event.resize.h;
  231. event_display(width, height);
  232. break;
  233. }
  234. }
  235. }
  236. return 0;
  237. }
  238. #else
  239. #error "Requires SDL to create GL Context"
  240. #endif
  241. int main(int argc, char *argv[])
  242. {
  243. printf("[GLString class test]\n");
  244. TEXT = new GLString();
  245. main_gl(argc, argv);
  246. return 0;
  247. }