Codechange: unroll the SDL2 main loop

This commit prepares for the next commit, as Emscripten needs to
have a way to trigger a single iteration of the main loop. To
keep the real changes more clear, this commit only unrolls the
loop, and makes no changes to the logic itself.
This commit is contained in:
Patric Stout 2020-12-06 20:38:34 +01:00 committed by Patric Stout
parent f2a93dba0d
commit 2da07f7615
2 changed files with 94 additions and 76 deletions

View File

@ -664,55 +664,16 @@ void VideoDriver_SDL::Stop()
} }
} }
void VideoDriver_SDL::MainLoop() void VideoDriver_SDL::LoopOnce()
{ {
uint32 cur_ticks = SDL_GetTicks();
uint32 last_cur_ticks = cur_ticks;
uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
uint32 mod; uint32 mod;
int numkeys; int numkeys;
const Uint8 *keys; const Uint8 *keys;
CheckPaletteAnim();
std::thread draw_thread;
std::unique_lock<std::recursive_mutex> draw_lock;
if (_draw_threaded) {
/* Initialise the mutex first, because that's the thing we *need*
* directly in the newly created thread. */
_draw_mutex = new std::recursive_mutex();
if (_draw_mutex == nullptr) {
_draw_threaded = false;
} else {
draw_lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
_draw_signal = new std::condition_variable_any();
_draw_continue = true;
_draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &DrawSurfaceToScreenThread);
/* Free the mutex if we won't be able to use it. */
if (!_draw_threaded) {
draw_lock.unlock();
draw_lock.release();
delete _draw_mutex;
delete _draw_signal;
_draw_mutex = nullptr;
_draw_signal = nullptr;
} else {
/* Wait till the draw mutex has started itself. */
_draw_signal->wait(*_draw_mutex);
}
}
}
DEBUG(driver, 1, "SDL2: using %sthreads", _draw_threaded ? "" : "no ");
for (;;) {
uint32 prev_cur_ticks = cur_ticks; // to check for wrapping uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
InteractiveRandom(); // randomness InteractiveRandom(); // randomness
while (PollEvent() == -1) {} while (PollEvent() == -1) {}
if (_exit_game) break; if (_exit_game) return;
mod = SDL_GetModState(); mod = SDL_GetModState();
keys = SDL_GetKeyboardState(&numkeys); keys = SDL_GetKeyboardState(&numkeys);
@ -779,6 +740,54 @@ void VideoDriver_SDL::MainLoop()
} }
} }
void VideoDriver_SDL::MainLoop()
{
cur_ticks = SDL_GetTicks();
last_cur_ticks = cur_ticks;
next_tick = cur_ticks + MILLISECONDS_PER_TICK;
CheckPaletteAnim();
if (_draw_threaded) {
/* Initialise the mutex first, because that's the thing we *need*
* directly in the newly created thread. */
_draw_mutex = new std::recursive_mutex();
if (_draw_mutex == nullptr) {
_draw_threaded = false;
} else {
draw_lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
_draw_signal = new std::condition_variable_any();
_draw_continue = true;
_draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &DrawSurfaceToScreenThread);
/* Free the mutex if we won't be able to use it. */
if (!_draw_threaded) {
draw_lock.unlock();
draw_lock.release();
delete _draw_mutex;
delete _draw_signal;
_draw_mutex = nullptr;
_draw_signal = nullptr;
} else {
/* Wait till the draw mutex has started itself. */
_draw_signal->wait(*_draw_mutex);
}
}
}
DEBUG(driver, 1, "SDL2: using %sthreads", _draw_threaded ? "" : "no ");
while (!_exit_game) {
LoopOnce();
}
MainLoopCleanup();
#endif
}
void VideoDriver_SDL::MainLoopCleanup()
{
if (_draw_mutex != nullptr) { if (_draw_mutex != nullptr) {
_draw_continue = false; _draw_continue = false;
/* Sending signal if there is no thread blocked /* Sending signal if there is no thread blocked

View File

@ -42,12 +42,21 @@ public:
const char *GetName() const override { return "sdl"; } const char *GetName() const override { return "sdl"; }
private: private:
int PollEvent(); int PollEvent();
void LoopOnce();
void MainLoopCleanup();
bool CreateMainSurface(uint w, uint h, bool resize); bool CreateMainSurface(uint w, uint h, bool resize);
/** /**
* This is true to indicate that keyboard input is in text input mode, and SDL_TEXTINPUT events are enabled. * This is true to indicate that keyboard input is in text input mode, and SDL_TEXTINPUT events are enabled.
*/ */
bool edit_box_focused; bool edit_box_focused;
uint32 cur_ticks;
uint32 last_cur_ticks;
uint32 next_tick;
std::thread draw_thread;
std::unique_lock<std::recursive_mutex> draw_lock;
}; };
/** Factory for the SDL video driver. */ /** Factory for the SDL video driver. */