From be993f55f5d93150b8b418b031d2e9c8d88978b2 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 15 Nov 2022 20:40:37 -0500 Subject: allow lua to create dialog box --- Makefile | 2 +- Scripts/init.lua | 1 + src/engine.cpp | 3 ++ src/events/render.hpp | 9 ++++++ src/render.cpp | 7 +++++ src/render.hpp | 1 + src/ui.cpp | 82 +++++++++++++++++++++++++++++---------------------- src/ui.hpp | 20 +++++++++++-- 8 files changed, 86 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 4ec54da..12d7790 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ LIBDIR = lib LIBS = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -lluajit -ldl -lGLEW -lGL \ -lSDL2_image -lsoil -lfreetype -lopenal -lalut -CXXFLAGS = -ggdb -std=c++17 -Wall -Wextra -Werror -pedantic \ +CXXFLAGS = -ggdb -std=c++20 -Wall -Wextra -Werror -pedantic \ -Wno-class-memaccess -Wno-implicit-fallthrough -Wno-unused-parameter CXXINCS = -I$(SRCDIR) \ diff --git a/Scripts/init.lua b/Scripts/init.lua index 60f21c6..441cc2a 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -30,6 +30,7 @@ player = { end end, JumpKeyReleased = function(self) + game.dialog(30, -150, 400, 100) end }, Position = { diff --git a/src/engine.cpp b/src/engine.cpp index d8eef23..02598d4 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -71,6 +71,9 @@ int Engine::init(void) script->addToGameNamespace("play", bindInstance(&AudioSystem::playSound, systems.system().get())); + script->addToGameNamespace("dialog", + bindInstance(&UISystem::createDialogBox, + systems.system().get())); script->init(); diff --git a/src/events/render.hpp b/src/events/render.hpp index 7bb45df..07f5d64 100644 --- a/src/events/render.hpp +++ b/src/events/render.hpp @@ -27,11 +27,20 @@ struct NewRenderEvent GLuint normal; unsigned int vertex; + NewRenderEvent() = default; NewRenderEvent(GLuint _vbo, GLuint _tex, GLuint _normal, unsigned int _vertex) : vbo(_vbo), tex(_tex), normal(_normal), vertex(_vertex) {} }; +struct DelRenderEvent +{ + GLuint vbo; + + DelRenderEvent(GLuint _vbo) : + vbo(_vbo) {} +}; + struct WorldMeshUpdateEvent { GLuint worldVBO; diff --git a/src/render.cpp b/src/render.cpp index 6e30f21..5ae2c15 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -29,6 +29,7 @@ void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities, [[maybe_unused]] entityx::EventManager& events) { events.subscribe(*this); + events.subscribe(*this); events.subscribe(*this); events.subscribe>(*this); @@ -404,6 +405,12 @@ void RenderSystem::receive(const NewRenderEvent &nre) UIRenderData(nre.tex, nre.normal, nre.vertex)); } +void RenderSystem::receive(const DelRenderEvent &dre) +{ + if (uiRenders.contains(dre.vbo)) + uiRenders.erase(dre.vbo); +} + void RenderSystem::receive(const WorldMeshUpdateEvent &wmu) { worldRenders.insert_or_assign( diff --git a/src/render.hpp b/src/render.hpp index 45fb548..ede3f88 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -116,6 +116,7 @@ public: ************/ void receive(const WorldMeshUpdateEvent &wmu); void receive(const NewRenderEvent &nre); + void receive(const DelRenderEvent &dre); void receive(const entityx::ComponentAddedEvent &cae); }; diff --git a/src/ui.cpp b/src/ui.cpp index 71cb875..5d8b0cc 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1,56 +1,68 @@ #include "ui.hpp" -#include "events/render.hpp" #include "text.hpp" #include #include #include -static NewRenderEvent NRE (0, 0, 0, 0); static const unsigned int NRE_TEX_DATA = 0xBBBBBBBB; -static std::basic_string buffer; void UISystem::configure(entityx::EntityManager&, entityx::EventManager&) { } +void UISystem::createDialogBox(float x, float y, float w, float h) +{ + // Queue for generation in the main thread. + m_boxes.emplace_back(0, x, y, w, h); +} + void UISystem::update(entityx::EntityManager&, entityx::EventManager& events, entityx::TimeDelta) { - static bool t = false; - - if (!t) { - t = true; - - glGenBuffers(1, &NRE.vbo); - glGenTextures(1, &NRE.tex); - - glBindTexture(GL_TEXTURE_2D, NRE.tex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &NRE_TEX_DATA); - - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - buffer += { 30, -50, -5, 0, 0, 1 }; - buffer += { 160, -50, -5, 0, 0, 1 }; - buffer += { 160, -10, -5, 0, 0, 1 }; - buffer += { 160, -10, -5, 0, 0, 1 }; - buffer += { 30, -10, -5, 0, 0, 1 }; - buffer += { 30, -50, -5, 0, 0, 1 }; - - glBindBuffer(GL_ARRAY_BUFFER, NRE.vbo); - glBufferData(GL_ARRAY_BUFFER, - buffer.size() * sizeof(TextMeshData), buffer.data(), - GL_DYNAMIC_DRAW); - - NRE.normal = 0; - NRE.vertex = buffer.size(); - events.emit(NRE); + for (auto& b : m_boxes) { + if (b.vbo == 0) { + auto nre = generateDialogBox(b); + events.emit(nre); + } } } +NewRenderEvent UISystem::generateDialogBox(Box& box) +{ + NewRenderEvent nre; + std::basic_string buffer; + + glGenBuffers(1, &nre.vbo); + glGenTextures(1, &nre.tex); + box.vbo = nre.vbo; + + glBindTexture(GL_TEXTURE_2D, nre.tex); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, + GL_UNSIGNED_BYTE, &NRE_TEX_DATA); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + + buffer += { box.x, box.y, -5, 0, 0, 1 }; + buffer += { box.x + box.w, box.y, -5, 0, 0, 1 }; + buffer += { box.x + box.w, box.y + box.h, -5, 0, 0, 1 }; + buffer += { box.x + box.w, box.y + box.h, -5, 0, 0, 1 }; + buffer += { box.x, box.y + box.h, -5, 0, 0, 1 }; + buffer += { box.x, box.y, -5, 0, 0, 1 }; + + glBindBuffer(GL_ARRAY_BUFFER, nre.vbo); + glBufferData(GL_ARRAY_BUFFER, + buffer.size() * sizeof(TextMeshData), buffer.data(), + GL_DYNAMIC_DRAW); + + nre.normal = 0; + nre.vertex = buffer.size(); + return nre; +} + diff --git a/src/ui.hpp b/src/ui.hpp index 809b5dc..1986c8f 100644 --- a/src/ui.hpp +++ b/src/ui.hpp @@ -21,11 +21,13 @@ #ifndef SYSTEM_UI_HPP_ #define SYSTEM_UI_HPP_ +#include "events/render.hpp" + #include +#include +#include +#include -#include -#include -#include #include class UISystem : public entityx::System @@ -36,6 +38,18 @@ public: void update(entityx::EntityManager&, entityx::EventManager&, entityx::TimeDelta) final; + + void createDialogBox(float x, float y, float w, float h); + +private: + struct Box { + GLuint vbo; + float x, y, w, h; + }; + + std::vector m_boxes; + + NewRenderEvent generateDialogBox(Box& box); }; #endif // SYSTEM_UI_HPP_ -- cgit v1.2.3