Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

GLString.cpp 6.2KB

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