#pragma once #include "../interfaces/i_script_engine_service.hpp" #include "../interfaces/i_audio_command_service.hpp" #include "../interfaces/i_mesh_service.hpp" #include "../interfaces/i_physics_bridge_service.hpp" #include "../interfaces/i_input_service.hpp" #include "../interfaces/i_window_service.hpp" #include "../interfaces/i_config_service.hpp" #include "../interfaces/i_logger.hpp" #include "../../di/lifecycle.hpp" #include #include struct lua_State; namespace sdl3cpp::services::impl { /** * @brief Service that owns the Lua runtime and script bindings. */ class ScriptEngineService : public IScriptEngineService, public di::IInitializable, public di::IShutdownable { public: ScriptEngineService(const std::filesystem::path& scriptPath, std::shared_ptr logger, std::shared_ptr meshService, std::shared_ptr audioCommandService, std::shared_ptr physicsBridgeService, std::shared_ptr inputService, std::shared_ptr windowService, std::shared_ptr configService, bool debugEnabled = false); ~ScriptEngineService() override; // Lifecycle void Initialize() override; void Shutdown() noexcept override; // IScriptEngineService interface lua_State* GetLuaState() const override; std::filesystem::path GetScriptDirectory() const override; bool IsInitialized() const override { if (logger_) { logger_->Trace("ScriptEngineService", "IsInitialized"); } return initialized_; } private: struct LuaBindingContext { std::shared_ptr meshService; std::shared_ptr audioCommandService; std::shared_ptr physicsBridgeService; std::shared_ptr inputService; std::shared_ptr windowService; std::shared_ptr configService; std::shared_ptr logger; }; void RegisterBindings(lua_State* L); static int LoadMeshFromFile(lua_State* L); static int LoadMeshFromArchive(lua_State* L); static int PhysicsCreateBox(lua_State* L); static int PhysicsCreateSphere(lua_State* L); static int PhysicsCreateStaticMesh(lua_State* L); static int PhysicsRemoveBody(lua_State* L); static int PhysicsSetTransform(lua_State* L); static int PhysicsApplyForce(lua_State* L); static int PhysicsApplyImpulse(lua_State* L); static int PhysicsSetLinearVelocity(lua_State* L); static int PhysicsGetLinearVelocity(lua_State* L); static int PhysicsSetGravity(lua_State* L); static int PhysicsStepSimulation(lua_State* L); static int PhysicsGetTransform(lua_State* L); static int PhysicsGetBodyCount(lua_State* L); static int PhysicsClear(lua_State* L); static int AudioPlayBackground(lua_State* L); static int AudioPlaySound(lua_State* L); static int AudioStopBackground(lua_State* L); static int GlmMatrixIdentity(lua_State* L); static int GlmMatrixMultiply(lua_State* L); static int GlmMatrixTranslation(lua_State* L); static int GlmMatrixRotationX(lua_State* L); static int GlmMatrixRotationY(lua_State* L); static int GlmMatrixLookAt(lua_State* L); static int GlmMatrixPerspective(lua_State* L); static int GlmMatrixFromTransform(lua_State* L); static int InputGetMousePosition(lua_State* L); static int InputGetMouseDelta(lua_State* L); static int InputGetMouseWheel(lua_State* L); static int InputIsKeyDown(lua_State* L); static int InputIsActionDown(lua_State* L); static int InputIsMouseDown(lua_State* L); static int InputGetText(lua_State* L); static int ConfigGetJson(lua_State* L); static int ConfigGetTable(lua_State* L); static int MaterialXGetSurfaceParameters(lua_State* L); static int TimeGetSeconds(lua_State* L); static int WindowGetSize(lua_State* L); static int WindowSetTitle(lua_State* L); static int WindowIsMinimized(lua_State* L); static int WindowSetMouseGrabbed(lua_State* L); static int WindowGetMouseGrabbed(lua_State* L); static int WindowSetRelativeMouseMode(lua_State* L); static int WindowGetRelativeMouseMode(lua_State* L); static int WindowSetCursorVisible(lua_State* L); static int WindowIsCursorVisible(lua_State* L); static int LuaPrint(lua_State* L); std::shared_ptr logger_; std::shared_ptr meshService_; std::shared_ptr audioCommandService_; std::shared_ptr physicsBridgeService_; std::shared_ptr inputService_; std::shared_ptr windowService_; std::shared_ptr configService_; std::filesystem::path scriptPath_; std::filesystem::path scriptDirectory_; bool debugEnabled_ = false; bool initialized_ = false; lua_State* luaState_ = nullptr; std::shared_ptr bindingContext_; }; } // namespace sdl3cpp::services::impl