From 731ad1ada57436a445a4fdf132f886c22b1015b6 Mon Sep 17 00:00:00 2001 From: qndel Date: Sat, 22 Apr 2023 13:53:03 +0200 Subject: [PATCH] use std::rotate (#6019) --- Source/controls/touch/renderers.cpp | 2 +- Source/engine.cpp | 2 +- Source/engine/palette.cpp | 62 ++++++++--------------------- Source/engine/palette.h | 11 ++--- Source/engine/render/blit_impl.hpp | 2 +- Source/lighting.cpp | 7 +--- Source/utils/sdl_bilinear_scale.cpp | 2 +- Source/utils/sdl_bilinear_scale.hpp | 4 +- 8 files changed, 31 insertions(+), 61 deletions(-) diff --git a/Source/controls/touch/renderers.cpp b/Source/controls/touch/renderers.cpp index a27d5eb50..5a610f999 100644 --- a/Source/controls/touch/renderers.cpp +++ b/Source/controls/touch/renderers.cpp @@ -131,7 +131,7 @@ void LoadPotionArt(ButtonTexture *potionArt, SDL_Renderer *renderer) SDL_PIXELFORMAT_INDEX8); auto palette = SDLWrap::AllocPalette(); - if (SDLC_SetSurfaceAndPaletteColors(surface.get(), palette.get(), orig_palette, 0, 256) < 0) + if (SDLC_SetSurfaceAndPaletteColors(surface.get(), palette.get(), orig_palette.data(), 0, 256) < 0) ErrSdl(); Uint32 bgColor = SDL_MapRGB(surface->format, orig_palette[1].r, orig_palette[1].g, orig_palette[1].b); diff --git a/Source/engine.cpp b/Source/engine.cpp index d1429cf1e..909733cd8 100644 --- a/Source/engine.cpp +++ b/Source/engine.cpp @@ -26,7 +26,7 @@ namespace { void DrawHalfTransparentUnalignedBlendedRectTo(const Surface &out, unsigned sx, unsigned sy, unsigned width, unsigned height) { uint8_t *pix = out.at(static_cast(sx), static_cast(sy)); - const uint8_t *lookupTable = paletteTransparencyLookup[0]; + const std::array &lookupTable = paletteTransparencyLookup[0]; const unsigned skipX = out.pitch() - width; for (unsigned y = 0; y < height; ++y) { for (unsigned x = 0; x < width; ++x, ++pix) { diff --git a/Source/engine/palette.cpp b/Source/engine/palette.cpp index 9fd8c50c1..81dcef17e 100644 --- a/Source/engine/palette.cpp +++ b/Source/engine/palette.cpp @@ -19,10 +19,10 @@ namespace devilution { -SDL_Color logical_palette[256]; -SDL_Color system_palette[256]; -SDL_Color orig_palette[256]; -Uint8 paletteTransparencyLookup[256][256]; +std::array logical_palette; +std::array system_palette; +std::array orig_palette; +std::array, 256> paletteTransparencyLookup; #if DEVILUTIONX_PALETTE_TRANSPARENCY_BLACK_16_LUT uint16_t paletteTransparencyLookupBlack16[65536]; @@ -40,7 +40,7 @@ void LoadGamma() sgOptions.Graphics.gammaCorrection.SetValue(gammaValue - gammaValue % 5); } -Uint8 FindBestMatchForColor(SDL_Color *palette, SDL_Color color, int skipFrom, int skipTo) +Uint8 FindBestMatchForColor(std::array &palette, SDL_Color color, int skipFrom, int skipTo) { Uint8 best; Uint32 bestDiff = SDL_MAX_UINT32; @@ -73,7 +73,7 @@ Uint8 FindBestMatchForColor(SDL_Color *palette, SDL_Color color, int skipFrom, i * @param skipTo Do not use colors between skipFrom and this index * @param toUpdate Only update the first n colors */ -void GenerateBlendedLookupTable(SDL_Color *palette, int skipFrom, int skipTo, int toUpdate = 256) +void GenerateBlendedLookupTable(std::array &palette, int skipFrom, int skipTo, int toUpdate = 256) { for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { @@ -119,28 +119,13 @@ void GenerateBlendedLookupTable(SDL_Color *palette, int skipFrom, int skipTo, in */ void CycleColors(int from, int to) { - { - SDL_Color col = system_palette[from]; - for (int i = from; i < to; i++) { - system_palette[i] = system_palette[i + 1]; - } - system_palette[to] = col; - } + std::rotate(system_palette.begin() + from, system_palette.begin() + from + 1, system_palette.begin() + to + 1); for (auto &palette : paletteTransparencyLookup) { - Uint8 col = palette[from]; - for (int j = from; j < to; j++) { - palette[j] = palette[j + 1]; - } - palette[to] = col; + std::rotate(palette.begin() + from, palette.begin() + from + 1, palette.begin() + to + 1); } - Uint8 colRow[256]; - memcpy(colRow, &paletteTransparencyLookup[from], sizeof(*paletteTransparencyLookup)); - for (int i = from; i < to; i++) { - memcpy(&paletteTransparencyLookup[i], &paletteTransparencyLookup[i + 1], sizeof(*paletteTransparencyLookup)); - } - memcpy(&paletteTransparencyLookup[to], colRow, sizeof(colRow)); + std::rotate(paletteTransparencyLookup.begin() + from, paletteTransparencyLookup.begin() + from + 1, paletteTransparencyLookup.begin() + to + 1); } /** @@ -150,28 +135,13 @@ void CycleColors(int from, int to) */ void CycleColorsReverse(int from, int to) { - { - SDL_Color col = system_palette[to]; - for (int i = to; i > from; i--) { - system_palette[i] = system_palette[i - 1]; - } - system_palette[from] = col; - } + std::rotate(system_palette.begin() + from + 1, system_palette.begin() + from, system_palette.begin() + to); for (auto &palette : paletteTransparencyLookup) { - Uint8 col = palette[to]; - for (int j = to; j > from; j--) { - palette[j] = palette[j - 1]; - } - palette[from] = col; + std::rotate(palette.begin() + from + 1, palette.begin() + from, palette.begin() + to); } - Uint8 colRow[256]; - memcpy(colRow, &paletteTransparencyLookup[to], sizeof(*paletteTransparencyLookup)); - for (int i = to; i > from; i--) { - memcpy(&paletteTransparencyLookup[i], &paletteTransparencyLookup[i - 1], sizeof(*paletteTransparencyLookup)); - } - memcpy(&paletteTransparencyLookup[from], colRow, sizeof(colRow)); + std::rotate(paletteTransparencyLookup.begin() + from + 1, paletteTransparencyLookup.begin() + from, paletteTransparencyLookup.begin() + to); } } // namespace @@ -182,13 +152,13 @@ void palette_update(int first, int ncolor) return; assert(Palette); - if (SDLC_SetSurfaceAndPaletteColors(PalSurface, Palette.get(), system_palette, first, ncolor) < 0) { + if (SDLC_SetSurfaceAndPaletteColors(PalSurface, Palette.get(), system_palette.data(), first, ncolor) < 0) { ErrSdl(); } pal_surface_palette_version++; } -void ApplyGamma(SDL_Color *dst, const SDL_Color *src, int n) +void ApplyGamma(std::array &dst, const std::array &src, int n) { double g = *sgOptions.Graphics.gammaCorrection / 100.0; @@ -203,7 +173,7 @@ void ApplyGamma(SDL_Color *dst, const SDL_Color *src, int n) void palette_init() { LoadGamma(); - memcpy(system_palette, orig_palette, sizeof(orig_palette)); + system_palette = orig_palette; InitPalette(); } @@ -351,7 +321,7 @@ void PaletteFadeIn(int fr) RenderPresent(); } - memcpy(logical_palette, orig_palette, sizeof(orig_palette)); + logical_palette = orig_palette; sgbFadedIn = true; } diff --git a/Source/engine/palette.h b/Source/engine/palette.h index d59fc1c52..a70451d12 100644 --- a/Source/engine/palette.h +++ b/Source/engine/palette.h @@ -5,6 +5,7 @@ */ #pragma once +#include #include #include "levels/gendung.h" @@ -31,11 +32,11 @@ namespace devilution { #define PAL16_RED 224 #define PAL16_GRAY 240 -extern SDL_Color logical_palette[256]; -extern SDL_Color system_palette[256]; -extern SDL_Color orig_palette[256]; +extern std::array logical_palette; +extern std::array system_palette; +extern std::array orig_palette; /** Lookup table for transparency */ -extern Uint8 paletteTransparencyLookup[256][256]; +extern std::array, 256> paletteTransparencyLookup; #if DEVILUTIONX_PALETTE_TRANSPARENCY_BLACK_16_LUT /** @@ -55,7 +56,7 @@ void palette_init(); void LoadPalette(const char *pszFileName, bool blend = true); void LoadRndLvlPal(dungeon_type l); void IncreaseGamma(); -void ApplyGamma(SDL_Color *dst, const SDL_Color *src, int n); +void ApplyGamma(std::array &dst, const std::array &src, int n); void DecreaseGamma(); int UpdateGamma(int gamma); void BlackPalette(); diff --git a/Source/engine/render/blit_impl.hpp b/Source/engine/render/blit_impl.hpp index 504fd8076..5066b7d51 100644 --- a/Source/engine/render/blit_impl.hpp +++ b/Source/engine/render/blit_impl.hpp @@ -72,7 +72,7 @@ DVL_ALWAYS_INLINE DVL_ATTRIBUTE_HOT void BlitFillBlended(uint8_t *dst, unsigned { assert(length != 0); const uint8_t *end = dst + length; - const uint8_t *tbl = paletteTransparencyLookup[color]; + const std::array &tbl = paletteTransparencyLookup[color]; while (dst + 3 < end) { *dst = tbl[*dst]; ++dst; diff --git a/Source/lighting.cpp b/Source/lighting.cpp index 5e7494074..566a90dcf 100644 --- a/Source/lighting.cpp +++ b/Source/lighting.cpp @@ -682,11 +682,8 @@ void ProcessVisionList() void lighting_color_cycling() { for (auto &lightTable : LightTables) { - uint8_t firstColor = lightTable[1]; - for (int colorIndex = 1; colorIndex < 31; colorIndex++) { - lightTable[colorIndex] = lightTable[colorIndex + 1]; - } - lightTable[31] = firstColor; + // shift elements between indexes 1-31 to left + std::rotate(lightTable.begin() + 1, lightTable.begin() + 2, lightTable.begin() + 32); } } diff --git a/Source/utils/sdl_bilinear_scale.cpp b/Source/utils/sdl_bilinear_scale.cpp index 06f61382e..078a65d16 100644 --- a/Source/utils/sdl_bilinear_scale.cpp +++ b/Source/utils/sdl_bilinear_scale.cpp @@ -148,7 +148,7 @@ void BilinearScale32(SDL_Surface *src, SDL_Surface *dst) } } -void BilinearDownscaleByHalf8(const SDL_Surface *src, const uint8_t (*paletteBlendingTable)[256], SDL_Surface *dst, uint8_t transparentIndex) +void BilinearDownscaleByHalf8(const SDL_Surface *src, const std::array, 256> &paletteBlendingTable, SDL_Surface *dst, uint8_t transparentIndex) { const auto *const srcPixelsBegin = static_cast(src->pixels) + static_cast(src->clip_rect.y * src->pitch + src->clip_rect.x); diff --git a/Source/utils/sdl_bilinear_scale.hpp b/Source/utils/sdl_bilinear_scale.hpp index ee6115dc2..156b154ab 100644 --- a/Source/utils/sdl_bilinear_scale.hpp +++ b/Source/utils/sdl_bilinear_scale.hpp @@ -8,6 +8,8 @@ #include #endif +#include + namespace devilution { /** @@ -20,6 +22,6 @@ void BilinearScale32(SDL_Surface *src, SDL_Surface *dst); * @brief Streamlined bilinear downscaling using blended transparency table. * Requires `src` and `dst` to have the same pixel format (INDEX8). */ -void BilinearDownscaleByHalf8(const SDL_Surface *src, const uint8_t (*paletteBlendingTable)[256], SDL_Surface *dst, uint8_t transparentIndex); +void BilinearDownscaleByHalf8(const SDL_Surface *src, const std::array, 256> &paletteBlendingTable, SDL_Surface *dst, uint8_t transparentIndex); } // namespace devilution