sokol-luajit/editor/editor.lua
2025-10-17 09:43:15 +10:30

176 lines
5.3 KiB
Lua

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")
sg = require("sokol_nuklear")
local nk = sg
local slib = require("sokol_libs") -- Warn - always after gfx!!
local hmm = require("hmm")
local hutils = require("hmm_utils")
local stb = require("stb")
local utils = require("utils")
local ffi = require("ffi")
gSmgr = require("engine.utils.statemanager")
-- --------------------------------------------------------------------------------------
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);
int ShowWindow(const void * hWnd, int nCmdShow);
]]
local SW_MAXIMIZE = 3
-- --------------------------------------------------------------------------------------
local engineState = require("engine.state_engine")
-- --------------------------------------------------------------------------------------
local enabled_profile = arg[1] == "-profile"
local profile = nil
if(enabled_profile) then
profile = require("jit.profile")
function cb(thread, samples, vmstate)
print(profile.dumpstack(thread, "l\n", 1))
end
profile.start("fi4", cb)
end
-- --------------------------------------------------------------------------------------
-- Grab some gui elements for testing
local SmainGui = require("editor.states.mainGui")
-- --------------------------------------------------------------------------------------
local width = 1920
local height = 1080
if(arg[1] and arg[2]) then
width = tonumber(arg[1])
height = tonumber(arg[2])
end
print("Display: "..width .. " x ".. height)
-- --------------------------------------------------------------------------------------
local function ErrorCheck(status, err)
if(status == false) then
print(status)
print(err)
print(debug.traceback())
os.exit()
end
end
-- --------------------------------------------------------------------------------------
-- 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")
local function init()
projectmgr:init()
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 )
local snk = ffi.new("snk_desc_t[1]")
snk[0].dpi_scale = sapp.sapp_dpi_scale()
snk[0].logger.func = slib.slog_func
nk.snk_setup(snk)
print("Sokol Is Valid: "..tostring(sg.sg_isvalid()))
local hwnd = sapp.sapp_win32_get_hwnd()
ffi.C.ShowWindow(hwnd, SW_MAXIMIZE)
sapp.sapp_show_mouse(true)
sapp.sapp_set_window_title("Thunc Editor v")
-- io.popen(cmd, "r")
ffi.C.Sleep(500)
-- Add the state to the statemanager and jump to it
gSmgr:Init()
gSmgr:CreateState("MainGui", SmainGui)
gSmgr:ChangeState("MainGui")
end
-- --------------------------------------------------------------------------------------
local function input(event)
engineState.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)
local dt = sapp.sapp_frame_duration()
local ctx = nk.snk_new_frame()
if(mainState.ctx == nil) then
mainState.ctx = ctx
ErrorCheck( pcall( engineState.init ) )
print("----------->>> Init")
nk.nk_style_show_cursor(ctx)
else
mainState.ctx = ctx
end
ErrorCheck( pcall( engineState.update, dt ) )
-- Display frame stats in console.
-- hutils.show_stats()
end
-- --------------------------------------------------------------------------------------
local function cleanup()
nk.snk_shutdown()
sg.sg_shutdown()
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 = width
app_desc[0].height = height
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].ios_keyboard_resizes_canvas = false
app_desc[0].logger.func = slib.slog_func
sapp.sapp_run( app_desc )
-- --------------------------------------------------------------------------------------
if(profile) then profile.stop() end
-- --------------------------------------------------------------------------------------