From d78adcb9c931481ce54617eb2989498b2abab483 Mon Sep 17 00:00:00 2001 From: jacob Date: Fri, 17 Jan 2025 10:14:21 -0600 Subject: [PATCH] copy random noise resource to memory on startup --- src/app.c | 24 ------------------------ src/config.h | 2 +- src/rng.c | 30 +++++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/app.c b/src/app.c index 7d3306fb..4f154dd8 100644 --- a/src/app.c +++ b/src/app.c @@ -118,30 +118,6 @@ void app_register_exit_callback(app_exit_callback_func *func) sys_mutex_unlock(&lock); } -/* ========================== * - * Generate random number file - * (unused function) - * ========================== */ - -#if 0 -#include "rng.h" -INTERNAL void gen_random_file(struct string path, u32 count) -{ - { - /* Clear file */ - struct sys_file f = sys_file_open_write(path); - sys_file_write(f, BUFFER(0, 0)); - sys_file_close(f); - } - struct sys_file f = sys_file_open_append(path); - for (u32 i = 0; i < count; ++i) { - u64 rand = rng_rand_u64(); - sys_file_write(f, BUFFER_FROM_STRUCT(&rand)); - } - sys_file_close(f); -} -#endif - /* ========================== * * Entry point * ========================== */ diff --git a/src/config.h b/src/config.h index 7546209b..e9cd9c4d 100644 --- a/src/config.h +++ b/src/config.h @@ -29,7 +29,7 @@ #define PIXELS_PER_UNIT 256.0 -#define GAME_FPS 100.0 +#define GAME_FPS 50.0 #define GAME_TIMESCALE 1 #define GAME_PHYSICS_SUBSTEPS 4 diff --git a/src/rng.c b/src/rng.c index 4c421021..b9187d35 100644 --- a/src/rng.c +++ b/src/rng.c @@ -1,12 +1,37 @@ #include "rng.h" #include "sys.h" #include "resource.h" +#include "arena.h" GLOBAL struct { + struct arena arena; u64 *noise; u64 noise_count; } G = ZI, DEBUG_ALIAS(G, G_rng); +/* ========================== * + * Generate random number file + * (unused function) + * ========================== */ + +#if 0 +INTERNAL void gen_random_file(struct string path, u32 count) +{ + { + /* Clear file */ + struct sys_file f = sys_file_open_write(path); + sys_file_write(f, BUFFER(0, 0)); + sys_file_close(f); + } + struct sys_file f = sys_file_open_append(path); + for (u32 i = 0; i < count; ++i) { + u64 rand = rng_rand_u64(); + sys_file_write(f, BUFFER_FROM_STRUCT(&rand)); + } + sys_file_close(f); +} +#endif + /* ========================== * * Startup * ========================== */ @@ -14,11 +39,14 @@ GLOBAL struct { struct rng_startup_receipt rng_startup(struct resource_startup_receipt *resource_sr) { (UNUSED)resource_sr; + G.arena = arena_alloc(GIGABYTE(64)); struct string rand_path = STR("res/rand.dat"); if (resource_exists(rand_path)) { struct resource r = resource_open(rand_path); - G.noise = (u64 *)r.bytes.data; G.noise_count = r.bytes.size / sizeof(*G.noise); + G.noise = arena_push_array(&G.arena, u64, G.noise_count); + MEMCPY(G.noise, r.bytes.data, r.bytes.size); + resource_close(r); } else { sys_panic(STR("Failed to locate pre-computed noise resource")); }