Window.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <iostream>
  2. #include "rendering/Window.h"
  3. Window::Window(const WindowOptions& options) : window(nullptr) {
  4. glfwDefaultWindowHints();
  5. glfwWindowHint(GLFW_VISIBLE, 0);
  6. glfwWindowHint(GLFW_RESIZABLE, 1);
  7. glfwWindowHint(GLFW_DECORATED, !options.fullscreen);
  8. glfwWindowHint(GLFW_DOUBLEBUFFER, 1);
  9. if(options.es) {
  10. glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
  11. } else {
  12. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  13. }
  14. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, options.majorVersion);
  15. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, options.minorVersion);
  16. GLFWmonitor* monitor =
  17. options.fullscreen ? glfwGetPrimaryMonitor() : nullptr;
  18. window = glfwCreateWindow(options.size.width, options.size.height,
  19. options.name, monitor, nullptr);
  20. if(window == nullptr) {
  21. std::cout << "could not create window\n";
  22. return;
  23. }
  24. glfwMakeContextCurrent(window);
  25. glfwSwapInterval(options.vsync);
  26. }
  27. Window::~Window() {
  28. if(window != nullptr) {
  29. glfwDestroyWindow(window);
  30. }
  31. }
  32. bool Window::hasError() const {
  33. return window == nullptr;
  34. }
  35. void Window::show() {
  36. glfwShowWindow(window);
  37. }
  38. bool Window::shouldClose() const {
  39. return glfwWindowShouldClose(window);
  40. }
  41. void Window::swapBuffers() {
  42. glfwSwapBuffers(window);
  43. }
  44. void Window::trapCursor(bool trap) {
  45. glfwSetInputMode(window, GLFW_CURSOR,
  46. trap ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
  47. }
  48. Size Window::getSize() const {
  49. Size size(0, 0);
  50. glfwGetWindowSize(window, &size.width, &size.height);
  51. return size;
  52. }
  53. Size Window::getFramebufferSize() const {
  54. Size size(0, 0);
  55. glfwGetFramebufferSize(window, &size.width, &size.height);
  56. return size;
  57. }
  58. bool Window::isKeyDown(int key) const {
  59. return glfwGetKey(window, key) == GLFW_PRESS;
  60. }