Skip to content

Commit

Permalink
Codechange: be consistent in naming the paint function Paint()
Browse files Browse the repository at this point in the history
Also move this function to be a class member. This to allow
further deduplicating of code in a later commit.
  • Loading branch information
TrueBrain committed Feb 20, 2021
1 parent 761efbb commit 790fa71
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/video/allegro_v.cpp
Expand Up @@ -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);

Expand Down Expand Up @@ -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. */
Expand Down
1 change: 1 addition & 0 deletions src/video/allegro_v.h
Expand Up @@ -35,6 +35,7 @@ class VideoDriver_Allegro : public VideoDriver {

protected:
void InputLoop() override;
void Paint() override;
};

/** Factory for the allegro video driver. */
Expand Down
2 changes: 1 addition & 1 deletion src/video/cocoa/cocoa_v.h
Expand Up @@ -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();
Expand All @@ -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);
};

Expand Down
9 changes: 4 additions & 5 deletions src/video/cocoa/cocoa_v.mm
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down
15 changes: 10 additions & 5 deletions src/video/sdl2_v.cpp
Expand Up @@ -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);

Expand Down Expand Up @@ -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<std::recursive_mutex> lock(*_draw_mutex);
Expand All @@ -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 },
Expand Down Expand Up @@ -820,7 +825,7 @@ void VideoDriver_SDL::LoopOnce()
if (_draw_mutex != nullptr && !HasModalProgress()) {
_draw_signal->notify_one();
} else {
Paint();
this->Paint();
}
}

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/video/sdl2_v.h
Expand Up @@ -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();
Expand Down Expand Up @@ -73,6 +75,8 @@ class VideoDriver_SDL : public VideoDriver {
int startup_display;
std::thread draw_thread;
std::unique_lock<std::recursive_mutex> draw_lock;

static void PaintThreadThunk(VideoDriver_SDL *drv);
};

/** Factory for the SDL video driver. */
Expand Down
15 changes: 10 additions & 5 deletions src/video/sdl_v.cpp
Expand Up @@ -147,7 +147,7 @@ static void CheckPaletteAnim()
}
}

static void DrawSurfaceToScreen()
void VideoDriver_SDL::Paint()
{
PerformanceMeasurer framerate(PFE_VIDEO);

Expand All @@ -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<std::recursive_mutex> lock(*_draw_mutex);
Expand All @@ -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},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -782,7 +787,7 @@ void VideoDriver_SDL::MainLoop()
_draw_signal->notify_one();
} else {
CheckPaletteAnim();
DrawSurfaceToScreen();
this->Paint();
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/video/sdl_v.h
Expand Up @@ -41,13 +41,17 @@ class VideoDriver_SDL : public VideoDriver {
void InputLoop() override;
bool LockVideoBuffer() override;
void UnlockVideoBuffer() override;
void Paint() override;
void PaintThread() override;

private:
std::unique_lock<std::recursive_mutex> draw_lock;

int PollEvent();
bool CreateMainSurface(uint w, uint h);
void SetupKeyboard();

static void PaintThreadThunk(VideoDriver_SDL *drv);
};

/** Factory for the SDL video driver. */
Expand Down
10 changes: 10 additions & 0 deletions src/video/video_driver.hpp
Expand Up @@ -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);
Expand Down
15 changes: 10 additions & 5 deletions src/video/win32_v.cpp
Expand Up @@ -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);

Expand Down Expand Up @@ -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<std::recursive_mutex> lock(*_draw_mutex);
Expand All @@ -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();
Expand All @@ -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)
{
Expand Down Expand Up @@ -1176,7 +1181,7 @@ void VideoDriver_Win32::MainLoop()
this->draw_lock = std::unique_lock<std::recursive_mutex>(*_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) {
Expand Down Expand Up @@ -1254,7 +1259,7 @@ void VideoDriver_Win32::MainLoop()
if (_draw_mutex != nullptr && !HasModalProgress()) {
_draw_signal->notify_one();
} else {
PaintWindow();
this->Paint();
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/video/win32_v.h
Expand Up @@ -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<std::recursive_mutex> draw_lock;
Expand Down

0 comments on commit 790fa71

Please sign in to comment.