|
|
|
@ -11,141 +11,75 @@
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <emscripten.h>
|
|
|
|
|
|
|
|
|
|
Game *game = nullptr;
|
|
|
|
|
|
|
|
|
|
struct context{
|
|
|
|
|
int main(int argc, const char * argv[])
|
|
|
|
|
{
|
|
|
|
|
const int FPS = 60;
|
|
|
|
|
const int frameDelay = 1000 / FPS;
|
|
|
|
|
|
|
|
|
|
Uint64 frameStart;
|
|
|
|
|
int frameTime;
|
|
|
|
|
// 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;
|
|
|
|
|
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;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void mainloop(void *arg) {
|
|
|
|
|
context *ctx = static_cast<context*>(arg);
|
|
|
|
|
while (game->running())
|
|
|
|
|
// =============================
|
|
|
|
|
// Load cJSON config.json file
|
|
|
|
|
// =============================
|
|
|
|
|
// Starting with Error Checking
|
|
|
|
|
std::string configPath = "src/config/config.json";
|
|
|
|
|
std::ifstream fin(configPath);
|
|
|
|
|
|
|
|
|
|
if(fin.is_open()){
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
isWindowFS = false;
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
isWindowFS = true;
|
|
|
|
|
}
|
|
|
|
|
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"<<std::endl;
|
|
|
|
|
// // }
|
|
|
|
|
//
|
|
|
|
|
// // if(ctx.fin.fail()){
|
|
|
|
|
// // std::cout<<"config.json load failed"<<std::endl;
|
|
|
|
|
// // }
|
|
|
|
|
//
|
|
|
|
|
// return 0;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
context ctx;
|
|
|
|
|
|
|
|
|
|
windowWidth = windowWidth*globalScale;
|
|
|
|
|
windowHeight = windowHeight*globalScale;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
emscripten_set_main_loop_arg(mainloop, &ctx, -1, 1);
|
|
|
|
|
game->init(windowName->valuestring, windowWidth, windowHeight, isWindowFS, globalScale);
|
|
|
|
|
// cJSON memory management
|
|
|
|
|
cJSON_Delete(myJSON);
|
|
|
|
|
|
|
|
|
|
while (game->running())
|
|
|
|
|
{
|
|
|
|
|
ctx.frameStart = SDL_GetTicks64();
|
|
|
|
|
frameStart = SDL_GetTicks64();
|
|
|
|
|
|
|
|
|
|
game->handleEvents();
|
|
|
|
|
game->update();
|
|
|
|
|
game->render();
|
|
|
|
|
|
|
|
|
|
ctx.frameTime = SDL_GetTicks64() - ctx.frameStart;
|
|
|
|
|
frameTime = SDL_GetTicks64() - frameStart;
|
|
|
|
|
|
|
|
|
|
if(ctx.frameDelay > ctx.frameTime)
|
|
|
|
|
if(frameDelay > frameTime)
|
|
|
|
|
{
|
|
|
|
|
SDL_Delay(ctx.frameDelay - ctx.frameTime);
|
|
|
|
|
SDL_Delay(frameDelay - frameTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
game->clean();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
std::cout<<"config.json not found or opened"<<std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(fin.fail()){
|
|
|
|
|
std::cout<<"config.json load failed"<<std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|