Browse Source

a working baseline for Emscripten builds

pull/8312/head
HoofedEar 4 months ago
parent
commit
b0dd56659d
  1. 4
      3rdParty/Lua/CMakeLists.txt
  2. 45
      CMake/emscripten_pre.js
  3. 18
      CMakeLists.txt
  4. 4
      Source/diablo.cpp
  5. 9
      Source/engine/dx.cpp

4
3rdParty/Lua/CMakeLists.txt vendored

@ -26,6 +26,10 @@ if(CMAKE_SYSTEM_NAME MATCHES "Darwin" AND DARWIN_MAJOR_VERSION VERSION_EQUAL 8)
# localtime_r gmtime_r
find_package(MacportsLegacySupport REQUIRED)
target_link_libraries(lua_static PRIVATE MacportsLegacySupport::MacportsLegacySupport)
elseif(EMSCRIPTEN)
# Enable pthread support for Emscripten to match SDL2's USE_PTHREADS=1
target_compile_options(lua_static PUBLIC -pthread)
target_link_options(lua_static PUBLIC -pthread)
elseif(TARGET_PLATFORM STREQUAL "dos")
target_compile_definitions(lua_static PUBLIC -DLUA_USE_C89)
elseif(ANDROID AND ("${ANDROID_ABI}" STREQUAL "armeabi-v7a" OR "${ANDROID_ABI}" STREQUAL "x86"))

45
CMake/emscripten_pre.js

@ -0,0 +1,45 @@
// Pre-load MPQ files from the server directory into Emscripten virtual filesystem
Module['preRun'] = Module['preRun'] || [];
Module['preRun'].push(function() {
// List of MPQ files to try loading (in priority order)
var mpqFiles = [
'diabdat.mpq',
'DIABDAT.MPQ',
'spawn.mpq',
'hellfire.mpq',
'hfmonk.mpq',
'hfmusic.mpq',
'hfvoice.mpq',
'hfbard.mpq',
'hfbarb.mpq'
];
// Create a promise-based loading system
var loadPromises = mpqFiles.map(function(filename) {
return new Promise(function(resolve) {
fetch(filename)
.then(function(response) {
if (response.ok) {
return response.arrayBuffer();
}
throw new Error('File not found');
})
.then(function(data) {
console.log('Loading ' + filename + ' into virtual filesystem...');
FS.writeFile('/' + filename, new Uint8Array(data));
console.log('Successfully loaded ' + filename);
resolve();
})
.catch(function() {
// File doesn't exist, skip silently
resolve();
});
});
});
// Wait for all MPQ files to load before continuing
Module.addRunDependency('loadMPQs');
Promise.all(loadPromises).then(function() {
Module.removeRunDependency('loadMPQs');
});
});

18
CMakeLists.txt

@ -271,7 +271,10 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" AND NOT PS4)
if(APPLE)
add_link_options("$<$<NOT:$<CONFIG:Debug>>:LINKER:-dead_strip>")
else()
add_link_options("$<$<NOT:$<CONFIG:Debug>>:LINKER:--gc-sections,--as-needed>")
add_link_options("$<$<NOT:$<CONFIG:Debug>>:LINKER:--gc-sections>")
if(NOT EMSCRIPTEN)
add_link_options("$<$<NOT:$<CONFIG:Debug>>:LINKER:--as-needed>")
endif()
endif()
endif()
@ -301,7 +304,7 @@ endif()
# Not a genexp because CMake doesn't support it
# https://gitlab.kitware.com/cmake/cmake/-/issues/20546
if(NOT DISABLE_LTO)
if(NOT DISABLE_LTO AND NOT EMSCRIPTEN)
# LTO if supported:
include(CheckIPOSupported)
check_ipo_supported(RESULT is_ipo_supported OUTPUT lto_error)
@ -369,7 +372,7 @@ else()
Packaging/windows/devilutionx.rc
Packaging/apple/LaunchScreen.storyboard)
if(CMAKE_STRIP AND NOT DEVILUTIONX_DISABLE_STRIP)
if(CMAKE_STRIP AND NOT DEVILUTIONX_DISABLE_STRIP AND NOT EMSCRIPTEN)
add_custom_command(
TARGET ${BIN_TARGET} POST_BUILD
COMMAND $<$<OR:$<CONFIG:Release>,$<CONFIG:MinSizeRel>>:${CMAKE_STRIP}>
@ -398,7 +401,14 @@ include(Assets)
include(Mods)
if(EMSCRIPTEN)
target_link_options(${BIN_TARGET} PRIVATE --preload-file assets)
target_link_options(${BIN_TARGET} PRIVATE
--preload-file assets
-sFORCE_FILESYSTEM=1
-sALLOW_MEMORY_GROWTH=1
-sASYNCIFY
)
# Add JavaScript to load MPQ files from the server directory at runtime
target_link_options(${BIN_TARGET} PRIVATE --pre-js ${CMAKE_CURRENT_SOURCE_DIR}/CMake/emscripten_pre.js)
endif()
if(NOT USE_SDL1 AND NOT UWP_LIB)

4
Source/diablo.cpp

@ -168,7 +168,11 @@ bool gbGameLoopStartup;
bool forceSpawn;
bool forceDiablo;
int sgnTimeoutCurs;
#ifdef __EMSCRIPTEN__
bool gbShowIntro = false; // Skip intro videos in browser for performance
#else
bool gbShowIntro = true;
#endif
/** To know if these things have been done when we get to the diablo_deinit() function */
bool was_archives_init = false;
/** To know if surfaces have been initialized or not */

9
Source/engine/dx.cpp

@ -17,6 +17,10 @@
#include <SDL.h>
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include "controls/control_mode.hpp"
#include "controls/plrctrls.h"
#include "engine/render/primitive_render.hpp"
@ -259,6 +263,11 @@ void RenderPresent()
}
SDL_RenderPresent(renderer);
#ifdef __EMSCRIPTEN__
// Yield to browser to allow rendering
emscripten_sleep(1);
#endif
if (*GetOptions().Graphics.frameRateControl != FrameRateControl::VerticalSync) {
LimitFrameRate();
}

Loading…
Cancel
Save