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 6.9KB

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