From 903f81f73828da45a282dd1aeb9f80996e996a39 Mon Sep 17 00:00:00 2001 From: Alan Youngblood Date: Thu, 10 Aug 2023 17:40:33 -0400 Subject: [PATCH] emscripten building, but broken on web --- build_emscripten.sh | 2 +- src/game/Game.cpp | 2 +- src/game/Main.cpp | 128 ++++++++++++++++++++++++++++++++------------ 3 files changed, 95 insertions(+), 37 deletions(-) diff --git a/build_emscripten.sh b/build_emscripten.sh index 686f7d1..1f4c3ba 100755 --- a/build_emscripten.sh +++ b/build_emscripten.sh @@ -1 +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 +emcc src/game/Main.cpp src/game/Game.cpp src/assetmgr/AssetManager.cpp src/assetmgr/GameObject.cpp src/assetmgr/Map.cpp src/assetmgr/MusicManager.cpp src/assetmgr/TextureManager.cpp src/cjson/cJSON.c src/ecs/ECS.cpp src/game/Collision.cpp src/game/GameStateManager.cpp src/game/StateMachine.cpp src/game/Vector2D.cpp src/ui/UINineSlice.cpp src/ui/UIText.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/Game.cpp b/src/game/Game.cpp index 083ccbf..e7d44e9 100644 --- a/src/game/Game.cpp +++ b/src/game/Game.cpp @@ -116,7 +116,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g flags = SDL_WINDOW_FULLSCREEN; } - if(SDL_Init(SDL_INIT_EVERYTHING) == 0) + if(SDL_Init(SDL_INIT_EVERYTHING & ~(SDL_INIT_TIMER | SDL_INIT_HAPTIC)) == 0) { window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags); SDL_SetWindowBordered(window,SDL_FALSE); diff --git a/src/game/Main.cpp b/src/game/Main.cpp index f4627be..687604a 100644 --- a/src/game/Main.cpp +++ b/src/game/Main.cpp @@ -21,48 +21,113 @@ struct context{ Uint64 frameStart; int frameTime; - std::string configPath = "src/config/config.json"; - std::ifstream fin(configPath); + // std::string configPath = "src/config/config.json"; + // std::ifstream fin(configPath); // 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(); - std::string json = tmp.str(); - cJSON * myJSON = cJSON_Parse(json.c_str()); - cJSON * windowName = cJSON_GetObjectItemCaseSensitive(myJSON, "WindowName"); - cJSON * windowSize = cJSON_GetObjectItem(myJSON, "WindowSize"); - int windowWidth = cJSON_GetObjectItem(windowSize, "w")->valueint; - int windowHeight = cJSON_GetObjectItem(windowSize, "h")->valueint; - int windowFS = cJSON_GetObjectItem(myJSON, "WindowFullScreen")->valueint; - int globalScale = cJSON_GetObjectItem(myJSON, "GlobalScale")->valueint; - bool isWindowFS; - if (windowFS==0) - { - isWindowFS = false; - } else - { - isWindowFS = true; - } + // std::ifstream jsonText("src/config/config.json"); + // std::ostringstream tmp; + // tmp << jsonText.rdbuf(); + // std::string json = tmp.str(); + // cJSON * myJSON = cJSON_Parse(json.c_str()); + // cJSON * windowName = cJSON_GetObjectItemCaseSensitive(myJSON, "WindowName"); + // cJSON * windowSize = cJSON_GetObjectItem(myJSON, "WindowSize"); + // int windowWidth = cJSON_GetObjectItem(windowSize, "w")->valueint; + // int windowHeight = cJSON_GetObjectItem(windowSize, "h")->valueint; + // int windowFS = cJSON_GetObjectItem(myJSON, "WindowFullScreen")->valueint; + // int globalScale = cJSON_GetObjectItem(myJSON, "GlobalScale")->valueint; + int windowWidth = 64; + int windowHeight = 64; + bool isWindowFS = false; + int globalScale = 10; + std::string windowName = "Kaiju Saves Earth"; + // bool isWindowFS; + // if (windowFS==0) + // { + // isWindowFS = false; + // } else + // { + // isWindowFS = true; + // } // } }; -int main(int argc, const char * argv[]) +void mainloop(void *arg) { + context *ctx = static_cast(arg); + while (game->running()) + { + ctx->frameStart = SDL_GetTicks64(); + + game->handleEvents(); + game->update(); + game->render(); + + ctx->frameTime = SDL_GetTicks64() - ctx->frameStart; + + if(ctx->frameDelay > ctx->frameTime) + { + SDL_Delay(ctx->frameDelay - ctx->frameTime); + } + } + std::cout << "Maybe this will at least work?" << std::endl; +} + +// int main(int argc, const char * argv[]) +// { +// context ctx; +// // if(ctx.fin.is_open()){ +// +// game = new Game(); +// ctx.windowWidth = ctx.windowWidth*ctx.globalScale; +// ctx.windowHeight = ctx.windowHeight*ctx.globalScale; +// game->init(ctx.windowName.c_str(), ctx.windowWidth, ctx.windowHeight, ctx.isWindowFS, ctx.globalScale); +// // cJSON memory management +// // cJSON_Delete(ctx.myJSON); +// +// // init(&ctx); +// emscripten_set_main_loop_arg(mainloop, &ctx, -1, 1); +// +// while (game->running()) +// { +// ctx.frameStart = SDL_GetTicks64(); +// +// game->handleEvents(); +// game->update(); +// game->render(); +// +// ctx.frameTime = SDL_GetTicks64() - ctx.frameStart; +// +// if(ctx.frameDelay > ctx.frameTime) +// { +// SDL_Delay(ctx.frameDelay - ctx.frameTime); +// } +// +// } +// game->clean(); +// // } else { +// // std::cout<<"config.json not found or opened"<init(ctx.windowName->valuestring, ctx.windowWidth, ctx.windowHeight, ctx.isWindowFS, ctx.globalScale); -// cJSON memory management - cJSON_Delete(ctx.myJSON); + game->init(ctx.windowName.c_str(), ctx.windowWidth, ctx.windowHeight, ctx.isWindowFS, ctx.globalScale); - init(&ctx); + emscripten_set_main_loop_arg(mainloop, &ctx, -1, 1); while (game->running()) { @@ -81,13 +146,6 @@ int main(int argc, const char * argv[]) } game->clean(); - } else { - std::cout<<"config.json not found or opened"<