Skip to content

Commit 3da9a15

Browse files
committed
Add Emscripten builds
1 parent 9a28224 commit 3da9a15

File tree

4 files changed

+127
-9
lines changed

4 files changed

+127
-9
lines changed

.github/workflows/cmake.yml

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
butler-url: https://broth.itch.ovh/butler/windows-amd64/LATEST/archive/default
2929
- os: macos-14
3030
triplet: arm64-osx
31-
platform-name: macos.x64
31+
platform-name: macos.arm64
3232
butler-url: https://broth.itch.ovh/butler/darwin-amd64/LATEST/archive/default
3333
- os: ubuntu-24.04
3434
triplet: x64-linux
@@ -128,7 +128,7 @@ jobs:
128128
tag: ${{ github.ref }}
129129
overwrite: true
130130
- name: Install Butler
131-
if: github.ref == 'refs/heads/main'
131+
if: github.ref == 'refs/heads/main' && matrix.butler-url
132132
run: |
133133
mkdir ~/bin
134134
cd ~/bin
@@ -138,7 +138,75 @@ jobs:
138138
echo "~/bin" >> $GITHUB_PATH
139139
~/bin/butler -V
140140
- name: Upload to Itch
141-
if: github.ref == 'refs/heads/main'
141+
if: github.ref == 'refs/heads/main' && matrix.butler-url
142142
env:
143143
BUTLER_API_KEY: ${{ secrets.BUTLER_API_KEY }}
144144
run: butler push dist/${{ env.archive-name }} hexdecimal/${{ env.project-name }}:${{ matrix.platform-name }}-latest
145+
146+
emscripten:
147+
runs-on: ubuntu-24.04
148+
strategy:
149+
matrix:
150+
build_type: [Debug, Release]
151+
env:
152+
EM_VERSION: 4.0.5
153+
EM_CACHE_FOLDER: "emsdk-cache"
154+
steps:
155+
- uses: actions/checkout@v4
156+
with:
157+
submodules: true
158+
- name: Restore vcpkg and its artifacts
159+
uses: actions/cache@v4
160+
with:
161+
path: |
162+
build/vcpkg_installed/
163+
${{ env.VCPKG_ROOT }}
164+
!${{ env.VCPKG_ROOT }}/buildtrees
165+
!${{ env.VCPKG_ROOT }}/packages
166+
!${{ env.VCPKG_ROOT }}/downloads
167+
key: wasm32-emscripten-${{ hashFiles('vcpkg.json', '.git/modules/vcpkg/HEAD') }}
168+
- name: Setup cache
169+
id: cache-system-libraries
170+
uses: actions/cache@v4
171+
with:
172+
path: ${{env.EM_CACHE_FOLDER}}
173+
key: ${{env.EM_VERSION}}-${{ runner.os }}
174+
- uses: mymindstorm/setup-emsdk@v14
175+
with:
176+
version: ${{env.EM_VERSION}}
177+
actions-cache-folder: ${{env.EM_CACHE_FOLDER}}
178+
- name: Verify
179+
run: emcc -v
180+
- uses: lukka/get-cmake@latest
181+
- name: Configure
182+
run: emcmake cmake -S . -B build -G Ninja
183+
-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE="${EMSDK}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"
184+
-DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake
185+
-DVCPKG_TARGET_TRIPLET=wasm32-emscripten
186+
-DCMAKE_BUILD_TYPE="${{ matrix.build_type }}"
187+
- name: Build
188+
run: cmake --build build
189+
- name: Show contents of the build directory
190+
run: find build
191+
- uses: actions/upload-artifact@v4
192+
with:
193+
name: emscripten-${{ matrix.build_type }}
194+
path: build/bin
195+
retention-days: 7
196+
if-no-files-found: error
197+
compression-level: 6
198+
- name: Install Butler
199+
if: github.ref == 'refs/heads/main' && matrix.build_type == 'Release'
200+
run: |
201+
mkdir ~/bin
202+
cd ~/bin
203+
curl -L -o butler.zip https://broth.itch.ovh/butler/linux-amd64/LATEST/archive/default
204+
unzip butler.zip
205+
chmod +x butler
206+
echo "~/bin" >> $GITHUB_PATH
207+
~/bin/butler -V
208+
- name: Upload to Itch
209+
if: github.ref == 'refs/heads/main' && matrix.build_type == 'Release'
210+
env:
211+
BUTLER_API_KEY: ${{ secrets.BUTLER_API_KEY }}
212+
run: butler push dist/${{ env.archive-name }} hexdecimal/${{ env.project-name }}:emscripten-latest

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ else()
3333
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra)
3434
endif()
3535

36+
if (EMSCRIPTEN)
37+
# Attach data folder to Emscripten builds.
38+
target_link_options(${PROJECT_NAME} PRIVATE
39+
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/data@data"
40+
-lidbfs.js # Enable IDBFS (browser file system.)
41+
)
42+
configure_file(
43+
${PROJECT_SOURCE_DIR}/emscripten/index.html
44+
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/index.html
45+
)
46+
endif()
47+
3648
target_compile_definitions(${PROJECT_NAME} PRIVATE _USE_MATH_DEFINES) # For M_PI
3749
target_compile_definitions(${PROJECT_NAME} PRIVATE NO_SOUND)
3850
target_compile_definitions(${PROJECT_NAME} PRIVATE NO_LUA)

emscripten/index.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!doctype HTML>
2+
<html lang=en-us>
3+
4+
<head>
5+
<meta charset=utf-8>
6+
<title>Pyromancer</title>
7+
<style>
8+
body {
9+
background-color: #000;
10+
margin: 0;
11+
padding: none;
12+
}
13+
14+
.center {
15+
display: flex;
16+
justify-content: center;
17+
align-items: center;
18+
}
19+
20+
canvas {
21+
aspect-ratio: 80 / 60;
22+
width: calc(min(100vw, 80 / 60 * 100vh));
23+
}
24+
</style>
25+
</head>
26+
27+
<body onload=window.focus()>
28+
<div class=center>
29+
<canvas id=canvas oncontextmenu=event.preventDefault() tabindex=-1 onclick=window.focus()></canvas>
30+
</div>
31+
<script>
32+
var Module = {
33+
print(...args) { console.log("%s", ...args); },
34+
canvas: document.getElementById('canvas'),
35+
};
36+
</script>
37+
<script async src=${PROJECT_NAME}.js></script>
38+
</body>
39+
40+
</html>

src/main.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626
#define SDL_MAIN_USE_CALLBACKS
2727
#include <SDL3/SDL_main.h>
28+
#include <SDL3/SDL_log.h>
2829
#include <time.h>
2930
#include <stdio.h>
3031
#include "main.hpp"
@@ -163,6 +164,7 @@ SDL_AppResult SDL_AppEvent(void*, SDL_Event* event) { return engine.onEvent(*eve
163164
SDL_AppResult SDL_AppIterate(void*) { return engine.onFrame(); }
164165

165166
SDL_AppResult SDL_AppInit(void**, int argc, char** argv) {
167+
SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE);
166168
// read main configuration file
167169
config.run("data/cfg/config.txt",NULL);
168170
ConditionType::init();
@@ -193,12 +195,8 @@ SDL_AppResult SDL_AppInit(void**, int argc, char** argv) {
193195

194196
sound.initialize();
195197
engine.setKeyboardMode(UMBRA_KEYBOARD_SDL);
196-
if (engine.initialise(TCOD_RENDERER_SDL2)) {
197-
//engine.run();
198-
//return SDL_APP_SUCCESS;
199-
return SDL_APP_CONTINUE;
200-
}
201-
return SDL_APP_FAILURE;
198+
if (!engine.initialise(TCOD_RENDERER_SDL2)) return SDL_APP_FAILURE;
199+
return SDL_APP_CONTINUE;
202200
}
203201

204202
void SDL_AppQuit(void*, SDL_AppResult) {

0 commit comments

Comments
 (0)