Luaエンジンに「Init」関数の追加
守矢諏訪子 <suwako@076.moe>、2026-06-27 03:06
コミット c46af6b42ab88cff26de6fdae057960b787fc779
・親 8ac2f63
・ファイルを見る
5個のファイル、+33 -1
| @@ -74,11 +74,14 @@ | ||
| 74 | 74 | |
| 75 | 75 | public: |
| 76 | 76 | void Reload(const string &code); |
| 77 | + void Init(); | |
| 77 | 78 | void Update(); |
| 78 | 79 | bool Validate(const string &code); |
| 79 | 80 | |
| 80 | 81 | public: |
| 81 | 82 | void SetProgram(Program *prog) { m_Program = prog; } |
| 83 | + bool GetInitializationState() const { return m_IsInitialized; } | |
| 84 | + void SetInitializationState(bool state) { m_IsInitialized = state; } | |
| 82 | 85 | string GetApiVersion() const { return m_ApiVersion; } |
| 83 | 86 | |
| 84 | 87 | private: |
| @@ -95,6 +98,7 @@ | ||
| 95 | 98 | LuaMesh m_Mesh; |
| 96 | 99 | sol::state m_Lua; |
| 97 | 100 | string m_Code; |
| 101 | + bool m_IsInitialized = false; | |
| 98 | 102 | Program *m_Program = nullptr; |
| 99 | 103 | Info *m_Info = {}; |
| 100 | 104 | string m_ApiVersion = "1.0.0"; |
変更
main.cc
| @@ -177,6 +177,7 @@ | ||
| 177 | 177 | while (!window.ShouldClose() && info.isRunning) { |
| 178 | 178 | #pragma region Lambda |
| 179 | 179 | info.compile = [&]() { |
| 180 | + if (luaEngine.GetInitializationState()) luaEngine.SetInitializationState(false); | |
| 180 | 181 | info.VERT.code = vertEditor.Get().GetText(); |
| 181 | 182 | info.FRAG.code = fragEditor.Get().GetText(); |
| 182 | 183 | info.LUA.code = luaEditor.Get().GetText(); |
| @@ -337,7 +338,7 @@ | ||
| 337 | 338 | |
| 338 | 339 | gui::LogEntry entry; |
| 339 | 340 | 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"); | |
| 341 | 342 | info.cmd->Add(entry); |
| 342 | 343 | std::cout << entry.text << std::endl; |
| 343 | 344 | }; |
| @@ -442,6 +443,7 @@ | ||
| 442 | 443 | |
| 443 | 444 | // シェーダープログラムを使用 |
| 444 | 445 | shaderProgram.Use(); |
| 446 | + luaEngine.Init(); | |
| 445 | 447 | luaEngine.Update(); |
| 446 | 448 | |
| 447 | 449 | // クアッドを描画 |
| @@ -109,6 +109,9 @@ | ||
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | le.gfx.set_mesh(mesh) |
| 112 | + | |
| 113 | +function init() | |
| 114 | +end | |
| 112 | 115 | |
| 113 | 116 | function update() |
| 114 | 117 | end)"; |
| @@ -62,7 +62,25 @@ | ||
| 62 | 62 | Execute(m_Lua); |
| 63 | 63 | } |
| 64 | 64 | |
| 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 | + | |
| 65 | 81 | void LuaEngine::Update() { |
| 82 | + if (!m_IsInitialized) return; | |
| 83 | + | |
| 66 | 84 | sol::function update = m_Lua["update"]; |
| 67 | 85 | if (update.valid()) { |
| 68 | 86 | sol::protected_function_result res = update(); |
| @@ -154,6 +154,11 @@ | ||
| 154 | 154 | |
| 155 | 155 | le.gfx.set_mesh(mesh) |
| 156 | 156 | |
| 157 | +function init() | |
| 158 | + le.log("screen res: ", le.sys.get_resolution()) | |
| 159 | + le.log("random: ", le.sys.random(2, 200)) | |
| 160 | +end | |
| 161 | + | |
| 157 | 162 | function update() |
| 158 | 163 | local t = le.sys.get_time() -- glfwGetTime() |
| 159 | 164 | local speed = 1.0 -- animation speed |