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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "config.h"
  10. #include "utils/strings.h"
  11. #include "WindowSDL.h"
  12. #define SUBSYSTEMS_USED (SDL_INIT_VIDEO | SDL_INIT_EVENTS)
  13. WindowSDL::WindowSDL() {
  14. mInit = false;
  15. mDriver = NULL;
  16. mWidth = DEFAULT_WIDTH;
  17. mHeight = DEFAULT_HEIGHT;
  18. mFullscreen = false;
  19. mMousegrab = false;
  20. mWindow = NULL;
  21. mGLContext = NULL;
  22. mFontInit = false;
  23. mFontName = NULL;
  24. mFont = NULL;
  25. #ifdef WIN32
  26. setDriver("libGL32.dll");
  27. #elif !defined(__APPLE__)
  28. setDriver("/usr/lib/libGL.so.1");
  29. #endif
  30. }
  31. WindowSDL::~WindowSDL() {
  32. if (mInit) {
  33. SDL_QuitSubSystem(SUBSYSTEMS_USED);
  34. SDL_Quit();
  35. }
  36. if (mFont)
  37. TTF_CloseFont(mFont);
  38. if (mFontInit)
  39. TTF_Quit();
  40. if (mDriver)
  41. delete [] mDriver;
  42. if (mFontName)
  43. delete [] mFontName;
  44. }
  45. void WindowSDL::setDriver(const char *driver) {
  46. assert(driver != NULL);
  47. assert(driver[0] != '\0');
  48. assert(mInit == false);
  49. mDriver = bufferString("%s", driver);
  50. }
  51. void WindowSDL::setSize(unsigned int width, unsigned int height) {
  52. assert(width > 0);
  53. assert(height > 0);
  54. mWidth = width;
  55. mHeight = height;
  56. if (mInit == true) {
  57. SDL_SetWindowSize(mWindow, mWidth, mHeight);
  58. resizeGL(mWidth, mHeight);
  59. }
  60. }
  61. void WindowSDL::setFullscreen(bool fullscreen) {
  62. mFullscreen = fullscreen;
  63. if (mInit == true) {
  64. if (mFullscreen)
  65. SDL_SetWindowFullscreen(mWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  66. else
  67. SDL_SetWindowFullscreen(mWindow, 0);
  68. }
  69. }
  70. void WindowSDL::setMousegrab(bool grab) {
  71. mMousegrab = grab;
  72. if (mInit == true) {
  73. if (mMousegrab) {
  74. SDL_SetRelativeMouseMode(SDL_TRUE);
  75. } else {
  76. SDL_SetRelativeMouseMode(SDL_FALSE);
  77. SDL_ShowCursor(1);
  78. }
  79. }
  80. }
  81. int WindowSDL::initialize() {
  82. assert(mInit == false);
  83. if (SDL_Init(SUBSYSTEMS_USED) != 0) {
  84. printf("SDL_Init Error: %s\n", SDL_GetError());
  85. return -1;
  86. }
  87. #ifndef __APPLE__
  88. assert(mDriver != NULL);
  89. assert(mDriver[0] != '\0');
  90. if (SDL_GL_LoadLibrary(mDriver) < 0) {
  91. SDL_ClearError();
  92. if (SDL_GL_LoadLibrary("libGL.so") < 0) {
  93. SDL_ClearError();
  94. if (SDL_GL_LoadLibrary("libGL.so.1") < 0) {
  95. printf("Could not load OpenGL driver!\n");
  96. printf("SDL_GL_LoadLibrary Error: %s\n", SDL_GetError());
  97. return -2;
  98. }
  99. }
  100. }
  101. #endif
  102. int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
  103. if (mFullscreen)
  104. flags |= SDL_WINDOW_FULLSCREEN;
  105. mInit = true;
  106. setMousegrab(mMousegrab);
  107. if ((SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5) != 0)
  108. || (SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5) != 0)
  109. || (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5) != 0)
  110. || (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16) != 0)
  111. || (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0)) {
  112. printf("SDL_GL_SetAttribute Error: %s\n", SDL_GetError());
  113. mInit = false;
  114. return -3;
  115. }
  116. mWindow = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  117. mWidth, mHeight, flags);
  118. if (mWindow == NULL) {
  119. printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
  120. mInit = false;
  121. return -4;
  122. }
  123. mGLContext = SDL_GL_CreateContext(mWindow);
  124. if (mGLContext == NULL) {
  125. printf("SDL_GL_CreateContext Error: %s\n", SDL_GetError());
  126. mInit = false;
  127. return -5;
  128. }
  129. setSize(mWidth, mHeight);
  130. return 0;
  131. }
  132. void WindowSDL::eventHandling() {
  133. SDL_Event event;
  134. assert(mInit == true);
  135. while(SDL_PollEvent(&event)) {
  136. switch (event.type) {
  137. case SDL_MOUSEMOTION:
  138. break;
  139. case SDL_MOUSEBUTTONDOWN:
  140. case SDL_MOUSEBUTTONUP:
  141. break;
  142. case SDL_KEYDOWN:
  143. case SDL_KEYUP:
  144. break;
  145. case SDL_WINDOWEVENT:
  146. if (event.window.event == SDL_WINDOWEVENT_RESIZED)
  147. setSize(event.window.data1, event.window.data2);
  148. break;
  149. case SDL_QUIT:
  150. exit(0);
  151. }
  152. }
  153. }
  154. void WindowSDL::delay(clock_t ms) {
  155. assert(mInit == true);
  156. SDL_Delay(ms);
  157. }
  158. void WindowSDL::swapBuffersGL() {
  159. assert(mInit == true);
  160. SDL_GL_SwapWindow(mWindow);
  161. }
  162. void WindowSDL::setFont(const char *font) {
  163. assert(font != NULL);
  164. assert(font[0] != '\0');
  165. assert(mFontInit == false);
  166. mFontName = fullPath(font, 0);
  167. }
  168. int WindowSDL::initializeFont() {
  169. assert(mFontInit == false);
  170. assert(mFontName != NULL);
  171. assert(mFontName[0] != '\0');
  172. if (TTF_Init() != 0) {
  173. printf("Could not initialize SDL-TTF!\n");
  174. return -1;
  175. }
  176. mFont = TTF_OpenFont(mFontName, 24);
  177. if (mFont == NULL) {
  178. printf("TTF_OpenFont Error: %s\n", TTF_GetError());
  179. return -2;
  180. }
  181. glGenTextures(1, &mFontTexture);
  182. mFontInit = true;
  183. return 0;
  184. }
  185. void WindowSDL::writeString(WindowString *s) {
  186. assert(s != NULL);
  187. assert(s->text != NULL);
  188. assert(mInit == true);
  189. SDL_Color color;
  190. color.r = s->color[0];
  191. color.g = s->color[1];
  192. color.b = s->color[2];
  193. SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s->text, color);
  194. if (surface == NULL) {
  195. printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
  196. return;
  197. }
  198. GLenum textureFormat;
  199. if (surface->format->BytesPerPixel == 4) {
  200. if (surface->format->Rmask == 0x000000FF)
  201. textureFormat = GL_RGBA;
  202. else
  203. textureFormat = GL_BGRA_EXT;
  204. } else {
  205. textureFormat = GL_RGB;
  206. }
  207. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  208. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  209. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  210. glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
  211. glEnter2D(mWidth, mHeight);
  212. glBegin(GL_QUADS);
  213. glTexCoord2f(0.0f, 0.0f);
  214. glVertex2i(s->x, s->y);
  215. glTexCoord2f(0.0f, 1.0f);
  216. glVertex2i(s->x, (int)((float)surface->h * s->scale));
  217. glTexCoord2f(1.0f, 1.0f);
  218. glVertex2i((int)((float)surface->w * s->scale), (int)((float)surface->h * s->scale));
  219. glTexCoord2f(1.0f, 0.0f);
  220. glVertex2i((int)((float)surface->w * s->scale), s->y);
  221. glEnd();
  222. glExit2D();
  223. SDL_FreeSurface(surface);
  224. }