Window.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "rendering/Window.h"
  2. int Window::glfwCounter = 0;
  3. int Window::glewCounter = 0;
  4. Window::Window(TextInput*& textInput, const WindowOptions& options)
  5. : textInput(textInput), window(nullptr), size(options.size) {
  6. if(glfwCounter == 0 && !glfwInit()) {
  7. error.message.clear().append("could not initialize GLFW");
  8. return;
  9. }
  10. glfwCounter++;
  11. glfwDefaultWindowHints();
  12. glfwWindowHint(GLFW_VISIBLE, false);
  13. glfwWindowHint(GLFW_RESIZABLE, true);
  14. glfwWindowHint(GLFW_DECORATED, !options.fullscreen);
  15. glfwWindowHint(GLFW_DOUBLEBUFFER, true);
  16. if(options.es) {
  17. glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
  18. } else {
  19. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  20. }
  21. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, options.majorVersion);
  22. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, options.minorVersion);
  23. GLFWmonitor* monitor =
  24. options.fullscreen ? glfwGetPrimaryMonitor() : nullptr;
  25. window = glfwCreateWindow(options.size.width, options.size.height,
  26. options.name, monitor, nullptr);
  27. if(window == nullptr) {
  28. error.message.clear().append("could not create window");
  29. return;
  30. }
  31. glfwSetWindowUserPointer(window, this);
  32. glfwSetKeyCallback(window, onKey);
  33. glfwSetCharCallback(window, onChar);
  34. glfwSetFramebufferSizeCallback(window, onResize);
  35. glfwSetMouseButtonCallback(window, onMouse);
  36. glfwSetCursorPosCallback(window, onMouseMove);
  37. glfwMakeContextCurrent(window);
  38. glfwSwapInterval(options.vsync);
  39. if(glewCounter == 0) {
  40. GLenum err = glewInit();
  41. if(err != GLEW_OK) {
  42. error.message.clear()
  43. .append("could not initialize GLEW: ")
  44. .append(glewGetErrorString(err));
  45. return;
  46. }
  47. }
  48. glewCounter++;
  49. }
  50. Window::~Window() {
  51. if(window != nullptr) {
  52. glfwDestroyWindow(window);
  53. }
  54. glfwCounter--;
  55. if(glfwCounter <= 0) {
  56. glfwTerminate();
  57. }
  58. }
  59. const Error& Window::getError() const {
  60. return error;
  61. }
  62. const Clock& Window::getFrameClock() const {
  63. return fps;
  64. }
  65. const Clock& Window::getTickClock() const {
  66. return tps;
  67. }
  68. const Size& Window::getSize() const {
  69. return size;
  70. }
  71. Buttons& Window::getButtons() {
  72. return buttons;
  73. }
  74. void Window::trapCursor(bool trap) {
  75. glfwSetInputMode(window, GLFW_CURSOR,
  76. trap ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
  77. }
  78. void Window::onKey(GLFWwindow* w, int key, int scancode, int action, int mods) {
  79. void* p = glfwGetWindowUserPointer(w);
  80. if(p != nullptr) {
  81. Window* rw = static_cast<Window*>(p);
  82. TextInput* input = rw->textInput;
  83. if(input != nullptr) {
  84. input->onKeyEvent(key, scancode, action, mods);
  85. }
  86. rw->buttons.onKey(key, scancode, action, mods);
  87. }
  88. }
  89. void Window::onChar(GLFWwindow* w, unsigned int codepoint) {
  90. void* p = glfwGetWindowUserPointer(w);
  91. if(p != nullptr) {
  92. TextInput* input = static_cast<Window*>(p)->textInput;
  93. if(input != nullptr) {
  94. input->onCharEvent(codepoint);
  95. }
  96. }
  97. }
  98. void Window::onResize(GLFWwindow* w, int width, int height) {
  99. void* p = glfwGetWindowUserPointer(w);
  100. if(p != nullptr) {
  101. Window* rw = static_cast<Window*>(p);
  102. rw->size.width = width;
  103. rw->size.height = height;
  104. }
  105. }
  106. void Window::onMouse(GLFWwindow* w, int button, int action, int mods) {
  107. void* p = glfwGetWindowUserPointer(w);
  108. if(p != nullptr) {
  109. static_cast<Window*>(p)->buttons.onMouse(button, action, mods);
  110. }
  111. }
  112. void Window::onMouseMove(GLFWwindow* w, double x, double y) {
  113. void* p = glfwGetWindowUserPointer(w);
  114. if(p != nullptr) {
  115. static_cast<Window*>(p)->buttons.onMouseMove(x, y);
  116. }
  117. }