Midnight Graphics
Create Fast and Simple Graphics in C++
Window.hpp
1 #pragma once
2 
3 #include <Def.hpp>
4 #include <Math.hpp>
5 #include <set>
6 
7 #include <Graphics/Backend/Command.hpp>
8 #include <Graphics/Backend/Sync.hpp>
9 
10 #include "Event.hpp"
11 #include "RenderFrame.hpp"
12 
13 namespace mn::Graphics
14 {
15  struct FrameData
16  {
17  std::unique_ptr<Backend::CommandBuffer> command_buffer;
18  std::unique_ptr<Backend::CommandPool> command_pool;
19  std::unique_ptr<Backend::Semaphore> swapchain_sem, render_sem;
20  std::unique_ptr<Backend::Fence> render_fence;
21 
22  // Here we can keep a std::vector<std::shared_ptr<void>> resources
23  // Everytime we use something in RenderFrame, we can push it onto this resources
24  // vector. Then, at the beginning of the frame when we wait on the render_fence (or during destruction
25  // of this FrameData object) we can clear it. This way we ensure if the resources used on this render
26  // are deleted elsewhere, they are at least still valid until the end of the render.
27 
28  std::set<std::shared_ptr<void>> resources;
29 
30  void release();
31 
32  void create();
33  void destroy();
34  };
35 
36  struct Window
37  {
38  inline static bool ImGui_Initialized = false;
39 
40  MN_SYMBOL Window(const Math::Vec2u& size, const std::string& name);
41  MN_SYMBOL Window(const std::string& config_file = "");
42 
43  MN_SYMBOL static Window fromLuaScript(const std::string& config_file);
44 
45  Window(const Window&) = delete;
46  Window(Window&&);
47 
48  MN_SYMBOL ~Window();
49 
50  MN_SYMBOL void close();
51 
52  MN_SYMBOL bool pollEvent(Event& event) const;
53 
54  auto size() const { return _size; }
55 
56  MN_SYMBOL RenderFrame startFrame() const;
57  MN_SYMBOL void endFrame(RenderFrame& rf) const;
58  MN_SYMBOL void runFrame(const std::function<void(RenderFrame& rf)>& func) const;
59 
60  MN_SYMBOL void setTitle(const std::string& title) const;
61  bool shouldClose() const { return _close; }
62  MN_SYMBOL void finishWork() const;
63 
64  MN_SYMBOL void showMouse(bool show);
65  MN_SYMBOL void setMousePos(Math::Vec2f position);
66 
67  float aspectRatio() const { return (float)Math::x(_size) / (float)Math::y(_size); }
68 
69  bool process_imgui_events = true;
70 
71  private:
72  void construct_swapchain();
73 
74  void _open(const Math::Vec2u& size, const std::string& name);
75 
76  uint32_t next_image_index(std::shared_ptr<FrameData> fd) const;
77  std::shared_ptr<FrameData> get_next_frame() const;
78 
79  bool _close;
80  Handle<Window> handle;
81  mn::handle_t surface, swapchain;
82 
83  std::shared_ptr<Image> imgui_surface;
84  std::vector<std::shared_ptr<Image>> images;
85 
86  std::vector<std::shared_ptr<FrameData>> frame_data;
87  Math::Vec2u _size;
88  };
89 }
Definition: Event.hpp:8
Definition: Window.hpp:16
Definition: RenderFrame.hpp:25
Definition: Window.hpp:37
Definition: Vector.hpp:12
Definition: TypedHandle.hpp:9