
local dirtools  = require("tools.vfs.dirtools")
dirtools.init("sokol%-luajit")
dirtools.add_package_path("editor"..dirtools.sep.."twig")

--_G.SOKOL_DLL    = "sokol_debug_dll"
local sapp      = require("sokol_app")
sg              = require("sokol_gfx")
local slib      = require("sokol_libs") -- Warn - always after gfx!!

local utils     = require("utils")

local ffi       = require("ffi")

-- --------------------------------------------------------------------------------------
local shell32   = ffi.load("shell32")

ffi.cdef[[
    void Sleep(uint32_t ms);
    void *  ShellExecuteA(const void * hwnd, const char * lpOperation, const char * lpFile, const char * lpParameters, char* lpDirectory, int nShowCmd);
]]

-- --------------------------------------------------------------------------------------
-- The project manager handles configuration of the project data (build, debug etc)
--   Project manager defines what worlds, assets and folders are accessible for the project
--   Note: the editor should _always_ have an active project. If it doesnt have one, it will 
--         show a load panel to load one. 
local projectmgr    = require("editor.project-manager")

-- --------------------------------------------------------------------------------------
-- Tiny ECS will be our core object manager. 
--    Rendering, physics collision and more will be components to this system
--    Rendering specifically will be built with a ldb that will run all the culling, sorting
--      and binning needed. This will be decoupled from the editor itself. 
local tiny          = require('engine.world.world-manager')


local function init()

    projectmgr:init()
    tiny:init({noserver = false})
    tiny.addmanager("projectmgr", projectmgr)

    local desc = ffi.new("sg_desc[1]")
    desc[0].environment = slib.sglue_environment()
    desc[0].logger.func = slib.slog_func
    desc[0].disable_validation = false
    sg.sg_setup( desc )
    print("Sokol Is Valid: "..tostring(sg.sg_isvalid()))

    local hwnd = sapp.sapp_win32_get_hwnd()

--    io.popen(cmd, "r")
    ffi.C.Sleep(500)
end


-- --------------------------------------------------------------------------------------

local function input(event) 
end


-- --------------------------------------------------------------------------------------

local function frame()

    -- /* NOTE: the vs_params_t struct has been code-generated by the shader-code-gen */
    local w         = sapp.sapp_widthf()
    local h         = sapp.sapp_heightf()
    local t         = (sapp.sapp_frame_duration() * 60.0)

    tiny:update(sapp.sapp_frame_duration())

    -- Display frame stats in console.
    -- hutils.show_stats()
end

-- --------------------------------------------------------------------------------------

local function cleanup()
    sg.sg_shutdown()

    tiny:final() 
end

-- --------------------------------------------------------------------------------------

local app_desc = ffi.new("sapp_desc[1]")
app_desc[0].init_cb     = init
app_desc[0].frame_cb    = frame
app_desc[0].cleanup_cb  = cleanup
app_desc[0].event_cb    = input
app_desc[0].width       = 1920
app_desc[0].height      = 1080
app_desc[0].window_title = "editor - sokol"
app_desc[0].fullscreen  = false
app_desc[0].icon.sokol_default = true 
app_desc[0].enable_clipboard = true
app_desc[0].logger.func = slib.slog_func 

sapp.sapp_run( app_desc )

-- --------------------------------------------------------------------------------------
