sdlPlatformGL.cpp 2.1 KB

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