diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2022-11-16 08:11:00 -0500 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2022-11-16 08:19:53 -0500 |
commit | 034c802edd39be537a626c1961272a6137b5980e (patch) | |
tree | d9b042878d8a8f573cf09744814270a32b920b89 | |
parent | f892f3c3afb4a3795dc3ba5c26cacd1f8088b902 (diff) |
text clears with dialog box
-rw-r--r-- | Scripts/init.lua | 4 | ||||
-rw-r--r-- | src/engine.cpp | 2 | ||||
-rw-r--r-- | src/events/ui.hpp | 27 | ||||
-rw-r--r-- | src/text.cpp | 38 | ||||
-rw-r--r-- | src/text.hpp | 4 | ||||
-rw-r--r-- | src/ui.cpp | 28 | ||||
-rw-r--r-- | src/ui.hpp | 9 |
7 files changed, 91 insertions, 21 deletions
diff --git a/Scripts/init.lua b/Scripts/init.lua index 7ac9160..370fc0e 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -1,4 +1,5 @@ game.loadFont("default", "Assets/FreePixel.ttf", 16) +game.loadFont("dialog", "Assets/FreePixel.ttf", 16) player = { Player = 0, @@ -7,6 +8,7 @@ player = { self.Velocity.x = self.Velocity.x - 3.0 --self.Velocity.y = self.Velocity.y - 1.0 self.Render.flipx = true; + game.dialogClear() end, MoveLeftReleased = function(self) self.Velocity.x = self.Velocity.x + 3.0 @@ -29,7 +31,7 @@ player = { end, JumpKeyReleased = function(self) game.dialog(30, 150, 400, 100) - game.puts("default", 36, 52, "Hey. Hag?") + game.puts("dialog", 36, 52, "Hey. Hag?") end }, Position = { diff --git a/src/engine.cpp b/src/engine.cpp index 02598d4..40f6e62 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -74,6 +74,8 @@ int Engine::init(void) script->addToGameNamespace("dialog", bindInstance(&UISystem::createDialogBox, systems.system<UISystem>().get())); + script->addToGameNamespace("dialogClear", + [this] { events.emit<HideDialog>(); }); script->init(); diff --git a/src/events/ui.hpp b/src/events/ui.hpp new file mode 100644 index 0000000..7d4ad5c --- /dev/null +++ b/src/events/ui.hpp @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2022 Clyne Sullivan <clyne@bitgloo.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef EVENTS_UI_HPP_ +#define EVENTS_UI_HPP_ + +struct HideDialog +{ + char unused; +}; + +#endif // EVENTS_UI_HPP + diff --git a/src/text.cpp b/src/text.cpp index 5490224..83b881c 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -1,6 +1,7 @@ #include "text.hpp" #include "events/render.hpp" +#include "events/ui.hpp" #include <iostream> @@ -18,6 +19,7 @@ void TextSystem::configure([[maybe_unused]] entityx::EntityManager& entities, shouldUpdateVBOs = false; events.subscribe<ShowTextEvent>(*this); + events.subscribe<HideDialog>(*this); if (FT_Init_FreeType(&freetype) != 0) { // TODO handle error @@ -36,7 +38,9 @@ void TextSystem::update([[maybe_unused]] entityx::EntityManager& entites, updateVBOs(); for (auto& [name, font] : fontData) { - if (font.text.size() != 0) { + if (font.changed) { + font.changed = false; + events.emit<NewRenderEvent>(font.vbo, font.tex, 0, font.buffer.size()); } @@ -49,6 +53,18 @@ void TextSystem::receive(const ShowTextEvent& ste) put(ste.font, ste.x, ste.y, ste.text); } +void TextSystem::receive(const HideDialog&) +{ + auto fd = fontData.find("dialog"); + + if (fd != fontData.end()) { + auto& font = fd->second; + + font.text.clear(); + font.changed = true; + } +} + void TextSystem::loadFont(const std::string& name, const std::string& file, int size) @@ -98,12 +114,6 @@ void TextSystem::loadFont(const std::string& name, glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - // // convert red-on-black to RGBA - // auto& g = face->glyph; - // std::vector<uint32_t> buf (g->bitmap.width * g->bitmap.rows, 0xFFFFFF); - // for (auto j = buf.size(); j--;) - // buf[j] |= g->bitmap.buffer[j] << 24; - // Load each character and add it to the texture // @@ -137,16 +147,19 @@ void TextSystem::loadFont(const std::string& name, << font.tex << ")" << std::endl; } -void TextSystem::put(const std::string& font, +void TextSystem::put(const std::string& fname, float x, float y, const std::string& text) { - if (fontData.find(font) == fontData.end()) + auto fd = fontData.find(fname); + if (fd == fontData.end()) return; - auto& vector = fontData[font].text; + auto& font = fd->second; + + auto& vector = font.text; - y = -(y + fontData[font].fontSize); + y = -(y + font.fontSize); const auto it = std::find_if(vector.begin(), vector.end(), [&x, &y](const Text& t) { @@ -156,9 +169,10 @@ void TextSystem::put(const std::string& font, *it = Text(text, x, y); } else { // Invert y axis so positive grows south. - fontData[font].text.emplace_back(text, x, y); + vector.emplace_back(text, x, y); } + font.changed = true; shouldUpdateVBOs = true; } diff --git a/src/text.hpp b/src/text.hpp index 0f98688..de25459 100644 --- a/src/text.hpp +++ b/src/text.hpp @@ -23,6 +23,8 @@ #ifndef SYSTEM_TEXT_HPP_ #define SYSTEM_TEXT_HPP_ +#include "events/ui.hpp" + #include <entityx/entityx.h> #include <ft2build.h> #include <freetype/freetype.h> @@ -67,6 +69,7 @@ struct Font { float width; float height; + bool changed = false; std::array<FT_Info, 96> data; // Stores currently shown text at given index into VBO? @@ -106,6 +109,7 @@ public: entityx::TimeDelta dt) final; void receive(const ShowTextEvent&); + void receive(const HideDialog&); void put(const std::string& font, float x, float y, const std::string& text); @@ -8,8 +8,9 @@ static const unsigned int NRE_TEX_DATA = 0xBBBBBBBB; -void UISystem::configure(entityx::EntityManager&, entityx::EventManager&) +void UISystem::configure(entityx::EntityManager&, entityx::EventManager &events) { + events.subscribe<HideDialog>(*this); } void UISystem::createDialogBox(float x, float y, float w, float h) @@ -22,14 +23,31 @@ void UISystem::update(entityx::EntityManager&, entityx::EventManager& events, entityx::TimeDelta) { - for (auto& b : m_boxes) { - if (b.vbo == 0) { - auto nre = generateDialogBox(b); - events.emit<NewRenderEvent>(nre); + if (m_clear_boxes) { + m_clear_boxes = false; + + for (const auto& b : m_boxes) { + if (b.vbo != 0) { + events.emit<DelRenderEvent>(b.vbo); + } + } + + m_boxes.clear(); + } else { + for (auto& b : m_boxes) { + if (b.vbo == 0) { + auto nre = generateDialogBox(b); + events.emit<NewRenderEvent>(nre); + } } } } +void UISystem::receive(const HideDialog&) +{ + m_clear_boxes = true; +} + NewRenderEvent UISystem::generateDialogBox(Box& box) { NewRenderEvent nre; @@ -22,15 +22,15 @@ #define SYSTEM_UI_HPP_ #include "events/render.hpp" +#include "events/ui.hpp" #include <entityx/entityx.h> #include <GL/glew.h> -#include <SDL2/SDL.h> -#include <SDL2/SDL_opengl.h> #include <vector> -class UISystem : public entityx::System<UISystem> +class UISystem : public entityx::System<UISystem>, + public entityx::Receiver<UISystem> { public: void configure(entityx::EntityManager&, entityx::EventManager&) final; @@ -39,6 +39,8 @@ public: entityx::EventManager&, entityx::TimeDelta) final; + void receive(const HideDialog &hd); + void createDialogBox(float x, float y, float w, float h); private: @@ -48,6 +50,7 @@ private: }; std::vector<Box> m_boxes; + bool m_clear_boxes = false; NewRenderEvent generateDialogBox(Box& box); }; |