Chira Engine
A customizable MIT-licensed game engine.
DeviceSDL.h
1 #pragma once
2 
3 #include <cstdint>
4 #include <string>
5 #include <string_view>
6 #include <unordered_map>
7 #include <entity/Viewport.h>
8 
9 struct SDL_Window;
10 struct ImGuiContext;
11 
12 namespace chira {
13 
14 class IPanel;
15 
16 } // namespace chira
17 
18 // SDL + SDL Renderer backend
19 namespace chira::Device {
20 
21 // Copied from SDL
22 enum PopupFlags {
23  POPUP_ERROR = 0x00000010,
24  POPUP_WARNING = 0x00000020,
25  POPUP_INFO = 0x00000040,
26  POPUP_BUTTONS_LEFT_TO_RIGHT = 0x00000080,
27  POPUP_BUTTONS_RIGHT_TO_LEFT = 0x00000100,
28 };
29 
30 struct WindowHandle {
31  std::unordered_map<uuids::uuid, IPanel*> panels{};
32  SDL_Window* window = nullptr;
33  ImGuiContext* imguiContext = nullptr;
34  int width = -1;
35  int height = -1;
36  bool hidden = false;
37  bool mouseCaptured = false;
38  bool shouldClose = false;
39 
40  Viewport* viewport = nullptr;
41  bool viewportIsSelfOwned = false;
42 
43  explicit inline operator bool() const { return window; }
44  inline bool operator!() const { return !window; }
45 };
46 
47 [[nodiscard]] bool initBackendAndCreateSplashscreen(bool splashScreenVisible);
48 void destroySplashscreen();
49 void destroyBackend();
50 
51 [[nodiscard]] std::uint64_t getTicks();
52 
54 [[nodiscard]] WindowHandle* createWindow(int width, int height, std::string_view title, Viewport* viewport);
55 void refreshWindows();
56 [[nodiscard]] int getWindowCount();
57 
58 [[nodiscard]] Viewport* getWindowViewport(WindowHandle* handle);
59 
60 void setWindowTitle(WindowHandle* handle, std::string_view title);
61 [[nodiscard]] std::string_view getWindowTitle(WindowHandle* handle);
62 
63 void setWindowMaximized(WindowHandle* handle, bool maximize);
64 [[nodiscard]] bool isWindowMaximized(WindowHandle* handle);
65 
66 void minimizeWindow(WindowHandle* handle, bool minimize);
67 [[nodiscard]] bool isWindowMinimized(WindowHandle* handle);
68 
69 void setWindowFullscreen(WindowHandle* handle, bool fullscreen);
70 [[nodiscard]] bool isWindowFullscreen(WindowHandle* handle);
71 
72 void setWindowVisibility(WindowHandle* handle, bool visible);
73 [[nodiscard]] bool isWindowVisible(WindowHandle* handle);
74 
75 void setWindowSize(WindowHandle* handle, int width, int height);
76 [[nodiscard]] glm::vec2i getWindowSize(WindowHandle* handle);
77 
78 void setWindowPosition(WindowHandle* handle, int width, int height);
79 void setWindowPositionFromCenter(WindowHandle* handle, int width, int height);
80 [[nodiscard]] glm::vec2i getWindowPosition(WindowHandle* handle);
81 
82 void setMousePositionGlobal(int x, int y);
83 void setMousePositionInWindow(WindowHandle* handle, int x, int y);
84 [[nodiscard]] glm::vec2i getMousePositionGlobal();
85 [[nodiscard]] glm::vec2i getMousePositionInFocusedWindow();
86 
87 void setMouseCapturedWindow(WindowHandle* handle, bool captured);
88 [[nodiscard]] bool isMouseCapturedWindow(WindowHandle* handle);
89 
91 void queueDestroyWindow(WindowHandle* handle, bool free);
92 [[nodiscard]] bool isWindowAboutToBeDestroyed(WindowHandle* handle);
93 void destroyWindow(WindowHandle* handle);
94 void destroyAllWindows();
95 
96 uuids::uuid addPanelToWindow(WindowHandle* handle, IPanel* panel);
97 [[nodiscard]] IPanel* getPanelOnWindow(WindowHandle* handle, const uuids::uuid& panelID);
98 void removePanelFromWindow(WindowHandle* handle, const uuids::uuid& panelID);
99 void removeAllPanelsFromWindow(WindowHandle* handle);
100 
102 void popup(std::string_view message, std::string_view title, unsigned int popupFlags, std::string_view ok = "OK");
104 void popupInfo(std::string_view message, std::string_view title = "Info");
106 void popupWarning(std::string_view message, std::string_view title = "Warning");
108 void popupError(std::string_view message, std::string_view title = "Error");
109 
111 bool popupChoice(std::string_view message, std::string_view title, unsigned int popupFlags, std::string_view ok = "OK", std::string_view cancel = "Cancel");
114 bool popupInfoChoice(std::string_view message, std::string_view title = "Info");
117 bool popupWarningChoice(std::string_view message, std::string_view title = "Warning");
120 bool popupErrorChoice(std::string_view message, std::string_view title = "Error");
121 
122 } // namespace chira::Device