From 071fdab236339f4bc2a7069051736b645789b935 Mon Sep 17 00:00:00 2001 From: Michael Lutz Date: Sat, 4 Nov 2023 15:20:21 +0100 Subject: [PATCH] Codechange: Replicate cursor screen backup to chat message display, removing explicit memory management. Incidentally, this makes Blitter::GetBytesPerPixel unneeed. --- src/blitter/32bpp_anim.hpp | 1 - src/blitter/32bpp_base.hpp | 1 - src/blitter/40bpp_anim.hpp | 1 - src/blitter/8bpp_base.hpp | 1 - src/blitter/base.hpp | 5 ----- src/blitter/null.hpp | 1 - src/network/network_chat_gui.cpp | 10 ++++------ 7 files changed, 4 insertions(+), 16 deletions(-) diff --git a/src/blitter/32bpp_anim.hpp b/src/blitter/32bpp_anim.hpp index 125295c41e..b5b19d784e 100644 --- a/src/blitter/32bpp_anim.hpp +++ b/src/blitter/32bpp_anim.hpp @@ -48,7 +48,6 @@ public: Blitter::PaletteAnimation UsePaletteAnimation() override; const char *GetName() override { return "32bpp-anim"; } - int GetBytesPerPixel() override { return 6; } void PostResize() override; /** diff --git a/src/blitter/32bpp_base.hpp b/src/blitter/32bpp_base.hpp index a1e1002cb8..f5d8a30c4e 100644 --- a/src/blitter/32bpp_base.hpp +++ b/src/blitter/32bpp_base.hpp @@ -30,7 +30,6 @@ public: size_t BufferSize(uint width, uint height) override; void PaletteAnimate(const Palette &palette) override; Blitter::PaletteAnimation UsePaletteAnimation() override; - int GetBytesPerPixel() override { return 4; } /** * Look up the colour in the current palette. diff --git a/src/blitter/40bpp_anim.hpp b/src/blitter/40bpp_anim.hpp index ecc7eb110d..fac3d64d3c 100644 --- a/src/blitter/40bpp_anim.hpp +++ b/src/blitter/40bpp_anim.hpp @@ -33,7 +33,6 @@ public: bool NeedsAnimationBuffer() override; const char *GetName() override { return "40bpp-anim"; } - int GetBytesPerPixel() override { return 5; } template void Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom); diff --git a/src/blitter/8bpp_base.hpp b/src/blitter/8bpp_base.hpp index 1d29333984..5a76d79521 100644 --- a/src/blitter/8bpp_base.hpp +++ b/src/blitter/8bpp_base.hpp @@ -28,7 +28,6 @@ public: size_t BufferSize(uint width, uint height) override; void PaletteAnimate(const Palette &palette) override; Blitter::PaletteAnimation UsePaletteAnimation() override; - int GetBytesPerPixel() override { return 1; } }; #endif /* BLITTER_8BPP_BASE_HPP */ diff --git a/src/blitter/base.hpp b/src/blitter/base.hpp index 1555d040f1..e55f9294e4 100644 --- a/src/blitter/base.hpp +++ b/src/blitter/base.hpp @@ -198,11 +198,6 @@ public: */ virtual const char *GetName() = 0; - /** - * Get how many bytes are needed to store a pixel. - */ - virtual int GetBytesPerPixel() = 0; - /** * Post resize event */ diff --git a/src/blitter/null.hpp b/src/blitter/null.hpp index 26e807c061..abf2a5b59f 100644 --- a/src/blitter/null.hpp +++ b/src/blitter/null.hpp @@ -32,7 +32,6 @@ public: Blitter::PaletteAnimation UsePaletteAnimation() override { return Blitter::PALETTE_ANIMATION_NONE; }; const char *GetName() override { return "null"; } - int GetBytesPerPixel() override { return 0; } }; /** Factory for the blitter that does nothing. */ diff --git a/src/network/network_chat_gui.cpp b/src/network/network_chat_gui.cpp index 24b328d92a..cdb2d1fba1 100644 --- a/src/network/network_chat_gui.cpp +++ b/src/network/network_chat_gui.cpp @@ -58,7 +58,7 @@ static std::chrono::steady_clock::time_point _chatmessage_dirty_time; * the left and pixels from the bottom. The height is the maximum height. */ static PointDimension _chatmsg_box; -static uint8_t *_chatmessage_backup = nullptr; ///< Backup in case text is moved. +static ReusableBuffer _chatmessage_backup; ///< Backup in case text is moved. /** * Test if there are any chat messages to display. @@ -103,7 +103,6 @@ void NetworkReInitChatBoxSize() { _chatmsg_box.y = 3 * FONT_HEIGHT_NORMAL; _chatmsg_box.height = MAX_CHAT_MESSAGES * (FONT_HEIGHT_NORMAL + ScaleGUITrad(NETWORK_CHAT_LINE_SPACING)) + ScaleGUITrad(4); - _chatmessage_backup = ReallocT(_chatmessage_backup, static_cast(_chatmsg_box.width) * _chatmsg_box.height * BlitterFactory::GetCurrentBlitter()->GetBytesPerPixel()); } /** Initialize all buffers of the chat visualisation. */ @@ -156,7 +155,7 @@ void NetworkUndrawChatMessage() _chatmessage_visible = false; /* Put our 'shot' back to the screen */ - blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height); + blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup.GetBuffer(), width, height); /* And make sure it is updated next time */ VideoDriver::GetInstance()->MakeDirty(x, y, width, height); @@ -208,10 +207,9 @@ void NetworkDrawChatMessage() } if (width <= 0 || height <= 0) return; - assert(blitter->BufferSize(width, height) <= static_cast(_chatmsg_box.width) * _chatmsg_box.height * blitter->GetBytesPerPixel()); - /* Make a copy of the screen as it is before painting (for undraw) */ - blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height); + uint8_t *buffer = _chatmessage_backup.Allocate(BlitterFactory::GetCurrentBlitter()->BufferSize(width, height)); + blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), buffer, width, height); _cur_dpi = &_screen; // switch to _screen painting