sdlPlatformGL.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <SDL.h>
  2. #include "windowManager/sdl/sdlWindow.h"
  3. #include "console/console.h"
  4. namespace PlatformGL
  5. {
  6. void init()
  7. {
  8. static bool inited = false;
  9. if(inited)
  10. return;
  11. inited = true;
  12. const U32 majorOGL = 3;
  13. const U32 minorOGL = 2;
  14. U32 debugFlag = 0;
  15. #ifdef TORQUE_DEBUG
  16. debugFlag |= SDL_GL_CONTEXT_DEBUG_FLAG;
  17. #endif
  18. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, majorOGL);
  19. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minorOGL);
  20. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  21. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, debugFlag);
  22. SDL_ClearError();
  23. }
  24. void* CreateContextGL( PlatformWindow *window )
  25. {
  26. init();
  27. PlatformWindowSDL* windowSdl = dynamic_cast<PlatformWindowSDL*>(window);
  28. AssertFatal(windowSdl, "");
  29. if( !windowSdl )
  30. return NULL;
  31. SDL_ClearError();
  32. SDL_GLContext ctx = SDL_GL_CreateContext( windowSdl->getSDLWindow() );
  33. if( !ctx )
  34. {
  35. const char *err = SDL_GetError();
  36. Con::printf( err );
  37. AssertFatal(0, err );
  38. }
  39. return ctx;
  40. }
  41. void MakeCurrentGL( PlatformWindow *window, void *glContext )
  42. {
  43. PlatformWindowSDL* windowSdl = dynamic_cast<PlatformWindowSDL*>(window);
  44. AssertFatal( windowSdl && glContext, "" );
  45. SDL_ClearError();
  46. SDL_GL_MakeCurrent( windowSdl->getSDLWindow(), glContext );
  47. const char *err = SDL_GetError();
  48. if( err && err[0] )
  49. {
  50. Con::printf( err );
  51. AssertFatal(0, err );
  52. }
  53. }
  54. void setVSync(const int i)
  55. {
  56. if( i == 1 || i == -1 )
  57. {
  58. int ret = SDL_GL_SetSwapInterval(-1);
  59. if( ret == -1)
  60. SDL_GL_SetSwapInterval(1);
  61. }
  62. else
  63. SDL_GL_SetSwapInterval(0);
  64. }
  65. }