@@ -108,22 +108,30 @@ CEnetObject enet;
108
108
109
109
int mouse_x = 0 , mouse_y = 0 , mouse_z = 0 ;
110
110
bool g_engineInitialized = false ;
111
- SDL_Event e ;
111
+ SDL_Event evt ;
112
112
Poco::Event g_windowEvent;
113
+
114
+ enum WindowEvent : asQWORD {
115
+ NONE = 0 ,
116
+ SHOW = 1 << 0 ,
117
+ HIDE = 1 << 1 ,
118
+ SET_TITLE = 1 << 2 ,
119
+ FULLSCREEN = 1 << 3 ,
120
+ KEYBOARD_RESET = 1 << 4 ,
121
+ PRESENT = 1 << 5 ,
122
+ PUSH = 1 << 6
123
+ };
124
+
125
+ asQWORD g_windowEventState = NONE;
126
+
127
+
113
128
bool window_is_focused = false ;
114
- bool window_event_show = false ;
115
- bool window_event_hide = false ;
116
- bool window_event_set_title = false ;
117
- bool window_event_push = false ;
118
- bool window_event_keyboard_reset = false ;
119
- bool window_event_fullscreen = false ;
120
129
bool window_fullscreen = false ;
121
130
long update_window_freq = 5 ;
122
131
const char * window_title = nullptr ;
123
132
int window_w, window_h;
124
133
bool window_thread_event_shutdown = false ;
125
134
bool default_screen_reader_interrupt = false ;
126
- bool window_event_present = false ;
127
135
bool window_has_renderer = false ;// We can't create window in other thread, if we need graphics
128
136
#ifdef _WIN32
129
137
wstring wstr (const string& utf8String)
@@ -136,7 +144,7 @@ wstring wstr(const string& utf8String)
136
144
std::array<DeviceButton, 512 > keys;
137
145
std::array<DeviceButton, 8 >buttons;
138
146
bool keyhook = false ;
139
- string inputtext ;
147
+ string g_inputText ;
140
148
int get_cpu_count () {
141
149
return SDL_GetNumLogicalCPUCores ();
142
150
}
@@ -180,12 +188,10 @@ bool wait_event_requested = false;
180
188
bool g_quitRequested = false ;
181
189
182
190
class WindowThread : public Poco ::Runnable {
183
- private:
184
- Poco::Thread thread;
185
191
public:
192
+ Poco::Thread thread;
186
193
SDL_Window* win = nullptr ;
187
194
SDL_Renderer* renderer = nullptr ;
188
- int thread_id = 0 ;
189
195
void start () {
190
196
thread.start (*this );
191
197
}
@@ -201,8 +207,8 @@ class WindowThread : public Poco::Runnable {
201
207
}
202
208
}
203
209
void monitor () {
204
- if (window_event_show ) {
205
- window_event_show = false ;
210
+ if (g_windowEventState & WindowEvent::SHOW ) {
211
+ g_windowEventState &= ~WindowEvent::SHOW ;
206
212
SDL_WindowFlags flags = 0 ;
207
213
if (SRAL_GetCurrentEngine () == ENGINE_JAWS) {
208
214
SDL_SetHint (SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, " 1" );
@@ -218,19 +224,19 @@ class WindowThread : public Poco::Runnable {
218
224
renderer = SDL_CreateRenderer (win, nullptr );
219
225
}
220
226
}
221
- if (window_event_hide ) {
222
- window_event_hide = false ;
227
+ if (g_windowEventState & WindowEvent::HIDE ) {
228
+ g_windowEventState &= ~WindowEvent::HIDE ;
223
229
SDL_DestroyRenderer (renderer);
224
230
SDL_DestroyWindow (win);
225
231
win = nullptr ;
226
232
}
227
- if (window_event_set_title ) {
228
- window_event_set_title = false ;
233
+ if (g_windowEventState & WindowEvent::SET_TITLE ) {
234
+ g_windowEventState &= ~WindowEvent::SET_TITLE ;
229
235
if (win == nullptr )return ;
230
236
SDL_SetWindowTitle (win, window_title);
231
237
}
232
- if (window_event_fullscreen ) {
233
- window_event_fullscreen = false ;
238
+ if (g_windowEventState & WindowEvent::FULLSCREEN ) {
239
+ g_windowEventState &= ~WindowEvent::FULLSCREEN ;
234
240
if (win == nullptr )return ;
235
241
SDL_SetWindowFullscreen (win, window_fullscreen);
236
242
}
@@ -239,67 +245,68 @@ class WindowThread : public Poco::Runnable {
239
245
bool result;
240
246
if (window_has_renderer && wait_event_requested) {
241
247
wait_event_requested = false ;
242
- result = SDL_WaitEvent (&e );
248
+ result = SDL_WaitEvent (&evt );
243
249
}
244
250
else
245
- result = SDL_PollEvent (&e );
251
+ result = SDL_PollEvent (&evt );
246
252
if (result == true ) {
247
253
if (!window_has_renderer) {
248
254
g_windowEvent.set ();
249
255
}
250
256
}
251
- if (window_event_push ) {
252
- window_event_push = false ;
253
- SDL_PushEvent (&e );
257
+ if (g_windowEventState & WindowEvent::PUSH ) {
258
+ g_windowEventState &= ~WindowEvent::PUSH ;
259
+ SDL_PushEvent (&evt );
254
260
}
255
- if (window_event_present ) {
256
- window_event_present = false ;
261
+ if (g_windowEventState & WindowEvent::PRESENT ) {
262
+ g_windowEventState &= ~WindowEvent::PRESENT ;
257
263
SDL_RenderPresent (renderer);
258
264
}
259
- if (window_event_keyboard_reset ) {
260
- window_event_keyboard_reset = false ;
265
+ if (g_windowEventState & WindowEvent::KEYBOARD_RESET ) {
266
+ g_windowEventState &= ~WindowEvent::KEYBOARD_RESET ;
261
267
SDL_ResetKeyboard ();
262
268
}
263
- if (e.type == SDL_EVENT_QUIT) {
269
+ switch (evt.type ) {
270
+ case SDL_EVENT_QUIT:
264
271
g_quitRequested = true ;
265
- }
266
- if (e.type == SDL_EVENT_TEXT_INPUT)
267
- {
268
- inputtext += e.text .text ;
269
- }
270
-
271
- if (e.type == SDL_EVENT_KEY_DOWN)
272
- {
273
- keys[e.key .scancode ].isDown = true ;
274
- keys[e.key .scancode ].isReleased = false ;
275
- }
276
- if (e.type == SDL_EVENT_KEY_UP)
277
- {
278
- keys[e.key .scancode ].isDown = false ;
279
- keys[e.key .scancode ].isPressed = false ;
280
- }
281
- if (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
282
- buttons[e.button .button ].isDown = true ;
283
- buttons[e.button .button ].isReleased = false ;
284
- }
285
- if (e.type == SDL_EVENT_MOUSE_BUTTON_UP) {
286
- buttons[e.button .button ].isDown = false ;
287
- buttons[e.button .button ].isPressed = false ;
288
- }
289
-
290
-
291
- if (e.type == SDL_EVENT_MOUSE_MOTION) {
292
- mouse_x = e.motion .x ;
293
- mouse_y = e.motion .y ;
294
- }
295
- if (e.type == SDL_EVENT_MOUSE_WHEEL) {
296
- mouse_z += e.wheel .y ;
297
- }
298
- if (e.type == SDL_EVENT_WINDOW_FOCUS_GAINED)
272
+ break ;
273
+ case SDL_EVENT_TEXT_INPUT:
274
+ g_inputText += evt.text .text ;
275
+ break ;
276
+ case SDL_EVENT_KEY_DOWN:
277
+ keys[evt.key .scancode ].isDown = true ;
278
+ keys[evt.key .scancode ].isReleased = false ;
279
+ break ;
280
+ case SDL_EVENT_KEY_UP:
281
+ keys[evt.key .scancode ].isDown = false ;
282
+ keys[evt.key .scancode ].isPressed = false ;
283
+ break ;
284
+ case SDL_EVENT_MOUSE_BUTTON_DOWN:
285
+ buttons[evt.button .button ].isDown = true ;
286
+ buttons[evt.button .button ].isReleased = false ;
287
+ break ;
288
+ case SDL_EVENT_MOUSE_BUTTON_UP:
289
+ buttons[evt.button .button ].isDown = false ;
290
+ buttons[evt.button .button ].isPressed = false ;
291
+ break ;
292
+
293
+ case SDL_EVENT_MOUSE_MOTION:
294
+ mouse_x = evt.motion .x ;
295
+ mouse_y = evt.motion .y ;
296
+ break ;
297
+ case SDL_EVENT_MOUSE_WHEEL:
298
+ mouse_z += evt.wheel .y ;
299
+ break ;
300
+ case SDL_EVENT_WINDOW_FOCUS_GAINED:
299
301
window_is_focused = true ;
300
- if (e.type == SDL_EVENT_WINDOW_FOCUS_LOST) {
301
- window_event_keyboard_reset = true ;
302
+ break ;
303
+ case SDL_EVENT_WINDOW_FOCUS_LOST:
304
+
305
+ g_windowEventState |= WindowEvent::KEYBOARD_RESET;
302
306
window_is_focused = false ;
307
+ break ;
308
+ default :
309
+ break ;
303
310
}
304
311
SDL_UpdateWindowSurface (win);
305
312
@@ -308,7 +315,6 @@ class WindowThread : public Poco::Runnable {
308
315
309
316
310
317
void run () {
311
- this ->thread_id = Thread::currentOsTid ();
312
318
while (!window_thread_event_shutdown) {
313
319
thread.sleep (update_window_freq + 1 );
314
320
this ->monitor ();
@@ -402,13 +408,12 @@ bool show_window(const string& title, int width, int height, bool enable_render)
402
408
window_w = width;
403
409
window_h = height;
404
410
// Starting window
405
- window_event_show = true ;
411
+ g_windowEventState |= WindowEvent::SHOW ;
406
412
// Initialize mouse and keyboard state.
407
413
keys_released ();
408
414
for (int i = 0 ; i < 8 ; i++) {
409
415
mouse_released (i);
410
416
}
411
- window_event_keyboard_reset = true ;
412
417
if (enable_render)windowRunnable->monitor ();
413
418
else {
414
419
while (windowRunnable->win == nullptr );
@@ -458,7 +463,7 @@ void* get_window_handle() {
458
463
return nullptr ;
459
464
}
460
465
void hide_window () {
461
- window_event_hide = true ;
466
+ g_windowEventState |= WindowEvent::HIDE ;
462
467
window_thread_event_shutdown = true ;
463
468
wait (20 );
464
469
windowRunnable->stop ();
@@ -469,29 +474,22 @@ void hide_window() {
469
474
}
470
475
void set_window_title (const string& new_title) {
471
476
window_title = new_title.c_str ();
472
- window_event_set_title = true ;
477
+ g_windowEventState |= WindowEvent::SET_TITLE ;
473
478
if (window_has_renderer)windowRunnable->monitor ();
474
479
}
475
480
SDL_Renderer* get_window_renderer () {
476
481
if (windowRunnable != nullptr )return windowRunnable->renderer ;
477
482
return nullptr ;
478
483
}
479
484
void window_present () {
480
- window_event_present = true ;
485
+ g_windowEventState |= WindowEvent::PRESENT ;
481
486
if (window_has_renderer)windowRunnable->monitor ();
482
487
}
483
488
void garbage_collect () {
484
489
asIScriptContext* ctx = asGetActiveContext ();
485
490
asIScriptEngine* engine = ctx->GetEngine ();
486
491
engine->GarbageCollect (1 | 2 , 1 );
487
492
}
488
- SDL_Surface* get_window_surface () {
489
- if (windowRunnable == nullptr || windowRunnable->win == nullptr )return NULL ;
490
- return SDL_GetWindowSurface (windowRunnable->win );
491
- }
492
- SDL_Surface* load_bmp (const string& file) {
493
- return SDL_LoadBMP (file.c_str ());
494
- }
495
493
string key_to_string (SDL_Scancode key) {
496
494
return string (SDL_GetScancodeName (key));
497
495
}
@@ -503,7 +501,7 @@ bool get_window_active() {
503
501
}
504
502
void set_window_fullscreen (bool fullscreen) {
505
503
window_fullscreen = fullscreen;
506
- window_event_fullscreen = true ;
504
+ g_windowEventState |= WindowEvent::FULLSCREEN ;
507
505
if (window_has_renderer)windowRunnable->monitor ();
508
506
}
509
507
bool mouse_pressed (unsigned char button)
@@ -683,8 +681,8 @@ string clipboard_read_text() {
683
681
return SDL_GetClipboardText ();
684
682
}
685
683
string get_input () {
686
- string temp = inputtext ;
687
- inputtext = " " ;
684
+ string temp = g_inputText ;
685
+ g_inputText = " " ;
688
686
return temp;
689
687
}
690
688
string input_box (const string& title, const string& text, const string& default_text, bool secure, bool multiline) {
@@ -730,13 +728,13 @@ string input_box(const string& title, const string& text, const string& default_
730
728
return " " ;
731
729
}
732
730
goto destroy;
733
- return " " ;
734
- destroy:
731
+ destroy: {
735
732
gui::delete_control (edit);
736
733
gui::delete_control (ok);
737
734
gui::delete_control (cancel);
738
735
gui::hide_window (main_window);
739
- return " " ;
736
+ }
737
+ return " " ;
740
738
#else
741
739
std::cout << title << " : " << text << std::endl << default_text;
742
740
std::string result;
@@ -771,30 +769,33 @@ bool key_down(int key_code)
771
769
}
772
770
bool key_repeat (int key_code)
773
771
{
774
- if (key_pressed (key_code) || (e .key .scancode == key_code && e .key .repeat == true )) {
772
+ if (key_pressed (key_code) || (evt .key .scancode == key_code && evt .key .repeat == true )) {
775
773
return true ;
776
774
}
777
775
return false ;
778
776
}
779
777
bool force_key_down (SDL_Scancode keycode) {
780
- e.type = SDL_EVENT_KEY_DOWN;
781
- e.key .scancode = keycode;
782
- keys[e.key .scancode ].isDown = true ;
783
- window_event_push = true ;
778
+ evt.type = SDL_EVENT_KEY_DOWN;
779
+ evt.key .scancode = keycode;
780
+ keys[evt.key .scancode ].isDown = true ;
781
+ g_windowEventState |= WindowEvent::PUSH;
782
+ if (!window_has_renderer && windowRunnable != nullptr )windowRunnable->monitor ();
784
783
return key_down (keycode);
785
784
}
786
785
bool force_key_up (SDL_Scancode keycode) {
787
- e.type = SDL_EVENT_KEY_UP;
788
- e.key .scancode = keycode;
789
- keys[e.key .scancode ].isDown = false ;
790
- window_event_push = true ;
786
+ evt.type = SDL_EVENT_KEY_UP;
787
+ evt.key .scancode = keycode;
788
+ keys[evt.key .scancode ].isDown = false ;
789
+ g_windowEventState |= WindowEvent::PUSH;
790
+ if (!window_has_renderer && windowRunnable != nullptr )windowRunnable->monitor ();
791
791
return !key_down (keycode);
792
792
}
793
793
void reset_keyboard () {
794
794
for (int i = 0 ; i < 512 ; i++) {
795
795
keys[i].isDown = false ;
796
796
}
797
- window_event_keyboard_reset = true ;
797
+ g_windowEventState |= WindowEvent::PUSH;
798
+ if (!window_has_renderer && windowRunnable != nullptr )windowRunnable->monitor ();
798
799
}
799
800
bool quit_requested () {
800
801
if (g_quitRequested) {
@@ -1077,22 +1078,23 @@ int message_box_script(const std::string& title, const std::string& text, CScrip
1077
1078
1078
1079
bool alert (const string& title, const string& text, const string& button_name)
1079
1080
{
1080
- return message_box (title, text, { " `OK " }, 0 ) == 0 ;
1081
+ return message_box (title, text, { button_name }, 0 ) == 0 ;
1081
1082
}
1082
1083
int question (const string& title, const string& text) {
1083
1084
return message_box (title, text, { " `Yes" , " ~No" }, 0 );
1084
1085
}
1085
1086
1087
+
1086
1088
timer g_windowUpdater;
1087
1089
void wait (uint64_t time) {
1088
- if (!window_has_renderer) {
1089
- std::this_thread::sleep_for ( std::chrono::milliseconds ( time ) );
1090
+ if (windowRunnable == nullptr || !window_has_renderer || windowRunnable-> thread . currentOsTid () != Poco::Thread::currentOsTid () ) {
1091
+ Poco::Thread::sleep ( time );
1090
1092
return ;
1091
1093
}
1092
1094
timer t;
1093
1095
for (int64_t i = 0 ; i < time ; ++i) {
1094
1096
if (t.elapsed_millis () > time )break ;
1095
- std::this_thread::sleep_for ( std::chrono::milliseconds ( 1 ) );
1097
+ Poco::Thread::sleep ( 1 );
1096
1098
if (windowRunnable != nullptr && g_windowUpdater.elapsed_millis () > update_window_freq) {
1097
1099
windowRunnable->monitor ();
1098
1100
g_windowUpdater.restart ();
0 commit comments