From 66b366183d44c1e0401480b3bb5848dc4a95f79b Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sun, 18 May 2025 14:33:12 +0100 Subject: [PATCH] Start rendering in the dod version and extract common header --- common.h | 20 ++++++++++++++ dod.c | 79 +++++++++++++++++++++++++++++++++++++++++--------------- no_dod.c | 65 +++++++++++++++++----------------------------- 3 files changed, 102 insertions(+), 62 deletions(-) create mode 100644 common.h diff --git a/common.h b/common.h new file mode 100644 index 0000000..5eb00b0 --- /dev/null +++ b/common.h @@ -0,0 +1,20 @@ +#pragma once + +#define WIDTH 1280 +#define HEIGHT 720 +#define HALF_WIDTH (WIDTH * 0.5f) +#define HALF_HEIGHT (HEIGHT * 0.5f) +#define MAX_WANDERER_DIM 8 +#define MIN_WANDERER_DIM 3 +#define MIN_ZONE_DIM 30 +#define BG_COLOR (Color){.r = 0xea, .g = 0xf2, .b = 0xe3, .a = 0xff} +#define FG_COLOR (Color){.r = 0x42, .g = 0x4C, .b = 0x55, .a = 0xff} +#define ZONE_COLOR (Color){.r = 0xb4, .g = 0x65, .b = 0x4a, .a = 0xff} +#define WANDERER_COUNT 300000 +#define ZONE_COUNT 5 +#define WANDERER_SLOWDOWN_FACTOR 0.5f +#define WANDERER_SPEEDUP_FACTOR 2.0f +#define MSG_BUF_LEN 4096 +#define abs(A) (A < 0 ? A * -1 : A) +#define min(A, B) (A < B ? A : B) +#define max(A, B) (A > B ? A : B) diff --git a/dod.c b/dod.c index 8ec994a..903ce13 100644 --- a/dod.c +++ b/dod.c @@ -1,29 +1,11 @@ #include "wapp.h" +#include "common.h" #include "raylib.h" #include #include #include #include -#define WIDTH 1280 -#define HEIGHT 720 -#define HALF_WIDTH (WIDTH * 0.5f) -#define HALF_HEIGHT (HEIGHT * 0.5f) -#define MAX_WANDERER_DIM 8 -#define MIN_WANDERER_DIM 3 -#define MIN_ZONE_DIM 30 -#define BG_COLOR (Color){.r = 0xea, .g = 0xf2, .b = 0xe3, .a = 0xff} -#define FG_COLOR (Color){.r = 0x42, .g = 0x4C, .b = 0x55, .a = 0xff} -#define ZONE_COLOR (Color){.r = 0xb4, .g = 0x65, .b = 0x4a, .a = 0xff} -#define WANDERER_COUNT 500000 -#define ZONE_COUNT 5 -#define WANDERER_SLOWDOWN_FACTOR 0.5f -#define WANDERER_SPEEDUP_FACTOR 2.0f -#define MSG_BUF_LEN 4096 -#define abs(A) (A < 0 ? A * -1 : A) -#define min(A, B) (A < B ? A : B) -#define max(A, B) (A > B ? A : B) - enum EntityTag { ENTITY_TAG_MOVABLE = 1 << 0, ENTITY_TAG_RENDERABLE = 1 << 1, @@ -65,6 +47,7 @@ struct Manager { typedef void (*ScaleInitialiser)(Scale *scale, XOR256State *state); typedef void (*VelocityInitialiser)(Velocity *velocity, XOR256State *state); +typedef void (*RaylibDrawRectFunc)(int posX, int posY, int width, int height, Color color); void init_manager(const Allocator *allocator, Manager *manager); void init_position(Position *position, XOR256State *state); @@ -72,13 +55,18 @@ void init_scale_wanderer(Scale *scale, XOR256State *state); void init_scale_zone(Scale *scale, XOR256State *state); void init_velocity(Velocity *velocity, XOR256State *state); void zero_velocity(Velocity *velocity, XOR256State *state); +void update_positions(Position *positions, Scale *scales, Velocity *velocities, u64 count); +void get_colliders(); +void render_entities(const Entity *entities, const Position *positions, const Scale *scales, u64 count); f32 get_random_float(XOR256State *state); int main(void) { InitWindow(WIDTH, HEIGHT, "DOD test"); SetTargetFPS(120); - Allocator arena = wapp_mem_arena_allocator_init(MB(20)); + Allocator arena = wapp_mem_arena_allocator_init(MB(20)); + assert(!wapp_mem_allocator_invalid(&arena)); + XOR256State state = wapp_prng_xorshift_init_state(); Manager manager = {0}; @@ -91,7 +79,12 @@ int main(void) { u8 is_zone = i < ZONE_COUNT; manager.entities[i].id = i; - manager.entities[i].tag = (ENTITY_TAG_COLLIDER & is_zone) | ENTITY_TAG_MOVABLE | ENTITY_TAG_RENDERABLE; + manager.entities[i].tag = (ENTITY_TAG_COLLIDER & (is_zone << 2)) | ENTITY_TAG_MOVABLE; +#ifndef NDEBUG + manager.entities[i].tag |= ENTITY_TAG_RENDERABLE; +#else + manager.entities[i].tag |= ENTITY_TAG_RENDERABLE & ~(is_zone << 1); +#endif init_position(&(manager.positions[i]), &state); scale_initialisers[is_zone](&(manager.scales[i]), &state); @@ -99,8 +92,21 @@ int main(void) { } while (!WindowShouldClose()) { + f64 time = GetTime(); + if (time >= 20.0) { + break; + } + + update_positions(manager.positions, manager.scales, manager.velocities, manager.count); + BeginDrawing(); + ClearBackground(BG_COLOR); + + render_entities(manager.entities, manager.positions, manager.scales, manager.count); + + DrawFPS(10, 10); + EndDrawing(); } @@ -159,6 +165,37 @@ void zero_velocity(Velocity *velocity, XOR256State *state) { velocity->y = 0.0f; } +void update_positions(Position *positions, Scale *scales, Velocity *velocities, u64 count) { + for (u64 i = 0; i < count; ++i) { + positions[i].x += velocities[i].x; + positions[i].y += velocities[i].y; + + f32 max_x = WIDTH - scales[i].width; + f32 max_y = HEIGHT - scales[i].height; + + if (positions[i].x < 0 || positions[i].x >= max_x) { + positions[i].x = min(max(positions[i].x, 0), max_x); + velocities[i].x *= -1; + } + + if (positions[i].y < 0 || positions[i].y >= max_y) { + positions[i].y = min(max(positions[i].y, 0), max_y); + velocities[i].y *= -1; + } + } +} + +void render_entities(const Entity *entities, const Position *positions, const Scale *scales, u64 count) { + persistent RaylibDrawRectFunc renderers[2] = {DrawRectangle, DrawRectangleLines}; + persistent Color colors[2] = {FG_COLOR, ZONE_COLOR}; + + for (u64 i = 0; i < count; ++i) { + u64 is_collider = (entities[i].tag & ENTITY_TAG_COLLIDER) == ENTITY_TAG_COLLIDER; + + renderers[is_collider](positions[i].x, positions[i].y, scales[i].width, scales[i].height, colors[is_collider]); + } +} + f32 get_random_float(XOR256State *state) { i64 random = (i64)wapp_prng_xorshift_256(state) - INT64_MAX; return (f32)(random) / (f32)(INT64_MAX); diff --git a/no_dod.c b/no_dod.c index 2ad47c9..c6d1c29 100644 --- a/no_dod.c +++ b/no_dod.c @@ -1,28 +1,10 @@ #include "wapp.h" +#include "common.h" #include "raylib.h" #include #include #include -#define WIDTH 1280 -#define HEIGHT 720 -#define HALF_WIDTH (WIDTH * 0.5f) -#define HALF_HEIGHT (HEIGHT * 0.5f) -#define MAX_WANDERER_DIM 8 -#define MIN_WANDERER_DIM 3 -#define MIN_ZONE_DIM 30 -#define BG_COLOR (Color){.r = 0xea, .g = 0xf2, .b = 0xe3, .a = 0xff} -#define FG_COLOR (Color){.r = 0x42, .g = 0x4C, .b = 0x55, .a = 0xff} -#define ZONE_COLOR (Color){.r = 0xb4, .g = 0x65, .b = 0x4a, .a = 0xff} -#define WANDERER_COUNT 500000 -#define ZONE_COUNT 5 -#define WANDERER_SLOWDOWN_FACTOR 0.5f -#define WANDERER_SPEEDUP_FACTOR 2.0f -#define MSG_BUF_LEN 4096 -#define abs(A) (A < 0 ? A * -1 : A) -#define min(A, B) (A < B ? A : B) -#define max(A, B) (A > B ? A : B) - #define OBJECT_COMMON struct {Position position; Scale scale;} typedef struct Position Position; @@ -89,31 +71,32 @@ int main(void) { init_slow_zone(&(zones[i]),&state); } -// while (!WindowShouldClose()) { -// for (u64 i = 0; i < WANDERER_COUNT; ++i) { -// move_wanderer(&(wanderers[i]), zones); -// } -// -// BeginDrawing(); -// -// ClearBackground(BG_COLOR); -// -// #ifndef NDEBUG -// for (u64 i = 0; i < ZONE_COUNT; ++i) { -// render_slow_zone(&(zones[i])); -// } -// #endif -// -// for (u64 i = 0; i < WANDERER_COUNT; ++i) { -// render_wanderer(&(wanderers[i])); -// } -// -// EndDrawing(); -// } - while (!WindowShouldClose()) { + f64 time = GetTime(); + if (time >= 20.0) { + break; + } + + for (u64 i = 0; i < WANDERER_COUNT; ++i) { + move_wanderer(&(wanderers[i]), zones); + } + BeginDrawing(); + ClearBackground(BG_COLOR); + +#ifndef NDEBUG + for (u64 i = 0; i < ZONE_COUNT; ++i) { + render_slow_zone(&(zones[i])); + } +#endif + + for (u64 i = 0; i < WANDERER_COUNT; ++i) { + render_wanderer(&(wanderers[i])); + } + + DrawFPS(10, 10); + EndDrawing(); }