Skip to content

Commit

Permalink
ChaosMod: Pack Color struct (#3621)
Browse files Browse the repository at this point in the history
Use std::uint8_t in place of int to save some precious bytes

---------

Co-authored-by: pongo1231 <pongo1999712@gmail.com>
  • Loading branch information
Rylxnd and pongo1231 authored Oct 21, 2023
1 parent 4b98b4b commit 51e9d59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
22 changes: 8 additions & 14 deletions ChaosMod/Memory/Drawing.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <cstdint>

// Thanks CitizenFX!
// Adapted by MoneyWasted for ChaosModV
// https://github.com/citizenfx/fivem/blob/master/code/components/extra-natives-five/src/Draw2dNatives.cpp
Expand All @@ -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);
Expand Down Expand Up @@ -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());
}
}
27 changes: 17 additions & 10 deletions ChaosMod/Util/Color.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
#pragma once

#include <cstdint>

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

0 comments on commit 51e9d59

Please sign in to comment.