summaryrefslogtreecommitdiff
path: root/src/video/win32_v.cpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-02-20 10:49:27 +0100
committerPatric Stout <github@truebrain.nl>2021-02-20 17:08:44 +0100
commit761efbb4571397fe9e5f19049ca64663e12cbc1e (patch)
tree4eaa9b3380305f16999d7cd0c13b506d621a3c45 /src/video/win32_v.cpp
parent661eb39ecc3a128c24dbbc4f53d1c075fe89bc93 (diff)
downloadopenttd-761efbb4571397fe9e5f19049ca64663e12cbc1e.tar.xz
Codechange: use (Un)LockVideoBuffer() to manage video buffer
Diffstat (limited to 'src/video/win32_v.cpp')
-rw-r--r--src/video/win32_v.cpp30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp
index f55a9c372..cca12c40c 100644
--- a/src/video/win32_v.cpp
+++ b/src/video/win32_v.cpp
@@ -1161,7 +1161,6 @@ void VideoDriver_Win32::MainLoop()
auto next_draw_tick = cur_ticks;
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*
@@ -1174,15 +1173,15 @@ void VideoDriver_Win32::MainLoop()
}
if (_draw_threaded) {
- draw_lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
+ this->draw_lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
_draw_continue = true;
_draw_threaded = StartNewThread(&draw_thread, "ottd:draw-win32", &PaintWindowThread);
/* Free the mutex if we won't be able to use it. */
if (!_draw_threaded) {
- draw_lock.unlock();
- draw_lock.release();
+ this->draw_lock.unlock();
+ this->draw_lock.release();
delete _draw_mutex;
delete _draw_signal;
_draw_mutex = nullptr;
@@ -1231,9 +1230,9 @@ void VideoDriver_Win32::MainLoop()
/* The game loop is the part that can run asynchronously.
* The rest except sleeping can't. */
- if (_draw_threaded) draw_lock.unlock();
+ this->UnlockVideoBuffer();
GameLoop();
- if (_draw_threaded) draw_lock.lock();
+ this->LockVideoBuffer();
}
/* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
@@ -1269,9 +1268,9 @@ void VideoDriver_Win32::MainLoop()
/* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
GdiFlush();
- if (_draw_mutex != nullptr) draw_lock.unlock();
+ this->UnlockVideoBuffer();
std::this_thread::sleep_for(next_tick - now);
- if (_draw_mutex != nullptr) draw_lock.lock();
+ this->LockVideoBuffer();
}
}
}
@@ -1281,8 +1280,8 @@ void VideoDriver_Win32::MainLoop()
/* Sending signal if there is no thread blocked
* is very valid and results in noop */
_draw_signal->notify_all();
- if (draw_lock.owns_lock()) draw_lock.unlock();
- draw_lock.release();
+ if (this->draw_lock.owns_lock()) this->draw_lock.unlock();
+ this->draw_lock.release();
draw_thread.join();
delete _draw_mutex;
@@ -1383,3 +1382,14 @@ float VideoDriver_Win32::GetDPIScale()
return cur_dpi > 0 ? cur_dpi / 96.0f : 1.0f; // Default Windows DPI value is 96.
}
+
+bool VideoDriver_Win32::LockVideoBuffer()
+{
+ if (_draw_threaded) this->draw_lock.lock();
+ return true;
+}
+
+void VideoDriver_Win32::UnlockVideoBuffer()
+{
+ if (_draw_threaded) this->draw_lock.unlock();
+}