generated from rpuzonas/raylib-cpp-template
account for when user comes back to page after a long time for wasm
This commit is contained in:
parent
ef426d2df0
commit
1478084ab6
7
Makefile
7
Makefile
@ -30,6 +30,7 @@ ifeq ($(OS), Windows_NT)
|
|||||||
RM := -del /q
|
RM := -del /q
|
||||||
COPY = -robocopy "$1" "$2" $3
|
COPY = -robocopy "$1" "$2" $3
|
||||||
EXT = .exe
|
EXT = .exe
|
||||||
|
MOVE := move
|
||||||
|
|
||||||
OS_NAME := windows
|
OS_NAME := windows
|
||||||
LINKER_FLAGS += -Wl,--allow-multiple-definition -pthread -lopengl32
|
LINKER_FLAGS += -Wl,--allow-multiple-definition -pthread -lopengl32
|
||||||
@ -40,6 +41,7 @@ else
|
|||||||
MKDIR := mkdir -p
|
MKDIR := mkdir -p
|
||||||
RM := rm -rf
|
RM := rm -rf
|
||||||
COPY = cp $1/$3 $2
|
COPY = cp $1/$3 $2
|
||||||
|
MOVE := mv
|
||||||
|
|
||||||
OS_NAME := linux
|
OS_NAME := linux
|
||||||
LINKER_FLAGS += -lGL -lm -lpthread -ldl -lrt -lX11
|
LINKER_FLAGS += -lGL -lm -lpthread -ldl -lrt -lX11
|
||||||
@ -108,6 +110,9 @@ emsdk: submodules
|
|||||||
# Link the program and create the executable
|
# Link the program and create the executable
|
||||||
$(MAIN_TARGET): $(OBJECTS)
|
$(MAIN_TARGET): $(OBJECTS)
|
||||||
$(CXX) $(OBJECTS) -o $(MAIN_TARGET) $(LINKER_FLAGS) -D$(RAYLIB_PLATFORM)
|
$(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
|
# Compile objects to the build directory
|
||||||
$(BUILD_DIR)/%.o: src/%.cpp Makefile
|
$(BUILD_DIR)/%.o: src/%.cpp Makefile
|
||||||
@ -117,7 +122,7 @@ $(BUILD_DIR)/%.o: src/%.cpp Makefile
|
|||||||
# Run the executable
|
# Run the executable
|
||||||
run:
|
run:
|
||||||
ifeq ($(PLATFORM), web)
|
ifeq ($(PLATFORM), web)
|
||||||
serve $(BUILD_DIR) $(ARGS)
|
python -m http.server 8080 -d $(BUILD_DIR)
|
||||||
else
|
else
|
||||||
$(MAIN_TARGET) $(ARGS)
|
$(MAIN_TARGET) $(ARGS)
|
||||||
endif
|
endif
|
||||||
|
21
src/main.cpp
21
src/main.cpp
@ -11,6 +11,9 @@
|
|||||||
#include "boid-playground.hpp"
|
#include "boid-playground.hpp"
|
||||||
#include "raycast.cpp"
|
#include "raycast.cpp"
|
||||||
|
|
||||||
|
#define FRAMERATE 60
|
||||||
|
#define TIME_PER_FRAME (1.0/FRAMERATE)
|
||||||
|
|
||||||
static World g_world;
|
static World g_world;
|
||||||
static Visuals g_visuals;
|
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;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void world_update(World *world) {
|
static void world_update(World *world, float dt) {
|
||||||
float dt = GetFrameTime();
|
|
||||||
|
|
||||||
for (int i = 0; i < world->boids.size(); i++) {
|
for (int i = 0; i < world->boids.size(); i++) {
|
||||||
Boid *boid = &world->boids[i];
|
Boid *boid = &world->boids[i];
|
||||||
Vector2 acc = { 1, 0 };
|
Vector2 acc = { 1, 0 };
|
||||||
@ -327,7 +328,17 @@ void UpdateDrawFrame() {
|
|||||||
// TODO: Show this on screen
|
// TODO: Show this on screen
|
||||||
// LogTrace("%d", count_out_of_bounds_boids(&world));
|
// 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
|
// Draw
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
@ -371,7 +382,7 @@ int main() {
|
|||||||
#ifdef PLATFORM_WEB
|
#ifdef PLATFORM_WEB
|
||||||
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
||||||
#else
|
#else
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(FRAMERATE);
|
||||||
while (!window.ShouldClose()) {
|
while (!window.ShouldClose()) {
|
||||||
UpdateDrawFrame();
|
UpdateDrawFrame();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user