This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathfrt_godot3.cc
434 lines (425 loc) · 12.4 KB
/
frt_godot3.cc
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// frt_godot3.cc
/*
FRT - A Godot platform targeting single board computers
Copyright (c) 2017-2023 Emanuele Fornara
SPDX-License-Identifier: MIT
*/
#include "frt.h"
#include "sdl2_adapter.h"
#include "sdl2_godot_map_2_3.h"
#include "drivers/gles3/rasterizer_gles3.h"
#define FRT_DL_SKIP
#include "drivers/gles2/rasterizer_gles2.h"
#include "core/print_string.h"
#include "drivers/unix/os_unix.h"
#pragma GCC diagnostic ignored "-Wvolatile"
#include "servers/audio_server.h"
#pragma GCC diagnostic pop
#include "servers/visual_server.h"
#include "servers/visual/visual_server_wrap_mt.h"
#include "servers/visual/rasterizer.h"
#include "servers/visual/visual_server_raster.h"
#include "main/main.h"
namespace frt {
static const char *default_audio_device = "default"; // TODO
class AudioDriverSDL2 : public AudioDriver, public SampleProducer {
private:
Audio audio_;
int mix_rate_;
SpeakerMode speaker_mode_;
public:
AudioDriverSDL2() : audio_(this) {
}
public: // AudioDriverSW
const char *get_name() const FRT_OVERRIDE {
return "SDL2";
}
Error init() FRT_OVERRIDE {
mix_rate_ = GLOBAL_GET("audio/mix_rate");
speaker_mode_ = SPEAKER_MODE_STEREO;
const int latency = GLOBAL_GET("audio/output_latency");
const int samples = closest_power_of_2(latency * mix_rate_ / 1000);
return audio_.init(mix_rate_, samples) ? OK : ERR_CANT_OPEN;
}
int get_mix_rate() const FRT_OVERRIDE {
return mix_rate_;
}
SpeakerMode get_speaker_mode() const FRT_OVERRIDE {
return speaker_mode_;
}
Array get_device_list() FRT_OVERRIDE {
Array list;
list.push_back(default_audio_device);
return list;
}
String get_device() FRT_OVERRIDE {
return default_audio_device;
}
void set_device(String device) FRT_OVERRIDE {
}
void start() FRT_OVERRIDE {
audio_.start();
}
void lock() FRT_OVERRIDE {
audio_.lock();
}
void unlock() FRT_OVERRIDE {
audio_.unlock();
}
void finish() FRT_OVERRIDE {
audio_.finish();
}
public: // SampleProducer
void produce_samples(int n_of_frames, int32_t *frames) FRT_OVERRIDE {
audio_server_process(n_of_frames, frames);
}
};
class Godot3_OS : public OS_Unix, public EventHandler {
private:
enum {
VIDEO_DRIVER_GLES2,
VIDEO_DRIVER_GLES3
};
MainLoop *main_loop_;
VideoMode video_mode_;
bool quit_;
OS_FRT os_;
int video_driver_;
VisualServer *visual_server_;
void init_video() {
if (video_driver_ == VIDEO_DRIVER_GLES2) {
frt_resolve_symbols_gles2(get_proc_address);
RasterizerGLES2::register_config();
RasterizerGLES2::make_current();
} else {
frt_resolve_symbols_gles3(get_proc_address);
RasterizerGLES3::register_config();
RasterizerGLES3::make_current();
}
visual_server_ = memnew(VisualServerRaster);
visual_server_->init();
}
void cleanup_video() {
visual_server_->finish();
memdelete(visual_server_);
}
AudioDriverSDL2 audio_driver_;
void init_audio(int id) {
AudioDriverManager::initialize(id);
}
void cleanup_audio() {
}
InputDefault *input_;
Point2 mouse_pos_;
int mouse_state_;
void init_input() {
input_ = memnew(InputDefault);
mouse_pos_ = Point2(-1, -1);
mouse_state_ = 0;
}
void cleanup_input() {
memdelete(input_);
}
void fill_modifier_state(Ref<InputEventWithModifiers> st) {
const InputModifierState *os_st = os_.get_modifier_state();
st->set_shift(os_st->shift);
st->set_alt(os_st->alt);
st->set_control(os_st->control);
st->set_metakey(os_st->meta);
}
public:
Godot3_OS() : os_(this) {
AudioDriverManager::add_driver(&audio_driver_);
main_loop_ = 0;
quit_ = false;
}
void run() {
if (main_loop_) {
main_loop_->init();
while (!quit_ && !Main::iteration())
os_.dispatch_events();
main_loop_->finish();
}
}
public: // OS
int get_video_driver_count() const FRT_OVERRIDE {
return 2;
}
#if FRT_GODOT_VERSION >= 30100
int get_current_video_driver() const FRT_OVERRIDE {
return video_driver_;
}
#endif
const char *get_video_driver_name(int driver) const FRT_OVERRIDE {
return driver == VIDEO_DRIVER_GLES3 ? "GLES3" : "GLES2";
}
bool _check_internal_feature_support(const String &feature) FRT_OVERRIDE {
if (video_driver_ == VIDEO_DRIVER_GLES3 && feature == "etc2")
return true;
return feature == "mobile" || feature == "etc";
}
String get_config_path() const FRT_OVERRIDE {
if (has_environment("XDG_CONFIG_HOME"))
return get_environment("XDG_CONFIG_HOME");
if (has_environment("HOME"))
return get_environment("HOME").plus_file(".config");
return ".";
}
String get_data_path() const FRT_OVERRIDE {
if (has_environment("XDG_DATA_HOME"))
return get_environment("XDG_DATA_HOME");
if (has_environment("HOME"))
return get_environment("HOME").plus_file(".local/share");
return get_config_path();
}
String get_cache_path() const FRT_OVERRIDE {
if (has_environment("XDG_CACHE_HOME"))
return get_environment("XDG_CACHE_HOME");
if (has_environment("HOME"))
return get_environment("HOME").plus_file(".cache");
return get_config_path();
}
Error initialize(const VideoMode &desired, int video_driver, int audio_driver) FRT_OVERRIDE {
video_mode_ = desired;
video_driver_ = video_driver;
const GraphicsAPI api = video_driver == VIDEO_DRIVER_GLES2 ? API_OpenGL_ES2 : API_OpenGL_ES3;
os_.init_gl(api, video_mode_.width, video_mode_.height, video_mode_.resizable, video_mode_.borderless_window, video_mode_.always_on_top);
_set_use_vsync(video_mode_.use_vsync);
init_video();
init_audio(audio_driver);
init_input();
#if FRT_GODOT_VERSION < 30300
_ensure_user_data_dir();
#endif
return OK;
}
void set_main_loop(MainLoop *main_loop) FRT_OVERRIDE {
main_loop_ = main_loop;
input_->set_main_loop(main_loop);
}
void delete_main_loop() FRT_OVERRIDE {
if (main_loop_)
memdelete(main_loop_);
main_loop_ = 0;
}
void finalize() FRT_OVERRIDE {
delete_main_loop();
cleanup_input();
cleanup_audio();
cleanup_video();
os_.cleanup();
}
Point2 get_mouse_position() const FRT_OVERRIDE {
return mouse_pos_;
}
int get_mouse_button_state() const FRT_OVERRIDE {
return mouse_state_;
}
void set_mouse_mode(OS::MouseMode mode) FRT_OVERRIDE {
os_.set_mouse_mode(map_mouse_mode(mode));
}
OS::MouseMode get_mouse_mode() const FRT_OVERRIDE {
return map_mouse_os_mode(os_.get_mouse_mode());
}
void set_window_title(const String &title) FRT_OVERRIDE {
os_.set_title(title.utf8().get_data());
}
void set_video_mode(const VideoMode &video_mode, int screen) FRT_OVERRIDE {
}
VideoMode get_video_mode(int screen = 0) const FRT_OVERRIDE {
return video_mode_;
}
void get_fullscreen_mode_list(List<VideoMode> *list, int screen) const FRT_OVERRIDE {
}
Size2 get_window_size() const FRT_OVERRIDE {
return Size2(video_mode_.width, video_mode_.height);
}
void set_window_size(const Size2 size) FRT_OVERRIDE {
ivec2 os_size = { (int)size.width, (int)size.height };
os_.set_size(os_size);
video_mode_.width = os_size.x;
video_mode_.height = os_size.y;
}
Point2 get_window_position() const FRT_OVERRIDE {
ivec2 pos = os_.get_pos();
return Point2(pos.x, pos.y);
}
void set_window_position(const Point2 &pos) FRT_OVERRIDE {
ivec2 os_pos = { (int)pos.width, (int)pos.height };
os_.set_pos(os_pos);
}
void set_window_fullscreen(bool enable) FRT_OVERRIDE {
os_.set_fullscreen(enable);
video_mode_.fullscreen = enable;
}
bool is_window_fullscreen() const FRT_OVERRIDE {
return os_.is_fullscreen();
}
void set_window_always_on_top(bool enable) FRT_OVERRIDE {
os_.set_always_on_top(enable);
video_mode_.always_on_top = enable;
}
bool is_window_always_on_top() const FRT_OVERRIDE {
return os_.is_always_on_top();
}
void set_window_resizable(bool enable) FRT_OVERRIDE {
os_.set_resizable(enable);
}
bool is_window_resizable() const FRT_OVERRIDE {
return os_.is_resizable();
}
void set_window_maximized(bool enable) FRT_OVERRIDE {
os_.set_maximized(enable);
}
bool is_window_maximized() const FRT_OVERRIDE {
return os_.is_maximized();
}
void set_window_minimized(bool enable) FRT_OVERRIDE {
os_.set_minimized(enable);
}
bool is_window_minimized() const FRT_OVERRIDE {
return os_.is_minimized();
}
MainLoop *get_main_loop() const FRT_OVERRIDE {
return main_loop_;
}
bool can_draw() const FRT_OVERRIDE {
return os_.can_draw();
}
void set_cursor_shape(CursorShape shape) FRT_OVERRIDE {
}
void set_custom_mouse_cursor(const RES &cursor, CursorShape shape, const Vector2 &hotspot) FRT_OVERRIDE {
}
void make_rendering_thread() FRT_OVERRIDE {
os_.make_current_gl();
}
void release_rendering_thread() FRT_OVERRIDE {
os_.release_current_gl();
}
void swap_buffers() FRT_OVERRIDE {
os_.swap_buffers_gl();
}
void _set_use_vsync(bool enable) FRT_OVERRIDE {
os_.set_use_vsync_gl(enable);
}
void set_icon(const Ref<Image> &icon) FRT_OVERRIDE {
if (icon.is_null())
return;
Ref<Image> i = icon->duplicate();
i->convert(Image::FORMAT_RGBA8);
PoolVector<uint8_t>::Read r = i->get_data().read();
os_.set_icon(i->get_width(), i->get_height(), r.ptr());
}
Size2 get_screen_size(int screen) const FRT_OVERRIDE {
ivec2 size = os_.get_screen_size();
return Size2(size.x, size.y);
}
int get_screen_dpi(int screen) const FRT_OVERRIDE {
return os_.get_screen_dpi();
}
#if FRT_GODOT_VERSION >= 30500
float get_screen_refresh_rate(int screen) const FRT_OVERRIDE {
return os_.get_screen_refresh_rate();
}
#endif
public: // EventHandler
void handle_resize_event(ivec2 size) FRT_OVERRIDE {
video_mode_.width = size.x;
video_mode_.height = size.y;
}
void handle_key_event(int sdl2_code, int unicode, bool pressed) FRT_OVERRIDE {
int code = map_key_sdl2_code(sdl2_code);
Ref<InputEventKey> key;
key.instance();
fill_modifier_state(key);
key->set_pressed(pressed);
key->set_scancode(code);
#if FRT_GODOT_VERSION >= 30400
key->set_physical_scancode(code); // TODO
#endif
key->set_unicode(unicode);
key->set_echo(false);
input_->parse_input_event(key);
}
void handle_mouse_motion_event(ivec2 pos, ivec2 dpos) FRT_OVERRIDE {
mouse_pos_.x = pos.x;
mouse_pos_.y = pos.y;
Ref<InputEventMouseMotion> mouse_motion;
mouse_motion.instance();
fill_modifier_state(mouse_motion);
Point2i posi(pos.x, pos.y);
mouse_motion->set_button_mask(mouse_state_);
mouse_motion->set_position(posi);
mouse_motion->set_global_position(posi);
input_->set_mouse_position(posi);
mouse_motion->set_speed(input_->get_last_mouse_speed());
Point2i reli(dpos.x, dpos.y);
mouse_motion->set_relative(reli);
input_->parse_input_event(mouse_motion);
}
void handle_mouse_button_event(int os_button, bool pressed, bool doubleclick) FRT_OVERRIDE {
int button = map_mouse_os_button(os_button);
int bit = (1 << (button - 1));
if (pressed)
mouse_state_ |= bit;
else
mouse_state_ &= ~bit;
Ref<InputEventMouseButton> mouse_button;
mouse_button.instance();
fill_modifier_state(mouse_button);
Point2i posi(mouse_pos_.x, mouse_pos_.y);
mouse_button->set_position(posi);
mouse_button->set_global_position(posi);
mouse_button->set_button_index(button);
mouse_button->set_button_mask(mouse_state_);
mouse_button->set_doubleclick(doubleclick);
mouse_button->set_pressed(pressed);
input_->parse_input_event(mouse_button);
}
void handle_js_status_event(int id, bool connected, const char *name, const char *guid) FRT_OVERRIDE {
input_->joy_connection_changed(id, connected, name, guid);
}
void handle_js_button_event(int id, int button, bool pressed) FRT_OVERRIDE {
input_->joy_button(id, button, pressed ? 1 : 0);
}
void handle_js_axis_event(int id, int axis, float value) FRT_OVERRIDE {
#if FRT_GODOT_VERSION >= 30500
input_->joy_axis(id, axis, value);
#else
InputDefault::JoyAxis v = { -1, value };
input_->joy_axis(id, axis, v);
#endif
}
void handle_js_hat_event(int id, int os_mask) FRT_OVERRIDE {
int mask = map_hat_os_mask(os_mask);
input_->joy_hat(id, mask);
}
void handle_js_vibra_event(int id, uint64_t timestamp) FRT_OVERRIDE {
uint64_t input_timestamp = input_->get_joy_vibration_timestamp(id);
if (input_timestamp > timestamp) {
Vector2 strength = input_->get_joy_vibration_strength(id);
float duration = input_->get_joy_vibration_duration(id);
os_.js_vibra(id, strength.x, strength.y, duration, input_timestamp);
}
}
void handle_quit_event() FRT_OVERRIDE {
quit_ = true;
}
void handle_flush_events() FRT_OVERRIDE {
#if FRT_GODOT_VERSION >= 30400
input_->flush_buffered_events();
#endif
}
};
} // namespace frt
#include "frt_lib.h"
extern "C" int frt_godot_main(int argc, char *argv[]) {
frt::Godot3_OS os;
Error err = Main::setup(argv[0], argc - 1, &argv[1]);
if (err != OK)
return 255;
if (Main::start())
os.run();
Main::cleanup();
return os.get_exit_code();
}