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

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