From e9b36ed8c32ee3da67541ec7f4caf37d39af5619 Mon Sep 17 00:00:00 2001 From: Alan Youngblood Date: Wed, 9 Aug 2023 17:01:59 -0400 Subject: [PATCH] Beginning emscripten refactor with separate build script --- CMakeLists.txt | 1 + build_emscripten.sh | 1 + src/game/Main.cpp | 44 ++++++++++++++++++++++++++------------------ 3 files changed, 28 insertions(+), 18 deletions(-) create mode 100755 build_emscripten.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 3955b63..de27460 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(KaijuSaveEarth VERSION 1.0.0 ) #set(CMAKE_CXX_STANDARD 11) # old requirements set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to be used") # new requirements for tileson set(CMAKE_CXX_REQUIRED ON) +#set(CMAKE_CXX_COMPILER "/home/ayoungblood/Git/emsdk/upstream/emscripten/emcc") include(FetchContent) include(FindPkgConfig) diff --git a/build_emscripten.sh b/build_emscripten.sh new file mode 100755 index 0000000..686f7d1 --- /dev/null +++ b/build_emscripten.sh @@ -0,0 +1 @@ +emcc src/game/Main.cpp -s WASM=1 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' -s USE_SDL_MIXER=2 -o build/index.js diff --git a/src/game/Main.cpp b/src/game/Main.cpp index 791169b..f4627be 100644 --- a/src/game/Main.cpp +++ b/src/game/Main.cpp @@ -11,25 +11,21 @@ #include #include #include +#include Game *game = nullptr; -int main(int argc, const char * argv[]) -{ +struct context{ const int FPS = 60; const int frameDelay = 1000 / FPS; Uint64 frameStart; int frameTime; - -// ============================= -// Load cJSON config.json file -// ============================= -// Starting with Error Checking std::string configPath = "src/config/config.json"; std::ifstream fin(configPath); - - if(fin.is_open()){ + // if(!fin.is_open()){ + // std::cout << "Failed to load Config.json" << std::endl; + // } else { std::ifstream jsonText("src/config/config.json"); std::ostringstream tmp; tmp << jsonText.rdbuf(); @@ -49,26 +45,38 @@ int main(int argc, const char * argv[]) { isWindowFS = true; } - windowWidth = windowWidth*globalScale; - windowHeight = windowHeight*globalScale; + + // } +}; + + +int main(int argc, const char * argv[]) +{ + context ctx; + if(ctx.fin.is_open()){ + game = new Game(); - game->init(windowName->valuestring, windowWidth, windowHeight, isWindowFS, globalScale); + ctx.windowWidth = ctx.windowWidth*ctx.globalScale; + ctx.windowHeight = ctx.windowHeight*ctx.globalScale; + game->init(ctx.windowName->valuestring, ctx.windowWidth, ctx.windowHeight, ctx.isWindowFS, ctx.globalScale); // cJSON memory management - cJSON_Delete(myJSON); + cJSON_Delete(ctx.myJSON); + + init(&ctx); while (game->running()) { - frameStart = SDL_GetTicks64(); + ctx.frameStart = SDL_GetTicks64(); game->handleEvents(); game->update(); game->render(); - frameTime = SDL_GetTicks64() - frameStart; + ctx.frameTime = SDL_GetTicks64() - ctx.frameStart; - if(frameDelay > frameTime) + if(ctx.frameDelay > ctx.frameTime) { - SDL_Delay(frameDelay - frameTime); + SDL_Delay(ctx.frameDelay - ctx.frameTime); } } @@ -77,7 +85,7 @@ int main(int argc, const char * argv[]) std::cout<<"config.json not found or opened"<