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

static
void InitializeRaylibContext() {
    // initialize fullscreen game window
    SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
    InitWindow(1280, 800, "Hello Raylib");
    if(!IsWindowFullscreen())
        ToggleFullscreen();
}

void RunGame(Scene *initial_scene) {
    SwitchScene(initial_scene);
    ASSERT_RETURN(GetMainScene() != NULL,, "RunGame: Initial scene cannot be NULL.");
    while (!WindowShouldClose()) {
        SceneTick(GetMainScene(), 1.0);
        RenderNextFrame();
    }
    ShutDown();
    UNREACHABLE("RunGame: Reached beyond ShutDown call");
}

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

void ShutDown() {
    CleanResourceSubsystem();
    CloseWindow();
    exit(0);
}
