diff --git a/.gitmodules b/.gitmodules index d039609..db63b3b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "depends/raylib"] path = depends/raylib url = https://github.com/raysan5/raylib +[submodule "depends/emsdk"] + path = depends/emsdk + url = git@github.com:emscripten-core/emsdk.git diff --git a/Makefile b/Makefile index ea729d0..d4c695d 100644 --- a/Makefile +++ b/Makefile @@ -10,42 +10,36 @@ platformpth = $(subst /,$(PATHSEP),$1) # Set global macros buildDir := bin -executable := app +executable := boids-playground target := $(buildDir)/$(executable) sources := $(call rwildcard,src/,*.cpp) -objects := $(patsubst src/%, $(buildDir)/%, $(patsubst %.cpp, %.o, $(sources))) +# objects := $(patsubst src/%, $(buildDir)/%, $(patsubst %.cpp, %.o, $(sources))) +objects := $(buildDir)/main.o depends := $(patsubst %.o, %.d, $(objects)) compileFlags := -std=c++17 -I include -linkFlags = -L lib/$(platform) -l raylib +linkFlags = -lraylib +emsdk_path = /usr/lib/emscripten +ext = + +# PLATFORM_DESKTOP or PLATFORM_WEB +PLATFORM ?= PLATFORM_DESKTOP # Check for Windows ifeq ($(OS), Windows_NT) # Set Windows macros - platform := Windows + osName := windows CXX ?= g++ linkFlags += -Wl,--allow-multiple-definition -pthread -lopengl32 -lgdi32 -lwinmm -mwindows -static -static-libgcc -static-libstdc++ - libGenDir := src THEN := && PATHSEP := \$(BLANK) MKDIR := -mkdir -p RM := -del /q COPY = -robocopy "$(call platformpth,$1)" "$(call platformpth,$2)" $3 else - # Check for MacOS/Linux - UNAMEOS := $(shell uname) - ifeq ($(UNAMEOS), Linux) - # Set Linux macros - platform := Linux - CXX ?= g++ - linkFlags += -l GL -l m -l pthread -l dl -l rt -l X11 - endif - ifeq ($(UNAMEOS), Darwin) - # Set macOS macros - platform := macOS - CXX ?= clang++ - linkFlags += -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL - libGenDir := src - endif + # Set Linux macros + osName := linux + CXX ?= g++ + linkFlags += -lGL -lm -lpthread -ldl -lrt -lX11 # Set UNIX macros THEN := ; @@ -55,11 +49,23 @@ else COPY = cp $1$(PATHSEP)$3 $2 endif -# Lists phony targets for Makefile -.PHONY: all setup submodules execute clean +RAYLIB_RELEASE_PATH = lib/$(osName) +ifeq ($(PLATFORM), PLATFORM_WEB) + RAYLIB_RELEASE_PATH = lib/$(osName)-web + CXX := emcc -# Default target, compiles, executes and cleans -all: $(target) execute clean + compileFlags += -I$(emscripten_path)/cache/sysroot/include + emscripten_path ?= $(emsdk_path)/upstream/emscripten + linkFlags += -s USE_GLFW=3 -s FORCE_FILESYSTEM=1 $(RAYLIB_RELEASE_PATH)/libraylib.a --shell-file src/shell.html + ext = .html +endif +linkFlags += -L$(RAYLIB_RELEASE_PATH) + +# Lists phony targets for Makefile +.PHONY: all setup submodules run clean + +# Default target, compiles, runss and cleans +all: $(target) run clean # Sets up the project for compiling, generates includes and libs setup: include lib @@ -78,13 +84,12 @@ include: submodules # Build the raylib static library file and copy it into lib lib: submodules - cd depends/raylib/src $(THEN) "$(MAKE)" PLATFORM=PLATFORM_DESKTOP - $(MKDIR) $(call platformpth, lib/$(platform)) - $(call COPY,depends/raylib/src/$(libGenDir),lib/$(platform),libraylib.a) + $(MKDIR) $(call platformpth, $(RAYLIB_RELEASE_PATH)) + cd depends/raylib/src $(THEN) "$(MAKE)" PLATFORM=$(PLATFORM) EMSDK_PATH=$(emsdk_path) RAYLIB_RELEASE_PATH=../../../$(RAYLIB_RELEASE_PATH) -B # Link the program and create the executable $(target): $(objects) - $(CXX) $(objects) -o $(target) $(linkFlags) + $(CXX) $(objects) -o $(target)$(ext) -DPLATFORM=$(PLATFORM) $(linkFlags) # Add all rules from dependency files -include $(depends) @@ -92,12 +97,18 @@ $(target): $(objects) # Compile objects to the build directory $(buildDir)/%.o: src/%.cpp Makefile $(MKDIR) $(call platformpth, $(@D)) - $(CXX) -MMD -MP -c $(compileFlags) $< -o $@ $(CXXFLAGS) + $(CXX) -MMD -MP -c $(compileFlags) $< -o $@ $(CXXFLAGS) -D$(PLATFORM) # Run the executable -execute: +run: +ifeq ($(PLATFORM), PLATFORM_WEB) + serve $(buildDir) $(ARGS) +else $(target) $(ARGS) +endif # Clean up all relevant files clean: $(RM) $(call platformpth, $(buildDir)/*) + +# emcc -o bin/game.html bin/main.o bin/raycast.o -std=c99 -Wall -Wno-missing-braces -Wunused-result -D_DEFAULT_SOURCE -Os -I. -I../raylib/src -I../raylib/src/external -I../raylib/src/extras -I/cache/sysroot/include -L. -L../depends/raylib/src -L../depends/raylib/src -s USE_GLFW=3 -s TOTAL_MEMORY=134217728 -s FORCE_FILESYSTEM=1 ~/code/fun/raylib-game-template/raylib/src/libraylib.a -DPLATFORM_WEB -s ASYNCIFY diff --git a/depends/emsdk b/depends/emsdk new file mode 160000 index 0000000..b6df670 --- /dev/null +++ b/depends/emsdk @@ -0,0 +1 @@ +Subproject commit b6df670bdb7cc696804495306e99a604f6641b38 diff --git a/src/main.cpp b/src/main.cpp index c04250f..8f8d598 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,9 +4,16 @@ #include #include +#ifdef PLATFORM_WEB + #include +#endif + #include "boid-playground.hpp" #include "raycast.cpp" +static World g_world; +static Visuals g_visuals; + static float vector2_atan2(Vector2 a) { return std::atan2(a.y, a.x); } @@ -316,6 +323,21 @@ static void world_draw(World *world, Visuals *visuals) { } } +void UpdateDrawFrame() { + // TODO: Show this on screen + // LogTrace("%d", count_out_of_bounds_boids(&world)); + + world_update(&g_world); + + // Draw + BeginDrawing(); + ClearBackground(RAYWHITE); + + world_draw(&g_world, &g_visuals); + + EndDrawing(); +} + int main() { SetTraceLogLevel(LOG_TRACE); @@ -324,18 +346,15 @@ int main() { raylib::Color text_color(LIGHTGRAY); raylib::Window window(screen_width, screen_height, "Boid Playground"); + window.SetState(FLAG_VSYNC_HINT); - SetTargetFPS(60); + g_world.size = { (float)screen_width, (float)screen_height }; - World world; - world.size = { (float)screen_width, (float)screen_height }; - Visuals visuals; - - float border = visuals.boid_edge_size; + float border = g_visuals.boid_edge_size; for (int i = 0; i < 100; i++) { Boid boid; - boid_rand_init(&world, &boid, border); - world.boids.push_back(boid); + boid_rand_init(&g_world, &boid, border); + g_world.boids.push_back(boid); } // world.boids.push_back({ @@ -349,21 +368,14 @@ int main() { // .speed = world.boid_min_speed // }); - // Main game loop - while (!window.ShouldClose()) { - // TODO: Show this on screen - // LogTrace("%d", count_out_of_bounds_boids(&world)); - - world_update(&world); - - // Draw - BeginDrawing(); - ClearBackground(RAYWHITE); - - world_draw(&world, &visuals); - - EndDrawing(); - } +#ifdef PLATFORM_WEB + emscripten_set_main_loop(UpdateDrawFrame, 0, 1); +#else + SetTargetFPS(60); + while (!window.ShouldClose()) { + UpdateDrawFrame(); + } +#endif return 0; } diff --git a/src/shell.html b/src/shell.html new file mode 100644 index 0000000..1578d39 --- /dev/null +++ b/src/shell.html @@ -0,0 +1,83 @@ + + + + + + + raylib web game + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ + {{{ SCRIPT }}} + +