FCT
载入中...
搜索中...
未找到
GLFW_Window.cpp
浏览该文件的文档.
1//
2// Created by Administrator on 2025/3/22.
3//
4#include "../FCTAPI.h"
5namespace FCT
6{
7#ifdef FCT_USE_VULKAN
8/* void GLFW_Window::createVulkanSwapChain() {
9 if (!m_vkSwapchain)
10 m_vkSwapchain = new VK_Swapchain((VK_Context*)m_ctx);
11 m_vkSwapchain->create(m_vkSurface,m_width,m_height);
12 }
13
14 void GLFW_Window::presentVulkan()
15 {
16 m_vkSwapchain->present();
17 }*/
18#endif
19}
20
22{
23 FCT::SwapchainTargetWrapper* ret = nullptr;
24 m_ctx = srcCtx;
25 if (false) {
26
27 }
28#ifdef FCT_USE_VULKAN
29 else if (dynamic_cast<VK_Context*>(srcCtx))
30 {
31 auto ctx = dynamic_cast<VK_Context*>(srcCtx);
32 auto res = glfwCreateWindowSurface(ctx->getVkInstance(), m_window, nullptr, &m_vkSurface);
33 if (res!= VK_SUCCESS)
34 {
35 ferr << "Failed to create Vulkan surface. Error code: " << res << std::endl;
36 }
39 }
40#endif
41 else {
42 ferr << "没有受支持的context" << std::endl;
43 }
44 return ret;
45}
46
48{
49 m_common = common;
50 m_rt = rt;
51 m_swapchain = nullptr;
52#ifdef FCT_USE_VULKAN
53 m_vkSurface = nullptr;
54#endif
55 m_window = nullptr;
56 m_width = 0;
57 m_height = 0;
58 m_title = "FCT";
59 m_ctx = nullptr;
61}
62
64{
66 /*
67 if (m_vkSwapchain)
68 {
69 m_vkSwapchain->destroy();
70 delete m_vkSwapchain;
71 }
72 */
73}
74
75void FCT::GLFW_Window::invokeResizeCallbacks(int width, int height)
76{
77 for (auto cb : m_handlers) {
78 cb->onResize(this, width, height);
79 }
80}
81
83{
84 for (auto cb : m_handlers) {
85 cb->onMouseMove(this, static_cast<int>(xpos), static_cast<int>(ypos));
86 }
87}
88
89void FCT::GLFW_Window::invokeMouseCallbacks(int button, int action, int mods)
90{
91 double xpos, ypos;
92 glfwGetCursorPos(m_window, &xpos, &ypos);
93
94 for (auto cb : m_handlers) {
95 if (action == GLFW_PRESS) {
96 if (button == GLFW_MOUSE_BUTTON_LEFT) {
97 cb->onLButtonDown(this, static_cast<int>(xpos), static_cast<int>(ypos));
98 } else if (button == GLFW_MOUSE_BUTTON_RIGHT) {
99 cb->onRButtonDown(this, static_cast<int>(xpos), static_cast<int>(ypos));
100 }
101 } else if (action == GLFW_RELEASE) {
102 if (button == GLFW_MOUSE_BUTTON_LEFT) {
103 cb->onLButtonUp(this, static_cast<int>(xpos), static_cast<int>(ypos));
104 } else if (button == GLFW_MOUSE_BUTTON_RIGHT) {
105 cb->onRButtonUp(this, static_cast<int>(xpos), static_cast<int>(ypos));
106 }
107 }
108 }
109}
110
111void FCT::GLFW_Window::invokeKeyCallbacks(int key, int scancode, int action, int mods)
112{
113 for (auto cb : m_handlers) {
114 if (action == GLFW_PRESS) {
115 cb->onKeyDown(this, key);
116 } else if (action == GLFW_RELEASE) {
117 cb->onKeyUp(this, key);
118 }
119 }
120}
121
122void FCT::GLFW_Window::invokeScrollCallbacks(int xoffset, int yoffset)
123{
124 for (auto cb : m_handlers) {
125 cb->onMouseWheel(this, yoffset);
126 }
127}
128
130{
131 m_common->postUiTask([this](void*)
132 {
133 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
134 m_window = glfwCreateWindow(m_width, m_height, m_title.c_str(), nullptr, nullptr);
135 glfwSetWindowUserPointer(m_window,this);
136 glfwSetFramebufferSizeCallback(m_window, [](GLFWwindow* window, int width, int height) {
137 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));\
138 wnd->invokeResizeCallbacks(width, height);
139 });
140 glfwSetMouseButtonCallback(m_window, [](GLFWwindow* window, int button, int action, int mods) {
141 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));\
142 wnd->invokeMouseCallbacks(button, action, mods);
143 });
144 glfwSetCursorPosCallback(m_window, [](GLFWwindow* window, double xpos, double ypos) {
145 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));\
146 wnd->invokeMouseMoveCallbacks(static_cast<int>(xpos), static_cast<int>(ypos));
147 });
148 glfwSetKeyCallback(m_window, [](GLFWwindow* window, int key, int scancode, int action, int mods) {
149 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));
150 wnd->invokeKeyCallbacks(key, scancode, action, mods);
151 });
152 glfwSetScrollCallback(m_window, [](GLFWwindow* window, double xoffset, double yoffset) {
153 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));
154 wnd->invokeScrollCallbacks(xoffset, yoffset);
155 });
156 });
157 if (m_behavior)
158 {
159 delete m_behavior;
160 m_behavior = nullptr;
161 }
163}
164
165
167{
168 return !glfwWindowShouldClose(m_window);
169}
170
172{
173 if (isRunning())
174 {
175 if (m_swapchain)
176 {
177 m_swapchain->present();
178 }
179 }
180 //glfwSwapBuffers(m_window);
181}
182
184{
185 int width, height;
186 glfwGetWindowSize(m_window, &width, &height);
187 return width;
188}
189
191{
192 int width, height;
193 glfwGetWindowSize(m_window, &width, &height);
194 return height;
195
196}
197
199{
200 glfwSetCursorPos(m_window, x, y);
201}
202
204{
205 return nullptr;
206}
207
209{
210 double xpos, ypos;
211 glfwGetCursorPos(m_window, &xpos, &ypos);
212 return FCT::Vec2(static_cast<float>(xpos), static_cast<float>(ypos));
213}
214
215/*
216std::vector<FCT::Image*> FCT::GLFW_Window::getTargetImages()
217{
218 return std::vector<FCT::Image*>();
219}
220*/
221
222
223
224
225void FCT::GLFW_Window::recreateSwapchain(int width, int height)
226{
227 if (!m_swapchain)
228 {
229 m_swapchain = m_ctx->createResource<RHI::Swapchain>();
230 }
232 m_swapchain->size(width, height);
233 m_swapchain->create();
234}
235
#define FCT_SAFE_RELEASE(obj)
void recreateSwapchain(int width, int height)
Vec2 getCursorPos() const override
void invokeScrollCallbacks(int xoffset, int yoffset)
bool isRunning() const override
friend class GLFW_WindowBehavior
void invokeResizeCallbacks(int width, int height)
GLFW_UICommon * m_common
int getWidth() override
GLFW_Window(GLFW_UICommon *common, Runtime *rt)
void invokeKeyCallbacks(int key, int scancode, int action, int mods)
void setCursorPos(int x, int y) override
void invokeMouseMoveCallbacks(int xpos, int ypos)
int getHeight() override
SwapchainTargetWrapper * getSwapchainTarget(Context *ctx) override
void swapBuffers() override
Image * getImage() const override
VkSurfaceKHR m_vkSurface
void invokeMouseCallbacks(int button, int action, int mods)
GLFWwindow * m_window
std::vector< EventHandler * > m_handlers
WindowBehavior * m_behavior
std::string m_title
Context * m_ctx
RHI::Swapchain * m_swapchain
std::ostream & ferr