Window.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <iostream>
  2. #include "wrapper/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 = options.fullscreen ? glfwGetPrimaryMonitor() : nullptr;
  17. window = glfwCreateWindow(options.size.width, options.size.height, options.name, monitor, nullptr);
  18. if(window == nullptr) {
  19. std::cout << "could not create window\n";
  20. return;
  21. }
  22. glfwMakeContextCurrent(window);
  23. glfwSwapInterval(options.vsync);
  24. }
  25. Window::~Window() {
  26. if(window != nullptr) {
  27. glfwDestroyWindow(window);
  28. }
  29. }
  30. bool Window::hasError() const {
  31. return window == nullptr;
  32. }
  33. void Window::show() {
  34. glfwShowWindow(window);
  35. }
  36. bool Window::shouldClose() const {
  37. return glfwWindowShouldClose(window);
  38. }
  39. void Window::swapBuffers() {
  40. glfwSwapBuffers(window);
  41. }
  42. void Window::trapCursor(bool trap) {
  43. glfwSetInputMode(window, GLFW_CURSOR, trap ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
  44. }
  45. Size Window::getSize() const {
  46. Size size(0, 0);
  47. glfwGetWindowSize(window, &size.width, &size.height);
  48. return size;
  49. }
  50. Size Window::getFramebufferSize() const {
  51. Size size(0, 0);
  52. glfwGetFramebufferSize(window, &size.width, &size.height);
  53. return size;
  54. }
  55. bool Window::isKeyDown(int key) const {
  56. return glfwGetKey(window, key) == GLFW_PRESS;
  57. }