Start rendering in the dod version and extract common header

This commit is contained in:
Abdelrahman Said 2025-05-18 14:33:12 +01:00
parent adb9d14c71
commit 66b366183d
3 changed files with 102 additions and 62 deletions

20
common.h Normal file
View File

@ -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)

79
dod.c
View File

@ -1,29 +1,11 @@
#include "wapp.h"
#include "common.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,
@ -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);

View File

@ -1,28 +1,10 @@
#include "wapp.h"
#include "common.h"
#include "raylib.h"
#include <stdint.h>
#include <stdbool.h>
#include <assert.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)
#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();
}