This commit is contained in:
Abdelrahman Said 2025-05-13 14:03:43 +01:00
parent 716ea8638d
commit adb9d14c71
4 changed files with 206 additions and 25 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
.cache
compile_commands.json
no_dod
*dod
*.png
raylib
raylib-build
*_stats

View File

@ -6,18 +6,22 @@ LDFLAGS = '-Wl,-rpath,$$ORIGIN/raylib/lib' -Lraylib/lib -lraylib
ifeq ($(BUILD_TYPE),debug)
CFLAGS += -g
else
CFLAGS += -O2 -DNDEBUG
CFLAGS += -O3 -DNDEBUG
endif
.PHONY: all raylib game
.PHONY: all raylib no_dod dod
all: game
all: no_dod dod
raylib:
cmake -S raylib-src -B raylib-build -DCMAKE_INSTALL_PREFIX=raylib -DCMAKE_CONFIGURATION_TYPES=Release -DBUILD_SHARED_LIBS=ON -G "Ninja Multi-Config"
cmake --build raylib-build --config=Release
cmake --install raylib-build --config=Release
game: raylib
no_dod: raylib
cd wapp && python3 -m codegen
$(CC) $(CFLAGS) $(LDFLAGS) no_dod.c wapp/src/wapp.c -o no_dod
dod: raylib
cd wapp && python3 -m codegen
$(CC) $(CFLAGS) $(LDFLAGS) dod.c wapp/src/wapp.c -o dod

165
dod.c Normal file
View File

@ -0,0 +1,165 @@
#include "wapp.h"
#include "raylib.h"
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#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,
ENTITY_TAG_COLLIDER = 1 << 2,
};
typedef struct Entity Entity;
struct Entity {
u16 id;
u8 tag;
};
typedef struct Position Position;
struct Position {
f32 x;
f32 y;
};
typedef struct Scale Scale;
struct Scale {
f32 width;
f32 height;
};
typedef struct Velocity Velocity;
struct Velocity {
f32 x;
f32 y;
};
typedef struct Manager Manager;
struct Manager {
Entity *entities;
Position *positions;
Scale *scales;
Velocity *velocities;
u64 count;
};
typedef void (*ScaleInitialiser)(Scale *scale, XOR256State *state);
typedef void (*VelocityInitialiser)(Velocity *velocity, XOR256State *state);
void init_manager(const Allocator *allocator, Manager *manager);
void init_position(Position *position, XOR256State *state);
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);
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));
XOR256State state = wapp_prng_xorshift_init_state();
Manager manager = {0};
init_manager(&arena, &manager);
ScaleInitialiser scale_initialisers[2] = {init_scale_wanderer, init_scale_zone};
VelocityInitialiser velocity_initialisers[2] = {init_velocity, zero_velocity};
for (u64 i = 0; i < manager.count; ++i) {
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;
init_position(&(manager.positions[i]), &state);
scale_initialisers[is_zone](&(manager.scales[i]), &state);
velocity_initialisers[is_zone](&(manager.velocities[i]), &state);
}
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(BG_COLOR);
EndDrawing();
}
wapp_mem_arena_allocator_destroy(&arena);
CloseWindow();
return 0;
}
void init_manager(const Allocator *allocator, Manager *manager) {
assert(allocator != NULL && manager != NULL);
u64 total_count = (u64)WANDERER_COUNT + (u64)ZONE_COUNT;
u64 entities_size = sizeof(Entity) * total_count;
u64 positions_size = sizeof(Position) * total_count;
u64 scales_size = sizeof(Scale) * total_count;
u64 velocities_size = sizeof(Velocity) * total_count;
u64 allocation_size = entities_size + positions_size + scales_size + velocities_size;
u8 *buffer = wapp_mem_allocator_alloc(allocator, allocation_size);
assert(buffer != NULL);
memset(buffer, 0, allocation_size);
manager->entities = (Entity *)buffer;
manager->positions = (Position *)(buffer + entities_size);
manager->scales = (Scale *)(buffer + entities_size + positions_size);
manager->velocities = (Velocity *)(buffer + entities_size + positions_size + scales_size);
manager->count = total_count;
}
void init_position(Position *position, XOR256State *state) {
position->x = wapp_prng_xorshift_256(state) % WIDTH;
position->y = wapp_prng_xorshift_256(state) % HEIGHT;
}
void init_scale_wanderer(Scale *scale, XOR256State *state) {
f32 value = (f32)((wapp_prng_xorshift_256(state) % (MAX_WANDERER_DIM + 1 - MIN_WANDERER_DIM)) + MIN_WANDERER_DIM);
scale->width = value;
scale->height = value;
}
void init_scale_zone(Scale *scale, XOR256State *state) {
scale->width = wapp_prng_xorshift_256(state) % ((u64)HALF_WIDTH - MIN_ZONE_DIM) + MIN_ZONE_DIM;
scale->height = wapp_prng_xorshift_256(state) % ((u64)HALF_HEIGHT - MIN_ZONE_DIM) + MIN_ZONE_DIM;
}
void init_velocity(Velocity *velocity, XOR256State *state) {
velocity->x = get_random_float(state);
velocity->y = get_random_float(state);
}
void zero_velocity(Velocity *velocity, XOR256State *state) {
velocity->x = 0.0f;
velocity->y = 0.0f;
}
f32 get_random_float(XOR256State *state) {
i64 random = (i64)wapp_prng_xorshift_256(state) - INT64_MAX;
return (f32)(random) / (f32)(INT64_MAX);
}

View File

@ -14,7 +14,7 @@
#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 50000
#define WANDERER_COUNT 500000
#define ZONE_COUNT 5
#define WANDERER_SLOWDOWN_FACTOR 0.5f
#define WANDERER_SPEEDUP_FACTOR 2.0f
@ -71,12 +71,17 @@ int main(void) {
InitWindow(WIDTH, HEIGHT, "DOD test");
SetTargetFPS(120);
Allocator arena = wapp_mem_arena_allocator_init(MB(10));
Allocator arena = wapp_mem_arena_allocator_init(MB(20));
XOR256State state = wapp_prng_xorshift_init_state();
Wanderer *wanderers = wapp_mem_allocator_alloc(&arena, sizeof(Wanderer) * WANDERER_COUNT);
SlowZone *zones = wapp_mem_allocator_alloc(&arena, sizeof(SlowZone) * ZONE_COUNT);
u64 wanderers_size = sizeof(Wanderer) * WANDERER_COUNT;
u64 zones_size = sizeof(SlowZone) * ZONE_COUNT;
Wanderer *wanderers = wapp_mem_allocator_alloc(&arena, wanderers_size);
SlowZone *zones = wapp_mem_allocator_alloc(&arena, zones_size);
assert(wanderers != NULL && zones != NULL);
memset(wanderers, 0, wanderers_size);
memset(zones, 0, zones_size);
for (u64 i = 0; i < WANDERER_COUNT; ++i) {
init_wanderer(&(wanderers[i]), &state);
}
@ -84,25 +89,31 @@ 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()) {
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();
}