From 0ffdc86a2cb175293a3a6d929ec176886a65ae14 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Tue, 6 May 2025 19:44:42 +0200 Subject: [PATCH] Codechange: use std::ranges::equal over memcmp --- src/gfx_type.h | 6 ++++++ src/music/midifile.cpp | 2 +- src/palette.cpp | 9 +++++---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/gfx_type.h b/src/gfx_type.h index e14e285a53..664aecaed9 100644 --- a/src/gfx_type.h +++ b/src/gfx_type.h @@ -183,6 +183,8 @@ union ColourRGBA { * @param data The colour in the correct packed format. */ constexpr ColourRGBA(uint data = 0) : data(data) { } + + bool operator==(const ColourRGBA &other) const { return this->data == other.data; }; }; /** Packed colour union to access the alpha, red, green, and blue channels from a 32 bit number for big-endian systems. */ @@ -206,6 +208,8 @@ union ColourARGB { * @param data The colour in the correct packed format. */ constexpr ColourARGB(uint data = 0) : data(data) { } + + bool operator==(const ColourARGB &other) const { return this->data == other.data; }; }; /** Packed colour union to access the alpha, red, green, and blue channels from a 32 bit number for little-endian systems. */ @@ -229,6 +233,8 @@ union ColourBGRA { * @param data The colour in the correct packed format. */ constexpr ColourBGRA(uint data = 0) : data(data) { } + + bool operator==(const ColourBGRA &other) const { return this->data == other.data; }; }; #if defined(__EMSCRIPTEN__) diff --git a/src/music/midifile.cpp b/src/music/midifile.cpp index c94625c233..a45688773d 100644 --- a/src/music/midifile.cpp +++ b/src/music/midifile.cpp @@ -198,7 +198,7 @@ static bool ReadTrackChunk(FileHandle &file, MidiFile &target) if (fread(buf, sizeof(magic), 1, file) != 1) { return false; } - if (memcmp(magic, buf, sizeof(magic)) != 0) { + if (!std::ranges::equal(magic, buf)) { return false; } diff --git a/src/palette.cpp b/src/palette.cpp index 96ab905d83..1665aaf7cf 100644 --- a/src/palette.cpp +++ b/src/palette.cpp @@ -253,7 +253,6 @@ void DoPaletteAnimations() Blitter *blitter = BlitterFactory::GetCurrentBlitter(); const Colour *s; const ExtraPaletteValues *ev = &_extra_palette_values; - Colour old_val[PALETTE_ANIM_SIZE]; const uint old_tc = palette_animation_counter; uint j; @@ -261,10 +260,12 @@ void DoPaletteAnimations() palette_animation_counter = 0; } - Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START]; // Points to where animations are taking place on the palette + std::span current_palette{&_cur_palette.palette[PALETTE_ANIM_START], PALETTE_ANIM_SIZE}; /* Makes a copy of the current animation palette in old_val, * so the work on the current palette could be compared, see if there has been any changes */ - memcpy(old_val, palette_pos, sizeof(old_val)); + std::array original_palette; + std::ranges::copy(current_palette, original_palette.begin()); + auto palette_pos = current_palette.begin(); // Points to where animations are taking place on the palette /* Fizzy Drink bubbles animation */ s = ev->fizzy_drink; @@ -344,7 +345,7 @@ void DoPaletteAnimations() if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PaletteAnimation::None) { palette_animation_counter = old_tc; - } else if (_cur_palette.count_dirty == 0 && memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0) { + } else if (_cur_palette.count_dirty == 0 && !std::ranges::equal(current_palette, original_palette)) { /* Did we changed anything on the palette? Seems so. Mark it as dirty */ _cur_palette.first_dirty = PALETTE_ANIM_START; _cur_palette.count_dirty = PALETTE_ANIM_SIZE;