Browse Source

use std::rotate (#6019)

pull/6020/head
qndel 3 years ago committed by GitHub
parent
commit
731ad1ada5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      Source/controls/touch/renderers.cpp
  2. 2
      Source/engine.cpp
  3. 62
      Source/engine/palette.cpp
  4. 11
      Source/engine/palette.h
  5. 2
      Source/engine/render/blit_impl.hpp
  6. 7
      Source/lighting.cpp
  7. 2
      Source/utils/sdl_bilinear_scale.cpp
  8. 4
      Source/utils/sdl_bilinear_scale.hpp

2
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);

2
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<int>(sx), static_cast<int>(sy));
const uint8_t *lookupTable = paletteTransparencyLookup[0];
const std::array<uint8_t, 256> &lookupTable = paletteTransparencyLookup[0];
const unsigned skipX = out.pitch() - width;
for (unsigned y = 0; y < height; ++y) {
for (unsigned x = 0; x < width; ++x, ++pix) {

62
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<SDL_Color, 256> logical_palette;
std::array<SDL_Color, 256> system_palette;
std::array<SDL_Color, 256> orig_palette;
std::array<std::array<Uint8, 256>, 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<SDL_Color, 256> &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<SDL_Color, 256> &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<SDL_Color, 256> &dst, const std::array<SDL_Color, 256> &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;
}

11
Source/engine/palette.h

@ -5,6 +5,7 @@
*/
#pragma once
#include <array>
#include <cstdint>
#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<SDL_Color, 256> logical_palette;
extern std::array<SDL_Color, 256> system_palette;
extern std::array<SDL_Color, 256> orig_palette;
/** Lookup table for transparency */
extern Uint8 paletteTransparencyLookup[256][256];
extern std::array<std::array<Uint8, 256>, 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<SDL_Color, 256> &dst, const std::array<SDL_Color, 256> &src, int n);
void DecreaseGamma();
int UpdateGamma(int gamma);
void BlackPalette();

2
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<uint8_t, 256> &tbl = paletteTransparencyLookup[color];
while (dst + 3 < end) {
*dst = tbl[*dst];
++dst;

7
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);
}
}

2
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<std::array<Uint8, 256>, 256> &paletteBlendingTable, SDL_Surface *dst, uint8_t transparentIndex)
{
const auto *const srcPixelsBegin = static_cast<const uint8_t *>(src->pixels)
+ static_cast<size_t>(src->clip_rect.y * src->pitch + src->clip_rect.x);

4
Source/utils/sdl_bilinear_scale.hpp

@ -8,6 +8,8 @@
#include <SDL_video.h>
#endif
#include <array>
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<std::array<Uint8, 256>, 256> &paletteBlendingTable, SDL_Surface *dst, uint8_t transparentIndex);
} // namespace devilution

Loading…
Cancel
Save