gl_window.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "gl_window.h"
  2. using namespace simple::graphical;
  3. using attribute = gl_window::attribute;
  4. SDL_GLattr gl_window::to_sdl_glatrr(attribute attr)
  5. {
  6. switch(attr)
  7. {
  8. case attribute::red : return SDL_GL_RED_SIZE;
  9. case attribute::green : return SDL_GL_GREEN_SIZE;
  10. case attribute::blue : return SDL_GL_BLUE_SIZE;
  11. case attribute::alpha : return SDL_GL_ALPHA_SIZE;
  12. case attribute::buffer : return SDL_GL_BUFFER_SIZE;
  13. case attribute::double_buffer : return SDL_GL_DOUBLEBUFFER;
  14. case attribute::depth : return SDL_GL_DEPTH_SIZE;
  15. case attribute::stencil : return SDL_GL_STENCIL_SIZE;
  16. case attribute::accum_red : return SDL_GL_ACCUM_RED_SIZE;
  17. case attribute::accum_green : return SDL_GL_ACCUM_GREEN_SIZE;
  18. case attribute::accum_blue : return SDL_GL_ACCUM_BLUE_SIZE;
  19. case attribute::accum_alpha : return SDL_GL_ACCUM_ALPHA_SIZE;
  20. case attribute::stereo : return SDL_GL_STEREO;
  21. case attribute::msaa_buffers : return SDL_GL_MULTISAMPLEBUFFERS;
  22. case attribute::msaa_samples : return SDL_GL_MULTISAMPLESAMPLES;
  23. case attribute::force_acceleration : return SDL_GL_ACCELERATED_VISUAL;
  24. case attribute::major_version : return SDL_GL_CONTEXT_MAJOR_VERSION;
  25. case attribute::minor_version : return SDL_GL_CONTEXT_MINOR_VERSION;
  26. case attribute::context_flags : return SDL_GL_CONTEXT_FLAGS;
  27. case attribute::context_profile : return SDL_GL_CONTEXT_PROFILE_MASK;
  28. case attribute::share_context : return SDL_GL_SHARE_WITH_CURRENT_CONTEXT;
  29. #if SDL_VERSION_ATLEAST(2,0,1)
  30. case attribute::srgb_framebuffer : return SDL_GL_FRAMEBUFFER_SRGB_CAPABLE;
  31. #endif
  32. #if SDL_VERSION_ATLEAST(2,0,4)
  33. case attribute::release_context : return SDL_GL_CONTEXT_RELEASE_BEHAVIOR;
  34. #endif
  35. }
  36. throw std::logic_error("simple::graphical::gl_window::attrribute invalid!");
  37. }
  38. void gl_window::config::register_window() noexcept
  39. {
  40. ++active_windows;
  41. }
  42. void gl_window::config::unregister_window() noexcept
  43. {
  44. --active_windows;
  45. }
  46. gl_window::config gl_window::global = config{};
  47. gl_window::gl_window(std::string title, int2 size, flags windowflags, int2 position)
  48. : window(title, size, windowflags | window::flags::opengl, position),
  49. context(SDL_GL_CreateContext(guts().get()), SDL_GL_DeleteContext)
  50. {
  51. global.register_window();
  52. global.ensure_requirements();
  53. }
  54. gl_window::~gl_window() noexcept
  55. {
  56. global.unregister_window();
  57. }
  58. void gl_window::update() noexcept
  59. {
  60. SDL_GL_SwapWindow(guts().get());
  61. }
  62. auto gl_window::vsync() const noexcept -> vsync_mode
  63. {
  64. return static_cast<vsync_mode>(SDL_GL_GetSwapInterval());
  65. }
  66. void gl_window::require_vsync(vsync_mode mode)
  67. {
  68. sdlcore::utils::throw_error(
  69. SDL_GL_SetSwapInterval(support::to_integer(mode)) );
  70. }
  71. bool gl_window::request_vsync(vsync_mode mode) noexcept
  72. {
  73. return !sdlcore::utils::check_error(
  74. SDL_GL_SetSwapInterval(support::to_integer(mode)) );
  75. }
  76. int2 gl_window::framebuffer_size() const noexcept
  77. {
  78. int2 size;
  79. SDL_GL_GetDrawableSize(guts().get(), &size.x(), &size.y());
  80. return size;
  81. }