sdlPlatformGL.cpp 2.1 KB

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