sdlPlatformGL.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 = 2;
  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. #ifdef TORQUE_GL_SOFTWARE
  23. SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 0);
  24. #endif
  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. }
  57. void setVSync(const int i)
  58. {
  59. PRESERVE_FRAMEBUFFER();
  60. // Nvidia needs to have the default framebuffer bound or the vsync calls fail
  61. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  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. }