Skip to content

Commit

Permalink
emulator: support webassembly and desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
gapry committed Jul 31, 2024
1 parent eddd820 commit 29e96d0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 73 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ jobs:
- uses: lukka/get-cmake@latest
- name: install dependencies
run: ./vcpkg.sh
- name: build
run: make build
- name: build
- name: build desktop
run: make build_desktop
- name: build webassembly
run: make build_webassembly
- name: build test
run: make test

ci-mac:
Expand All @@ -39,9 +41,11 @@ jobs:
- uses: lukka/get-cmake@latest
- name: install dependencies
run: ./vcpkg.sh
- name: build
run: make build
- name: build
- name: build desktop
run: make build_desktop
- name: build webassembly
run: make build_webassembly
- name: build test
run: make test

fmt:
Expand Down
35 changes: 10 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ file(GLOB_RECURSE src_app "${dir_app}/*.cpp")
file(GLOB_RECURSE src_emulator "${dir_emulator}/*.cpp")
file(GLOB_RECURSE src_test "${dir_test}/*.cpp")

OPTION(BUILD_APP "Build App" OFF)
OPTION(BUILD_TEST "Build Test" OFF)
OPTION(BUILD_WASM "Build WASM" OFF)
OPTION(BUILD_DESKTOP "Build Desktop" OFF)
OPTION(BUILD_TEST "Build Test" OFF)
OPTION(BUILD_WASM "Build Webassembly" OFF)

IF(BUILD_APP)
add_library(${dir_emulator} ${src_emulator})
add_library(${dir_emulator} ${src_emulator})

target_compile_options(${dir_emulator} PUBLIC -g)
target_compile_options(${dir_emulator} PUBLIC -O0)
target_compile_options(${dir_emulator} PUBLIC -g)
target_compile_options(${dir_emulator} PUBLIC -O0)

target_link_libraries(${dir_emulator} PUBLIC -lm)
IF(BUILD_DESKTOP)
target_link_libraries(${dir_emulator} PUBLIC fmt::fmt)
target_link_libraries(${dir_emulator} PUBLIC SDL2::SDL2)

Expand All @@ -43,15 +42,9 @@ IF(BUILD_APP)
target_compile_options(${project_name}.out PRIVATE -O0)

target_link_libraries(${project_name}.out PUBLIC ${dir_emulator})
ENDIF(BUILD_APP)
ENDIF(BUILD_DESKTOP)

IF(BUILD_TEST)
add_library(${dir_emulator} ${src_emulator})

target_compile_options(${dir_emulator} PUBLIC -g)
target_compile_options(${dir_emulator} PUBLIC -O0)

target_link_libraries(${dir_emulator} PUBLIC -lm)
target_link_libraries(${dir_emulator} PUBLIC fmt::fmt)
target_link_libraries(${dir_emulator} PUBLIC SDL2::SDL2)

Expand All @@ -68,22 +61,14 @@ IF(BUILD_TEST)
ENDIF(BUILD_TEST)

IF(BUILD_WASM)
set(CMAKE_CXX_COMPILER em++)
set(CMAKE_C_COMPILER emcc)
set(CMAKE_EXECUTABLE_SUFFIX ".html")

add_library(${dir_emulator} ${src_emulator})

target_compile_options(${dir_emulator} PUBLIC -g)
target_compile_options(${dir_emulator} PUBLIC -O0)

target_link_libraries(${dir_emulator} PUBLIC "-s WASM=1 -s USE_SDL=2 -s -O3 --preload-file ../roms@/roms")

add_executable(${project_name}_wasm ${src_app})

target_compile_options(${project_name}_wasm PRIVATE -O3)
target_compile_options(${project_name}_wasm PRIVATE -g)
target_compile_options(${project_name}_wasm PRIVATE -O0)

target_link_libraries(${project_name}_wasm PUBLIC ${dir_emulator})

set_target_properties(${project_name}_wasm PROPERTIES LINK_FLAGS "-s WASM=1 -s USE_SDL=2 -s -O3 --preload-file ../roms@/roms")
ENDIF(BUILD_WASM)
52 changes: 28 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
app = arabica
test = test_$(app)
cc = clang
cxx = clang++
cc_desktop = clang
cxx_desktop = clang++
cc_wasm = emcc
cxx_wasm = em++
src_app = app
src_emulator = arabica
src_test = test
Expand All @@ -10,40 +12,42 @@ dir_build = build
dir_rom = roms
game = Tetris_Fran_Dachille_1991.ch8

default: build_sdl execute
default: desktop

fmt:
for dir in $(src); do \
find $$dir -type f -iname "*.cpp" -o -iname "*.hpp" | xargs clang-format -i -style=file; \
done

build_sdl: clean
build_desktop: clean
mkdir $(dir_build);
cd $(dir_build); cmake -DCMAKE_C_COMPILER="$(cc)" -DCMAKE_CXX_COMPILER="$(cxx)" -DBUILD_APP=ON -GNinja ..; ninja;
cd $(dir_build); cmake -DCMAKE_C_COMPILER="$(cc_desktop)" -DCMAKE_CXX_COMPILER="$(cxx_desktop)" -DBUILD_DESKTOP=ON -GNinja ..; ninja;

build_wasm: clean
build_webassembly: clean
mkdir -p $(dir_build)
cd $(dir_build) && cmake -DCMAKE_C_COMPILER=emcc -DCMAKE_CXX_COMPILER=em++ -DBUILD_WASM=ON -GNinja .. && ninja
cd $(dir_build) && cmake -DCMAKE_C_COMPILER="$(cc_wasm)" -DCMAKE_CXX_COMPILER="$(cxx_wasm)" -DBUILD_WASM=ON -GNinja .. && ninja;

execute:
build_test: clean
mkdir $(dir_build);
cd $(dir_build); cmake -DCMAKE_C_COMPILER="$(cc_desktop)" -DCMAKE_CXX_COMPILER="$(cxx_desktop)" -DBUILD_TEST=ON -GNinja ..; ninja;

desktop: build_desktop
./$(dir_build)/$(app).out $(dir_rom)/$(game)

clean:
rm -rf $(dir_build)
webassembly: build_webassembly kill_webserver
cd build; python3 -m http.server 8080 &
firefox http://localhost:8080/arabica_wasm.html

test: clean
mkdir $(dir_build); \
cd $(dir_build); cmake -DCMAKE_C_COMPILER="$(cc)" -DCMAKE_CXX_COMPILER="$(cxx)" -DBUILD_TEST=ON -GNinja ..; ninja; \
./$(test).out
test: build_test
./$(test).app

debug: clean build_sdl
debug: build_desktop
gdb -x commands.gdb --args ./$(dir_build)/$(app).out $(dir_rom)/$(game).ch8

web: build_wasm
cd build; python3 -m http.server 8080 &
firefox http://localhost:8080/arabica_wasm.html
clean:
rm -rf $(dir_build)

fmt:
for dir in $(src); do \
find $$dir -type f -iname "*.cpp" -o -iname "*.hpp" | xargs clang-format -i -style=file; \
done

kill_webserver:
pkill -f 8080

.PHONY: default fmt build_sdl build_wasm execute clean debug
.PHONY: default build_desktop build_webassembly build_test desktop webassembly test debug clean fmt kill_webserver
27 changes: 9 additions & 18 deletions app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,21 @@
#include <sstream>
#endif

std::string rom_file(int argc, char* argv[]) {
#ifdef __EMSCRIPTEN__
std::string read_file_from_browser(const std::string& file_name) {
std::ifstream file(file_name);
if (!file.is_open()) {
ARABICA_LOG_INFO("Failed to open file: {}\n", file_name);
const char* const rom = "/roms/Tetris_Fran_Dachille_1991.ch8";
return rom;
#else
if (argc != 2) {
ARABICA_LOG_INFO("Usage: ./arabica.out rom-file\n");
std::exit(1);
}
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
return argv[1];
#endif
}

int main(int argc, char* argv[]) {
#ifdef __EMSCRIPTEN__
std::string rom_file = "/roms/Tetris_Fran_Dachille_1991.ch8";
#else
if (argc != 2) {
ARABICA_LOG_INFO("{}\n", "Usage: ./arabica.out rom-file");
return 1;
}
std::string rom_file = argv[1];
#endif
arabica::Window window("Arabica Emulator", 640, 320, rom_file);
arabica::Window window("Arabica Emulator", 640, 320, rom_file(argc, argv));
window.execute();
return 0;
}

0 comments on commit 29e96d0

Please sign in to comment.