Window.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <iostream>
  2. #include "rendering/Window.h"
  3. Window::Window(TextInput*& textInput, const WindowOptions& options)
  4. : textInput(textInput), window(nullptr) {
  5. glfwDefaultWindowHints();
  6. glfwWindowHint(GLFW_VISIBLE, 0);
  7. glfwWindowHint(GLFW_RESIZABLE, 1);
  8. glfwWindowHint(GLFW_DECORATED, !options.fullscreen);
  9. glfwWindowHint(GLFW_DOUBLEBUFFER, 1);
  10. if(options.es) {
  11. glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
  12. } else {
  13. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  14. }
  15. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, options.majorVersion);
  16. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, options.minorVersion);
  17. GLFWmonitor* monitor =
  18. options.fullscreen ? glfwGetPrimaryMonitor() : nullptr;
  19. window = glfwCreateWindow(options.size.width, options.size.height,
  20. options.name, monitor, nullptr);
  21. if(window == nullptr) {
  22. std::cout << "could not create window\n";
  23. return;
  24. }
  25. glfwSetWindowUserPointer(window, this);
  26. glfwSetKeyCallback(window, keyCallback);
  27. glfwSetCharCallback(window, charCallback);
  28. glfwMakeContextCurrent(window);
  29. glfwSwapInterval(options.vsync);
  30. }
  31. Window::~Window() {
  32. if(window != nullptr) {
  33. glfwDestroyWindow(window);
  34. }
  35. }
  36. bool Window::hasError() const {
  37. return window == nullptr;
  38. }
  39. void Window::show() {
  40. glfwShowWindow(window);
  41. }
  42. bool Window::shouldClose() const {
  43. return glfwWindowShouldClose(window);
  44. }
  45. void Window::swapBuffers() {
  46. glfwSwapBuffers(window);
  47. }
  48. void Window::trapCursor(bool trap) {
  49. glfwSetInputMode(window, GLFW_CURSOR,
  50. trap ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
  51. }
  52. Size Window::getSize() const {
  53. Size size(0, 0);
  54. glfwGetWindowSize(window, &size.width, &size.height);
  55. return size;
  56. }
  57. Size Window::getFramebufferSize() const {
  58. Size size(0, 0);
  59. glfwGetFramebufferSize(window, &size.width, &size.height);
  60. return size;
  61. }
  62. bool Window::isKeyDown(int key) const {
  63. return glfwGetKey(window, key) == GLFW_PRESS;
  64. }
  65. bool Window::isMouseDown(int mouse) const {
  66. return glfwGetMouseButton(window, mouse) == GLFW_PRESS;
  67. }
  68. void Window::getMousePosition(double& x, double& y) const {
  69. glfwGetCursorPos(window, &x, &y);
  70. }
  71. void Window::keyCallback(GLFWwindow* w, int key, int scancode, int action,
  72. int mods) {
  73. void* p = glfwGetWindowUserPointer(w);
  74. if(p != nullptr) {
  75. TextInput* input = static_cast<Window*>(p)->textInput;
  76. if(input != nullptr) {
  77. input->onKeyEvent(key, scancode, action, mods);
  78. }
  79. }
  80. }
  81. void Window::charCallback(GLFWwindow* w, unsigned int codepoint) {
  82. void* p = glfwGetWindowUserPointer(w);
  83. if(p != nullptr) {
  84. TextInput* input = static_cast<Window*>(p)->textInput;
  85. if(input != nullptr) {
  86. input->onCharEvent(codepoint);
  87. }
  88. }
  89. }