Beginning emscripten refactor with separate build script

This commit is contained in:
Alan Youngblood 2023-08-09 17:01:59 -04:00
parent bab3487115
commit e9b36ed8c3
3 changed files with 28 additions and 18 deletions

View File

@ -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)

1
build_emscripten.sh Executable file
View File

@ -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

View File

@ -11,25 +11,21 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <emscripten.h>
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"<<std::endl;
}
if(fin.fail()){
if(ctx.fin.fail()){
std::cout<<"config.json load failed"<<std::endl;
}