sdlPlatformGL.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 = 4;
  13. const U32 minorOGL = 2;
  14. U32 debugFlag = 0;
  15. #ifdef TORQUE_DEBUG
  16. debugFlag |= SDL_GL_CONTEXT_DEBUG_FLAG;
  17. #endif
  18. #if 0 // cause problem with glew, no extension load
  19. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, majorOGL);
  20. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minorOGL);
  21. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  22. #endif
  23. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, debugFlag);
  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. }
  56. void setVSync(const int i)
  57. {
  58. if( i == 1 || i == -1 )
  59. {
  60. int ret = SDL_GL_SetSwapInterval(-1);
  61. if( ret == -1)
  62. SDL_GL_SetSwapInterval(1);
  63. }
  64. else
  65. SDL_GL_SetSwapInterval(0);
  66. }
  67. }