From 58cf23807695e56bdc4941989ba329a404cdb45f Mon Sep 17 00:00:00 2001 From: Carsten Teibes Date: Thu, 19 Sep 2024 11:15:10 +0200 Subject: [PATCH] Initial emscripten build Related #112 --- .github/workflows/ci.yml | 34 ++++++ CMakeLists.txt | 17 ++- builds/cmake/Platform-Helpers.cmake | 4 + res/emscripten/shell.html | 162 ++++++++++++++++++++++++++++ src/io/gfx/video.h | 5 + src/io/sound.cpp | 2 + 6 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 res/emscripten/shell.html diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4b0d431d..e9710e88 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -158,6 +158,40 @@ jobs: name: openjazz-windows-mingw-x86_64 path: OJ-*/ + web-emscripten: + name: Build (Emscripten) + needs: prepare + runs-on: emscripten/emsdk + steps: + - name: Install dependencies + run: | + sudo apt-get update -yqq + sudo apt-get install -yqq build-essential cmake ninja-build \ + asciidoctor w3m + + - name: Checkout + uses: actions/checkout@v4 + + - name: Build OpenJazz + run: | + emcmake cmake --workflow --preset release -DDATAPATH=/data -DNETWORK=OFF + touch build-release/data.js # dummy data + ls -l build-release + + - name: Prepare artifact + env: + OJ_VERSION: ${{ needs.prepare.outputs.oj_version }} + run: | + mkdir OJ-${OJ_VERSION} + builds/ci/adoc2txt ${OJ_VERSION} < res/unix/OpenJazz.6.adoc > OJ-${OJ_VERSION}/Manual.txt + cp build-release/*.{wasm,js,html,data} README.md COPYING licenses.txt OJ-${OJ_VERSION}/ + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: openjazz-web + path: OJ-*/ + nintendo-devkitpro: strategy: fail-fast: false diff --git a/CMakeLists.txt b/CMakeLists.txt index 278f36ed..578717d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,7 +130,12 @@ endif() # libraries -if(LEGACY_SDL) +if(EMSCRIPTEN) + # emscripten ships a "port" of SDL2 + set(OJ_LIBS_SDL "SDL2") + set(SDL_STATUS "SDL2 (emscripten port)") + target_compile_options(OpenJazz PRIVATE "--use-port=sdl2;-flto;-fexceptions") +elseif(LEGACY_SDL) find_package(SDL REQUIRED) target_include_directories(OpenJazz PRIVATE ${SDL_INCLUDE_DIR}) set(OJ_LIBS_SDL ${SDL_LIBRARY}) @@ -260,6 +265,16 @@ if(WIN32) set_target_properties(OpenJazz PROPERTIES WIN32_EXECUTABLE $<$>:TRUE>) # add icon target_sources(OpenJazz PRIVATE res/windows/OpenJazz.rc) +elseif(EMSCRIPTEN) + set_target_properties(OpenJazz PROPERTIES SUFFIX ".html") + set(JS_SHELL "${CMAKE_CURRENT_SOURCE_DIR}/res/emscripten/shell.html") + + set_property(TARGET OpenJazz PROPERTY LINK_FLAGS + "--shell-file ${JS_SHELL} -sMINIFY_HTML=0 -flto --use-port=sdl2 + -sFORCE_FILESYSTEM -sEXIT_RUNTIME=1 -sASYNCIFY + -sENVIRONMENT=web --closure 1 -sEXPORTED_RUNTIME_METHODS=['allocate'] + -sASSERTIONS -sNO_DISABLE_EXCEPTION_CATCHING") + set_source_files_properties("src/main.cpp" PROPERTIES OBJECT_DEPENDS "${JS_SHELL}") elseif(RISCOS) target_sources(OpenJazz PRIVATE src/platforms/riscos.cpp diff --git a/builds/cmake/Platform-Helpers.cmake b/builds/cmake/Platform-Helpers.cmake index 4d57a35c..c6f0b3ad 100644 --- a/builds/cmake/Platform-Helpers.cmake +++ b/builds/cmake/Platform-Helpers.cmake @@ -38,6 +38,10 @@ elseif(HAIKU) list(APPEND PLATFORM_LIST ${OJ_HOST}) elseif(WIN32) set(OJ_LIBS_NET "-lws2_32") +elseif(EMSCRIPTEN) + add_compile_definitions(EMSCRIPTEN) + set(OJ_HOST "Web") + set(OJ_ALLOW_SCALE OFF) elseif(PSP) set(OJ_HOST "PSP") set(OJ_ALLOW_SCALE OFF) diff --git a/res/emscripten/shell.html b/res/emscripten/shell.html new file mode 100644 index 00000000..3902ccca --- /dev/null +++ b/res/emscripten/shell.html @@ -0,0 +1,162 @@ + + + + + + OpenJazz + + + +
+
Downloading...
+ +
+ +
+ + + +

+ +
+ This is OpenJazz powered by emscripten. +
+ + + + {{{ SCRIPT }}} + + diff --git a/src/io/gfx/video.h b/src/io/gfx/video.h index 3efe343a..5ab72123 100644 --- a/src/io/gfx/video.h +++ b/src/io/gfx/video.h @@ -100,6 +100,11 @@ #define NO_RESIZE #define FULLSCREEN_FLAGS (SDL_FULLSCREEN | SDL_HWSURFACE) +#elif defined(EMSCRIPTEN) + #define DEFAULT_SCREEN_WIDTH 640 + #define DEFAULT_SCREEN_HEIGHT 480 + + #define FULLSCREEN_FLAGS (SDL_FULLSCREEN | SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWPALETTE) #else #define DEFAULT_SCREEN_WIDTH SW #define DEFAULT_SCREEN_HEIGHT SH diff --git a/src/io/sound.cpp b/src/io/sound.cpp index 9a846118..10da24d6 100644 --- a/src/io/sound.cpp +++ b/src/io/sound.cpp @@ -194,6 +194,8 @@ void openAudio () { (SDL_AUDIO_BITSIZE(audioSpec.format) != 8 && SDL_AUDIO_BITSIZE(audioSpec.format) != 16)) { LOG_DEBUG("SDL audio format unsupported, letting SDL convert it."); + if(audioDevice) SDL_CloseAudioDevice(audioDevice); + audioDevice = SDL_OpenAudioDevice(nullptr, 0, &asDesired, &audioSpec, 0); } audioOk = (audioDevice != 0);