コンソール+キーボード入力

守矢諏訪子 <suwako@076.moe>、2026-06-25 04:53
コミット 887c6a62c3e9e1181908155413789521336dd3c4 ・親 e2e8dd0 ・ファイルを見る

6個のファイル、+178 -58

@@ -0,0 +1,29 @@
1+#pragma once
2+
3+#include <util/types.hh>
4+
5+namespace gui {
6+ enum class LogType {
7+ Normal,
8+ Info,
9+ Warning,
10+ Error,
11+ };
12+
13+ struct LogEntry {
14+ LogType type;
15+ string text;
16+ };
17+
18+ class ConsoleLog {
19+ public:
20+ void Add(LogEntry entry);
21+ void Draw(cstr title = "コンソール");
22+
23+ private:
24+ void Clear();
25+
26+ private:
27+ vector<LogEntry> m_Lines;
28+ };
29+} // namespace gui
@@ -41,6 +41,10 @@
4141
4242 struct Program;
4343
44+namespace gui {
45+ class ConsoleLog;
46+} // namespace gui
47+
4448 namespace lua {
4549 struct LuaMesh {
4650 vector<f32> vertices;
@@ -58,14 +62,15 @@
5862
5963 class LuaEngine {
6064 public:
61- LuaEngine(const string &code, Program *prog);
65+ LuaEngine(const string &code, Program *prog, gui::ConsoleLog *cmd = nullptr);
6266 ~LuaEngine();
6367
6468 LuaMesh &GetMesh() { return m_Mesh; };
6569 const LuaMesh &GetMesh() const { return m_Mesh; };
6670 void Reload(const string &code);
6771 void Update();
68- void SetProgram(Program *prog);
72+ void SetProgram(Program *prog) { m_Program = prog; }
73+ void SetConsole(gui::ConsoleLog *cmd) { m_Console = cmd; }
6974
7075 private:
7176 void BindGraphics();
@@ -76,5 +81,6 @@
7681 sol::state m_Lua;
7782 string m_Code;
7883 Program *m_Program = nullptr;
84+ gui::ConsoleLog *m_Console = nullptr;
7985 };
8086 } // namespace lua
変更 main.cc
@@ -47,6 +47,7 @@
4747 #include <gui/guiengine.hh>
4848 #include <gui/titlebar.hh>
4949 #include <gui/editor.hh>
50+#include <gui/consolelog.hh>
5051
5152 #include <imgui.h>
5253 #include <backends/imgui_impl_glfw.h>
@@ -95,6 +96,9 @@
9596 sqlite::Instance db;
9697 db::Initialize(db);
9798
99+ // コンソール
100+ gui::ConsoleLog cmd;
101+
98102 // データベースからシェーダーとLuaコードの受け取り
99103 vector<db::ShaderData> codeMap = db::GetAllShaders(db);
100104 u32 shaderId;
@@ -128,7 +132,7 @@
128132 Program shaderProgram(vertexShader, fragmentShader);
129133
130134 // 頂点データはLuaから受け取る
131- lua::LuaEngine luaEngine(LUA, &shaderProgram);
135+ lua::LuaEngine luaEngine(LUA, &shaderProgram, &cmd);
132136 auto &mesh = luaEngine.GetMesh();
133137
134138 VertexArrays VAO(1);
@@ -147,9 +151,95 @@
147151 glEnableVertexAttribArray(a.location);
148152 }
149153
154+ bool isHold = false;
155+
150156 // メインレンダリングループ
151157 while (!window.ShouldClose()) {
158+ auto compileCommand = [&]() {
159+ VERT = vertEditor.Get().GetText();
160+ FRAG = fragEditor.Get().GetText();
161+ LUA = luaEditor.Get().GetText();
162+
163+ try {
164+ shaderProgram.Reload(VERT, FRAG);
165+ luaEngine.SetProgram(&shaderProgram);
166+ luaEngine.Reload(LUA);
167+ {
168+ lua::LuaMesh newMesh = luaEngine.GetMesh();
169+
170+ VAO.BindVertexArray();
171+
172+ VBO.BindBuffer();
173+ VBO.BufferData(
174+ newMesh.vertices.data(),
175+ newMesh.vertices.size() * sizeof(f32)
176+ );
177+
178+ EBO.BindBuffer();
179+ EBO.BufferData(
180+ newMesh.indices.data(),
181+ newMesh.indices.size() * sizeof(f32)
182+ );
183+
184+ for (const auto &a : newMesh.attr) {
185+ glVertexAttribPointer(
186+ a.location,
187+ a.size,
188+ GL_FLOAT,
189+ GL_FALSE,
190+ a.stride,
191+ (any)(uintptr_t)a.offset
192+ );
193+ glEnableVertexAttribArray(a.location);
194+ }
195+ }
196+ } catch (const std::exception &e) {
197+ std::cerr << e.what() << std::endl;
198+ }
199+
200+ gui::LogEntry entry;
201+ entry.type = gui::LogType::Info;
202+ entry.text = "シェーダーをコンパイルしました。";
203+ cmd.Add(entry);
204+ std::cout << entry.text << std::endl;
205+ };
206+
207+ auto saveCommand = [&]() {
208+ VERT = vertEditor.Get().GetText();
209+ FRAG = fragEditor.Get().GetText();
210+ LUA = luaEditor.Get().GetText();
211+
212+ db::SaveCode(db, shaderId, VERT, "vertex_shader");
213+ db::SaveCode(db, shaderId, FRAG, "fragment_shader");
214+ db::SaveCode(db, shaderId, LUA, "lua_code");
215+
216+ gui::LogEntry entry;
217+ entry.type = gui::LogType::Info;
218+ entry.text = "シェーダー及びLuaファイルを保存しました。";
219+ cmd.Add(entry);
220+ std::cout << entry.text << std::endl;
221+ };
222+
152223 glfw.PollEvents();
224+
225+ bool isCompileKey = window.GetKey(GLFW_KEY_F5);
226+ bool ctrlMod = (window.GetKey(GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || window.GetKey(GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS);
227+ bool isSaveKey = (window.GetKey(GLFW_KEY_S) == GLFW_PRESS && ctrlMod);
228+
229+ if (!isHold) {
230+ if (isCompileKey) {
231+ //isHold = true;
232+ compileCommand();
233+ }
234+
235+ if (isSaveKey) {
236+ //isHold = true;
237+ saveCommand();
238+ }
239+ }
240+
241+ isHold = isSaveKey || isCompileKey;
242+
153243 if (window.GetAttrib(glfwpp::Attributes::Iconified) != 0) {
154244 ImGui_ImplGlfw_Sleep(10);
155245 continue;
@@ -164,6 +254,7 @@
164254 vertEditor.Render();
165255 fragEditor.Render();
166256 luaEditor.Render();
257+ cmd.Draw();
167258
168259 {
169260 static float f = 0.0f;
@@ -172,58 +263,13 @@
172263 ImGui::Begin("コントロール");
173264
174265 if (ImGui::Button("コンパイル(F5)")) {
175- VERT = vertEditor.Get().GetText();
176- FRAG = fragEditor.Get().GetText();
177- LUA = luaEditor.Get().GetText();
178-
179- try {
180- shaderProgram.Reload(VERT, FRAG);
181- luaEngine.SetProgram(&shaderProgram);
182- luaEngine.Reload(LUA);
183- {
184- lua::LuaMesh newMesh = luaEngine.GetMesh();
185-
186- VAO.BindVertexArray();
187-
188- VBO.BindBuffer();
189- VBO.BufferData(
190- newMesh.vertices.data(),
191- newMesh.vertices.size() * sizeof(f32)
192- );
193-
194- EBO.BindBuffer();
195- EBO.BufferData(
196- newMesh.indices.data(),
197- newMesh.indices.size() * sizeof(f32)
198- );
199-
200- for (const auto &a : newMesh.attr) {
201- glVertexAttribPointer(
202- a.location,
203- a.size,
204- GL_FLOAT,
205- GL_FALSE,
206- a.stride,
207- (any)(uintptr_t)a.offset
208- );
209- glEnableVertexAttribArray(a.location);
210- }
211- }
212- } catch (const std::exception &e) {
213- std::cerr << e.what() << std::endl;
214- }
266+ compileCommand();
215267 }
216268
217269 ImGui::SameLine();
218270
219271 if (ImGui::Button("保存(CTRL + S)")) {
220- VERT = vertEditor.Get().GetText();
221- FRAG = fragEditor.Get().GetText();
222- LUA = luaEditor.Get().GetText();
223-
224- db::SaveCode(db, shaderId, VERT, "vertex_shader");
225- db::SaveCode(db, shaderId, FRAG, "fragment_shader");
226- db::SaveCode(db, shaderId, LUA, "lua_code");
272+ saveCommand();
227273 }
228274
229275 ImGui::Text("現在フレームレート: %.3f ms/frame (%.1f FPS)", 1000.0f / ge.GetIO().Framerate, ge.GetIO().Framerate);
@@ -0,0 +1,29 @@
1+#include <gui/consolelog.hh>
2+#include <imgui.h>
3+
4+namespace gui {
5+ void ConsoleLog::Add(LogEntry entry) {
6+ m_Lines.push_back(entry);
7+ }
8+
9+ void ConsoleLog::Draw(cstr title) {
10+ ImGui::Begin(title);
11+ if (ImGui::Button("消す")) Clear();
12+ ImGui::Separator();
13+ for (const auto &line : m_Lines) {
14+ if (line.type == LogType::Error)
15+ ImGui::TextColored(ImVec4(1, 0, 0, 1), "%s", line.text.c_str());
16+ else if (line.type == LogType::Warning)
17+ ImGui::TextColored(ImVec4(1, 1, 0, 1), "%s", line.text.c_str());
18+ else if (line.type == LogType::Info)
19+ ImGui::TextColored(ImVec4(0, 1, 1, 1), "%s", line.text.c_str());
20+ else
21+ ImGui::TextWrapped("%s", line.text.c_str());
22+ }
23+ ImGui::End();
24+ }
25+
26+ void ConsoleLog::Clear() {
27+ m_Lines.clear();
28+ }
29+} // namespace gui
@@ -18,6 +18,9 @@
1818 } else if (lang == Lua) {
1919 m_Editor.SetLanguage(TextEditor::Language::Lua());
2020 }
21+
22+ //m_Editor.SetTabSize(2);
23+ //m_Editor.SetInsertSpacesOnTabs(true);
2124 }
2225
2326 Editor::~Editor() {}
@@ -36,11 +36,14 @@
3636 #include <glad/glad.h>
3737 #include <shader/shader.hh>
3838 #include <glfw/glfw3.h>
39+#include <gui/consolelog.hh>
3940
4041 namespace lua {
41- LuaEngine::LuaEngine(const string &code, Program *prog)
42+ LuaEngine::LuaEngine(const string &code, Program *prog, gui::ConsoleLog *cmd)
4243 : m_Code(code)
43- , m_Program(prog) {
44+ , m_Program(prog)
45+ , m_Console(cmd)
46+ {
4447 m_Lua.open_libraries(
4548 sol::lib::base,
4649 sol::lib::math,
@@ -57,7 +60,6 @@
5760
5861 void LuaEngine::Reload(const string &code) {
5962 m_Code = code;
60- //BindGraphics();
6163 Execute();
6264 }
6365
@@ -68,14 +70,15 @@
6870
6971 if (!res.valid()) {
7072 sol::error err = res;
71- std::cerr << err.what() << std::endl;
73+ gui::LogEntry entry = {};
74+ entry.type = gui::LogType::Error;
75+ entry.text = err.what();
76+ if (m_Console) m_Console->Add(entry);
77+ else std::cerr << err.what() << std::endl;
7278 }
7379 }
7480 }
7581
76- void LuaEngine::SetProgram(Program *prog) {
77- }
78-
7982 void LuaEngine::BindGraphics() {
8083 m_Lua["graphics"] = m_Lua.create_table();
8184
@@ -118,7 +121,11 @@
118121
119122 if (!res.valid()) {
120123 sol::error err = res;
121- std::cerr << err.what() << std::endl;
124+ gui::LogEntry entry = {};
125+ entry.type = gui::LogType::Error;
126+ entry.text = err.what();
127+ if (m_Console) m_Console->Add(entry);
128+ else std::cerr << err.what() << std::endl;
122129 }
123130 }
124131 } // namespace lua