boids-playground/src/main.cpp

139 lines
2.7 KiB
C++

#include "raylib.h"
#include "raymath.h"
#include <cmath>
#include <optional>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#define RPROF_IMPLEMENTATION
// #define RPROF_STUB_OUT
// #define RPROF_ONLY_TOTAL_TIME
#include "rprof.h"
#include "boid-playground.hpp"
#include "raycast.cpp"
#include "memory-arena.cpp"
#include "boid-list.cpp"
#include "world.cpp"
#include "ui.cpp"
//#define USE_TEST_MAIN
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#define FRAMERATE 60
#define TIME_PER_FRAME (1.0/FRAMERATE)
static World g_world;
static Visuals g_visuals;
static UI g_ui;
void UpdateDrawFrame();
static void profiling_test();
int main() {
// profiling_test();
// return 0;
SetTraceLogLevel(LOG_TRACE);
int screen_width = 1280;
int screen_height = 720;
raylib::Window window(screen_width, screen_height, "Boid Playground");
window.SetState(FLAG_VSYNC_HINT);
GuiLoadStyleDefault();
rprof_init();
world_init(&g_world, screen_width, screen_height);
float border = g_world.collision_avoidance_distance;
for (int i = 0; i < 10000; i++) {
Boid boid;
boid_rand_init(&g_world, &boid, border);
g_world.boids.push_back(boid);
}
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
SetTargetFPS(FRAMERATE);
while (!window.ShouldClose()) {
UpdateDrawFrame();
}
#endif
window.Close();
world_free(&g_world);
rprof_end();
rprof_output(NULL);
return 0;
}
static void profiling_test() {
rprof_init();
{
world_init(&g_world, 1280, 720);
SetRandomSeed(10);
float border = g_visuals.boid_edge_size;
for (int i = 0; i < 45000; i++) {
Boid boid;
boid_rand_init(&g_world, &boid, border);
g_world.boids.push_back(boid);
}
for (int i = 0; i < FRAMERATE; i++) {
world_update(&g_world, TIME_PER_FRAME);
}
printf("arena: %ld (%.03fMiB)\n", g_world.frame_arena.offset, (float)g_world.frame_arena.offset / 1024 / 1024);
world_free(&g_world);
}
rprof_end();
printf("interactions: %d\n", g_prof_interactions);
if (g_prof_interactions != 33119854) { // 22 051 739
printf("!!!!!! ITERACTIONS DONT MATCH, %d\n", g_prof_interactions - 33119854);
}
rprof_output(NULL);
}
void UpdateDrawFrame() {
float dt = GetFrameTime();
RPROF_START("Update");
#ifdef PLATFORM_WEB
// If user goes to another tab and comes back, the time that the user was gone needs to be ignored.
// So boids wouldn't tunnel through walls and do other shenanigans.
if (dt <= 5*TIME_PER_FRAME) {
world_update(&g_world, dt);
}
#else
world_update(&g_world, dt);
#endif
RPROF_STOP();
// Draw
BeginDrawing();
RPROF_START("Draw");
ClearBackground(g_visuals.bg_color);
world_draw(&g_world, &g_visuals);
ui_draw(&g_world, &g_visuals, &g_ui);
RPROF_STOP();
EndDrawing();
}