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

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