Loop for set count instead of time

This commit is contained in:
Abdelrahman Said 2025-05-31 03:01:14 +01:00
parent 89a72ad7c1
commit 62a305451b
3 changed files with 30 additions and 17 deletions

View File

@ -18,6 +18,7 @@
#define WANDERER_SPEEDUP_FACTOR 2.0f
#define MSG_BUF_LEN 4096
#define LINE_BUF_LEN 1024
#define LOOPS 100
#define abs(A) (A < 0 ? A * -1 : A)
#define min(A, B) (A < B ? A : B)

34
dod.c
View File

@ -5,7 +5,6 @@
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#define MOVABLE_TAG_SHIFT 0
#define RENDERABLE_TAG_SHIFT 1
@ -66,12 +65,12 @@ 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(u8 *tags, Rect *rects, Velocity *velocities, u64 count, f32 delta);
u64 update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count, f32 delta);
void render_entities(const u8 *tags, const Rect *rects, u64 count);
u8 collides(const Rect *rect, const Rect *collider);
i16 get_random_velocity(XOR256State *state);
void log_data(const Rect *rects, u64 count, FILE *fp, char *line);
u64 log_data(const Rect *rects, u64 count, FILE *fp, char *line);
int main(void) {
Allocator arena = wapp_mem_arena_allocator_init(MB(20));
@ -99,21 +98,22 @@ int main(void) {
FILE *fp = fopen("dod.out", "w");
assert(fp != NULL);
time_t start = time(NULL);
time_t elapsed = (time_t)-1;
u64 update_counter = 0;
u64 render_counter = 0;
f32 delta = 0.016666666f;
while (elapsed < 20) {
update_positions(manager.tags, manager.rects, manager.velocities, manager.count, delta);
for (u64 i = 0; i < LOOPS; ++i) {
update_counter += update_positions(manager.tags, manager.rects, manager.velocities, manager.count, delta);
char line[LINE_BUF_LEN] = {0};
log_data(manager.rects, manager.count, fp, line);
elapsed = time(NULL) - start;
render_counter += log_data(manager.rects, manager.count, fp, line);
}
fclose(fp);
wapp_mem_arena_allocator_destroy(&arena);
printf("UPDATE: %lu, RENDER: %lu\n", update_counter, render_counter);
return 0;
}
@ -167,7 +167,7 @@ void zero_velocity(Velocity *velocity, XOR256State *state) {
velocity->y = 0.0f;
}
void update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count, f32 delta) {
u64 update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count, f32 delta) {
persistent f32 multipliers[2] = {1.0f, 0.5f};
persistent u64 inside_zone_mask = 0x7;
@ -175,6 +175,7 @@ void update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count, f3
f32 pos_x, pos_y;
f32 max_x, max_y;
u64 total = 0;
for (u64 i = ZONE_COUNT; i < count; ++i) {
tags[i] &= inside_zone_mask;
for (u64 j = 0; j < ZONE_COUNT; ++j) {
@ -201,7 +202,11 @@ void update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count, f3
rects[i].position.x = roundf(pos_x);
rects[i].position.y = roundf(pos_y);
++total;
}
return total;
}
u8 collides(const Rect *rect, const Rect *collider) {
@ -223,7 +228,8 @@ i16 get_random_velocity(XOR256State *state) {
return (wapp_prng_xorshift_256(state) % (MAX_ABS_VELOCITY + 1 - MIN_ABS_VELOCITY)) + MIN_ABS_VELOCITY;
}
void log_data(const Rect *rects, u64 count, FILE *fp, char *line) {
u64 log_data(const Rect *rects, u64 count, FILE *fp, char *line) {
u64 total = 0;
for (u64 i = 0; i < count; ++i) {
snprintf(
line, LINE_BUF_LEN,
@ -236,5 +242,9 @@ void log_data(const Rect *rects, u64 count, FILE *fp, char *line) {
u64 size = strlen(line);
fwrite(line, sizeof(char), size, fp);
memset(line, 0, size);
++total;
}
return total;
}

View File

@ -66,11 +66,12 @@ int main(void) {
FILE *fp = fopen("no_dod.out", "w");
assert(fp != NULL);
time_t start = time(NULL);
time_t elapsed = (time_t)-1;
while (elapsed < 20) {
u64 update_counter = 0;
u64 render_counter = 0;
for (u64 i = 0; i < LOOPS; ++i) {
for (u64 i = 0; i < WANDERER_COUNT; ++i) {
move_wanderer(&(wanderers[i]), zones);
++update_counter;
}
char line[LINE_BUF_LEN] = {0};
@ -87,14 +88,15 @@ int main(void) {
u64 size = strlen(line);
fwrite(line, sizeof(char), size, fp);
memset(line, 0, size);
++render_counter;
}
elapsed = time(NULL) - start;
}
fclose(fp);
wapp_mem_arena_allocator_destroy(&arena);
printf("UPDATE: %lu, RENDER: %lu\n", update_counter, render_counter);
return 0;
}