From 695fe7d1558965c07f80b0819eafc987f25331ce Mon Sep 17 00:00:00 2001 From: Par Wenaker Date: Sat, 19 Feb 2022 17:33:30 +0100 Subject: [PATCH] Add Emscripten build --- .gitignore | 5 ++++- CMakeLists.txt | 20 +++++++++++++++----- README.md | 21 +++++++++++++++++++++ src/Main.cpp | 22 +++++++++++++++++----- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index e2b458e..134a64c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,7 @@ lib/* bin/* # Ignore Mac specific files -.DS_Store \ No newline at end of file +.DS_Store + +# Ignore VSCODE settings +.vscode \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d987ba..c8b3e32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,10 +4,18 @@ project(smb) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") -find_package(SDL2_mixer REQUIRED) -find_package(SDL2 REQUIRED) -find_package(SDL2_image REQUIRED) -find_package(SDL2_ttf REQUIRED) +if ( ${CMAKE_SYSTEM_NAME} MATCHES "Emscripten" ) + set(USE_FLAGS "-s USE_SDL=2 -s USE_SDL_IMAGE=2 -s USE_SDL_TTF=2 -s USE_SDL_MIXER=2") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${USE_FLAGS}") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${USE_FLAGS}") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${USE_FLAGS} -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 --preload-file ../assets@/assets --use-preload-plugins") + set(CMAKE_EXECUTABLE_SUFFIX .html) +else () + find_package(SDL2_mixer REQUIRED) + find_package(SDL2 REQUIRED) + find_package(SDL2_image REQUIRED) + find_package(SDL2_ttf REQUIRED) +endif () set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin") @@ -26,5 +34,7 @@ set_property(TARGET smb PROPERTY CXX_STANDARD 17) set_target_properties(smb PROPERTIES OUTPUT_NAME smb-${CMAKE_BUILD_TYPE}) target_include_directories(smb PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) target_include_directories(smb PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor) -target_link_libraries(smb PRIVATE ${SDL2_MIXER_LIBRARY} ${SDL2} ${SDL2_IMAGE} ${SDL2_TTF}) +if ( NOT ${CMAKE_SYSTEM_NAME} MATCHES "Emscripten" ) + target_link_libraries(smb PRIVATE ${SDL2_MIXER_LIBRARY} ${SDL2} ${SDL2_IMAGE} ${SDL2_TTF}) +endif () diff --git a/README.md b/README.md index a6fbdb2..e48b1d1 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,27 @@ $ cd ../bin/ $ ./smb- ``` +## Webassembly + +Either install [emsdk](https://emscripten.org/docs/getting_started/downloads.html) and run +``` +$ mkdir build +$ cd build +$ emcmake cmake .. +$ make +``` +or build with [Docker](https://www.docker.com/) +``` +$ docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk emcmake cmake -S . -B build +$ docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk cmake --build build +``` +Start a webserver in the bin folder +``` +$ cd bin +$ python -m SimpleHTTPServer 8000 +$ open http://localhost:8000/smb-.html +``` + ## Screenshots ![Game screenshot](https://github.com/feresr/super-mario-bros/blob/master/readme/game.png) diff --git a/src/Main.cpp b/src/Main.cpp index ad7f4eb..86a6476 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,22 +1,34 @@ +#ifdef __EMSCRIPTEN__ +#include +#endif #include "Game.h" +void runLoop(void* arg) { + Game *game = (Game*)arg; + game->handleEvents(); + game->update(); +} + int main() { uint32_t frameStart, frameTime; Game* game = new Game(); game->init("Super Mario Bros", - SNES_RESOLUTION_WIDTH * ZOOM, - SNES_RESOLUTION_HEIGHT * ZOOM, - false); + SNES_RESOLUTION_WIDTH * ZOOM, + SNES_RESOLUTION_HEIGHT * ZOOM, + false); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop_arg(runLoop, game, 0, 1); +#else while (game->running()) { frameStart = SDL_GetTicks(); - game->handleEvents(); - game->update(); + runLoop(game); frameTime = SDL_GetTicks() - frameStart; if (frameTime < FRAME_DURATION) SDL_Delay(FRAME_DURATION - frameTime); } +#endif game->clean(); delete game; return EXIT_SUCCESS;