1
0
mirror of https://github.com/k4zmu2a/SpaceCadetPinball.git synced 2024-11-25 12:12:32 +01:00

ColorRgba: replaced union with bit shifts.

Fixed bad clamping in frame time tool.
This commit is contained in:
Muzychenko Andrey 2021-11-13 09:00:58 +03:00
parent f3e4211226
commit 8ab50ea7b7
5 changed files with 56 additions and 105 deletions

View File

@ -1,50 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="objlist_class">
<DisplayString>{{ size={ListPtr->Count} }}</DisplayString>
<Type Name="ColorRgba">
<DisplayString>{{ Color:{Color} }}</DisplayString>
<Expand>
<Item Name="[size]" ExcludeView="simple">ListPtr->Count</Item>
<Item Name="[capacity]" ExcludeView="simple">ListPtr->Size</Item>
<ArrayItems>
<Size>ListPtr->Size</Size>
<ValuePointer>ListPtr->Array</ValuePointer>
</ArrayItems>
<Item Name="[Alpha]" ExcludeView="simple">Color>>alphaOffset &amp; 255</Item>
<Item Name="[Red]" ExcludeView="simple">Color>>redOffset &amp; 255</Item>
<Item Name="[Green]" ExcludeView="simple">Color>>greenOffset &amp; 255</Item>
<Item Name="[Blue]" ExcludeView="simple">Color>>blueOffset &amp; 255</Item>
<Item Name="[Color]" ExcludeView="simple">Color</Item>
</Expand>
</Type>
<Type Name="objlist_struct1">
<DisplayString>{{ size={Count} }}</DisplayString>
<Expand>
<Item Name="[size]" ExcludeView="simple">Count</Item>
<Item Name="[capacity]" ExcludeView="simple">Size</Item>
<ArrayItems>
<Size>Size</Size>
<ValuePointer>Array</ValuePointer>
</ArrayItems>
</Expand>
</Type>
<Type Name="datFileStruct">
<DisplayString>{{ NumberOfGroups={NumberOfGroups} }}</DisplayString>
<Expand>
<Item Name="[NumberOfGroups]" ExcludeView="simple">NumberOfGroups</Item>
<Item Name="[Description]" ExcludeView="simple">Description</Item>
<ArrayItems>
<Size>NumberOfGroups</Size>
<ValuePointer>GroupData</ValuePointer>
</ArrayItems>
</Expand>
</Type>
<Type Name="datGroupData">
<DisplayString>{{ EntryCount={EntryCount} }}</DisplayString>
<Expand>
<Item Name="[EntryCount]" ExcludeView="simple">EntryCount</Item>
<ArrayItems>
<Size>EntryCount</Size>
<ValuePointer>Entries</ValuePointer>
</ArrayItems>
</Expand>
</Type>
</AutoVisualizer>

View File

@ -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 = &current_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)
{

View File

@ -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");

View File

@ -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<int>(std::round(targetVal * scale)), width);
auto targetHeight = std::min(static_cast<int>(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<int>(std::round(diffVal * scale)), width);
auto diffHeight = std::min(static_cast<int>(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;

View File

@ -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<uint8_t>((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;
}