From 790fa7102ec934544b427dd5fa9fc4f7099fd4da Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sat, 20 Feb 2021 11:08:20 +0100 Subject: [PATCH] Codechange: be consistent in naming the paint function Paint() Also move this function to be a class member. This to allow further deduplicating of code in a later commit. --- src/video/allegro_v.cpp | 4 ++-- src/video/allegro_v.h | 1 + src/video/cocoa/cocoa_v.h | 2 +- src/video/cocoa/cocoa_v.mm | 9 ++++----- src/video/sdl2_v.cpp | 15 ++++++++++----- src/video/sdl2_v.h | 4 ++++ src/video/sdl_v.cpp | 15 ++++++++++----- src/video/sdl_v.h | 4 ++++ src/video/video_driver.hpp | 10 ++++++++++ src/video/win32_v.cpp | 15 ++++++++++----- src/video/win32_v.h | 2 ++ 11 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/video/allegro_v.cpp b/src/video/allegro_v.cpp index 5ddc8b808773a..9b5b2e1d9644b 100644 --- a/src/video/allegro_v.cpp +++ b/src/video/allegro_v.cpp @@ -56,7 +56,7 @@ void VideoDriver_Allegro::MakeDirty(int left, int top, int width, int height) _num_dirty_rects++; } -static void DrawSurfaceToScreen() +void VideoDriver_Allegro::Paint() { PerformanceMeasurer framerate(PFE_VIDEO); @@ -524,7 +524,7 @@ void VideoDriver_Allegro::MainLoop() UpdateWindows(); CheckPaletteAnim(); - DrawSurfaceToScreen(); + this->Paint(); } /* If we are not in fast-forward, create some time between calls to ease up CPU usage. */ diff --git a/src/video/allegro_v.h b/src/video/allegro_v.h index 1c3a05af750f5..3c55a6691e37b 100644 --- a/src/video/allegro_v.h +++ b/src/video/allegro_v.h @@ -35,6 +35,7 @@ class VideoDriver_Allegro : public VideoDriver { protected: void InputLoop() override; + void Paint() override; }; /** Factory for the allegro video driver. */ diff --git a/src/video/cocoa/cocoa_v.h b/src/video/cocoa/cocoa_v.h index f42d57dfe3cf4..09668ed8be868 100644 --- a/src/video/cocoa/cocoa_v.h +++ b/src/video/cocoa/cocoa_v.h @@ -75,6 +75,7 @@ class VideoDriver_Cocoa : public VideoDriver { Dimension GetScreenSize() const override; float GetDPIScale() override; void InputLoop() override; + void Paint() override; private: bool PollEvent(); @@ -89,7 +90,6 @@ class VideoDriver_Cocoa : public VideoDriver { void UpdatePalette(uint first_color, uint num_colors); void CheckPaletteAnim(); - void Draw(bool force_update = false); void BlitIndexedToView32(int left, int top, int right, int bottom); }; diff --git a/src/video/cocoa/cocoa_v.mm b/src/video/cocoa/cocoa_v.mm index 5e8c82b2abe1b..1847d53fc8b59 100644 --- a/src/video/cocoa/cocoa_v.mm +++ b/src/video/cocoa/cocoa_v.mm @@ -465,10 +465,10 @@ - (instancetype)initWithFrame:(NSRect)frameRect andDriver:(VideoDriver_Cocoa *)d } /** - * Draw window. + * Paint window. * @param force_update Whether to redraw unconditionally */ -void VideoDriver_Cocoa::Draw(bool force_update) +void VideoDriver_Cocoa::Paint() { PerformanceMeasurer framerate(PFE_VIDEO); @@ -502,9 +502,8 @@ - (instancetype)initWithFrame:(NSRect)frameRect andDriver:(VideoDriver_Cocoa *)d dirtyrect.size.height = this->dirty_rects[i].bottom - this->dirty_rects[i].top; /* Normally drawRect will be automatically called by Mac OS X during next update cycle, - * and then blitting will occur. If force_update is true, it will be done right now. */ + * and then blitting will occur. */ [ this->cocoaview setNeedsDisplayInRect:[ this->cocoaview getVirtualRect:dirtyrect ] ]; - if (force_update) [ this->cocoaview displayIfNeeded ]; } this->num_dirty_rects = 0; @@ -708,7 +707,7 @@ static void ClearWindowBuffer(uint32 *buffer, uint32 pitch, uint32 height) UpdateWindows(); this->CheckPaletteAnim(); - this->Draw(); + this->Paint(); } /* If we are not in fast-forward, create some time between calls to ease up CPU usage. */ diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp index 8bee575e287bf..1cc6c55988d6d 100644 --- a/src/video/sdl2_v.cpp +++ b/src/video/sdl2_v.cpp @@ -124,7 +124,7 @@ void VideoDriver_SDL::CheckPaletteAnim() this->MakeDirty(0, 0, _screen.width, _screen.height); } -static void Paint() +void VideoDriver_SDL::Paint() { PerformanceMeasurer framerate(PFE_VIDEO); @@ -161,7 +161,7 @@ static void Paint() MemSetT(&_dirty_rect, 0); } -static void PaintThread() +void VideoDriver_SDL::PaintThread() { /* First tell the main thread we're started */ std::unique_lock lock(*_draw_mutex); @@ -172,11 +172,16 @@ static void PaintThread() while (_draw_continue) { /* Then just draw and wait till we stop */ - Paint(); + this->Paint(); _draw_signal->wait(lock); } } +/* static */ void VideoDriver_SDL::PaintThreadThunk(VideoDriver_SDL *drv) +{ + drv->PaintThread(); +} + static const Dimension default_resolutions[] = { { 640, 480 }, { 800, 600 }, @@ -820,7 +825,7 @@ void VideoDriver_SDL::LoopOnce() if (_draw_mutex != nullptr && !HasModalProgress()) { _draw_signal->notify_one(); } else { - Paint(); + this->Paint(); } } @@ -861,7 +866,7 @@ void VideoDriver_SDL::MainLoop() _draw_signal = new std::condition_variable_any(); _draw_continue = true; - _draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &PaintThread); + _draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &VideoDriver_SDL::PaintThreadThunk, this); /* Free the mutex if we won't be able to use it. */ if (!_draw_threaded) { diff --git a/src/video/sdl2_v.h b/src/video/sdl2_v.h index 116cef51ff8c3..91f4b2c7f6edf 100644 --- a/src/video/sdl2_v.h +++ b/src/video/sdl2_v.h @@ -46,6 +46,8 @@ class VideoDriver_SDL : public VideoDriver { void InputLoop() override; bool LockVideoBuffer() override; void UnlockVideoBuffer() override; + void Paint() override; + void PaintThread() override; private: int PollEvent(); @@ -73,6 +75,8 @@ class VideoDriver_SDL : public VideoDriver { int startup_display; std::thread draw_thread; std::unique_lock draw_lock; + + static void PaintThreadThunk(VideoDriver_SDL *drv); }; /** Factory for the SDL video driver. */ diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp index 8190dc4d62e0e..0aa216df2e10e 100644 --- a/src/video/sdl_v.cpp +++ b/src/video/sdl_v.cpp @@ -147,7 +147,7 @@ static void CheckPaletteAnim() } } -static void DrawSurfaceToScreen() +void VideoDriver_SDL::Paint() { PerformanceMeasurer framerate(PFE_VIDEO); @@ -173,7 +173,7 @@ static void DrawSurfaceToScreen() } } -static void DrawSurfaceToScreenThread() +void VideoDriver_SDL::PaintThread() { /* First tell the main thread we're started */ std::unique_lock lock(*_draw_mutex); @@ -185,11 +185,16 @@ static void DrawSurfaceToScreenThread() while (_draw_continue) { CheckPaletteAnim(); /* Then just draw and wait till we stop */ - DrawSurfaceToScreen(); + this->Paint(); _draw_signal->wait(lock); } } +/* static */ void VideoDriver_SDL::PaintThreadThunk(VideoDriver_SDL *drv) +{ + drv->PaintThread(); +} + static const Dimension _default_resolutions[] = { { 640, 480}, { 800, 600}, @@ -717,7 +722,7 @@ void VideoDriver_SDL::MainLoop() _draw_signal = new std::condition_variable_any(); _draw_continue = true; - _draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &DrawSurfaceToScreenThread); + _draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &VideoDriver_SDL::PaintThreadThunk, this); /* Free the mutex if we won't be able to use it. */ if (!_draw_threaded) { @@ -782,7 +787,7 @@ void VideoDriver_SDL::MainLoop() _draw_signal->notify_one(); } else { CheckPaletteAnim(); - DrawSurfaceToScreen(); + this->Paint(); } } diff --git a/src/video/sdl_v.h b/src/video/sdl_v.h index 896a6f5b8e1b2..a51062d850aa7 100644 --- a/src/video/sdl_v.h +++ b/src/video/sdl_v.h @@ -41,6 +41,8 @@ class VideoDriver_SDL : public VideoDriver { void InputLoop() override; bool LockVideoBuffer() override; void UnlockVideoBuffer() override; + void Paint() override; + void PaintThread() override; private: std::unique_lock draw_lock; @@ -48,6 +50,8 @@ class VideoDriver_SDL : public VideoDriver { int PollEvent(); bool CreateMainSurface(uint w, uint h); void SetupKeyboard(); + + static void PaintThreadThunk(VideoDriver_SDL *drv); }; /** Factory for the SDL video driver. */ diff --git a/src/video/video_driver.hpp b/src/video/video_driver.hpp index 875125ebcb142..b8acb2bb8895f 100644 --- a/src/video/video_driver.hpp +++ b/src/video/video_driver.hpp @@ -174,6 +174,16 @@ class VideoDriver : public Driver { */ virtual void UnlockVideoBuffer() {} + /** + * Paint the window. + */ + virtual void Paint() {} + + /** + * Thread function for threaded drawing. + */ + virtual void PaintThread() {} + std::chrono::steady_clock::duration GetGameInterval() { return std::chrono::milliseconds(MILLISECONDS_PER_TICK); diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index cca12c40c1d5b..8e887f0aa9dd3 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -332,7 +332,7 @@ bool VideoDriver_Win32::MakeWindow(bool full_screen) } /** Do palette animation and blit to the window. */ -static void PaintWindow() +void VideoDriver_Win32::Paint() { PerformanceMeasurer framerate(PFE_VIDEO); @@ -385,7 +385,7 @@ static void PaintWindow() MemSetT(&_dirty_rect, 0); } -static void PaintWindowThread() +void VideoDriver_Win32::PaintThread() { /* First tell the main thread we're started */ std::unique_lock lock(*_draw_mutex); @@ -395,7 +395,7 @@ static void PaintWindowThread() _draw_signal->wait(*_draw_mutex); while (_draw_continue) { - PaintWindow(); + this->Paint(); /* Flush GDI buffer to ensure drawing here doesn't conflict with any GDI usage in the main WndProc. */ GdiFlush(); @@ -404,6 +404,11 @@ static void PaintWindowThread() } } +/* static */ void VideoDriver_Win32::PaintThreadThunk(VideoDriver_Win32 *drv) +{ + drv->PaintThread(); +} + /** Forward key presses to the window system. */ static LRESULT HandleCharMsg(uint keycode, WChar charcode) { @@ -1176,7 +1181,7 @@ void VideoDriver_Win32::MainLoop() this->draw_lock = std::unique_lock(*_draw_mutex); _draw_continue = true; - _draw_threaded = StartNewThread(&draw_thread, "ottd:draw-win32", &PaintWindowThread); + _draw_threaded = StartNewThread(&draw_thread, "ottd:draw-win32", &VideoDriver_Win32::PaintThreadThunk, this); /* Free the mutex if we won't be able to use it. */ if (!_draw_threaded) { @@ -1254,7 +1259,7 @@ void VideoDriver_Win32::MainLoop() if (_draw_mutex != nullptr && !HasModalProgress()) { _draw_signal->notify_one(); } else { - PaintWindow(); + this->Paint(); } } diff --git a/src/video/win32_v.h b/src/video/win32_v.h index 0a80afc4d56d0..d515944187eb8 100644 --- a/src/video/win32_v.h +++ b/src/video/win32_v.h @@ -47,6 +47,8 @@ class VideoDriver_Win32 : public VideoDriver { void InputLoop() override; bool LockVideoBuffer() override; void UnlockVideoBuffer() override; + void Paint() override; + void PaintThread() override; private: std::unique_lock draw_lock;