Skip to content

Commit

Permalink
Merge pull request #4 from parwen68/add-wasm-target
Browse files Browse the repository at this point in the history
Add Emscripten build
  • Loading branch information
feresr committed Apr 23, 2022
2 parents 520ed00 + 9579322 commit 21fd13d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ vendor/*

# Ignore Mac specific files
.DS_Store

# Ignore VSCODE settings
.vscode
20 changes: 15 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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 ()

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 17 additions & 5 deletions src/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#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;
Expand Down

0 comments on commit 21fd13d

Please sign in to comment.