Window.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <iostream>
  2. #include "rendering/wrapper/Window.h"
  3. Window::Window(WindowSize& size, const Options& options) : window(nullptr), size(size) {
  4. glfwDefaultWindowHints();
  5. glfwWindowHint(GLFW_VISIBLE, 0);
  6. glfwWindowHint(GLFW_RESIZABLE, 1);
  7. glfwWindowHint(GLFW_DECORATED, !options.fullscreen);
  8. glfwWindowHint(GLFW_DOUBLEBUFFER, 1);
  9. glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
  10. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  11. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  12. GLFWmonitor* monitor = options.fullscreen ? glfwGetPrimaryMonitor() : nullptr;
  13. window = glfwCreateWindow(size.width, size.height, options.name, monitor, nullptr);
  14. if(window == nullptr) {
  15. std::cout << "could not create window\n";
  16. return;
  17. }
  18. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  19. glfwMakeContextCurrent(window);
  20. glfwSwapInterval(0);
  21. glfwSetWindowUserPointer(window, this);
  22. glfwSetFramebufferSizeCallback(window, [](GLFWwindow* glfwWindow, int newWidth, int newHeight) {
  23. glViewport(0, 0, newWidth, newHeight);
  24. void* data = glfwGetWindowUserPointer(glfwWindow);
  25. if(data == nullptr) {
  26. return;
  27. }
  28. Window& w = *static_cast<Window*>(data);
  29. w.size.width = newWidth;
  30. w.size.height = newHeight;
  31. std::cout << "WUSI\n";
  32. });
  33. }
  34. Window::~Window() {
  35. if(window != nullptr) {
  36. glfwDestroyWindow(window);
  37. }
  38. }
  39. bool Window::hasError() const {
  40. return window == nullptr;
  41. }
  42. void Window::show() {
  43. glfwShowWindow(window);
  44. }
  45. bool Window::shouldClose() const {
  46. return glfwWindowShouldClose(window);
  47. }
  48. void Window::swapBuffers() {
  49. glfwSwapBuffers(window);
  50. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  51. }
  52. void Window::setKeyCallback(GLFWkeyfun f) {
  53. glfwSetKeyCallback(window, f);
  54. }