c: Shared random implementation

This commit is contained in:
2025-06-01 11:36:15 +10:00
parent ca30c29c06
commit f791a1d3d5
3 changed files with 23 additions and 18 deletions

View File

@@ -55,18 +55,6 @@ static float zoom = -15.0f;
static float tilt = 90.0f;
static float spin = 0.0f;
static uint32_t rngState = 1;
static inline int RngNext(void)
{
//TODO: check which one of these matches win32
#if 0
rngState = rngState * 1103515245 + 12345;
#else
rngState = rngState * 214013 + 2531011;
#endif
return (int)((rngState >> 16) & 0x7FFF); // (s / 65536) % 32768
}
static bool Lesson9_Init(NeHeContext* ctx)
{
@@ -192,9 +180,9 @@ static bool Lesson9_Init(NeHeContext* ctx)
{
.angle = 0.0f,
.distance = 5.0f * ((float)i / (float)numStars),
.r = (uint8_t)(RngNext() % 256),
.g = (uint8_t)(RngNext() % 256),
.b = (uint8_t)(RngNext() % 256)
.r = (uint8_t)(NeHe_Random() % 256),
.g = (uint8_t)(NeHe_Random() % 256),
.b = (uint8_t)(NeHe_Random() % 256)
};
}
@@ -270,9 +258,9 @@ static void Lesson9_Draw(NeHeContext* restrict ctx, SDL_GPUCommandBuffer* restri
if (star->distance < 0.0f)
{
star->distance += 5.0f;
star->r = (uint8_t)(RngNext() % 256);
star->g = (uint8_t)(RngNext() % 256);
star->b = (uint8_t)(RngNext() % 256);
star->r = (uint8_t)(NeHe_Random() % 256);
star->g = (uint8_t)(NeHe_Random() % 256);
star->b = (uint8_t)(NeHe_Random() % 256);
}
}
SDL_UnmapGPUTransferBuffer(ctx->device, instanceXferBuffer);

View File

@@ -6,6 +6,20 @@
#include "nehe.h"
static uint32_t rngState = 1;
int NeHe_Random(void)
{
rngState = rngState * 214013 + 2531011;
return (int)((rngState >> 16) & 0x7FFF); // (s / 65536) % 32768
}
void NeHe_RandomSeed(uint32_t seed)
{
rngState = seed;
}
bool NeHe_InitGPU(NeHeContext* ctx, const char* title, int width, int height)
{
// Create window

View File

@@ -26,6 +26,9 @@ typedef struct
unsigned fragmentSamplers;
} NeHeShaderProgramCreateInfo;
int NeHe_Random(void);
void NeHe_RandomSeed(uint32_t seed);
bool NeHe_InitGPU(NeHeContext* ctx, const char* title, int width, int height);
bool NeHe_SetupDepthTexture(NeHeContext* ctx, uint32_t width, uint32_t height,
SDL_GPUTextureFormat format, float clearDepth);