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"?> <?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010"> <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="objlist_class"> <Type Name="ColorRgba">
<DisplayString>{{ size={ListPtr->Count} }}</DisplayString> <DisplayString>{{ Color:{Color} }}</DisplayString>
<Expand> <Expand>
<Item Name="[size]" ExcludeView="simple">ListPtr->Count</Item> <Item Name="[Alpha]" ExcludeView="simple">Color>>alphaOffset &amp; 255</Item>
<Item Name="[capacity]" ExcludeView="simple">ListPtr->Size</Item> <Item Name="[Red]" ExcludeView="simple">Color>>redOffset &amp; 255</Item>
<ArrayItems> <Item Name="[Green]" ExcludeView="simple">Color>>greenOffset &amp; 255</Item>
<Size>ListPtr->Size</Size> <Item Name="[Blue]" ExcludeView="simple">Color>>blueOffset &amp; 255</Item>
<ValuePointer>ListPtr->Array</ValuePointer> <Item Name="[Color]" ExcludeView="simple">Color</Item>
</ArrayItems>
</Expand> </Expand>
</Type> </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> </AutoVisualizer>

View File

@ -122,7 +122,7 @@ void gdrv_bitmap8::CreateTexture(const char* scaleHint, int access)
Texture = SDL_CreateTexture Texture = SDL_CreateTexture
( (
winmain::Renderer, winmain::Renderer,
SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_BGRA32,
access, access,
Width, Height Width, Height
); );
@ -151,43 +151,33 @@ void gdrv_bitmap8::BlitToTexture()
int gdrv::display_palette(ColorRgba* plt) int gdrv::display_palette(ColorRgba* plt)
{ {
const uint32_t sysPaletteColors[] // Colors from Windows system palette
const ColorRgba sysPaletteColors[10]
{ {
0xff000000, // Color 0: transparent ColorRgba{0, 0, 0, 0}, // Color 0: transparent
0xff000080, ColorRgba{0x80, 0, 0, 0xff},
0xff008000, ColorRgba{0, 0x80, 0, 0xff},
0xff008080, ColorRgba{0x80, 0x80, 0, 0xff},
0xff800000, ColorRgba{0, 0, 0x80, 0xff},
0xff800080, ColorRgba{0x80, 0, 0x80, 0xff},
0xff808000, ColorRgba{0, 0x80, 0x80, 0xff},
0xffC0C0C0, ColorRgba{0xC0, 0xC0, 0xC0, 0xff},
0xffC0DCC0, ColorRgba{0xC0, 0xDC, 0xC0, 0xff},
0xffF0CAA6 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]; current_palette[255] = ColorRgba::White();
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;
for (const auto group : pb::record_table->Groups) for (const auto group : pb::record_table->Groups)
{ {

View File

@ -8,22 +8,15 @@ enum class BitmapTypes : uint8_t
Spliced = 3, Spliced = 3,
}; };
struct ColorRgba
struct Rgba
{ {
uint8_t Blue; static constexpr ColorRgba Black() { return ColorRgba{ 0, 0, 0, 255 }; }
uint8_t Green; static constexpr ColorRgba White() { return ColorRgba{ 255, 255, 255, 255 }; }
uint8_t Red; static constexpr ColorRgba Red() { return ColorRgba{ 255, 0, 0, 255 }; }
uint8_t Alpha; static constexpr ColorRgba Green() { return ColorRgba{ 0, 255,0, 255 }; }
}; static constexpr ColorRgba Blue() { return ColorRgba{ 0, 0, 255, 255 }; }
union ColorRgba uint32_t Color;
{
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} }; }
ColorRgba() = default; ColorRgba() = default;
@ -32,13 +25,22 @@ union ColorRgba
{ {
} }
explicit constexpr ColorRgba(Rgba rgba) explicit constexpr ColorRgba(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
: rgba(rgba) : Color(alpha << alphaOffset | red << redOffset | green << greenOffset | blue << blueOffset)
{ {
} }
uint32_t Color; uint8_t GetAlpha() const { return (Color >> alphaOffset) & 0xffu; }
Rgba rgba; 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"); 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 gdrv::fill_bitmap(gfr_display, 1, height, width - 1, 0, ColorRgba::Black()); // Background
auto targetVal = dt < target ? dt : target; 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 gdrv::fill_bitmap(gfr_display, 1, targetHeight, width - 1, height - targetHeight, ColorRgba::White()); // Target
auto diffVal = dt < target ? target - dt : dt - 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 gdrv::fill_bitmap(gfr_display, 1, diffHeight, width - 1, height - targetHeight - diffHeight, ColorRgba::Red()); // Target diff
} }
updateCounter++; updateCounter++;
@ -691,7 +691,7 @@ int winmain::event_handler(const SDL_Event* event)
redGreen = i1; redGreen = i1;
} }
*pltPtr++ = ColorRgba{ Rgba{redGreen, redGreen, blue, 0} }; *pltPtr++ = ColorRgba{ blue, redGreen, redGreen, 0 };
} }
gdrv::display_palette(plt); gdrv::display_palette(plt);
delete[] plt; delete[] plt;

View File

@ -112,7 +112,6 @@ void zdrv::CreatePreview(zmap_header_type& zMap)
auto tmpBuff = new ColorRgba[zMap.Width * zMap.Height]; auto tmpBuff = new ColorRgba[zMap.Width * zMap.Height];
ColorRgba color{};
auto dst = tmpBuff; auto dst = tmpBuff;
auto src = zMap.ZPtr1; auto src = zMap.ZPtr1;
for (auto y = 0; y < zMap.Height; y++) 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++) for (auto x = 0; x < zMap.Width; x++)
{ {
auto depth = static_cast<uint8_t>((0xffff - *src++) / 0xff); auto depth = static_cast<uint8_t>((0xffff - *src++) / 0xff);
color.rgba.Blue = depth; *dst++ = ColorRgba{ depth, depth, depth, 0xff };
color.rgba.Green = depth;
color.rgba.Red = depth;
*dst++ = color;
} }
src += zMap.Stride - zMap.Width; src += zMap.Stride - zMap.Width;
} }