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%%)");
}
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);
}
}
#include "State.hpp"
+#include "../ui/Progress.hpp"
+#include "../graphics/Font.hpp"
+
#include <cstddef>
private:
Environment &env;
ChunkLoader &loader;
+ Font font;
+ Progress progress;
+ std::size_t total;
std::size_t per_update;
};
+++ /dev/null
-#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
+++ /dev/null
-#ifndef BLANK_GRAPHICS_MESSAGEBOX_HPP_
-#define BLANK_GRAPHICS_MESSAGEBOX_HPP_
-
-#include "align.hpp"
-#include "Text.hpp"
-
-#include <deque>
-#include <string>
-#include <glm/glm.hpp>
-
-
-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<Text> lines;
- std::size_t max_lines;
-
- glm::vec3 pos;
- glm::vec3 adv;
-
- glm::vec4 bg;
- glm::vec4 fg;
-
- Gravity grav;
-
-};
-
-}
-
-#endif
+++ /dev/null
-#ifndef BLANK_GRAPHICS_TEXT_HPP_
-#define BLANK_GRAPHICS_TEXT_HPP_
-
-#include "align.hpp"
-#include "Texture.hpp"
-#include "../model/SpriteModel.hpp"
-
-#include <string>
-#include <glm/glm.hpp>
-
-
-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
#include "BlendedSprite.hpp"
-#include "FixedText.hpp"
#include "Font.hpp"
#include "Format.hpp"
-#include "MessageBox.hpp"
-#include "Text.hpp"
#include "Texture.hpp"
#include "Viewport.hpp"
}
-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)
--- /dev/null
+#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
#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"
#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"
--- /dev/null
+#ifndef BLANK_UI_MESSAGEBOX_HPP_
+#define BLANK_UI_MESSAGEBOX_HPP_
+
+#include "Text.hpp"
+#include "../graphics/align.hpp"
+
+#include <deque>
+#include <string>
+#include <glm/glm.hpp>
+
+
+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<Text> lines;
+ std::size_t max_lines;
+
+ glm::vec3 pos;
+ glm::vec3 adv;
+
+ glm::vec4 bg;
+ glm::vec4 fg;
+
+ Gravity grav;
+
+};
+
+}
+
+#endif
--- /dev/null
+#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
--- /dev/null
+#ifndef BLANK_UI_TEXT_HPP_
+#define BLANK_UI_TEXT_HPP_
+
+#include "../graphics/align.hpp"
+#include "../graphics/Texture.hpp"
+#include "../model/SpriteModel.hpp"
+
+#include <string>
+#include <glm/glm.hpp>
+
+
+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
--- /dev/null
+#include "FixedText.hpp"
+#include "MessageBox.hpp"
+#include "Progress.hpp"
+#include "Text.hpp"
+
+#include "../graphics/Font.hpp"
+#include "../graphics/Viewport.hpp"
+
+#include <cstdio>
+
+
+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();
+}
+
+}