account for when user comes back to page after a long time for wasm

This commit is contained in:
Rokas Puzonas 2023-07-19 21:27:00 +03:00
parent ef426d2df0
commit 1478084ab6
2 changed files with 22 additions and 6 deletions

View File

@ -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

View File

@ -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();
}