#include "engine_loop.h"
#include "engine_global.h"
#include "render.h"
#include "resources.h"
#include "utils/debug.h"
#include "stdlib.h"
#include "raylib.h"

//! Set up raylib window
static
void InitializeRaylibContext() {
    // initialize fullscreen game window
    SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
    InitWindow(1280, 800, "FOGD Engine");
    if(!IsWindowFullscreen())
        ToggleFullscreen();
    SetTargetFPS(120);
}

void RunGame(Scene *initial_scene) {
    // set the main scene
    SwitchScene(initial_scene);
    ASSERT_RETURN(GetMainScene() != NULL,, "RunGame: Initial scene cannot be NULL.");
    // Main update loop
    while (!WindowShouldClose()) {
        SceneTick(GetMainScene(), GetFrameTime());
        RenderNextFrame();
    }
    ShutDown();
    UNREACHABLE("RunGame: Reached beyond ShutDown call");
}

void InitializeEngine() {
    // initialize context
    InitializeRaylibContext();
    // initialize engine subsystems
    InitializeResourceSubsystem();
    InitializeRenderingSubsystem();
}

void ShutDown() {
    // deallocate the main scene
    if(GetMainScene() != NULL)
        DestroyScene(GetMainScene());
    // clean up subsystem resources
    CleanResourceSubsystem();
    CleanupRenderingSubsystem();
    CloseWindow();
    exit(0);
}
