Test without Raylib
This commit is contained in:
		
							
								
								
									
										6
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Makefile
									
									
									
									
									
								
							@@ -1,6 +1,8 @@
 | 
			
		||||
CC            = clang
 | 
			
		||||
CFLAGS        = -g -O3 -Iraylib/include -Iwapp/src
 | 
			
		||||
LDFLAGS       = '-Wl,-rpath,$$ORIGIN/raylib/lib' -Lraylib/lib -lraylib -lm -lpthread
 | 
			
		||||
# CFLAGS        = -g -O3 -Iraylib/include -Iwapp/src
 | 
			
		||||
# LDFLAGS       = '-Wl,-rpath,$$ORIGIN/raylib/lib' -Lraylib/lib -lraylib -lm -lpthread
 | 
			
		||||
CFLAGS        = -g -O3 -Iwapp/src
 | 
			
		||||
LDFLAGS       = -lm
 | 
			
		||||
BASEDIR       = $(shell realpath ./)
 | 
			
		||||
RL_SRCDIR     = ${BASEDIR}/raylib-src/src
 | 
			
		||||
RL_BUILDDIR   = ${BASEDIR}/raylib-build
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								common.h
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								common.h
									
									
									
									
									
								
							@@ -17,6 +17,7 @@
 | 
			
		||||
#define WANDERER_SLOWDOWN_FACTOR 0.5f
 | 
			
		||||
#define WANDERER_SPEEDUP_FACTOR 2.0f
 | 
			
		||||
#define MSG_BUF_LEN 4096
 | 
			
		||||
#define LINE_BUF_LEN 1024
 | 
			
		||||
 | 
			
		||||
#define abs(A) (A < 0 ? A * -1 : A)
 | 
			
		||||
#define min(A, B) (A < B ? A : B)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										155
									
								
								dod.c
									
									
									
									
									
								
							
							
						
						
									
										155
									
								
								dod.c
									
									
									
									
									
								
							@@ -1,14 +1,11 @@
 | 
			
		||||
#include "wapp.h"
 | 
			
		||||
#include "common.h"
 | 
			
		||||
#include "raylib.h"
 | 
			
		||||
#include <math.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
 | 
			
		||||
#define THREAD_COUNT 4
 | 
			
		||||
#include <time.h>
 | 
			
		||||
 | 
			
		||||
#define MOVABLE_TAG_SHIFT     0
 | 
			
		||||
#define RENDERABLE_TAG_SHIFT  1
 | 
			
		||||
@@ -60,20 +57,8 @@ struct Manager {
 | 
			
		||||
  u64      count;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct PositionThreadArgs PositionThreadArgs;
 | 
			
		||||
struct PositionThreadArgs {
 | 
			
		||||
  const Rect *zones;
 | 
			
		||||
  Rect       *rects;
 | 
			
		||||
  Velocity   *velocities;
 | 
			
		||||
  u8         *tags;
 | 
			
		||||
  u64        count;
 | 
			
		||||
  f32        delta;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef void *(*PThreadRoutine)(void *);
 | 
			
		||||
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);
 | 
			
		||||
@@ -82,18 +67,13 @@ 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);
 | 
			
		||||
void *update_position_thread(PositionThreadArgs *args);
 | 
			
		||||
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);
 | 
			
		||||
 | 
			
		||||
int main(void) {
 | 
			
		||||
  i32 target_fps = 120;
 | 
			
		||||
 | 
			
		||||
  SetTraceLogLevel(LOG_NONE);
 | 
			
		||||
  InitWindow(WIDTH, HEIGHT, "DOD test");
 | 
			
		||||
  SetTargetFPS(target_fps);
 | 
			
		||||
 | 
			
		||||
  Allocator arena = wapp_mem_arena_allocator_init(MB(20));
 | 
			
		||||
  assert(!wapp_mem_allocator_invalid(&arena));
 | 
			
		||||
 | 
			
		||||
@@ -116,35 +96,24 @@ int main(void) {
 | 
			
		||||
    velocity_initialisers[is_zone](&(manager.velocities[i]), &state);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  f32 last_time = GetFrameTime();
 | 
			
		||||
  f32 delta, cur_time;
 | 
			
		||||
 | 
			
		||||
  while (!WindowShouldClose()) {
 | 
			
		||||
    f64 time = GetTime();
 | 
			
		||||
    if (time >= 20.0) {
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cur_time = GetFrameTime();
 | 
			
		||||
    delta    = cur_time - last_time;
 | 
			
		||||
  FILE *fp = fopen("dod.out", "w");
 | 
			
		||||
  assert(fp != NULL);
 | 
			
		||||
 | 
			
		||||
  time_t start   = time(NULL);
 | 
			
		||||
  time_t elapsed = (time_t)-1;
 | 
			
		||||
  f32 delta = 0.016666666f;
 | 
			
		||||
  while (elapsed < 20) {
 | 
			
		||||
    update_positions(manager.tags, manager.rects, manager.velocities, manager.count, delta);
 | 
			
		||||
 | 
			
		||||
    BeginDrawing();
 | 
			
		||||
    char line[LINE_BUF_LEN] = {0};
 | 
			
		||||
 | 
			
		||||
    ClearBackground(BG_COLOR);
 | 
			
		||||
    log_data(manager.rects, manager.count, fp, line);
 | 
			
		||||
 | 
			
		||||
    render_entities(manager.tags, manager.rects, manager.count);
 | 
			
		||||
 | 
			
		||||
    DrawFPS(10, 10);
 | 
			
		||||
 | 
			
		||||
    EndDrawing();
 | 
			
		||||
    elapsed = time(NULL) - start;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  wapp_mem_arena_allocator_destroy(&arena);
 | 
			
		||||
 | 
			
		||||
  CloseWindow();
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -199,46 +168,6 @@ void zero_velocity(Velocity *velocity, XOR256State *state) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count, f32 delta) {
 | 
			
		||||
  persistent pthread_t threads[THREAD_COUNT]       = {0};
 | 
			
		||||
  persistent PositionThreadArgs args[THREAD_COUNT] = {0};
 | 
			
		||||
 | 
			
		||||
  u64 total_entities_count  = count - ZONE_COUNT;
 | 
			
		||||
  u64 thread_entities_count = (u64)(ceil((f64)total_entities_count / THREAD_COUNT));
 | 
			
		||||
 | 
			
		||||
  i32 result;
 | 
			
		||||
  u64 start = ZONE_COUNT;
 | 
			
		||||
  u64 end, args_count;
 | 
			
		||||
 | 
			
		||||
  for (u64 i = 0; i < THREAD_COUNT; ++i) {
 | 
			
		||||
    if (total_entities_count > thread_entities_count) {
 | 
			
		||||
      end = start + thread_entities_count;
 | 
			
		||||
      total_entities_count -= thread_entities_count;
 | 
			
		||||
    } else {
 | 
			
		||||
      end = start + total_entities_count;
 | 
			
		||||
      total_entities_count = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    args_count = end - start;
 | 
			
		||||
 | 
			
		||||
    args[i].zones      = &(rects[0]);
 | 
			
		||||
    args[i].tags       = &(tags[start]);
 | 
			
		||||
    args[i].rects      = &(rects[start]);
 | 
			
		||||
    args[i].velocities = &(velocities[start]);
 | 
			
		||||
    args[i].count      = args_count;
 | 
			
		||||
    args[i].delta      = delta;
 | 
			
		||||
 | 
			
		||||
    start += args_count;
 | 
			
		||||
 | 
			
		||||
    result = pthread_create(&(threads[i]), NULL, (PThreadRoutine)update_position_thread, (void *)&(args[i]));
 | 
			
		||||
    assert(result == 0);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for (u64 i = 0; i < THREAD_COUNT; ++i) {
 | 
			
		||||
    pthread_join(threads[i], NULL);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void *update_position_thread(PositionThreadArgs *args) {
 | 
			
		||||
  persistent f32 multipliers[2] = {1.0f, 0.5f};
 | 
			
		||||
  persistent u64 inside_zone_mask = 0x7;
 | 
			
		||||
 | 
			
		||||
@@ -246,52 +175,32 @@ void *update_position_thread(PositionThreadArgs *args) {
 | 
			
		||||
  f32 pos_x, pos_y;
 | 
			
		||||
  f32 max_x, max_y;
 | 
			
		||||
 | 
			
		||||
  for (u64 i = 0; i < args->count; ++i) {
 | 
			
		||||
    args->tags[i] &= inside_zone_mask;
 | 
			
		||||
  for (u64 i = ZONE_COUNT; i < count; ++i) {
 | 
			
		||||
    tags[i] &= inside_zone_mask;
 | 
			
		||||
    for (u64 j = 0; j < ZONE_COUNT; ++j) {
 | 
			
		||||
      args->tags[i] |= collides(&args->rects[i], &args->zones[j]) << INSIDE_ZONE_TAG_SHIFT;
 | 
			
		||||
      tags[i] |= collides(&rects[i], &rects[j]) << INSIDE_ZONE_TAG_SHIFT;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    index = (args->tags[i] & ENTITY_TAG_INSIDE_ZONE) >> INSIDE_ZONE_TAG_SHIFT;
 | 
			
		||||
    index = (tags[i] & ENTITY_TAG_INSIDE_ZONE) >> INSIDE_ZONE_TAG_SHIFT;
 | 
			
		||||
 | 
			
		||||
    max_x = WIDTH - args->rects[i].scale.width;
 | 
			
		||||
    max_y = HEIGHT - args->rects[i].scale.height;
 | 
			
		||||
    max_x = WIDTH - rects[i].scale.width;
 | 
			
		||||
    max_y = HEIGHT - rects[i].scale.height;
 | 
			
		||||
 | 
			
		||||
    pos_x = args->rects[i].position.x + args->velocities[i].x * multipliers[index] * args->delta;
 | 
			
		||||
    pos_y = args->rects[i].position.y + args->velocities[i].y * multipliers[index] * args->delta;
 | 
			
		||||
    pos_x = rects[i].position.x + velocities[i].x * multipliers[index] * delta;
 | 
			
		||||
    pos_y = rects[i].position.y + velocities[i].y * multipliers[index] * delta;
 | 
			
		||||
 | 
			
		||||
    if (pos_x < 0 || pos_x >= max_x) {
 | 
			
		||||
      pos_x = min(max(pos_x, 0), max_x);
 | 
			
		||||
      args->velocities[i].x *= -1;
 | 
			
		||||
      velocities[i].x *= -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (pos_y < 0 || pos_y >= max_y) {
 | 
			
		||||
      pos_y = min(max(pos_y, 0), max_y);
 | 
			
		||||
      args->velocities[i].y *= -1;
 | 
			
		||||
      velocities[i].y *= -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    args->rects[i].position.x = roundf(pos_x);
 | 
			
		||||
    args->rects[i].position.y = roundf(pos_y);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return args;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void render_entities(const u8 *tags, const Rect *rects, u64 count) {
 | 
			
		||||
  persistent RaylibDrawRectFunc renderers[2] = {DrawRectangle, DrawRectangleLines};
 | 
			
		||||
  persistent Color colors[2]                 = {FG_COLOR, ZONE_COLOR};
 | 
			
		||||
 | 
			
		||||
  for (u64 i = 0; i < count; ++i) {
 | 
			
		||||
    u8 func_index  = (tags[i] & ENTITY_TAG_COLLIDER) >> COLLIDER_TAG_SHIFT;
 | 
			
		||||
    u8 color_index = func_index | ((tags[i] & ENTITY_TAG_INSIDE_ZONE) >> INSIDE_ZONE_TAG_SHIFT);
 | 
			
		||||
 | 
			
		||||
    renderers[func_index](
 | 
			
		||||
      rects[i].position.x,
 | 
			
		||||
      rects[i].position.y,
 | 
			
		||||
      rects[i].scale.width,
 | 
			
		||||
      rects[i].scale.height,
 | 
			
		||||
      colors[color_index]
 | 
			
		||||
    );
 | 
			
		||||
    rects[i].position.x = roundf(pos_x);
 | 
			
		||||
    rects[i].position.y = roundf(pos_y);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -313,3 +222,19 @@ u8 collides(const Rect *rect, const Rect *collider) {
 | 
			
		||||
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) {
 | 
			
		||||
  for (u64 i = 0; i < count; ++i) {
 | 
			
		||||
    snprintf(
 | 
			
		||||
      line, LINE_BUF_LEN,
 | 
			
		||||
      "x: %d, y: %d, width: %d, height: %d\n",
 | 
			
		||||
      rects[i].position.x,
 | 
			
		||||
      rects[i].position.y,
 | 
			
		||||
      rects[i].scale.width,
 | 
			
		||||
      rects[i].scale.height
 | 
			
		||||
    );
 | 
			
		||||
    u64 size = strlen(line);
 | 
			
		||||
    fwrite(line, sizeof(char), size, fp);
 | 
			
		||||
    memset(line, 0, size);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										69
									
								
								no_dod.c
									
									
									
									
									
								
							
							
						
						
									
										69
									
								
								no_dod.c
									
									
									
									
									
								
							@@ -1,9 +1,10 @@
 | 
			
		||||
#include "wapp.h"
 | 
			
		||||
#include "common.h"
 | 
			
		||||
#include "raylib.h"
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#define OBJECT_COMMON struct {Position position; Scale scale;}
 | 
			
		||||
 | 
			
		||||
@@ -39,19 +40,11 @@ struct SlowZone {
 | 
			
		||||
 | 
			
		||||
void init_wanderer(Wanderer *wanderer, XOR256State *state);
 | 
			
		||||
void move_wanderer(Wanderer *wanderer, const SlowZone *zones);
 | 
			
		||||
void render_wanderer(const Wanderer *wanderer);
 | 
			
		||||
void init_slow_zone(SlowZone *zone,  XOR256State *state);
 | 
			
		||||
 | 
			
		||||
void render_slow_zone(const SlowZone *zone);
 | 
			
		||||
 | 
			
		||||
bool inside_zone(const Wanderer *wanderer, const SlowZone *zone);
 | 
			
		||||
f32 get_random_float(XOR256State *state);
 | 
			
		||||
 | 
			
		||||
int main(void) {
 | 
			
		||||
  SetTraceLogLevel(LOG_NONE);
 | 
			
		||||
  InitWindow(WIDTH, HEIGHT, "No-DOD test");
 | 
			
		||||
  SetTargetFPS(120);
 | 
			
		||||
 | 
			
		||||
  Allocator arena       = wapp_mem_arena_allocator_init(MB(20));
 | 
			
		||||
  XOR256State state     = wapp_prng_xorshift_init_state();
 | 
			
		||||
  u64 wanderers_size    = sizeof(Wanderer) * WANDERER_COUNT;
 | 
			
		||||
@@ -70,37 +63,38 @@ int main(void) {
 | 
			
		||||
    init_slow_zone(&(zones[i]),&state);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  while (!WindowShouldClose()) {
 | 
			
		||||
    f64 time = GetTime();
 | 
			
		||||
    if (time >= 20.0) {
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
  FILE *fp = fopen("no_dod.out", "w");
 | 
			
		||||
  assert(fp != NULL);
 | 
			
		||||
 | 
			
		||||
  time_t start   = time(NULL);
 | 
			
		||||
  time_t elapsed = (time_t)-1;
 | 
			
		||||
  while (elapsed < 20) {
 | 
			
		||||
    for (u64 i = 0; i < WANDERER_COUNT; ++i) {
 | 
			
		||||
      move_wanderer(&(wanderers[i]), zones);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BeginDrawing();
 | 
			
		||||
 | 
			
		||||
    ClearBackground(BG_COLOR);
 | 
			
		||||
 | 
			
		||||
    for (u64 i = 0; i < ZONE_COUNT; ++i) {
 | 
			
		||||
      render_slow_zone(&(zones[i]));
 | 
			
		||||
    }
 | 
			
		||||
    char line[LINE_BUF_LEN] = {0};
 | 
			
		||||
 | 
			
		||||
    for (u64 i = 0; i < WANDERER_COUNT; ++i) {
 | 
			
		||||
      render_wanderer(&(wanderers[i]));
 | 
			
		||||
      snprintf(
 | 
			
		||||
        line, LINE_BUF_LEN,
 | 
			
		||||
        "x: %f, y: %f, width: %f, height: %f\n",
 | 
			
		||||
        wanderers[i].position.x,
 | 
			
		||||
        wanderers[i].position.y,
 | 
			
		||||
        wanderers[i].scale.width,
 | 
			
		||||
        wanderers[i].scale.height
 | 
			
		||||
      );
 | 
			
		||||
      u64 size = strlen(line);
 | 
			
		||||
      fwrite(line, sizeof(char), size, fp);
 | 
			
		||||
      memset(line, 0, size);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    DrawFPS(10, 10);
 | 
			
		||||
 | 
			
		||||
    EndDrawing();
 | 
			
		||||
    elapsed = time(NULL) - start;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fclose(fp);
 | 
			
		||||
  wapp_mem_arena_allocator_destroy(&arena);
 | 
			
		||||
 | 
			
		||||
  CloseWindow();
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -157,17 +151,6 @@ void move_wanderer(Wanderer *wanderer, const SlowZone *zones) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void render_wanderer(const Wanderer *wanderer) {
 | 
			
		||||
  Color color = wanderer->slow ? ZONE_COLOR : FG_COLOR;
 | 
			
		||||
  DrawRectangle(
 | 
			
		||||
    (i32)wanderer->position.x,
 | 
			
		||||
    (i32)wanderer->position.y,
 | 
			
		||||
    (i32)wanderer->scale.width,
 | 
			
		||||
    (i32)wanderer->scale.height,
 | 
			
		||||
    color
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void init_slow_zone(SlowZone *zone,  XOR256State *state) {
 | 
			
		||||
  zone->position = (Position){
 | 
			
		||||
    .x = wapp_prng_xorshift_256(state) % WIDTH,
 | 
			
		||||
@@ -180,16 +163,6 @@ void init_slow_zone(SlowZone *zone,  XOR256State *state) {
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void render_slow_zone(const SlowZone *zone) {
 | 
			
		||||
  DrawRectangleLines(
 | 
			
		||||
    zone->position.x,
 | 
			
		||||
    zone->position.y,
 | 
			
		||||
    zone->scale.width,
 | 
			
		||||
    zone->scale.height,
 | 
			
		||||
    ZONE_COLOR
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool inside_zone(const Wanderer *wanderer, const SlowZone *zone) {
 | 
			
		||||
  f32 wanderer_min_x = wanderer->position.x + wanderer->scale.width;
 | 
			
		||||
  f32 wanderer_min_y = wanderer->position.y + wanderer->scale.height;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user