CX11OpenGLDriver.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "CX11OpenGLDriver.h"
  2. #include "common.h"
  3. #include <FreeImage.h>
  4. #define GLFW_EXPOSE_NATIVE_X11
  5. #include <GLFW/glfw3native.h>
  6. using namespace WallpaperEngine::Render::Drivers;
  7. void CustomGLFWErrorHandler (int errorCode, const char* reason)
  8. {
  9. sLog.error ("GLFW error ", errorCode, ": ", reason);
  10. }
  11. CX11OpenGLDriver::CX11OpenGLDriver (const char* windowTitle) :
  12. m_frameCounter (0)
  13. {
  14. glfwSetErrorCallback (CustomGLFWErrorHandler);
  15. // initialize glfw
  16. if (glfwInit () == GLFW_FALSE)
  17. sLog.exception ("Failed to initialize glfw");
  18. // set some window hints (opengl version to be used)
  19. glfwWindowHint (GLFW_SAMPLES, 4);
  20. glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
  21. glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
  22. glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  23. glfwWindowHint (GLFW_VISIBLE, GLFW_FALSE);
  24. #if !NDEBUG
  25. glfwWindowHint (GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
  26. #endif /* DEBUG */
  27. // create window, size doesn't matter as long as we don't show it
  28. this->m_window = glfwCreateWindow (640, 480, windowTitle, nullptr, nullptr);
  29. if (this->m_window == nullptr)
  30. sLog.exception ("Cannot create window");
  31. // make context current, required for glew initialization
  32. glfwMakeContextCurrent (this->m_window);
  33. // initialize glew for rendering
  34. GLenum result = glewInit ();
  35. if (result != GLEW_OK)
  36. sLog.error("Failed to initialize GLEW: ", glewGetErrorString (result));
  37. // initialize free image
  38. FreeImage_Initialise (TRUE);
  39. }
  40. CX11OpenGLDriver::~CX11OpenGLDriver ()
  41. {
  42. glfwTerminate ();
  43. FreeImage_DeInitialise();
  44. }
  45. void* CX11OpenGLDriver::getWindowHandle () const
  46. {
  47. return reinterpret_cast <void*> (glfwGetX11Window (this->m_window));
  48. }
  49. float CX11OpenGLDriver::getRenderTime () const
  50. {
  51. return (float) glfwGetTime ();
  52. }
  53. bool CX11OpenGLDriver::closeRequested ()
  54. {
  55. return glfwWindowShouldClose (this->m_window);
  56. }
  57. void CX11OpenGLDriver::resizeWindow (glm::ivec2 size)
  58. {
  59. glfwSetWindowSize (this->m_window, size.x, size.y);
  60. }
  61. void CX11OpenGLDriver::resizeWindow (glm::ivec4 sizeandpos)
  62. {
  63. glfwSetWindowPos (this->m_window, sizeandpos.x, sizeandpos.y);
  64. glfwSetWindowSize (this->m_window, sizeandpos.z, sizeandpos.w);
  65. }
  66. void CX11OpenGLDriver::showWindow ()
  67. {
  68. glfwShowWindow (this->m_window);
  69. }
  70. void CX11OpenGLDriver::hideWindow ()
  71. {
  72. glfwHideWindow (this->m_window);
  73. }
  74. glm::ivec2 CX11OpenGLDriver::getFramebufferSize () const
  75. {
  76. glm::ivec2 size;
  77. glfwGetFramebufferSize (this->m_window, &size.x, &size.y);
  78. return size;
  79. }
  80. void CX11OpenGLDriver::swapBuffers ()
  81. {
  82. // do buffer swapping first
  83. glfwSwapBuffers (this->m_window);
  84. // poll for events
  85. glfwPollEvents ();
  86. // increase frame counter
  87. this->m_frameCounter ++;
  88. }
  89. uint32_t CX11OpenGLDriver::getFrameCounter () const
  90. {
  91. return this->m_frameCounter;
  92. }
  93. GLFWwindow* CX11OpenGLDriver::getWindow ()
  94. {
  95. return this->m_window;
  96. }