From 1478084ab6d6c4d96cb685247a3b03781904b7fa Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Wed, 19 Jul 2023 21:27:00 +0300 Subject: [PATCH] account for when user comes back to page after a long time for wasm --- Makefile | 7 ++++++- src/main.cpp | 21 ++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 92f6756..8cd6d99 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ ifeq ($(OS), Windows_NT) RM := -del /q COPY = -robocopy "$1" "$2" $3 EXT = .exe + MOVE := move OS_NAME := windows LINKER_FLAGS += -Wl,--allow-multiple-definition -pthread -lopengl32 @@ -40,6 +41,7 @@ else MKDIR := mkdir -p RM := rm -rf COPY = cp $1/$3 $2 + MOVE := mv OS_NAME := linux LINKER_FLAGS += -lGL -lm -lpthread -ldl -lrt -lX11 @@ -108,6 +110,9 @@ emsdk: submodules # Link the program and create the executable $(MAIN_TARGET): $(OBJECTS) $(CXX) $(OBJECTS) -o $(MAIN_TARGET) $(LINKER_FLAGS) -D$(RAYLIB_PLATFORM) +ifeq ($(PLATFORM), web) + $(MOVE) $(MAIN_TARGET) $(BUILD_DIR)/index.html +endif # Compile objects to the build directory $(BUILD_DIR)/%.o: src/%.cpp Makefile @@ -117,7 +122,7 @@ $(BUILD_DIR)/%.o: src/%.cpp Makefile # Run the executable run: ifeq ($(PLATFORM), web) - serve $(BUILD_DIR) $(ARGS) + python -m http.server 8080 -d $(BUILD_DIR) else $(MAIN_TARGET) $(ARGS) endif diff --git a/src/main.cpp b/src/main.cpp index 8f8d598..f28051a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,9 @@ #include "boid-playground.hpp" #include "raycast.cpp" +#define FRAMERATE 60 +#define TIME_PER_FRAME (1.0/FRAMERATE) + static World g_world; static Visuals g_visuals; @@ -186,9 +189,7 @@ static int get_boids_in_view_cone(Boid **boids_in_view, Boid *boid, float view_r return count; } -static void world_update(World *world) { - float dt = GetFrameTime(); - +static void world_update(World *world, float dt) { for (int i = 0; i < world->boids.size(); i++) { Boid *boid = &world->boids[i]; Vector2 acc = { 1, 0 }; @@ -327,7 +328,17 @@ void UpdateDrawFrame() { // TODO: Show this on screen // LogTrace("%d", count_out_of_bounds_boids(&world)); - world_update(&g_world); + float dt = GetFrameTime(); + +#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 // Draw BeginDrawing(); @@ -371,7 +382,7 @@ int main() { #ifdef PLATFORM_WEB emscripten_set_main_loop(UpdateDrawFrame, 0, 1); #else - SetTargetFPS(60); + SetTargetFPS(FRAMERATE); while (!window.ShouldClose()) { UpdateDrawFrame(); }