sdlPlatformGL.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }
  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. }