From 8ab50ea7b719418dc684b6e4c8f163b3f61e8378 Mon Sep 17 00:00:00 2001
From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com>
Date: Sat, 13 Nov 2021 09:00:58 +0300
Subject: [PATCH] ColorRgba: replaced union with bit shifts. Fixed bad clamping
in frame time tool.
---
SpaceCadetPinball/NatvisFile.natvis | 59 ++++++-----------------------
SpaceCadetPinball/gdrv.cpp | 52 ++++++++++---------------
SpaceCadetPinball/gdrv.h | 38 ++++++++++---------
SpaceCadetPinball/winmain.cpp | 6 +--
SpaceCadetPinball/zdrv.cpp | 6 +--
5 files changed, 56 insertions(+), 105 deletions(-)
diff --git a/SpaceCadetPinball/NatvisFile.natvis b/SpaceCadetPinball/NatvisFile.natvis
index b8dac18..cc64cd6 100644
--- a/SpaceCadetPinball/NatvisFile.natvis
+++ b/SpaceCadetPinball/NatvisFile.natvis
@@ -1,50 +1,13 @@
-
+
-
- {{ size={ListPtr->Count} }}
-
- - ListPtr->Count
- - ListPtr->Size
-
- ListPtr->Size
- ListPtr->Array
-
-
-
-
-
- {{ size={Count} }}
-
- - Count
- - Size
-
- Size
- Array
-
-
-
-
-
- {{ NumberOfGroups={NumberOfGroups} }}
-
- - NumberOfGroups
- - Description
-
- NumberOfGroups
- GroupData
-
-
-
-
- {{ EntryCount={EntryCount} }}
-
- - EntryCount
-
- EntryCount
- Entries
-
-
-
-
-
+
+ {{ Color:{Color} }}
+
+ - Color>>alphaOffset & 255
+ - Color>>redOffset & 255
+ - Color>>greenOffset & 255
+ - Color>>blueOffset & 255
+ - Color
+
+
\ No newline at end of file
diff --git a/SpaceCadetPinball/gdrv.cpp b/SpaceCadetPinball/gdrv.cpp
index c808db7..fc46860 100644
--- a/SpaceCadetPinball/gdrv.cpp
+++ b/SpaceCadetPinball/gdrv.cpp
@@ -122,7 +122,7 @@ void gdrv_bitmap8::CreateTexture(const char* scaleHint, int access)
Texture = SDL_CreateTexture
(
winmain::Renderer,
- SDL_PIXELFORMAT_ARGB8888,
+ SDL_PIXELFORMAT_BGRA32,
access,
Width, Height
);
@@ -151,43 +151,33 @@ void gdrv_bitmap8::BlitToTexture()
int gdrv::display_palette(ColorRgba* plt)
{
- const uint32_t sysPaletteColors[]
+ // Colors from Windows system palette
+ const ColorRgba sysPaletteColors[10]
{
- 0xff000000, // Color 0: transparent
- 0xff000080,
- 0xff008000,
- 0xff008080,
- 0xff800000,
- 0xff800080,
- 0xff808000,
- 0xffC0C0C0,
- 0xffC0DCC0,
- 0xffF0CAA6
+ ColorRgba{0, 0, 0, 0}, // Color 0: transparent
+ ColorRgba{0x80, 0, 0, 0xff},
+ ColorRgba{0, 0x80, 0, 0xff},
+ ColorRgba{0x80, 0x80, 0, 0xff},
+ ColorRgba{0, 0, 0x80, 0xff},
+ ColorRgba{0x80, 0, 0x80, 0xff},
+ ColorRgba{0, 0x80, 0x80, 0xff},
+ ColorRgba{0xC0, 0xC0, 0xC0, 0xff},
+ ColorRgba{0xC0, 0xDC, 0xC0, 0xff},
+ ColorRgba{0xA6, 0xCA, 0xF0, 0xff},
};
- memcpy(current_palette, sysPaletteColors, sizeof sysPaletteColors);
+ std::memset(current_palette, 0, sizeof current_palette);
+ std::memcpy(current_palette, sysPaletteColors, sizeof sysPaletteColors);
- for (int i = 0; i < 256; i++)
+ for (int index = 10; plt && index < 246; index++)
{
- current_palette[i].rgba.Alpha = 0;
+ auto srcClr = plt[index];
+ srcClr.SetAlpha(0xff);
+ current_palette[index] = ColorRgba{ srcClr };
+ current_palette[index].SetAlpha(2);
}
- auto pltSrc = &plt[10];
- auto pltDst = ¤t_palette[10];
- for (int index = 236; index > 0; --index)
- {
- if (plt)
- {
- pltDst->rgba.Blue = pltSrc->rgba.Blue;
- pltDst->rgba.Green = pltSrc->rgba.Green;
- pltDst->rgba.Red = pltSrc->rgba.Red;
- }
- pltDst->rgba.Alpha = 0xFF;
- pltSrc++;
- pltDst++;
- }
-
- current_palette[255].Color = 0xffFFFFFF;
+ current_palette[255] = ColorRgba::White();
for (const auto group : pb::record_table->Groups)
{
diff --git a/SpaceCadetPinball/gdrv.h b/SpaceCadetPinball/gdrv.h
index 6099b17..f30fd96 100644
--- a/SpaceCadetPinball/gdrv.h
+++ b/SpaceCadetPinball/gdrv.h
@@ -8,22 +8,15 @@ enum class BitmapTypes : uint8_t
Spliced = 3,
};
-
-struct Rgba
+struct ColorRgba
{
- uint8_t Blue;
- uint8_t Green;
- uint8_t Red;
- uint8_t Alpha;
-};
+ static constexpr ColorRgba Black() { return ColorRgba{ 0, 0, 0, 255 }; }
+ static constexpr ColorRgba White() { return ColorRgba{ 255, 255, 255, 255 }; }
+ static constexpr ColorRgba Red() { return ColorRgba{ 255, 0, 0, 255 }; }
+ static constexpr ColorRgba Green() { return ColorRgba{ 0, 255,0, 255 }; }
+ static constexpr ColorRgba Blue() { return ColorRgba{ 0, 0, 255, 255 }; }
-union ColorRgba
-{
- static constexpr ColorRgba Black() { return ColorRgba{ Rgba{0, 0, 0, 255} }; }
- static constexpr ColorRgba White() { return ColorRgba{ Rgba{255, 255, 255, 255} }; }
- static constexpr ColorRgba Red() { return ColorRgba{ Rgba{0, 0, 255, 255} }; }
- static constexpr ColorRgba Green() { return ColorRgba{ Rgba{0, 255,0, 255} }; }
- static constexpr ColorRgba Blue() { return ColorRgba{ Rgba{255, 0, 0, 255} }; }
+ uint32_t Color;
ColorRgba() = default;
@@ -32,13 +25,22 @@ union ColorRgba
{
}
- explicit constexpr ColorRgba(Rgba rgba)
- : rgba(rgba)
+ explicit constexpr ColorRgba(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
+ : Color(alpha << alphaOffset | red << redOffset | green << greenOffset | blue << blueOffset)
{
}
- uint32_t Color;
- Rgba rgba;
+ uint8_t GetAlpha() const { return (Color >> alphaOffset) & 0xffu; }
+ uint8_t GetRed() const { return (Color >> redOffset) & 0xffu; }
+ uint8_t GetGreen() const { return (Color >> greenOffset) & 0xffu; }
+ uint8_t GetBlue() const { return (Color >> blueOffset) & 0xffu; }
+ void SetAlpha(uint8_t val) { Color = (Color & (~(0xffu << alphaOffset))) | (val << alphaOffset); }
+ void SetRed(uint8_t val) { Color = (Color & (~(0xffu << redOffset))) | (val << redOffset); }
+ void SetGreen(uint8_t val) { Color = (Color & (~(0xffu << greenOffset))) | (val << greenOffset); }
+ void SetBlue(uint8_t val) { Color = (Color & (~(0xffu << blueOffset))) | (val << blueOffset); }
+
+private:
+ static const unsigned alphaOffset = 3 * 8, redOffset = 2 * 8, greenOffset = 1 * 8, blueOffset = 0 * 8;
};
static_assert(sizeof(ColorRgba) == 4, "Wrong size of RGBA color");
diff --git a/SpaceCadetPinball/winmain.cpp b/SpaceCadetPinball/winmain.cpp
index 8442fc6..09400bd 100644
--- a/SpaceCadetPinball/winmain.cpp
+++ b/SpaceCadetPinball/winmain.cpp
@@ -250,11 +250,11 @@ int winmain::WinMain(LPCSTR lpCmdLine)
gdrv::fill_bitmap(gfr_display, 1, height, width - 1, 0, ColorRgba::Black()); // Background
auto targetVal = dt < target ? dt : target;
- auto targetHeight = std::min(static_cast(std::round(targetVal * scale)), width);
+ auto targetHeight = std::min(static_cast(std::round(targetVal * scale)), height);
gdrv::fill_bitmap(gfr_display, 1, targetHeight, width - 1, height - targetHeight, ColorRgba::White()); // Target
auto diffVal = dt < target ? target - dt : dt - target;
- auto diffHeight = std::min(static_cast(std::round(diffVal * scale)), width);
+ auto diffHeight = std::min(static_cast(std::round(diffVal * scale)), height);
gdrv::fill_bitmap(gfr_display, 1, diffHeight, width - 1, height - targetHeight - diffHeight, ColorRgba::Red()); // Target diff
}
updateCounter++;
@@ -691,7 +691,7 @@ int winmain::event_handler(const SDL_Event* event)
redGreen = i1;
}
- *pltPtr++ = ColorRgba{ Rgba{redGreen, redGreen, blue, 0} };
+ *pltPtr++ = ColorRgba{ blue, redGreen, redGreen, 0 };
}
gdrv::display_palette(plt);
delete[] plt;
diff --git a/SpaceCadetPinball/zdrv.cpp b/SpaceCadetPinball/zdrv.cpp
index 470190c..5ed5925 100644
--- a/SpaceCadetPinball/zdrv.cpp
+++ b/SpaceCadetPinball/zdrv.cpp
@@ -112,7 +112,6 @@ void zdrv::CreatePreview(zmap_header_type& zMap)
auto tmpBuff = new ColorRgba[zMap.Width * zMap.Height];
- ColorRgba color{};
auto dst = tmpBuff;
auto src = zMap.ZPtr1;
for (auto y = 0; y < zMap.Height; y++)
@@ -120,10 +119,7 @@ void zdrv::CreatePreview(zmap_header_type& zMap)
for (auto x = 0; x < zMap.Width; x++)
{
auto depth = static_cast((0xffff - *src++) / 0xff);
- color.rgba.Blue = depth;
- color.rgba.Green = depth;
- color.rgba.Red = depth;
- *dst++ = color;
+ *dst++ = ColorRgba{ depth, depth, depth, 0xff };
}
src += zMap.Stride - zMap.Width;
}