1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
#include "cru/platform/gui/sdl/UiApplication.h"
#include "cru/base/Base.h"
#include "cru/platform/graphics/Factory.h"
#include "cru/platform/gui/sdl/Base.h"
#include "cru/platform/gui/sdl/Clipboard.h"
#include "cru/platform/gui/sdl/Cursor.h"
#include "cru/platform/gui/sdl/Window.h"
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_timer.h>
#include <algorithm>
#include <chrono>
#include <functional>
#include <memory>
#include <optional>
namespace cru::platform::gui::sdl {
SdlUiApplication::SdlUiApplication(graphics::IGraphicsFactory* graphics_factory,
bool release_graphics_factory)
: graphics_factory_(graphics_factory),
release_graphics_factory_(release_graphics_factory),
quit_code_(0) {
CheckSdlReturn(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS));
empty_event_type_ = SDL_RegisterEvents(1);
CheckSdlReturn(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1));
CheckSdlReturn(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3));
CheckSdlReturn(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2));
CheckSdlReturn(SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE));
cursor_manager_ = std::make_unique<SdlCursorManager>();
clipboard_ = std::make_unique<SdlClipboard>();
}
SdlUiApplication::~SdlUiApplication() {
SDL_Quit();
delete_later_pool_.Clean();
if (release_graphics_factory_) {
delete graphics_factory_;
}
}
int SdlUiApplication::Run() {
while (true) {
auto timeout = timers_.NextTimeout(std::chrono::steady_clock::now());
SDL_Event event;
bool has_event = false;
if (timeout) {
if (*timeout == std::chrono::milliseconds::zero()) {
has_event = SDL_PollEvent(&event);
} else {
has_event = SDL_WaitEventTimeout(&event, timeout->count());
}
} else {
CheckSdlReturn(SDL_WaitEvent(&event));
has_event = true;
}
if (has_event) {
if (event.type == SDL_EVENT_QUIT) {
break;
}
// char buf[512];
// SDL_GetEventDescription(&event, buf, sizeof(buf) / sizeof(*buf));
// CruLogDebug(kLogTag, "{}", buf);
DispatchEvent(event);
} else {
if (auto result = timers_.Update(std::chrono::steady_clock::now())) {
result->data();
}
}
delete_later_pool_.Clean();
}
for (const auto& handler : this->quit_handlers_) {
handler();
}
return quit_code_;
}
void SdlUiApplication::RequestQuit(int quit_code) {
quit_code_ = quit_code;
SDL_Event event;
event.type = SDL_EVENT_QUIT;
event.quit.timestamp = SDL_GetTicksNS();
CheckSdlReturn(SDL_PushEvent(&event));
}
void SdlUiApplication::AddOnQuitHandler(std::function<void()> handler) {
this->quit_handlers_.push_back(std::move(handler));
}
bool SdlUiApplication::IsQuitOnAllWindowClosed() {
return is_quit_on_all_window_closed_;
}
void SdlUiApplication::SetQuitOnAllWindowClosed(
bool quit_on_all_window_closed) {
is_quit_on_all_window_closed_ = quit_on_all_window_closed;
}
long long SdlUiApplication::SetImmediate(std::function<void()> action) {
return SetTimeout(std::chrono::milliseconds::zero(), std::move(action));
}
long long SdlUiApplication::SetTimeout(std::chrono::milliseconds milliseconds,
std::function<void()> action) {
return SetTimer(milliseconds, std::move(action), false);
}
long long SdlUiApplication::SetInterval(std::chrono::milliseconds milliseconds,
std::function<void()> action) {
return SetTimer(milliseconds, std::move(action), true);
}
void SdlUiApplication::CancelTimer(long long id) {
return timers_.Remove(static_cast<int>(id));
}
void SdlUiApplication::DeleteLater(Object* object) {
delete_later_pool_.Add(object);
}
std::vector<INativeWindow*> SdlUiApplication::GetAllWindow() {
std::vector<INativeWindow*> windows(windows_.size());
std::ranges::copy(windows_, windows.begin());
return windows;
}
INativeWindow* SdlUiApplication::CreateWindow() { return new SdlWindow(this); }
cru::platform::graphics::IGraphicsFactory*
SdlUiApplication::GetGraphicsFactory() {
return graphics_factory_;
}
ICursorManager* SdlUiApplication::GetCursorManager() {
return cursor_manager_.get();
}
IClipboard* SdlUiApplication::GetClipboard() { return clipboard_.get(); }
IMenu* SdlUiApplication::GetApplicationMenu() { return nullptr; }
void SdlUiApplication::RegisterWindow(SdlWindow* window) {
windows_.push_back(window);
}
void SdlUiApplication::UnregisterWindow(SdlWindow* window) {
std::erase(windows_, window);
}
void SdlUiApplication::RunOnMainThread(std::function<void()> action) {
auto p = new std::function<void()>(std::move(action));
SDL_RunOnMainThread(
[](void* userdata) {
auto action = static_cast<std::function<void()>*>(userdata);
(*action)();
delete action;
},
p, false);
}
void SdlUiApplication::PostEmptyEvent() {
SDL_Event event;
SDL_zero(event);
event.type = empty_event_type_;
event.user.timestamp = SDL_GetTicksNS();
SDL_PushEvent(&event);
}
long long SdlUiApplication::SetTimer(std::chrono::milliseconds milliseconds,
std::function<void()> action,
bool repeat) {
PostEmptyEvent();
return timers_.Add(std::move(action), milliseconds, repeat);
}
bool SdlUiApplication::DispatchEvent(const SDL_Event& event) {
for (auto window : windows_) {
if (window->HandleEvent(&event)) {
return true;
}
}
return false;
}
} // namespace cru::platform::gui::sdl
|