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.

WindowSDL.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*!
  2. * \file src/WindowSDL.cpp
  3. * \brief SDL windowing implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdio>
  8. #include <assert.h>
  9. #include "SDL_ttf.h"
  10. #ifdef __APPLE__
  11. #include <OpenGL/gl.h>
  12. #include <OpenGL/glu.h>
  13. #else
  14. #include <GL/gl.h>
  15. #include <GL/glu.h>
  16. #endif
  17. #include "config.h"
  18. #include "utils/strings.h"
  19. #include "WindowSDL.h"
  20. #define SUBSYSTEMS_USED (SDL_INIT_VIDEO | SDL_INIT_EVENTS)
  21. WindowSDL::WindowSDL() {
  22. mInit = false;
  23. mDriver = NULL;
  24. mWidth = DEFAULT_WIDTH;
  25. mHeight = DEFAULT_HEIGHT;
  26. mFullscreen = false;
  27. mMousegrab = false;
  28. mFont = NULL;
  29. mFontInit = false;
  30. mWindow = NULL;
  31. mGLContext = NULL;
  32. #ifdef WIN32
  33. setDriver("libGL32.dll");
  34. #elif !defined(__APPLE__)
  35. setDriver("/usr/lib/libGL.so.1");
  36. #endif
  37. }
  38. WindowSDL::~WindowSDL() {
  39. if (mInit) {
  40. SDL_QuitSubSystem(SUBSYSTEMS_USED);
  41. SDL_Quit();
  42. }
  43. if (mFontInit)
  44. TTF_Quit();
  45. if (mDriver)
  46. delete [] mDriver;
  47. if (mFont)
  48. delete [] mFont;
  49. }
  50. void WindowSDL::setDriver(const char *driver) {
  51. assert(driver != NULL);
  52. assert(driver[0] != '\0');
  53. assert(mInit == false);
  54. mDriver = bufferString("%s", driver);
  55. }
  56. void WindowSDL::setSize(unsigned int width, unsigned int height) {
  57. assert(width > 0);
  58. assert(height > 0);
  59. mWidth = width;
  60. mHeight = height;
  61. if (mInit == true) {
  62. SDL_SetWindowSize(mWindow, mWidth, mHeight);
  63. resizeGL(mWidth, mHeight);
  64. }
  65. }
  66. void WindowSDL::setFullscreen(bool fullscreen) {
  67. mFullscreen = fullscreen;
  68. if (mInit == true) {
  69. if (mFullscreen)
  70. SDL_SetWindowFullscreen(mWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  71. else
  72. SDL_SetWindowFullscreen(mWindow, 0);
  73. }
  74. }
  75. void WindowSDL::setMousegrab(bool grab) {
  76. mMousegrab = grab;
  77. if (mInit == true) {
  78. if (mMousegrab) {
  79. SDL_SetRelativeMouseMode(SDL_TRUE);
  80. } else {
  81. SDL_SetRelativeMouseMode(SDL_FALSE);
  82. SDL_ShowCursor(1);
  83. }
  84. }
  85. }
  86. int WindowSDL::initialize() {
  87. assert(mInit == false);
  88. if (SDL_Init(SUBSYSTEMS_USED) != 0) {
  89. printf("SDL_Init Error: %s\n", SDL_GetError());
  90. return -1;
  91. }
  92. #ifndef __APPLE__
  93. assert(mDriver != NULL);
  94. assert(mDriver[0] != '\0');
  95. if (SDL_GL_LoadLibrary(mDriver) < 0) {
  96. SDL_ClearError();
  97. if (SDL_GL_LoadLibrary("libGL.so") < 0) {
  98. SDL_ClearError();
  99. if (SDL_GL_LoadLibrary("libGL.so.1") < 0) {
  100. printf("Could not load OpenGL driver!\n");
  101. printf("SDL_GL_LoadLibrary Error: %s\n", SDL_GetError());
  102. return -2;
  103. }
  104. }
  105. }
  106. #endif
  107. int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
  108. if (mFullscreen)
  109. flags |= SDL_WINDOW_FULLSCREEN;
  110. mInit = true;
  111. setMousegrab(mMousegrab);
  112. if ((SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5) != 0)
  113. || (SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5) != 0)
  114. || (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5) != 0)
  115. || (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16) != 0)
  116. || (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0)) {
  117. printf("SDL_GL_SetAttribute Error: %s\n", SDL_GetError());
  118. mInit = false;
  119. return -3;
  120. }
  121. mWindow = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  122. mWidth, mHeight, flags);
  123. if (mWindow == NULL) {
  124. printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
  125. mInit = false;
  126. return -4;
  127. }
  128. mGLContext = SDL_GL_CreateContext(mWindow);
  129. if (mGLContext == NULL) {
  130. printf("SDL_GL_CreateContext Error: %s\n", SDL_GetError());
  131. mInit = false;
  132. return -5;
  133. }
  134. setSize(mWidth, mHeight);
  135. return 0;
  136. }
  137. void WindowSDL::eventHandling() {
  138. SDL_Event event;
  139. assert(mInit == true);
  140. while(SDL_PollEvent(&event)) {
  141. switch (event.type) {
  142. case SDL_MOUSEMOTION:
  143. break;
  144. case SDL_MOUSEBUTTONDOWN:
  145. case SDL_MOUSEBUTTONUP:
  146. break;
  147. case SDL_KEYDOWN:
  148. case SDL_KEYUP:
  149. break;
  150. case SDL_WINDOWEVENT:
  151. if (event.window.event == SDL_WINDOWEVENT_RESIZED)
  152. setSize(event.window.data1, event.window.data2);
  153. break;
  154. case SDL_QUIT:
  155. exit(0);
  156. }
  157. }
  158. }
  159. void WindowSDL::delay(clock_t ms) {
  160. assert(mInit == true);
  161. SDL_Delay(ms);
  162. }
  163. void WindowSDL::swapBuffersGL() {
  164. assert(mInit == true);
  165. SDL_GL_SwapWindow(mWindow);
  166. }
  167. void WindowSDL::setFont(const char *font) {
  168. assert(font != NULL);
  169. assert(font[0] != '\0');
  170. assert(mFontInit == false);
  171. mFont = fullPath(font, 0);
  172. }
  173. int WindowSDL::initializeFont() {
  174. assert(mFontInit == false);
  175. assert(mFont != NULL);
  176. assert(mFont[0] != '\0');
  177. if (TTF_Init() != 0) {
  178. printf("Could not initialize SDL-TTF!\n");
  179. return -1;
  180. }
  181. mFontInit = true;
  182. return 0;
  183. }
  184. void WindowSDL::writeString(WindowString *s) {
  185. assert(s != NULL);
  186. assert(s->text != NULL);
  187. assert(mInit == true);
  188. }