2
0

sdlPlatformGL.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "gfx/gl/gfxGLUtils.h"
  8. namespace PlatformGL
  9. {
  10. void init()
  11. {
  12. const U32 majorOGL = 3;
  13. const U32 minorOGL = 3;
  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_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
  23. #ifdef TORQUE_GL_SOFTWARE
  24. SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 0);
  25. #endif
  26. SDL_ClearError();
  27. }
  28. void* CreateContextGL( PlatformWindow *window )
  29. {
  30. init();
  31. PlatformWindowSDL* windowSdl = dynamic_cast<PlatformWindowSDL*>(window);
  32. AssertFatal(windowSdl, "");
  33. if( !windowSdl )
  34. return NULL;
  35. SDL_ClearError();
  36. SDL_GLContext ctx = SDL_GL_CreateContext( windowSdl->getSDLWindow() );
  37. if( !ctx )
  38. {
  39. const char *err = SDL_GetError();
  40. Con::printf( err );
  41. AssertFatal(0, err );
  42. }
  43. return ctx;
  44. }
  45. void MakeCurrentGL( PlatformWindow *window, void *glContext )
  46. {
  47. PlatformWindowSDL* windowSdl = dynamic_cast<PlatformWindowSDL*>(window);
  48. AssertFatal( windowSdl && glContext, "" );
  49. SDL_ClearError();
  50. SDL_GL_MakeCurrent( windowSdl->getSDLWindow(), glContext );
  51. const char *err = SDL_GetError();
  52. if( err && err[0] )
  53. {
  54. Con::printf( err );
  55. AssertFatal(0, err );
  56. }
  57. }
  58. void setVSync(const int i)
  59. {
  60. PRESERVE_FRAMEBUFFER();
  61. // Nvidia needs to have the default framebuffer bound or the vsync calls fail
  62. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  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. }