From 51e9d594f211357d0c05a831035867a6b3c38785 Mon Sep 17 00:00:00 2001 From: Ryland <91900600+Rylxnd@users.noreply.github.com> Date: Sat, 21 Oct 2023 12:56:41 -0400 Subject: [PATCH] ChaosMod: Pack Color struct (#3621) Use std::uint8_t in place of int to save some precious bytes --------- Co-authored-by: pongo1231 --- ChaosMod/Memory/Drawing.h | 22 ++++++++-------------- ChaosMod/Util/Color.h | 27 +++++++++++++++++---------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/ChaosMod/Memory/Drawing.h b/ChaosMod/Memory/Drawing.h index 42388e089..14ae94952 100644 --- a/ChaosMod/Memory/Drawing.h +++ b/ChaosMod/Memory/Drawing.h @@ -1,5 +1,7 @@ #pragma once +#include + // Thanks CitizenFX! // Adapted by MoneyWasted for ChaosModV // https://github.com/citizenfx/fivem/blob/master/code/components/extra-natives-five/src/Draw2dNatives.cpp @@ -11,7 +13,7 @@ namespace Memory { - inline void DrawLine(float x1, float y1, float x2, float y2, float width, int r, int g, int b, int a) + inline void DrawLine(float x1, float y1, float x2, float y2, float width, std::uint32_t color) { static char *(*AllocateDrawRect)(void *); static void (*SetDrawRectCoords)(void *, float, float, float, float); @@ -50,26 +52,18 @@ namespace Memory // Analysis in IDA shows that the return value of AllocateDrawRect would be a structure. if (auto rect = AllocateDrawRect(&drawRects[drawRectsSize * *mainThreadFrameIndex])) { - // Clamp each color value then combine them. - uint32_t color = ( - (std::clamp(a, 0, 255) << 24) | - (std::clamp(r, 0, 255) << 16) | - (std::clamp(g, 0, 255) << 8) | - (std::clamp(b, 0, 255) << 0) - ); - SetDrawRectCoords(rect, x1, y1, x2, y2); // Set unknown structure pointers. - *(uint32_t *)(rect + 0x34) &= 0xFA; - *(uint32_t *)(rect + 0x34) |= 0x8A; + *(std::uint32_t *)(rect + 0x34) &= 0xFA; + *(std::uint32_t *)(rect + 0x34) |= 0x8A; *(float *)(rect + 0x1C) = width; - *(uint32_t *)(rect + 0x28) = color; + *(std::uint32_t *)(rect + 0x28) = color; } } - inline void DrawLine(ChaosVector2 firstPos, ChaosVector2 secondPos, Color color, float thickness) + inline void DrawLine(ChaosVector2 firstPos, ChaosVector2 secondPos, Color &color, float thickness) { - DrawLine(firstPos.x, firstPos.y, secondPos.x, secondPos.y, thickness, color.R, color.G, color.B, color.A); + DrawLine(firstPos.x, firstPos.y, secondPos.x, secondPos.y, thickness, color.Get()); } } \ No newline at end of file diff --git a/ChaosMod/Util/Color.h b/ChaosMod/Util/Color.h index 59576f8a3..33796fe34 100644 --- a/ChaosMod/Util/Color.h +++ b/ChaosMod/Util/Color.h @@ -1,28 +1,35 @@ #pragma once +#include + struct Color { - unsigned int R, G, B, A; + std::uint8_t R, G, B, A; + + std::uint32_t Get() const + { + return A << 24 | R << 16 | G << 8 | B; + } }; // Returns a random RGBA value without a random alpha: 255 or max. -inline Color GetRandomColorRGB(unsigned int min = 0, unsigned int max = 255) +inline Color GetRandomColorRGB(std::uint8_t min = 0, std::uint8_t max = 255) { return { - (unsigned int)g_Random.GetRandomInt(min, max), // R - (unsigned int)g_Random.GetRandomInt(min, max), // G - (unsigned int)g_Random.GetRandomInt(min, max), // B + (std::uint8_t)g_Random.GetRandomInt(min, max), // R + (std::uint8_t)g_Random.GetRandomInt(min, max), // G + (std::uint8_t)g_Random.GetRandomInt(min, max), // B 255 // A }; } // Returns a random RGBA value with a random alpha. -inline Color GetRandomColorRGBA(unsigned int min = 0, unsigned int max = 255) +inline Color GetRandomColorRGBA(std::uint8_t min = 0, std::uint8_t max = 255) { return { - (unsigned int)g_Random.GetRandomInt(min, max), // R - (unsigned int)g_Random.GetRandomInt(min, max), // G - (unsigned int)g_Random.GetRandomInt(min, max), // B - (unsigned int)g_Random.GetRandomInt(min, max) // A + (std::uint8_t)g_Random.GetRandomInt(min, max), // R + (std::uint8_t)g_Random.GetRandomInt(min, max), // G + (std::uint8_t)g_Random.GetRandomInt(min, max), // B + (std::uint8_t)g_Random.GetRandomInt(min, max) // A }; } \ No newline at end of file