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 std::vector<std::string> filePaths;
132 for (int i = 0; i < count; ++i)
133 filePaths.push_back(paths[i]);
134 for (auto cb : m_handlers) {
135 cb->onFileDrop(this, filePaths);
136 }
137}
138
140{
141 m_common->postUiTask([this](void*)
142 {
143 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
144 m_window = glfwCreateWindow(m_width, m_height, m_title.c_str(), nullptr, nullptr);
145 glfwSetWindowUserPointer(m_window,this);
146 glfwSetFramebufferSizeCallback(m_window, [](GLFWwindow* window, int width, int height) {
147 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));\
148 wnd->invokeResizeCallbacks(width, height);
149 });
150 glfwSetMouseButtonCallback(m_window, [](GLFWwindow* window, int button, int action, int mods) {
151 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));\
152 wnd->invokeMouseCallbacks(button, action, mods);
153 });
154 glfwSetCursorPosCallback(m_window, [](GLFWwindow* window, double xpos, double ypos) {
155 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));\
156 wnd->invokeMouseMoveCallbacks(static_cast<int>(xpos), static_cast<int>(ypos));
157 });
158 glfwSetKeyCallback(m_window, [](GLFWwindow* window, int key, int scancode, int action, int mods) {
159 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));
160 wnd->invokeKeyCallbacks(key, scancode, action, mods);
161 });
162 glfwSetScrollCallback(m_window, [](GLFWwindow* window, double xoffset, double yoffset) {
163 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));
164 wnd->invokeScrollCallbacks(xoffset, yoffset);
165 });
166 glfwSetDropCallback(m_window, [](GLFWwindow* window, int count, const char** paths) {
167 auto* wnd = static_cast<FCT::GLFW_Window*>(glfwGetWindowUserPointer(window));
168 wnd->invokeFileDropCallbacks(count, paths);
169 });
170 });
171 if (m_behavior)
172 {
173 delete m_behavior;
174 m_behavior = nullptr;
175 }
177}
178
179
181{
182 return !glfwWindowShouldClose(m_window);
183}
184
186{
187 if (isRunning())
188 {
189 if (m_swapchain)
190 {
191 m_swapchain->present();
192 }
193 }
194 //glfwSwapBuffers(m_window);
195}
196
198{
199 int width, height;
200 glfwGetWindowSize(m_window, &width, &height);
201 return width;
202}
203
205{
206 int width, height;
207 glfwGetWindowSize(m_window, &width, &height);
208 return height;
209
210}
211
213{
214 glfwSetCursorPos(m_window, x, y);
215}
216
218{
219 return nullptr;
220}
221
223{
224 double xpos, ypos;
225 glfwGetCursorPos(m_window, &xpos, &ypos);
226 return FCT::Vec2(static_cast<float>(xpos), static_cast<float>(ypos));
227}
228
229/*
230std::vector<FCT::Image*> FCT::GLFW_Window::getTargetImages()
231{
232 return std::vector<FCT::Image*>();
233}
234*/
235
236
237
238
239void FCT::GLFW_Window::recreateSwapchain(int width, int height)
240{
241 if (!m_swapchain)
242 {
243 m_swapchain = m_ctx->createResource<RHI::Swapchain>();
244 }
246 m_swapchain->size(width, height);
247 m_swapchain->create();
248}
249
#define FCT_SAFE_RELEASE(obj)
void recreateSwapchain(int width, int height)
Vec2 getCursorPos() const override
void invokeScrollCallbacks(int xoffset, int yoffset)
void invokeFileDropCallbacks(int count, const char **paths)
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