add crude compilation to web platform

This commit is contained in:
Rokas Puzonas 2023-07-19 19:16:33 +03:00
parent d9ec40aeec
commit 6a7266b524
5 changed files with 163 additions and 53 deletions

3
.gitmodules vendored
View File

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

View File

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

1
depends/emsdk Submodule

@ -0,0 +1 @@
Subproject commit b6df670bdb7cc696804495306e99a604f6641b38

View File

@ -4,9 +4,16 @@
#include <cmath>
#include <optional>
#ifdef PLATFORM_WEB
#include <emscripten/emscripten.h>
#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;
}

83
src/shell.html Normal file
View File

@ -0,0 +1,83 @@
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>raylib web game</title>
<meta name="title" content="raylib web game">
<meta name="description" content="New raylib web videogame, developed using raylib videogames library">
<meta name="keywords" content="raylib, games, html5, programming, C, C++, library, learn, videogames">
<meta name="viewport" content="width=device-width">
<!-- Open Graph metatags for sharing -->
<meta property="og:title" content="raylib web game">
<meta property="og:image:type" content="image/png">
<meta property="og:image" content="https://www.raylib.com/common/img/raylib_logo.png">
<meta property="og:site_name" content="raylib.com">
<meta property="og:url" content="https://www.raylib.com/games.html">
<meta property="og:description" content="New raylib web videogame, developed using raylib videogames library">
<!-- Twitter metatags for sharing -->
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@raysan5">
<meta name="twitter:title" content="raylib web game">
<meta name="twitter:image" content="https://www.raylib.com/common/raylib_logo.png">
<meta name="twitter:url" content="https://www.raylib.com/games.html">
<meta name="twitter:description" content="New raylib web game, developed using raylib videogames library">
<!-- Favicon -->
<link rel="shortcut icon" href="https://www.raylib.com/favicon.ico">
<style>
body { margin: 0px; }
canvas.emscripten { border: 0px none; background-color: black; }
</style>
<script type='text/javascript' src="https://cdn.jsdelivr.net/gh/eligrey/FileSaver.js/dist/FileSaver.min.js">
</script>
<script type='text/javascript'>
function saveFileFromMEMFSToDisk(memoryFSname, localFSname) // This can be called by C/C++ code
{
var isSafari = false; // Not supported, navigator.userAgent access is being restricted
//var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
var data = FS.readFile(memoryFSname);
var blob;
if (isSafari) blob = new Blob([data.buffer], { type: "application/octet-stream" });
else blob = new Blob([data.buffer], { type: "application/octet-binary" });
// NOTE: SaveAsDialog is a browser setting. For example, in Google Chrome,
// in Settings/Advanced/Downloads section you have a setting:
// 'Ask where to save each file before downloading' - which you can set true/false.
// If you enable this setting it would always ask you and bring the SaveAsDialog
saveAs(blob, localFSname);
}
</script>
</head>
<body>
<canvas class=emscripten id=canvas oncontextmenu=event.preventDefault() tabindex=-1></canvas>
<p id="output" />
<script>
var Module = {
print: (function() {
var element = document.getElementById('output');
if (element) element.value = ''; // clear browser cache
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
if (element) {
element.value += text + "\n";
element.scrollTop = element.scrollHeight; // focus on bottom
}
};
})(),
canvas: (function() {
var canvas = document.getElementById('canvas');
return canvas;
})()
};
</script>
{{{ SCRIPT }}}
</body>
</html>