203 lines
6.1 KiB
C
203 lines
6.1 KiB
C
#include "wapp.h"
|
|
#include "common.h"
|
|
#include "raylib.h"
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
|
|
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);
|
|
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);
|
|
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));
|
|
assert(!wapp_mem_allocator_invalid(&arena));
|
|
|
|
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 << 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);
|
|
velocity_initialisers[is_zone](&(manager.velocities[i]), &state);
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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);
|
|
}
|