const std = @import("std");
const rl = @import("raylib");

const Entity_t = @import("Entity.zig").Entity_t;
const EntitiesList_t = std.MultiArrayList(Entity_t);
var G_alloc: *std.mem.Allocator = undefined;
var G_camera: *rl.Camera3D = undefined;

fn Render3D() void {
    rl.clearBackground(.white);
    
    rl.drawCube(
        .{ .x=4, .y=-4, .z=4 },
        2,
        2,
        2,
        .{ .r=255, .b=0, .g=0, .a=255 }
    );
    rl.drawCubeWires(
        .{ .x=4, .y=-4, .z=4 },
        2,
        2,
        2,
        .{ .r=0, .b=0, .g=0, .a=255 }
    );

    rl.drawSphere(
        .{.x=4, .y=0, .z=4},
        2,
        .{.r=0, .g=255, .b=0, .a=255},
    );

    rl.updateCamera(G_camera, rl.CameraMode.first_person);
}

fn RenderUI() void {
    rl.drawFPS(10, 10);
}

fn PhysicsUpdate() void {

}

pub fn main() !void {
    // Allocator
    var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true } ){};
    var alloc = gpa.allocator();
    defer alloc.destroy(G_alloc);
    G_alloc = &alloc;

    // Create the Main Camera
    var MainCamera = rl.Camera3D{
        .up = rl.Vector3{ .x=0, .y=1, .z=0 },
        .position = rl.Vector3{ .x=0, .y=0, .z=0},
        .projection = rl.CameraProjection.perspective,
        .fovy = 60,
        .target = rl.Vector3{ .x=1, .y=0, .z=0 }
    };
    G_camera = &MainCamera;

    // Player List
    var Entities = EntitiesList_t{};
    defer Entities.deinit(G_alloc.*);

    // Create the Root Node
    var RootNode = Entity_t{};
    RootNode.Node.Name = "Root Node";
    try Entities.append(G_alloc.*, RootNode);

    // Setup
    //rl.disableCursor();
    rl.setTargetFPS(60);

    // Create window
    rl.initWindow( 512, 512, "Hello world");
    defer rl.closeWindow();
    while(!rl.windowShouldClose()){

        // Start Render Loop
        rl.beginDrawing();
        rl.beginMode3D(MainCamera);
        Render3D();
        rl.endMode3D();
        RenderUI();
        rl.endDrawing();
        PhysicsUpdate();
        //std.debug.print("{s}", .{ Player1.Node.Name });

    }
}