coral_window.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "coral_window.h"
  2. #include "coral_input.h"
  3. #include <stdexcept>
  4. using namespace coral_3d;
  5. coral_window::coral_window(int width, int height, const std::string& name)
  6. : width_(width)
  7. , height_(height)
  8. , window_name_(name)
  9. {
  10. init_window();
  11. }
  12. coral_window::~coral_window()
  13. {
  14. glfwDestroyWindow(pWindow_);
  15. glfwTerminate();
  16. }
  17. void coral_window::framebuffer_resize_callback(GLFWwindow* pWindow, int width, int height)
  18. {
  19. auto window = reinterpret_cast<coral_window*>(glfwGetWindowUserPointer(pWindow));
  20. window->is_framebuffer_resized_ = true;
  21. window->width_ = width;
  22. window->height_ = height;
  23. }
  24. void coral_window::init_window()
  25. {
  26. glfwInit();
  27. // Tell GLFW to not use OpenGL
  28. glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  29. glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
  30. pWindow_ = glfwCreateWindow(width_, height_, window_name_.c_str(), nullptr, nullptr);
  31. // Set window user pointer
  32. glfwSetWindowUserPointer(pWindow_, this);
  33. glfwSetFramebufferSizeCallback(pWindow_, framebuffer_resize_callback);
  34. // Set mouse input mode
  35. glfwSetInputMode(pWindow_, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  36. // Initialize input
  37. coral_input::initialize(pWindow_);
  38. }
  39. void coral_window::create_window_surface(VkInstance instance, VkSurfaceKHR* surface)
  40. {
  41. if (glfwCreateWindowSurface(instance, pWindow_, nullptr, surface) != VK_SUCCESS)
  42. throw std::runtime_error(
  43. "ERROR! coral_window::create_window_surface() >> Failed to create window surface!");
  44. }