Skip to content
This repository was archived by the owner on May 26, 2025. It is now read-only.

Commit 9eb8ad2

Browse files
committed
Refactor AI components.
1 parent c767730 commit 9eb8ad2

File tree

4 files changed

+104
-101
lines changed

4 files changed

+104
-101
lines changed

Release/Tests/WindowThread/WindowInput.ngt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ if(wait_time.elapsed_millis>= 8000)
1313
{
1414
wait_time.pause();
1515
alert("Thanks", "Thanks for waiting for me. You typed the following characters within 8 seconds: " + get_input() + ". Goodbye.");
16+
break;
1617
}
1718
}
1819
}

SRC/ngt.cpp

Lines changed: 101 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,30 @@ CEnetObject enet;
108108

109109
int mouse_x = 0, mouse_y = 0, mouse_z = 0;
110110
bool g_engineInitialized = false;
111-
SDL_Event e;
111+
SDL_Event evt;
112112
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+
113128
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;
120129
bool window_fullscreen = false;
121130
long update_window_freq = 5;
122131
const char* window_title = nullptr;
123132
int window_w, window_h;
124133
bool window_thread_event_shutdown = false;
125134
bool default_screen_reader_interrupt = false;
126-
bool window_event_present = false;
127135
bool window_has_renderer = false;// We can't create window in other thread, if we need graphics
128136
#ifdef _WIN32
129137
wstring wstr(const string& utf8String)
@@ -136,7 +144,7 @@ wstring wstr(const string& utf8String)
136144
std::array<DeviceButton, 512> keys;
137145
std::array<DeviceButton, 8>buttons;
138146
bool keyhook = false;
139-
string inputtext;
147+
string g_inputText;
140148
int get_cpu_count() {
141149
return SDL_GetNumLogicalCPUCores();
142150
}
@@ -180,12 +188,10 @@ bool wait_event_requested = false;
180188
bool g_quitRequested = false;
181189

182190
class WindowThread : public Poco::Runnable {
183-
private:
184-
Poco::Thread thread;
185191
public:
192+
Poco::Thread thread;
186193
SDL_Window* win = nullptr;
187194
SDL_Renderer* renderer = nullptr;
188-
int thread_id = 0;
189195
void start() {
190196
thread.start(*this);
191197
}
@@ -201,8 +207,8 @@ class WindowThread : public Poco::Runnable {
201207
}
202208
}
203209
void monitor() {
204-
if (window_event_show) {
205-
window_event_show = false;
210+
if (g_windowEventState & WindowEvent::SHOW) {
211+
g_windowEventState &= ~WindowEvent::SHOW;
206212
SDL_WindowFlags flags = 0;
207213
if (SRAL_GetCurrentEngine() == ENGINE_JAWS) {
208214
SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "1");
@@ -218,19 +224,19 @@ class WindowThread : public Poco::Runnable {
218224
renderer = SDL_CreateRenderer(win, nullptr);
219225
}
220226
}
221-
if (window_event_hide) {
222-
window_event_hide = false;
227+
if (g_windowEventState & WindowEvent::HIDE) {
228+
g_windowEventState &= ~WindowEvent::HIDE;
223229
SDL_DestroyRenderer(renderer);
224230
SDL_DestroyWindow(win);
225231
win = nullptr;
226232
}
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;
229235
if (win == nullptr)return;
230236
SDL_SetWindowTitle(win, window_title);
231237
}
232-
if (window_event_fullscreen) {
233-
window_event_fullscreen = false;
238+
if (g_windowEventState & WindowEvent::FULLSCREEN) {
239+
g_windowEventState &= ~WindowEvent::FULLSCREEN;
234240
if (win == nullptr)return;
235241
SDL_SetWindowFullscreen(win, window_fullscreen);
236242
}
@@ -239,67 +245,68 @@ class WindowThread : public Poco::Runnable {
239245
bool result;
240246
if (window_has_renderer && wait_event_requested) {
241247
wait_event_requested = false;
242-
result = SDL_WaitEvent(&e);
248+
result = SDL_WaitEvent(&evt);
243249
}
244250
else
245-
result = SDL_PollEvent(&e);
251+
result = SDL_PollEvent(&evt);
246252
if (result == true) {
247253
if (!window_has_renderer) {
248254
g_windowEvent.set();
249255
}
250256
}
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);
254260
}
255-
if (window_event_present) {
256-
window_event_present = false;
261+
if (g_windowEventState & WindowEvent::PRESENT) {
262+
g_windowEventState &= ~WindowEvent::PRESENT;
257263
SDL_RenderPresent(renderer);
258264
}
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;
261267
SDL_ResetKeyboard();
262268
}
263-
if (e.type == SDL_EVENT_QUIT) {
269+
switch (evt.type) {
270+
case SDL_EVENT_QUIT:
264271
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:
299301
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;
302306
window_is_focused = false;
307+
break;
308+
default:
309+
break;
303310
}
304311
SDL_UpdateWindowSurface(win);
305312

@@ -308,7 +315,6 @@ class WindowThread : public Poco::Runnable {
308315

309316

310317
void run() {
311-
this->thread_id = Thread::currentOsTid();
312318
while (!window_thread_event_shutdown) {
313319
thread.sleep(update_window_freq + 1);
314320
this->monitor();
@@ -402,13 +408,12 @@ bool show_window(const string& title, int width, int height, bool enable_render)
402408
window_w = width;
403409
window_h = height;
404410
// Starting window
405-
window_event_show = true;
411+
g_windowEventState |= WindowEvent::SHOW;
406412
// Initialize mouse and keyboard state.
407413
keys_released();
408414
for (int i = 0; i < 8; i++) {
409415
mouse_released(i);
410416
}
411-
window_event_keyboard_reset = true;
412417
if (enable_render)windowRunnable->monitor();
413418
else {
414419
while (windowRunnable->win == nullptr);
@@ -458,7 +463,7 @@ void* get_window_handle() {
458463
return nullptr;
459464
}
460465
void hide_window() {
461-
window_event_hide = true;
466+
g_windowEventState |= WindowEvent::HIDE;
462467
window_thread_event_shutdown = true;
463468
wait(20);
464469
windowRunnable->stop();
@@ -469,29 +474,22 @@ void hide_window() {
469474
}
470475
void set_window_title(const string& new_title) {
471476
window_title = new_title.c_str();
472-
window_event_set_title = true;
477+
g_windowEventState |= WindowEvent::SET_TITLE;
473478
if (window_has_renderer)windowRunnable->monitor();
474479
}
475480
SDL_Renderer* get_window_renderer() {
476481
if (windowRunnable != nullptr)return windowRunnable->renderer;
477482
return nullptr;
478483
}
479484
void window_present() {
480-
window_event_present = true;
485+
g_windowEventState |= WindowEvent::PRESENT;
481486
if (window_has_renderer)windowRunnable->monitor();
482487
}
483488
void garbage_collect() {
484489
asIScriptContext* ctx = asGetActiveContext();
485490
asIScriptEngine* engine = ctx->GetEngine();
486491
engine->GarbageCollect(1 | 2, 1);
487492
}
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-
}
495493
string key_to_string(SDL_Scancode key) {
496494
return string(SDL_GetScancodeName(key));
497495
}
@@ -503,7 +501,7 @@ bool get_window_active() {
503501
}
504502
void set_window_fullscreen(bool fullscreen) {
505503
window_fullscreen = fullscreen;
506-
window_event_fullscreen = true;
504+
g_windowEventState |= WindowEvent::FULLSCREEN;
507505
if (window_has_renderer)windowRunnable->monitor();
508506
}
509507
bool mouse_pressed(unsigned char button)
@@ -683,8 +681,8 @@ string clipboard_read_text() {
683681
return SDL_GetClipboardText();
684682
}
685683
string get_input() {
686-
string temp = inputtext;
687-
inputtext = "";
684+
string temp = g_inputText;
685+
g_inputText = "";
688686
return temp;
689687
}
690688
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_
730728
return "";
731729
}
732730
goto destroy;
733-
return "";
734-
destroy:
731+
destroy: {
735732
gui::delete_control(edit);
736733
gui::delete_control(ok);
737734
gui::delete_control(cancel);
738735
gui::hide_window(main_window);
739-
return"";
736+
}
737+
return"";
740738
#else
741739
std::cout << title << ": " << text << std::endl << default_text;
742740
std::string result;
@@ -771,30 +769,33 @@ bool key_down(int key_code)
771769
}
772770
bool key_repeat(int key_code)
773771
{
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)) {
775773
return true;
776774
}
777775
return false;
778776
}
779777
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();
784783
return key_down(keycode);
785784
}
786785
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();
791791
return !key_down(keycode);
792792
}
793793
void reset_keyboard() {
794794
for (int i = 0; i < 512; i++) {
795795
keys[i].isDown = false;
796796
}
797-
window_event_keyboard_reset = true;
797+
g_windowEventState |= WindowEvent::PUSH;
798+
if (!window_has_renderer && windowRunnable != nullptr)windowRunnable->monitor();
798799
}
799800
bool quit_requested() {
800801
if (g_quitRequested) {
@@ -1077,22 +1078,23 @@ int message_box_script(const std::string& title, const std::string& text, CScrip
10771078

10781079
bool alert(const string& title, const string& text, const string& button_name)
10791080
{
1080-
return message_box(title, text, { "`OK" }, 0) == 0;
1081+
return message_box(title, text, { button_name }, 0) == 0;
10811082
}
10821083
int question(const string& title, const string& text) {
10831084
return message_box(title, text, { "`Yes", "~No" }, 0);
10841085
}
10851086

1087+
10861088
timer g_windowUpdater;
10871089
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);
10901092
return;
10911093
}
10921094
timer t;
10931095
for (int64_t i = 0; i < time; ++i) {
10941096
if (t.elapsed_millis() > time)break;
1095-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
1097+
Poco::Thread::sleep(1);
10961098
if (windowRunnable != nullptr && g_windowUpdater.elapsed_millis() > update_window_freq) {
10971099
windowRunnable->monitor();
10981100
g_windowUpdater.restart();

0 commit comments

Comments
 (0)