From: Daniel Karbach Date: Thu, 6 Aug 2015 15:20:23 +0000 (+0200) Subject: simple (text) progress display for preloader X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=376fc1fca87fcdd22dabadf6d01d245ef8c3cedd;p=blank.git simple (text) progress display for preloader --- diff --git a/src/app/PreloadState.cpp b/src/app/PreloadState.cpp index c56e4fe..32d0b3a 100644 --- a/src/app/PreloadState.cpp +++ b/src/app/PreloadState.cpp @@ -11,8 +11,12 @@ namespace blank { PreloadState::PreloadState(Environment &env, ChunkLoader &loader) : env(env) , loader(loader) -, per_update(256) { - +, font(env.assets.LoadFont("DejaVuSans", 24)) +, progress(font) +, total(loader.ToLoad()) +, per_update(64) { + progress.Position(glm::vec3(0.0f), Gravity::CENTER); + progress.Template("Preloading chunks: %d/%d (%d%%)"); } @@ -22,18 +26,17 @@ void PreloadState::Handle(const SDL_Event &) { void PreloadState::Update(int dt) { loader.LoadN(per_update); if (loader.ToLoad() == 0) { - std::cout << "preload: populating VBOs" << std::endl; for (auto &chunk : loader.Loaded()) { chunk.CheckUpdate(); } - std::cout << "preload: complete" << std::endl; env.state.Pop(); + } else { + progress.Update(total - loader.ToLoad(), total); } } -void PreloadState::Render(Viewport &) { - // TODO: make a nice progress bar or some other fancy shit - std::cout << "preload: " << loader.ToLoad() << " chunks remaining" << std::endl; +void PreloadState::Render(Viewport &viewport) { + progress.Render(viewport); } } diff --git a/src/app/PreloadState.hpp b/src/app/PreloadState.hpp index 2c85f98..daf0a37 100644 --- a/src/app/PreloadState.hpp +++ b/src/app/PreloadState.hpp @@ -3,6 +3,9 @@ #include "State.hpp" +#include "../ui/Progress.hpp" +#include "../graphics/Font.hpp" + #include @@ -24,6 +27,9 @@ public: private: Environment &env; ChunkLoader &loader; + Font font; + Progress progress; + std::size_t total; std::size_t per_update; }; diff --git a/src/graphics/FixedText.hpp b/src/graphics/FixedText.hpp deleted file mode 100644 index 7f05c0a..0000000 --- a/src/graphics/FixedText.hpp +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef BLANK_GRAPHICS_FIXEDTEXT_HPP_ -#define BLANK_GRAPHICS_FIXEDTEXT_HPP_ - -#include "Text.hpp" - - -namespace blank { - -class FixedText -: public Text { - -public: - FixedText() noexcept; - - void Position(const glm::vec3 &p) noexcept { - pos = p; - } - void Position( - const glm::vec3 &p, - Gravity g - ) noexcept { - pos = p; - grav = g; - Pivot(g); - } - void Position( - const glm::vec3 &p, - Gravity g, - Gravity pv - ) noexcept { - pos = p; - grav = g; - Pivot(pv); - } - - void Foreground(const glm::vec4 &col) noexcept { fg = col; } - void Background(const glm::vec4 &col) noexcept { bg = col; } - - void Show() noexcept { visible = true; } - void Hide() noexcept { visible = false; } - void Toggle() noexcept { visible = !visible; } - bool Visible() const noexcept { return visible; } - - void Render(Viewport &) noexcept; - -private: - glm::vec4 bg; - glm::vec4 fg; - glm::vec3 pos; - Gravity grav; - bool visible; - -}; - -} - -#endif diff --git a/src/graphics/MessageBox.hpp b/src/graphics/MessageBox.hpp deleted file mode 100644 index a52c9b0..0000000 --- a/src/graphics/MessageBox.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef BLANK_GRAPHICS_MESSAGEBOX_HPP_ -#define BLANK_GRAPHICS_MESSAGEBOX_HPP_ - -#include "align.hpp" -#include "Text.hpp" - -#include -#include -#include - - -namespace blank { - -class Font; -class Viewport; - -class MessageBox { - -public: - explicit MessageBox(const Font &); - - void Position(const glm::vec3 &, Gravity) noexcept; - - void Foreground(const glm::vec4 &col) noexcept { fg = col; } - void Background(const glm::vec4 &col) noexcept { bg = col; } - - void PushLine(const char *); - void PushLine(const std::string &l) { - PushLine(l.c_str()); - } - - void Render(Viewport &) noexcept; - -private: - const Font &font; - std::deque lines; - std::size_t max_lines; - - glm::vec3 pos; - glm::vec3 adv; - - glm::vec4 bg; - glm::vec4 fg; - - Gravity grav; - -}; - -} - -#endif diff --git a/src/graphics/Text.hpp b/src/graphics/Text.hpp deleted file mode 100644 index 989145f..0000000 --- a/src/graphics/Text.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef BLANK_GRAPHICS_TEXT_HPP_ -#define BLANK_GRAPHICS_TEXT_HPP_ - -#include "align.hpp" -#include "Texture.hpp" -#include "../model/SpriteModel.hpp" - -#include -#include - - -namespace blank { - -class Font; -class Viewport; - -class Text { - -public: - Text() noexcept; - - void Set(const Font &, const char *); - void Set(const Font &f, const std::string &s) { - Set(f, s.c_str()); - } - - void Pivot(Gravity p) { - pivot = p; - dirty = true; - } - - void Render(Viewport &) noexcept; - -private: - void Update(); - -private: - Texture tex; - SpriteModel sprite; - glm::vec2 size; - Gravity pivot; - bool dirty; - -}; - -} - -#endif diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index f78f9cb..2b22742 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -1,9 +1,6 @@ #include "BlendedSprite.hpp" -#include "FixedText.hpp" #include "Font.hpp" #include "Format.hpp" -#include "MessageBox.hpp" -#include "Text.hpp" #include "Texture.hpp" #include "Viewport.hpp" @@ -160,111 +157,6 @@ void Format::ReadPixelFormat(const SDL_PixelFormat &fmt) { } -MessageBox::MessageBox(const Font &f) -: font(f) -, lines() -, max_lines(10) -, pos(0.0f) -, adv(0.0f, font.LineSkip(), 0.0f) -, bg(1.0f, 1.0f, 1.0f, 0.0f) -, fg(1.0f, 1.0f, 1.0f, 1.0f) -, grav(Gravity::NORTH_WEST) { - -} - -void MessageBox::Position(const glm::vec3 &p, Gravity g) noexcept { - pos = p; - grav = g; - if (get_y(g) == Align::END) { - adv.y = -font.LineSkip(); - } else { - adv.y = font.LineSkip(); - } - for (Text &txt : lines) { - txt.Pivot(g); - } -} - -void MessageBox::PushLine(const char *text) { - lines.emplace_front(); - Text &txt = lines.front(); - txt.Set(font, text); - txt.Pivot(grav); - - while (lines.size() > max_lines) { - lines.pop_back(); - } -} - -void MessageBox::Render(Viewport &viewport) noexcept { - BlendedSprite &prog = viewport.SpriteProgram(); - prog.SetBG(bg); - prog.SetFG(fg); - viewport.SetCursor(pos, grav); - for (Text &txt : lines) { - prog.SetM(viewport.Cursor()); - txt.Render(viewport); - viewport.MoveCursor(adv); - } -} - - -Text::Text() noexcept -: tex() -, sprite() -, size(0.0f) -, pivot(Gravity::NORTH_WEST) -, dirty(false) { - -} - -FixedText::FixedText() noexcept -: Text() -, bg(1.0f, 1.0f, 1.0f, 0.0f) -, fg(1.0f, 1.0f, 1.0f, 1.0f) -, pos(0.0f) -, grav(Gravity::NORTH_WEST) -, visible(false) { - -} - -void Text::Set(const Font &font, const char *text) { - font.Render(text, tex); - size = font.TextSize(text); - dirty = true; -} - -namespace { - -SpriteModel::Buffer sprite_buf; - -} - -void Text::Update() { - sprite_buf.LoadRect(size.x, size.y, align(pivot, size)); - sprite.Update(sprite_buf); - dirty = false; -} - -void FixedText::Render(Viewport &viewport) noexcept { - BlendedSprite &prog = viewport.SpriteProgram(); - viewport.SetCursor(pos, grav); - prog.SetM(viewport.Cursor()); - prog.SetBG(bg); - prog.SetFG(fg); - Text::Render(viewport); -} - -void Text::Render(Viewport &viewport) noexcept { - if (dirty) { - Update(); - } - BlendedSprite &prog = viewport.SpriteProgram(); - prog.SetTexture(tex); - sprite.Draw(); -} - - Texture::Texture() : handle(0) , width(0) diff --git a/src/ui/FixedText.hpp b/src/ui/FixedText.hpp new file mode 100644 index 0000000..dbe1217 --- /dev/null +++ b/src/ui/FixedText.hpp @@ -0,0 +1,57 @@ +#ifndef BLANK_UI_FIXEDTEXT_HPP_ +#define BLANK_UI_FIXEDTEXT_HPP_ + +#include "Text.hpp" + + +namespace blank { + +class FixedText +: public Text { + +public: + FixedText() noexcept; + + void Position(const glm::vec3 &p) noexcept { + pos = p; + } + void Position( + const glm::vec3 &p, + Gravity g + ) noexcept { + pos = p; + grav = g; + Pivot(g); + } + void Position( + const glm::vec3 &p, + Gravity g, + Gravity pv + ) noexcept { + pos = p; + grav = g; + Pivot(pv); + } + + void Foreground(const glm::vec4 &col) noexcept { fg = col; } + void Background(const glm::vec4 &col) noexcept { bg = col; } + + void Show() noexcept { visible = true; } + void Hide() noexcept { visible = false; } + void Toggle() noexcept { visible = !visible; } + bool Visible() const noexcept { return visible; } + + void Render(Viewport &) noexcept; + +private: + glm::vec4 bg; + glm::vec4 fg; + glm::vec3 pos; + Gravity grav; + bool visible; + +}; + +} + +#endif diff --git a/src/ui/HUD.hpp b/src/ui/HUD.hpp index a1662f4..8978c91 100644 --- a/src/ui/HUD.hpp +++ b/src/ui/HUD.hpp @@ -1,7 +1,7 @@ #ifndef BLANK_UI_HUD_H_ #define BLANK_UI_HUD_H_ -#include "../graphics/FixedText.hpp" +#include "FixedText.hpp" #include "../model/EntityModel.hpp" #include "../model/OutlineModel.hpp" diff --git a/src/ui/Interface.hpp b/src/ui/Interface.hpp index f810ff0..82eade3 100644 --- a/src/ui/Interface.hpp +++ b/src/ui/Interface.hpp @@ -1,13 +1,13 @@ #ifndef BLANK_UI_INTERFACE_HPP_ #define BLANK_UI_INTERFACE_HPP_ +#include "FixedText.hpp" #include "HUD.hpp" +#include "MessageBox.hpp" #include "../app/FPSController.hpp" #include "../app/IntervalTimer.hpp" #include "../audio/Sound.hpp" -#include "../graphics/FixedText.hpp" #include "../graphics/Font.hpp" -#include "../graphics/MessageBox.hpp" #include "../model/geometry.hpp" #include "../model/OutlineModel.hpp" #include "../world/Block.hpp" diff --git a/src/ui/MessageBox.hpp b/src/ui/MessageBox.hpp new file mode 100644 index 0000000..2191627 --- /dev/null +++ b/src/ui/MessageBox.hpp @@ -0,0 +1,51 @@ +#ifndef BLANK_UI_MESSAGEBOX_HPP_ +#define BLANK_UI_MESSAGEBOX_HPP_ + +#include "Text.hpp" +#include "../graphics/align.hpp" + +#include +#include +#include + + +namespace blank { + +class Font; +class Viewport; + +class MessageBox { + +public: + explicit MessageBox(const Font &); + + void Position(const glm::vec3 &, Gravity) noexcept; + + void Foreground(const glm::vec4 &col) noexcept { fg = col; } + void Background(const glm::vec4 &col) noexcept { bg = col; } + + void PushLine(const char *); + void PushLine(const std::string &l) { + PushLine(l.c_str()); + } + + void Render(Viewport &) noexcept; + +private: + const Font &font; + std::deque lines; + std::size_t max_lines; + + glm::vec3 pos; + glm::vec3 adv; + + glm::vec4 bg; + glm::vec4 fg; + + Gravity grav; + +}; + +} + +#endif diff --git a/src/ui/Progress.hpp b/src/ui/Progress.hpp new file mode 100644 index 0000000..9ea9f6d --- /dev/null +++ b/src/ui/Progress.hpp @@ -0,0 +1,32 @@ +#ifndef BLANK_UI_PROGRESS_HPP_ +#define BLANK_UI_PROGRESS_HPP_ + +#include "FixedText.hpp" + + +namespace blank { + +class Font; +class Viewport; + +class Progress { + +public: + explicit Progress(Font &) noexcept; + + void Position(const glm::vec3 &p, Gravity g) noexcept { text.Position(p, g); } + void Template(const char *t) noexcept { tpl = t; } + + void Update(int current, int total); + void Render(Viewport &) noexcept; + +private: + Font &font; + FixedText text; + const char *tpl; + +}; + +} + +#endif diff --git a/src/ui/Text.hpp b/src/ui/Text.hpp new file mode 100644 index 0000000..6c7c6a5 --- /dev/null +++ b/src/ui/Text.hpp @@ -0,0 +1,48 @@ +#ifndef BLANK_UI_TEXT_HPP_ +#define BLANK_UI_TEXT_HPP_ + +#include "../graphics/align.hpp" +#include "../graphics/Texture.hpp" +#include "../model/SpriteModel.hpp" + +#include +#include + + +namespace blank { + +class Font; +class Viewport; + +class Text { + +public: + Text() noexcept; + + void Set(const Font &, const char *); + void Set(const Font &f, const std::string &s) { + Set(f, s.c_str()); + } + + void Pivot(Gravity p) { + pivot = p; + dirty = true; + } + + void Render(Viewport &) noexcept; + +private: + void Update(); + +private: + Texture tex; + SpriteModel sprite; + glm::vec2 size; + Gravity pivot; + bool dirty; + +}; + +} + +#endif diff --git a/src/ui/widgets.cpp b/src/ui/widgets.cpp new file mode 100644 index 0000000..852d2f1 --- /dev/null +++ b/src/ui/widgets.cpp @@ -0,0 +1,141 @@ +#include "FixedText.hpp" +#include "MessageBox.hpp" +#include "Progress.hpp" +#include "Text.hpp" + +#include "../graphics/Font.hpp" +#include "../graphics/Viewport.hpp" + +#include + + +namespace blank { + +MessageBox::MessageBox(const Font &f) +: font(f) +, lines() +, max_lines(10) +, pos(0.0f) +, adv(0.0f, font.LineSkip(), 0.0f) +, bg(1.0f, 1.0f, 1.0f, 0.0f) +, fg(1.0f, 1.0f, 1.0f, 1.0f) +, grav(Gravity::NORTH_WEST) { + +} + +void MessageBox::Position(const glm::vec3 &p, Gravity g) noexcept { + pos = p; + grav = g; + if (get_y(g) == Align::END) { + adv.y = -font.LineSkip(); + } else { + adv.y = font.LineSkip(); + } + for (Text &txt : lines) { + txt.Pivot(g); + } +} + +void MessageBox::PushLine(const char *text) { + lines.emplace_front(); + Text &txt = lines.front(); + txt.Set(font, text); + txt.Pivot(grav); + + while (lines.size() > max_lines) { + lines.pop_back(); + } +} + +void MessageBox::Render(Viewport &viewport) noexcept { + BlendedSprite &prog = viewport.SpriteProgram(); + prog.SetBG(bg); + prog.SetFG(fg); + viewport.SetCursor(pos, grav); + for (Text &txt : lines) { + prog.SetM(viewport.Cursor()); + txt.Render(viewport); + viewport.MoveCursor(adv); + } +} + + +Progress::Progress(Font &font) noexcept +: font(font) +, text() +, tpl("%d/%d (%d%%)") { + +} + +namespace { + +char buf[128] = { '\0' }; + +} + +void Progress::Update(int current, int total) { + std::snprintf(buf, sizeof(buf), tpl, current, total, current * 100 / total); + text.Set(font, buf); +} + +void Progress::Render(Viewport &viewport) noexcept { + text.Render(viewport); +} + + +Text::Text() noexcept +: tex() +, sprite() +, size(0.0f) +, pivot(Gravity::NORTH_WEST) +, dirty(false) { + +} + +FixedText::FixedText() noexcept +: Text() +, bg(1.0f, 1.0f, 1.0f, 0.0f) +, fg(1.0f, 1.0f, 1.0f, 1.0f) +, pos(0.0f) +, grav(Gravity::NORTH_WEST) +, visible(false) { + +} + +void Text::Set(const Font &font, const char *text) { + font.Render(text, tex); + size = font.TextSize(text); + dirty = true; +} + +namespace { + +SpriteModel::Buffer sprite_buf; + +} + +void Text::Update() { + sprite_buf.LoadRect(size.x, size.y, align(pivot, size)); + sprite.Update(sprite_buf); + dirty = false; +} + +void FixedText::Render(Viewport &viewport) noexcept { + BlendedSprite &prog = viewport.SpriteProgram(); + viewport.SetCursor(pos, grav); + prog.SetM(viewport.Cursor()); + prog.SetBG(bg); + prog.SetFG(fg); + Text::Render(viewport); +} + +void Text::Render(Viewport &viewport) noexcept { + if (dirty) { + Update(); + } + BlendedSprite &prog = viewport.SpriteProgram(); + prog.SetTexture(tex); + sprite.Draw(); +} + +}