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

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