Open Source Tomb Raider Engine
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

GLString.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. const char *errorText = "TEXT->glPrintf> ERROR code %i\n";
  82. // Setup GL
  83. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  84. glShadeModel(GL_SMOOTH);
  85. glEnable(GL_DEPTH_TEST);
  86. glDepthFunc(GL_LESS);
  87. glEnable(GL_BLEND);
  88. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  89. event_resize(width, height);
  90. // Mongoose 2002.01.01, Texture setup
  91. gTexture.reset();
  92. gTexture.setFlag(Texture::fUseMipmaps);
  93. gTexture.setMaxTextureCount(32);
  94. gTexture.loadFontTTF("data/test.ttf", 32, 126 - 32); // ASCII
  95. TEXT->Init(4);
  96. i = TEXT->glPrintf((width/2)-50, height/2-32, "OpenRaider");
  97. if (i) {
  98. printf(errorText, i);
  99. }
  100. i = TEXT->glPrintf((width/2)-50, height/2, "GLString");
  101. if (i) {
  102. printf(errorText, i);
  103. }
  104. TEXT->Scale(1.2);
  105. i = TEXT->glPrintf((width/2)-100, height/2+32, "Unit Test by Mongoose");
  106. if (i) {
  107. printf(errorText, i);
  108. }
  109. i = TEXT->glPrintf((width/2)-100, height/2+64, "ported to TTF by xythobuz");
  110. if (i) {
  111. printf(errorText, i);
  112. }
  113. TEXT->setActive(0, true);
  114. TEXT->setActive(1, true);
  115. TEXT->setActive(2, true);
  116. TEXT->setActive(3, true);
  117. }
  118. int main_gl(int argc, char *argv[])
  119. {
  120. SDL_Event event;
  121. unsigned int mkeys, mod, key;
  122. int flags;
  123. unsigned int width = 640;
  124. unsigned int height = 480;
  125. bool fullscreen = false;
  126. #ifndef __APPLE__
  127. char *driver = NULL;
  128. #endif
  129. // Setup clean up on exit
  130. atexit(shutdown_gl);
  131. // Get user settings
  132. //event_init(&width, &height, &fullscreen, &driver, argc, argv);
  133. // Create GL context
  134. SDL_Init(SDL_INIT_VIDEO);
  135. #ifndef __APPLE__
  136. if (!driver || !driver[0] || SDL_GL_LoadLibrary(driver) < 0)
  137. {
  138. SDL_ClearError();
  139. // Fallback 1
  140. if (SDL_GL_LoadLibrary("libGL.so") < 0)
  141. {
  142. SDL_ClearError();
  143. // Fallback 2
  144. if (SDL_GL_LoadLibrary("libGL.so.1") < 0)
  145. {
  146. fprintf(stderr, "main_gl> SDL_GL_LoadLibrary failed!\n");
  147. fprintf(stderr, "main_gl> Error is [%s].\n", SDL_GetError());
  148. exit(1);
  149. }
  150. }
  151. }
  152. #endif
  153. flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
  154. if (fullscreen)
  155. {
  156. flags |= SDL_FULLSCREEN;
  157. SDL_ShowCursor(SDL_DISABLE);
  158. }
  159. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  160. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  161. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  162. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  163. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  164. SDL_WINDOW = SDL_SetVideoMode(width, height, 16, flags);
  165. SDL_WM_SetCaption("GLString Test", "GLString Test");
  166. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  167. // Init rendering
  168. init_gl(width, height);
  169. for (;;)
  170. {
  171. // Pause for 10-20 ms
  172. SDL_Delay(10);
  173. event_display(width, height);
  174. while (SDL_PollEvent(&event))
  175. {
  176. switch (event.type)
  177. {
  178. case SDL_QUIT:
  179. exit(0);
  180. break;
  181. case SDL_MOUSEMOTION:
  182. break;
  183. case SDL_MOUSEBUTTONDOWN:
  184. case SDL_MOUSEBUTTONUP:
  185. break;
  186. case SDL_KEYDOWN:
  187. mkeys = (unsigned int)SDL_GetModState();
  188. mod = 0;
  189. if (mkeys & KMOD_LSHIFT)
  190. mod |= KMOD_LSHIFT;
  191. if (mkeys & KMOD_RSHIFT)
  192. mod |= KMOD_RSHIFT;
  193. if (mkeys & KMOD_LCTRL)
  194. mod |= KMOD_LCTRL;
  195. if (mkeys & KMOD_RCTRL)
  196. mod |= KMOD_RCTRL;
  197. if (mkeys & KMOD_LALT)
  198. mod |= KMOD_LALT;
  199. if (mkeys & KMOD_RALT)
  200. mod |= KMOD_RALT;
  201. if (mkeys & KMOD_LMETA)
  202. mod |= KMOD_LMETA;
  203. if (mkeys & KMOD_RMETA)
  204. mod |= KMOD_RMETA;
  205. key = event.key.keysym.sym;
  206. switch (key)
  207. {
  208. case 0x1B: // 27d, ESC
  209. exit(0);
  210. break;
  211. #ifdef __APPLE__
  212. case 113: // q
  213. if ((mod & KMOD_RMETA) || (mod & KMOD_LMETA))
  214. exit(0);
  215. break;
  216. #endif
  217. case 114: // r
  218. break;
  219. }
  220. break;
  221. case SDL_KEYUP:
  222. break;
  223. case SDL_VIDEORESIZE:
  224. width = event.resize.w;
  225. height = event.resize.h;
  226. event_resize(width, height);
  227. event_display(width, height);
  228. break;
  229. }
  230. }
  231. }
  232. return 0;
  233. }
  234. #else
  235. #error "Requires SDL to create GL Context"
  236. #endif
  237. int main(int argc, char *argv[])
  238. {
  239. printf("[GLString class test]\n");
  240. TEXT = new GLString();
  241. main_gl(argc, argv);
  242. return 0;
  243. }