Luaエンジンに「Init」関数の追加

守矢諏訪子 <suwako@076.moe>、2026-06-27 03:06
コミット c46af6b42ab88cff26de6fdae057960b787fc779 ・親 8ac2f63 ・ファイルを見る

5個のファイル、+33 -1

@@ -74,11 +74,14 @@
7474
7575 public:
7676 void Reload(const string &code);
77+ void Init();
7778 void Update();
7879 bool Validate(const string &code);
7980
8081 public:
8182 void SetProgram(Program *prog) { m_Program = prog; }
83+ bool GetInitializationState() const { return m_IsInitialized; }
84+ void SetInitializationState(bool state) { m_IsInitialized = state; }
8285 string GetApiVersion() const { return m_ApiVersion; }
8386
8487 private:
@@ -95,6 +98,7 @@
9598 LuaMesh m_Mesh;
9699 sol::state m_Lua;
97100 string m_Code;
101+ bool m_IsInitialized = false;
98102 Program *m_Program = nullptr;
99103 Info *m_Info = {};
100104 string m_ApiVersion = "1.0.0";
変更 main.cc
@@ -177,6 +177,7 @@
177177 while (!window.ShouldClose() && info.isRunning) {
178178 #pragma region Lambda
179179 info.compile = [&]() {
180+ if (luaEngine.GetInitializationState()) luaEngine.SetInitializationState(false);
180181 info.VERT.code = vertEditor.Get().GetText();
181182 info.FRAG.code = fragEditor.Get().GetText();
182183 info.LUA.code = luaEditor.Get().GetText();
@@ -337,7 +338,7 @@
337338
338339 gui::LogEntry entry;
339340 entry.type = gui::LogType::Info;
340- entry.text = info.i18n->GetWord("consoleloginfocreateok") + ", ID: " + std::to_string(info.shaderId);
341+ entry.text = info.i18n->GetWord("consoleloginfocreateok");
341342 info.cmd->Add(entry);
342343 std::cout << entry.text << std::endl;
343344 };
@@ -442,6 +443,7 @@
442443
443444 // シェーダープログラムを使用
444445 shaderProgram.Use();
446+ luaEngine.Init();
445447 luaEngine.Update();
446448
447449 // クアッドを描画
@@ -109,6 +109,9 @@
109109 }
110110
111111 le.gfx.set_mesh(mesh)
112+
113+function init()
114+end
112115
113116 function update()
114117 end)";
@@ -62,7 +62,25 @@
6262 Execute(m_Lua);
6363 }
6464
65+ void LuaEngine::Init() {
66+ if (m_IsInitialized) return;
67+
68+ sol::function init = m_Lua["init"];
69+ if (init.valid()) {
70+ sol::protected_function_result res = init();
71+
72+ if (!res.valid()) {
73+ sol::error err = res;
74+ LogError(err);
75+ }
76+ }
77+
78+ m_IsInitialized = true;
79+ }
80+
6581 void LuaEngine::Update() {
82+ if (!m_IsInitialized) return;
83+
6684 sol::function update = m_Lua["update"];
6785 if (update.valid()) {
6886 sol::protected_function_result res = update();
@@ -154,6 +154,11 @@
154154
155155 le.gfx.set_mesh(mesh)
156156
157+function init()
158+ le.log("screen res: ", le.sys.get_resolution())
159+ le.log("random: ", le.sys.random(2, 200))
160+end
161+
157162 function update()
158163 local t = le.sys.get_time() -- glfwGetTime()
159164 local speed = 1.0 -- animation speed