Skip to content

Commit 478acff

Browse files
committedJun 18, 2021
Emscripten support
1 parent 2d071da commit 478acff

File tree

6 files changed

+97
-34
lines changed

6 files changed

+97
-34
lines changed
 

‎.ci_scripts/build_upload.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ case "$DEPLOY" in
6262
;;
6363
"emscripten")
6464
PACKAGE=emscripten
65-
if [ -f "${build_dir}/julius.zip" ]
65+
if [ -f "${build_dir}/augustus.zip" ]
6666
then
67-
DEPLOY_FILE=julius-$VERSION-emscripten.zip
68-
cp "${build_dir}/julius.zip" "deploy/$DEPLOY_FILE"
67+
DEPLOY_FILE=augustus-$VERSION-emscripten.zip
68+
cp "${build_dir}/augustus.zip" "deploy/$DEPLOY_FILE"
6969
fi
7070
;;
7171
*)

‎.ci_scripts/run_build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ case "$BUILD_TARGET" in
6565
;;
6666
"emscripten")
6767
cd build && make -j4
68-
zip julius.zip julius.html
68+
zip augustus.zip augustus.html augustus.data
6969
;;
7070
*)
7171
cd build && make -j4 && make

‎.github/workflows/main.yml

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ jobs:
5555
SDL_MIXER_VERSION: 2.0.4
5656
BUILD_TARGET: android
5757
DEPLOY: android
58+
- name: Emscripten
59+
os: ubuntu-20.04
60+
cache-key: emscripten
61+
SDL_VERSION: 2.0.14
62+
SDL_MIXER_VERSION: 2.0.4
63+
MPG123_VERSION: 1.26.5
64+
BUILD_TARGET: emscripten
65+
DEPLOY: emscripten
5866
name: ${{ matrix.name }}
5967
runs-on: ${{ matrix.os }}
6068
env:

‎CMakeLists.txt

+68-23
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ string(TOLOWER ${TARGET_PLATFORM} TARGET_PLATFORM)
88

99
option(DRAW_FPS "Draw FPS on the top left corner of the window." OFF)
1010
option(SYSTEM_LIBS "Use system libraries when available." ON)
11+
option(EMSCRIPTEN_LOAD_SDL_PORTS "Load SDL and SDL_mixer emscripten ports instead of compiling them" OFF)
12+
option(LINK_MPG123 "Link mpg123 statically to Julius instead of relying on a library." OFF)
1113

1214
if(${TARGET_PLATFORM} STREQUAL "vita" AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
1315
if(DEFINED ENV{VITASDK})
@@ -26,6 +28,15 @@ if(${TARGET_PLATFORM} STREQUAL "switch" AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
2628
endif()
2729
endif()
2830

31+
if(${TARGET_PLATFORM} STREQUAL "emscripten" AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
32+
if(DEFINED ENV{EMSDK})
33+
set(EMSDK $ENV{EMSDK})
34+
set(CMAKE_TOOLCHAIN_FILE "$ENV{EMSDK}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" CACHE PATH "toolchain file")
35+
else()
36+
message(FATAL_ERROR "Please define EMSDK to point to your Emscripten SDK path!")
37+
endif()
38+
endif()
39+
2940
set(SHORT_NAME augustus)
3041
set(USER_FRIENDLY_NAME Augustus)
3142
project(${SHORT_NAME} C)
@@ -131,6 +142,10 @@ configure_file(${PROJECT_SOURCE_DIR}/gen/version.c.in ${PROJECT_SOURCE_DIR}/src/
131142
configure_file(${PROJECT_SOURCE_DIR}/gen/version.rc.in ${PROJECT_SOURCE_DIR}/res/version.rc)
132143
configure_file(${PROJECT_SOURCE_DIR}/gen/version.txt.in ${PROJECT_SOURCE_DIR}/res/version.txt)
133144

145+
if(${TARGET_PLATFORM} STREQUAL "emscripten")
146+
configure_file(${PROJECT_SOURCE_DIR}/gen/shell.html.in ${PROJECT_SOURCE_DIR}/res/shell.html)
147+
endif()
148+
134149
if(DRAW_FPS)
135150
add_definitions(-DDRAW_FPS)
136151
endif()
@@ -633,6 +648,11 @@ if(APPLE)
633648
)
634649
endif()
635650

651+
set(EMSCRIPTEN_FILES "")
652+
if(${TARGET_PLATFORM} STREQUAL "emscripten")
653+
set(EMSCRIPTEN_FILES ${PROJECT_SOURCE_DIR}/res/shell.html)
654+
endif()
655+
636656
set(SOURCE_FILES
637657
${PLATFORM_FILES}
638658
${CORE_FILES}
@@ -657,36 +677,54 @@ set(SOURCE_FILES
657677
${MACOSX_FILES}
658678
)
659679

660-
function(GET_SDL_EXT_DIR result module)
661-
if(NOT module STREQUAL "")
662-
set(module "_${module}")
663-
endif()
664-
set(SDL_LOCATION ${PROJECT_SOURCE_DIR}/ext/SDL2)
665-
file(GLOB children
666-
RELATIVE ${SDL_LOCATION}
667-
CONFIGURE_DEPENDS
668-
${SDL_LOCATION}/SDL${module}
669-
${SDL_LOCATION}/SDL2${module}
670-
${SDL_LOCATION}/SDL${module}-*
671-
${SDL_LOCATION}/SDL2${module}-*
672-
)
673-
foreach(child ${children})
674-
if(IS_DIRECTORY "${SDL_LOCATION}/${child}")
675-
set(${result} "${SDL_LOCATION}/${child}" PARENT_SCOPE)
676-
break()
680+
if(${TARGET_PLATFORM} STREQUAL "emscripten" AND EMSCRIPTEN_LOAD_SDL_PORTS)
681+
set(USE_FLAGS "-s USE_SDL=2 -s USE_SDL_MIXER=2 -s SDL2_MIXER_FORMATS=[\"mp3\"] -s USE_MPG123=1")
682+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${USE_FLAGS}")
683+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${USE_FLAGS}")
684+
else()
685+
function(GET_SDL_EXT_DIR result module)
686+
if(NOT module STREQUAL "")
687+
set(module "_${module}")
677688
endif()
678-
endforeach()
679-
endfunction()
689+
set(SDL_LOCATION ${PROJECT_SOURCE_DIR}/ext/SDL2)
690+
file(GLOB children
691+
RELATIVE ${SDL_LOCATION}
692+
CONFIGURE_DEPENDS
693+
${SDL_LOCATION}/SDL${module}
694+
${SDL_LOCATION}/SDL2${module}
695+
${SDL_LOCATION}/SDL${module}-*
696+
${SDL_LOCATION}/SDL2${module}-*
697+
)
698+
foreach(child ${children})
699+
if(IS_DIRECTORY "${SDL_LOCATION}/${child}")
700+
set(${result} "${SDL_LOCATION}/${child}" PARENT_SCOPE)
701+
break()
702+
endif()
703+
endforeach()
704+
endfunction()
705+
706+
find_package(SDL2 REQUIRED)
707+
find_package(SDL2_mixer REQUIRED)
708+
if(${TARGET_PLATFORM} STREQUAL "emscripten")
709+
set(LINK_MPG123 true)
710+
endif()
711+
endif()
680712

681-
find_package(SDL2 REQUIRED)
682-
find_package(SDL2_mixer REQUIRED)
713+
if(LINK_MPG123)
714+
find_package(MPG123 REQUIRED)
715+
endif()
683716

684717
if(${TARGET_PLATFORM} STREQUAL "android")
685718
add_library(${SHORT_NAME} SHARED ${SDL2_ANDROID_HOOK} ${SOURCE_FILES})
686719
else()
687720
add_executable(${SHORT_NAME} WIN32 ${SOURCE_FILES})
688721
endif()
689722

723+
if(${TARGET_PLATFORM} STREQUAL "emscripten")
724+
set(CMAKE_EXECUTABLE_SUFFIX .html)
725+
set_target_properties(${SHORT_NAME} PROPERTIES LINK_FLAGS "-lidbfs.js -s SINGLE_FILE=1 -s DEMANGLE_SUPPORT=1 -s DYNCALLS=1 --shell-file ${PROJECT_SOURCE_DIR}/res/shell.html --preload-file ${PROJECT_SOURCE_DIR}/assets@/assets -s INITIAL_MEMORY=268435456 -s ALLOW_MEMORY_GROWTH=1 -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=0 -s SAFE_HEAP=1 -s INVOKE_RUN=0 -s EXPORTED_FUNCTIONS=[\"_main\"] -s EXPORTED_RUNTIME_METHODS=[\"callMain\",\"FS\"] --bind")
726+
endif()
727+
690728
if(MSVC)
691729
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
692730
endif()
@@ -753,8 +791,12 @@ if(APPLE)
753791
" BUNDLE DESTINATION ${CMAKE_BINARY_DIR})
754792
endif()
755793

756-
include_directories(${SDL2_INCLUDE_DIR})
757-
include_directories(${SDL2_MIXER_INCLUDE_DIR})
794+
if(SDL2_INCLUDE_DIR)
795+
include_directories(${SDL2_INCLUDE_DIR})
796+
endif()
797+
if(SDL2_MIXER_INCLUDE_DIR)
798+
include_directories(${SDL2_MIXER_INCLUDE_DIR})
799+
endif()
758800

759801
if(SYSTEM_LIBS)
760802
find_package(ZLIB)
@@ -903,6 +945,9 @@ else()
903945
find_library(android-lib android)
904946
target_link_libraries(${SHORT_NAME} ${log-lib} ${android-lib})
905947
endif()
948+
if(LINK_MPG123)
949+
target_link_libraries(${SHORT_NAME} ${MPG123_LIBRARY})
950+
endif()
906951
target_link_libraries (${SHORT_NAME} ${SDL2_LIBRARY} ${SDL2_MIXER_LIBRARY})
907952
if(NOT APPLE AND NOT ${TARGET_PLATFORM} STREQUAL "android")
908953
install(TARGETS ${SHORT_NAME} RUNTIME DESTINATION bin)

‎gen/shell.html.in

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8">
55
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<title>Julius</title>
7+
<title>Augustus</title>
88
<script async src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.4/FileSaver.min.js"></script>
99
<script async src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.5.0/jszip.min.js"></script>
1010
<style>
@@ -105,7 +105,7 @@
105105
background-position: center;
106106
background-repeat: no-repeat;
107107
background-size: contain;
108-
background-image: url(https://raw.githubusercontent.com/bvschaik/julius/master/res/julius_512.png);
108+
background-image: url(https://raw.githubusercontent.com/Keriew/augustus/master/res/augustus_512.png);
109109
opacity: 0.25;
110110
top: 0;
111111
left: 0;
@@ -239,15 +239,15 @@
239239
</head>
240240
<body>
241241
<div id="infoLocation">
242-
<h1>Julius <small>online</small></h1>
242+
<h1>Augustus <small>online</small></h1>
243243
<div id="middleElement">
244244
<div id="loadStatus">
245245
<div id="spinner" class="spinner"></div>
246-
<div id="status"><noscript>Please enable JavaScript to play Julius</noscript>Waiting for JavaScript to load...</div>
246+
<div id="status"><noscript>Please enable JavaScript to play Augustus</noscript>Waiting for JavaScript to load...</div>
247247
<progress id="progress"></progress>
248248
</div>
249249
<div id="startOptions">
250-
<div id="play" onclick="Module.startGame()">Play Julius</div>
250+
<div id="play" onclick="Module.startGame()">Play Augustus</div>
251251
<input type="button" id="downloadSaves" onclick="Module.downloadSaves()" value="Download Saves">
252252
<input type="button" class="red" onclick="Module.showSubWindow(confirmElement)" value="Delete all files">
253253
</div>
@@ -692,7 +692,7 @@
692692
var hasData = false;
693693
Module.setStatus('Checking for downloadable saves...');
694694
Object.keys(FS.lookupPath(C3Dir).node.contents).forEach(element => {
695-
if (element.endsWith('.sav') || element.endsWith('.map')) {
695+
if (element.endsWith('.sav') || element.endsWith('.svx') || element.endsWith('.map')) {
696696
var array = FS.readFile(C3Dir + '/' + element);
697697
zip.file(element, array);
698698
hasData = true;
@@ -706,7 +706,7 @@
706706
Module.setStatus('Creating zip file...');
707707
zip.generateAsync({type:"blob"}).then(function(blob) {
708708
Module.showFirstScreen();
709-
saveAs(blob, 'julius savefiles.zip');
709+
saveAs(blob, 'augustus savefiles.zip');
710710
}, function (err) {
711711
Module.setStatus('There was an error creating the zip file:<br><br>' + err +
712712
'<br><br><small>(Click to exit this prompt)</small>');

‎src/platform/file_manager.c

+10
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ static int is_file(int mode)
117117
static const char *ASSET_DIRS[MAX_ASSET_DIRS] = {
118118
#ifdef _WIN32
119119
"***SDL_BASE_PATH***",
120+
#endif
121+
#ifdef __EMSCRIPTEN__
122+
"",
120123
#endif
121124
".",
122125
#ifdef __vita__
@@ -453,6 +456,13 @@ int platform_file_manager_remove_file(const char *filename)
453456
return 0;
454457
}
455458

459+
FILE *platform_file_manager_open_asset(const char *asset, const char *mode)
460+
{
461+
get_assets_directory();
462+
const char *cased_asset_path = dir_get_asset(assets_directory, asset);
463+
return fopen(cased_asset_path, mode);
464+
}
465+
456466
#else
457467

458468
FILE *platform_file_manager_open_file(const char *filename, const char *mode)

0 commit comments

Comments
 (0)
Please sign in to comment.