Skip to content
Snippets Groups Projects
Commit c6d9823c authored by Sebastian Gaida's avatar Sebastian Gaida
Browse files

[#89] create get focussed window function

parent e8406a9d
No related branches found
No related tags found
1 merge request!90Resolve "Mehrere Fenster, Abhängigkeiten von Core zu Fenster+Swapchain etc"
#pragma once #pragma once
/**
* @authors Sebastian Gaida
* @file src/vkcv/Window.hpp
* @brief Window class to handle a basic rendering surface and input
*/
#define NOMINMAX #define NOMINMAX
#include <algorithm> #include <algorithm>
...@@ -16,11 +11,12 @@ struct GLFWwindow; ...@@ -16,11 +11,12 @@ struct GLFWwindow;
namespace vkcv { namespace vkcv {
class Window { class Window {
friend class WindowManager;
private: private:
std::string m_title; std::string m_title;
bool m_resizable; bool m_resizable;
GLFWwindow *m_window; GLFWwindow *m_window;
public: public:
/** /**
* creates an uninitialized #Window * creates an uninitialized #Window
...@@ -60,6 +56,13 @@ namespace vkcv { ...@@ -60,6 +56,13 @@ namespace vkcv {
[[nodiscard]] [[nodiscard]]
bool isOpen() const; bool isOpen() const;
/**
* gets the currently focused window and returns it
* only accessible to WindowManager
* @return
*/
static Window& getFocusedWindow();
/** /**
* polls all events on the GLFWwindow * polls all events on the GLFWwindow
*/ */
...@@ -115,7 +118,12 @@ namespace vkcv { ...@@ -115,7 +118,12 @@ namespace vkcv {
* Destructor of #Window, terminates GLFW * Destructor of #Window, terminates GLFW
*/ */
virtual ~Window(); virtual ~Window();
/**
* destroys the window
*/
void destroyWindow();
/** /**
* gets the windows framebuffer size * gets the windows framebuffer size
* @param width * @param width
......
/**
* @authors Sebastian Gaida
* @file src/vkcv/Window.cpp
* @brief Window class to handle a basic rendering surface and input
*/
#include <thread> #include <thread>
#include <vector> #include <vector>
...@@ -63,20 +58,7 @@ namespace vkcv { ...@@ -63,20 +58,7 @@ namespace vkcv {
static std::vector<GLFWwindow*> s_Windows; static std::vector<GLFWwindow*> s_Windows;
void Window_onGamepadEvent(int gamepadIndex) { void Window_onGamepadEvent(int gamepadIndex) {
size_t activeWindowIndex = std::find_if( Window::getFocusedWindow().e_gamepad(gamepadIndex);
s_Windows.begin(),
s_Windows.end(),
[](GLFWwindow* window){return glfwGetWindowAttrib(window, GLFW_FOCUSED);}
) - s_Windows.begin();
// fixes index getting out of bounds (e.g. if there is no focused window)
activeWindowIndex *= (activeWindowIndex < s_Windows.size());
auto window = static_cast<Window *>(glfwGetWindowUserPointer(s_Windows[activeWindowIndex]));
if (window != nullptr) {
window->e_gamepad(gamepadIndex);
}
} }
static GLFWwindow* createGLFWWindow(const char *windowTitle, int width, int height, bool resizable) { static GLFWwindow* createGLFWWindow(const char *windowTitle, int width, int height, bool resizable) {
...@@ -162,6 +144,21 @@ namespace vkcv { ...@@ -162,6 +144,21 @@ namespace vkcv {
glfwTerminate(); glfwTerminate();
} }
} }
void Window::destroyWindow() {
Window::e_mouseButton.unlock();
Window::e_mouseMove.unlock();
Window::e_mouseScroll.unlock();
Window::e_resize.unlock();
Window::e_key.unlock();
Window::e_char.unlock();
Window::e_gamepad.unlock();
if (m_window) {
s_Windows.erase(std::find(s_Windows.begin(), s_Windows.end(), m_window));
glfwDestroyWindow(m_window);
}
}
Window::Window(const Window &other) : Window::Window(const Window &other) :
m_title(other.getTitle()), m_title(other.getTitle()),
...@@ -291,5 +288,21 @@ namespace vkcv { ...@@ -291,5 +288,21 @@ namespace vkcv {
height = 0; height = 0;
} }
} }
Window& Window::getFocusedWindow() {
static Window defaultWindow;
auto activeWindowIterator = std::find_if(
s_Windows.begin(),
s_Windows.end(),
[](GLFWwindow *window) { return glfwGetWindowAttrib(window, GLFW_FOCUSED); }
);
if( activeWindowIterator == s_Windows.end() )
{
return defaultWindow;
}
Window& window = *static_cast<Window *>(glfwGetWindowUserPointer(*activeWindowIterator));
return window;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment