#include "HydraWindowManager.h" #include "../HydraDefines.h" // #include "../UI/HydraUIManager.h" #include "../engine/HydraEngine.h" std::vector HydraWindowManager::m_key_presses; std::queue HydraWindowManager::m_mouse_moves; glm::vec2 HydraWindowManager::m_mouse_position; std::mutex HydraWindowManager::m_mouse_pos_mutex; void HydraWindowManager::key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { if (action == GLFW_PRESS) { m_key_presses.push_back(HydraKeyPressStuct(key, KEY_PRESS_ACTIONS::HYDRA_KEY_PRESSED)); } else if (action == GLFW_RELEASE) { m_key_presses.push_back(HydraKeyPressStuct(key, KEY_PRESS_ACTIONS::HYDRA_KEY_RELEASED)); } } bool HydraWindowManager::CreateRenderWindow(std::string title, HydraRenderSettings renderSettings) { /* Init GLFW */ if (!glfwInit()) exit(EXIT_FAILURE); GLFWmonitor *monitor = glfwGetPrimaryMonitor(); if (monitor) { const GLFWvidmode *mode = glfwGetVideoMode(monitor); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); m_window = glfwCreateWindow(renderSettings.ViewportWidth, renderSettings.DisplayHeight, "Hydra", NULL, NULL); if (!m_window) { glfwTerminate(); exit(EXIT_FAILURE); } // glfwSetWindowMonitor(m_window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); } else { exit(EXIT_FAILURE); } // glfwSetWindowAspectRatio(m_window, 1, 1); glfwSetKeyCallback(m_window, key_callback); glfwSetMouseButtonCallback(m_window, mouse_button_callback); glfwSetCursorPosCallback(m_window, cursor_position_callback); glfwMakeContextCurrent(m_window); // VSYNC glfwSwapInterval(0); glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); if (glfwRawMouseMotionSupported()) { glfwSetInputMode(m_window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE); } // GetEngine()->UIManager()->InitialsGUI(m_window); return true; } void HydraWindowManager::mouse_button_callback(GLFWwindow *window, int button, int action, int mods) { if (button != GLFW_MOUSE_BUTTON_LEFT) return; } void HydraWindowManager::cursor_position_callback(GLFWwindow *window, double x, double y) { SetMousePosition(glm::vec2((float)x, (float)y)); m_mouse_moves.push(HydraMouseMoveStruct((int)x, (int)y)); } void HydraWindowManager::SetMousePosition(glm::vec2 mouse_pos) { std::lock_guard lock(m_mouse_pos_mutex); m_mouse_position = mouse_pos; } bool HydraWindowManager::HandleMessages() { glfwPollEvents(); return false; } void HydraWindowManager::Shutdown() { // GetEngine()->UIManager()->ShutdownGUI(); glfwTerminate(); } bool HydraWindowManager::GetKeyPress(HydraKeyPressStuct &key_press) { if (!m_key_presses.empty()) { key_press = m_key_presses.back(); m_key_presses.pop_back(); return true; } return false; } bool HydraWindowManager::GetMouseMove(HydraMouseMoveStruct &mouse_move) { if (!m_mouse_moves.empty()) { mouse_move = m_mouse_moves.front(); m_mouse_moves.pop(); return true; } return false; } glm::vec2 HydraWindowManager::GetMousePosition() { std::lock_guard lock(m_mouse_pos_mutex); return m_mouse_position; } void HydraWindowManager::Swap() { glfwMakeContextCurrent(m_window); // GetEngine()->UIManager()->BeginGUIRender(); // GetEngine()->UIManager()->RenderGUI(); // GetEngine()->UIManager()->EndGUIRender(); glfwSwapBuffers(m_window); }