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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. const char *errorText = "TEXT->glPrintf> ERROR code %i\n";
  86. // Mongoose 2002.01.01, Texture setup
  87. gTexture.reset();
  88. gTexture.setFlag(Texture::fUseMipmaps);
  89. gTexture.setMaxTextureCount(32);
  90. gTexture.loadFontTTF("data/test.ttf", 32, 126 - 32); // ASCII
  91. TEXT->Init(4);
  92. i = TEXT->glPrintf(50, 50, "OpenRaider GLString");
  93. if (i) {
  94. printf(errorText, i);
  95. }
  96. i = TEXT->glPrintf(50, 100, "Unit Test by Mongoose");
  97. if (i) {
  98. printf(errorText, i);
  99. }
  100. TEXT->Scale(1.2f);
  101. i = TEXT->glPrintf(50, 150, "ported to SDL2 & TTF");
  102. if (i) {
  103. printf(errorText, i);
  104. }
  105. i = TEXT->glPrintf(50, 200, "by xythobuz");
  106. if (i) {
  107. printf(errorText, i);
  108. }
  109. TEXT->setActive(0, true);
  110. TEXT->setActive(1, true);
  111. TEXT->setActive(2, true);
  112. TEXT->setActive(3, true);
  113. }
  114. [[noreturn]] void main_gl() {
  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. // Create GL context
  127. SDL_Init(SDL_INIT_VIDEO);
  128. #ifndef __APPLE__
  129. if (!driver || !driver[0] || SDL_GL_LoadLibrary(driver) < 0) {
  130. SDL_ClearError();
  131. // Fallback 1
  132. if (SDL_GL_LoadLibrary("libGL.so") < 0) {
  133. SDL_ClearError();
  134. // Fallback 2
  135. if (SDL_GL_LoadLibrary("libGL.so.1") < 0) {
  136. fprintf(stderr, "main_gl> SDL_GL_LoadLibrary failed!\n");
  137. fprintf(stderr, "main_gl> Error is [%s].\n", SDL_GetError());
  138. exit(1);
  139. }
  140. }
  141. }
  142. #endif
  143. flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
  144. if (fullscreen) {
  145. flags |= SDL_WINDOW_FULLSCREEN;
  146. SDL_ShowCursor(SDL_DISABLE);
  147. }
  148. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  149. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  150. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  151. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  152. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  153. sdlWindow = SDL_CreateWindow("GLString Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
  154. glContext = SDL_GL_CreateContext(sdlWindow);
  155. // Init rendering
  156. init_gl();
  157. event_resize(width, height);
  158. init_text();
  159. for (;;) {
  160. // Pause for 10-20 ms
  161. SDL_Delay(10);
  162. event_display(width, height);
  163. while (SDL_PollEvent(&event)) {
  164. switch (event.type) {
  165. case SDL_QUIT:
  166. exit(0);
  167. case SDL_MOUSEMOTION:
  168. break;
  169. case SDL_MOUSEBUTTONDOWN:
  170. case SDL_MOUSEBUTTONUP:
  171. break;
  172. case SDL_KEYDOWN:
  173. mkeys = (unsigned int)SDL_GetModState();
  174. mod = 0;
  175. if (mkeys & KMOD_LSHIFT)
  176. mod |= KMOD_LSHIFT;
  177. if (mkeys & KMOD_RSHIFT)
  178. mod |= KMOD_RSHIFT;
  179. if (mkeys & KMOD_LCTRL)
  180. mod |= KMOD_LCTRL;
  181. if (mkeys & KMOD_RCTRL)
  182. mod |= KMOD_RCTRL;
  183. if (mkeys & KMOD_LALT)
  184. mod |= KMOD_LALT;
  185. if (mkeys & KMOD_RALT)
  186. mod |= KMOD_RALT;
  187. if (mkeys & KMOD_LGUI)
  188. mod |= KMOD_LGUI;
  189. if (mkeys & KMOD_RGUI)
  190. mod |= KMOD_RGUI;
  191. key = event.key.keysym.sym;
  192. switch (key)
  193. {
  194. case 0x1B: // 27d, ESC
  195. exit(0);
  196. #ifdef __APPLE__
  197. case 113: // q
  198. if ((mod & KMOD_RGUI) || (mod & KMOD_LGUI))
  199. exit(0);
  200. break;
  201. #endif
  202. }
  203. break;
  204. case SDL_KEYUP:
  205. break;
  206. case SDL_WINDOWEVENT:
  207. switch(event.window.event) {
  208. case SDL_WINDOWEVENT_RESIZED:
  209. width = event.window.data1;
  210. height = event.window.data2;
  211. event_resize(width, height);
  212. event_display(width, height);
  213. break;
  214. }
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. int main(int argc, char *argv[]) {
  221. if (argc == 1) {
  222. TEXT = new GLString();
  223. main_gl();
  224. } else {
  225. printf("Usage:\n\t%s\n", argv[0]);
  226. return 1;
  227. }
  228. }