FCT
载入中...
搜索中...
未找到
Window.h
浏览该文件的文档.
1#ifndef FCT_WINDOW_H
2#define FCT_WINDOW_H
6#include "./EventHandler.h"
7#include "./CallBackHandler.h"
9#include "../RHI/Semaphore.h"
10#include "../RHI/Swapcain.h"
12#include "./AutoViewport.h"
14#include "../Context/Context.h"
16
17namespace FCT {
18 namespace RHI
19 {
20 class Swapchain;
21 }
22
24 {
25 public:
26 virtual void pos(int x,int y) = 0;
27 virtual void size(int w, int h) = 0;
28 virtual void title(const std::string& title) = 0;
29 private:
30
31 };
33 class EventHandler;
34 using VFuncII = std::function<void(int,int)>;
36 {
37 public:
38 virtual ~SwapchainTargetWrapper() = default;
39 virtual void* getNativeHandler() = 0;
40 private:
41 };
42 namespace WindowModule
43 {
44 struct Swapchain {
46 explicit Swapchain(Context* context) : ctx(context) {}
47 };
48
49 struct AutoViewport {
50 uint32_t width, height;
51 AutoViewport(uint32_t w, uint32_t h) : width(w), height(h) {}
52 };
53
54 struct DepthStencil {
56 explicit DepthStencil(Format fmt) : format(fmt) {}
57 };
58 }
66 class Window : public RefCount,public IRenderTarget {
67 protected:
70 public:
73 Window();
74 virtual ~Window() {
76 m_callbackHandler->release();
77 }
78 void pos(int x, int y) {
79 m_behavior->pos(x, y);
80 }
81 void size(int w, int h) {
82 m_behavior->size(w, h);
83 }
84 virtual void createPlatform() = 0;
85 void create(WindowCreateFlags flags = WindowCreateFlag::defaultConfig);
86 void bind(Context* ctx) override;
87 virtual bool isRunning() const = 0;
88 virtual void swapBuffers() = 0;
89 virtual int getWidth() = 0;
90 virtual int getHeight() = 0;
91 //virtual void viewport(Vec2 lt, Vec2 rb) = 0;
92 virtual void setCursorPos(int x, int y) = 0;
93 void registerHandler(EventHandler* handler);
94 void unregisterHandler(EventHandler* handler);
95 virtual Vec2 getCursorPos() const = 0;
96 void clearHandler();
98 {
99 return m_swapchain->target()->targetImage();
100 }
105 return m_swapchain->target();
106 }
108 {
109 if (m_swapchain)
110 {
111 m_swapchain->addRenderFinshSemaphore(semaphore);
112 return;
113 }
114 ferr << "未 bind Context" << std::endl;
115 }
117 {
118 m_swapchain->clearRenderFinshSemaphores();
119 }
121 {
122 m_swapchain->setPresentFinshSemaphore(semaphore);
123 }
125 {
126 m_swapchain->addRenderFinshFence(fence);
127 }
129 {
130 m_swapchain->clearRenderFinshFences();
131 }
132 void initRender();
133 void title(const std::string& title)
134 {
135 m_behavior->title(title);
136 }
137 //目前只能在bind前 调用,bind后调用为未定义 行为
139 {
140 if (m_swapchain)
141 {
142 m_swapchain->enableDepthBuffer(format);
144 } else
145 {
147 m_depthBufferFormat = format;
148 }
149 }
150 uint32_t getSwapchainImageCount() const
151 {
152 return m_swapchain->getImageCount();
153 }
155 {
156 return m_swapchain->getSampleCount();
157 }
159 {
160 return m_swapchain;
161 }
162 template<typename... Components>
163 void addModule(Components&&... components) {
164 (addSingleComponent(std::forward<Components>(components)), ...);
165 }
166
167 template<typename Component>
168 auto getModule() const -> std::conditional_t<
169 std::is_same_v<Component, WindowModule::Swapchain>, RHI::Swapchain*,
170 std::conditional_t<
171 std::is_same_v<Component, WindowModule::AutoViewport>, AutoViewport*,
172 std::conditional_t<
173 std::is_same_v<Component, WindowModule::DepthStencil>, Format,
174 void*
175 >
176 >
177 > {
178 if constexpr (std::is_same_v<Component, WindowModule::Swapchain>) {
179 return m_swapchain;
180 } else if constexpr (std::is_same_v<Component, WindowModule::AutoViewport>) {
181 return m_autoViewport;
182 } else if constexpr (std::is_same_v<Component, WindowModule::DepthStencil>) {
183 return m_depthBufferFormat;
184 } else {
185 static_assert(sizeof(Component) == 0, "Unsupported module type");
186 return nullptr;
187 }
188 }
189 private:
190 protected:
193 std::vector<EventHandler*> m_handlers;
195 std::string m_title;
201 private:
202 void addSingleComponent(const WindowModule::Swapchain& component);
203 void addSingleComponent(const WindowModule::AutoViewport& component);
204 void addSingleComponent(const WindowModule::DepthStencil& component);
205 };
206
207
209 private:
211 public:
213 void pos(int x, int y) override {
214 m_window->m_x = x;
215 m_window->m_y = y;
216 }
217 void size(int w, int h) override {
218 m_window->m_width = w;
219 m_window->m_height = h;
220 }
221 void title(const std::string& title) override {
222 m_window->m_title = title;
223 }
224 };
225 inline Window::Window() {
226 m_autoViewport = nullptr;
227 m_swapchain = nullptr;
231 m_callbackHandler->addResizeCallback([this](Window*,int w,int h)
232 {
233 if (m_swapchain)
234 {
235 //m_swapchain->size(w,h);
236 m_swapchain->needRecreate(w,h);
237 }
238 });
240 }
241
242}
243#endif
#define FCT_DECLARE_FLAGS(BitType)
void title(const std::string &title) override
void size(int w, int h) override
void pos(int x, int y) override
SetParamWindowBehavior(Window *window)
virtual void * getNativeHandler()=0
virtual ~SwapchainTargetWrapper()=default
virtual void size(int w, int h)=0
virtual void title(const std::string &title)=0
virtual void pos(int x, int y)=0
uint32_t getSwapchainImageCount() const
virtual bool isRunning() const =0
Image * targetImage() const
void initRender()
std::vector< EventHandler * > m_handlers
virtual Vec2 getCursorPos() const =0
bool m_needEnableDepthBuffer
WindowBehavior * m_behavior
void size(int w, int h)
RenderTargetType getType() const override
void addRenderFinshFence(RHI::Fence *fence)
void pos(int x, int y)
void setPresentFinshSemaphore(RHI::Semaphore *semaphore)
virtual SwapchainTargetWrapper * getSwapchainTarget(Context *ctx)=0
void title(const std::string &title)
void create(WindowCreateFlags flags=WindowCreateFlag::defaultConfig)
virtual void setCursorPos(int x, int y)=0
Format m_depthBufferFormat
virtual void swapBuffers()=0
void addSingleComponent(const WindowModule::Swapchain &component)
auto getModule() const -> std::conditional_t< std::is_same_v< Component, WindowModule::Swapchain >, RHI::Swapchain *, std::conditional_t< std::is_same_v< Component, WindowModule::AutoViewport >, AutoViewport *, std::conditional_t< std::is_same_v< Component, WindowModule::DepthStencil >, Format, void * > > >
std::string m_title
Samples getSwapchainSampleCount() const
void enableDepthBuffer(Format format)
void clearRenderFinshFences()
RHI::Swapchain * swapchain() const
virtual ~Window()
void bind(Context *ctx) override
Context * m_ctx
void clearRenderFinshSemaphores()
void addModule(Components &&... components)
virtual int getHeight()=0
void addRenderFinshSemaphore(RHI::Semaphore *semaphore)
virtual int getWidth()=0
AutoViewport * m_autoViewport
ImageRenderTarget * getCurrentTarget()
CallBackEventHandler * getCallBack() const
RHI::Swapchain * m_swapchain
void registerHandler(EventHandler *handler)
virtual void createPlatform()=0
CallBackEventHandler * m_callbackHandler
void unregisterHandler(EventHandler *handler)
friend class SetParamWindowBehavior
EventDispatcher< EventSystemConfig::IdentifierTriggerOnly > m_delayModuleCreate
void clearHandler()
WindowCreateFlag
std::ostream & ferr
IEventSystem< Config > EventDispatcher
std::function< void(int, int)> VFuncII
AutoViewport(uint32_t w, uint32_t h)
Swapchain(Context *context)