value = 0;
speed = 0;
}
+ void Reset() noexcept {
+ value = 0;
+ }
bool Running() const noexcept {
return speed != 0;
bool Hit() const noexcept {
return Running() && value % intv < last_dt;
}
+ bool HitOnce() const noexcept {
+ return value >= intv;
+ }
int Elapsed() const noexcept {
return value;
}
--- /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
Set(f, s.c_str());
}
- void Position(const glm::vec3 &p) noexcept {
- pos = p;
- }
- void Position(
- const glm::vec3 &p,
- Gravity g
- ) noexcept {
- pos = p;
- grav = g;
- pivot = g;
- dirty = true;
- }
- void Position(
- const glm::vec3 &p,
- Gravity g,
- Gravity pv
- ) noexcept {
- pos = p;
- grav = g;
- pivot = pv;
+ void Pivot(Gravity p) {
+ pivot = p;
dirty = true;
}
- void Foreground(const glm::vec4 &col) noexcept { fg = col; }
- void Background(const glm::vec4 &col) noexcept { bg = col; }
-
void Render(Viewport &) noexcept;
- void Show() noexcept { visible = true; }
- void Hide() noexcept { visible = false; }
- void Toggle() noexcept { visible = !visible; }
- bool Visible() const noexcept { return visible; }
-
private:
void Update();
private:
Texture tex;
SpriteModel sprite;
- glm::vec4 bg;
- glm::vec4 fg;
glm::vec2 size;
- glm::vec3 pos;
- Gravity grav;
Gravity pivot;
bool dirty;
- bool visible;
};
void ClearDepth() noexcept;
void SetCursor(const glm::vec3 &);
- void SetCursor(const glm::vec3 &, Gravity = Gravity::NORTH_WEST);
+ void SetCursor(const glm::vec3 &, Gravity);
void MoveCursor(const glm::vec3 &);
const glm::mat4 &Cursor() const noexcept { return cursor; }
namespace blank {
enum class Align {
- LEFT,
- CENTER,
- RIGHT,
+ BEGIN,
+ MIDDLE,
+ END,
};
enum class Gravity {
#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)
-, size(0.0f)
, pos(0.0f)
, grav(Gravity::NORTH_WEST)
-, pivot(Gravity::NORTH_WEST)
-, dirty(false)
, visible(false) {
}
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();
- viewport.SetCursor(pos, grav);
- prog.SetM(viewport.Cursor());
prog.SetTexture(tex);
- prog.SetBG(bg);
- prog.SetFG(fg);
sprite.Draw();
}
#ifndef BLANK_UI_HUD_H_
#define BLANK_UI_HUD_H_
-#include "../graphics/Text.hpp"
+#include "../graphics/FixedText.hpp"
#include "../model/Model.hpp"
#include "../model/OutlineModel.hpp"
Model::Buffer block_buf;
glm::mat4 block_transform;
- Text block_label;
+ FixedText block_label;
bool block_visible;
#include "HUD.hpp"
#include "../app/FPSController.hpp"
#include "../app/IntervalTimer.hpp"
+#include "../graphics/FixedText.hpp"
#include "../graphics/Font.hpp"
-#include "../graphics/Text.hpp"
+#include "../graphics/MessageBox.hpp"
#include "../model/geometry.hpp"
#include "../model/OutlineModel.hpp"
#include "../world/Block.hpp"
+#include <string>
#include <glm/glm.hpp>
void ToggleCounter();
void UpdateCounter();
+ void PostMessage(const char *);
+ void PostMessage(const std::string &msg) {
+ PostMessage(msg.c_str());
+ }
+
void Update(int dt);
void Render(Viewport &) noexcept;
OutlineModel outline;
glm::mat4 outline_transform;
- Text counter_text;
+ FixedText counter_text;
+ MessageBox messages;
+ IntervalTimer msg_timer;
Config config;
, outline()
, outline_transform(1.0f)
, counter_text()
+, messages(font)
+, msg_timer(5000)
, config(config)
, place_timer(256)
, remove_timer(256)
counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
counter_text.Foreground(glm::vec4(1.0f));
counter_text.Background(glm::vec4(0.5f));
+ messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
+ messages.Foreground(glm::vec4(1.0f));
+ messages.Background(glm::vec4(0.5f));
hud.Display(selection);
}
void Interface::ToggleCollision() {
ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
- std::cout << "collision " << (ctrl.Controlled().WorldCollidable() ? "on" : "off") << std::endl;
+ if (ctrl.Controlled().WorldCollidable()) {
+ PostMessage("collision on");
+ } else {
+ PostMessage("collision off");
+ }
}
void Interface::PrintBlockInfo() {
std::cout << std::endl;
if (!aim_chunk) {
- std::cout << "not looking at any block" << std::endl;
+ PostMessage("not looking at any block");
Ray aim = ctrl.Aim();
- std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
+ std::stringstream s;
+ s << "aim ray: " << aim.orig << ", " << aim.dir;
+ PostMessage(s.str());
return;
}
- std::cout << "looking at block " << aim_block
+ std::stringstream s;
+ s << "looking at block " << aim_block
<< " " << Chunk::ToCoords(aim_block)
<< " of chunk " << aim_chunk->Position()
- << std::endl;
+ ;
+ PostMessage(s.str());
Print(aim_chunk->BlockAt(aim_block));
}
void Interface::PrintChunkInfo() {
std::cout << std::endl;
if (!aim_chunk) {
- std::cout << "not looking at any block" << std::endl;
+ PostMessage("not looking at any block");
return;
}
- std::cout << "looking at chunk "
- << aim_chunk->Position()
- << std::endl;
+ std::stringstream s;
+ s << "looking at chunk " << aim_chunk->Position();
+ PostMessage(s.str());
- std::cout << " neighbors:" << std::endl;
+ PostMessage(" neighbors:");
if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
- std::cout << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
+ s.str("");
+ s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
+ PostMessage(s.str());
}
if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
- std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
+ s.str("");
+ s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
+ PostMessage(s.str());
}
if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
- std::cout << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
+ s.str("");
+ s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
+ PostMessage(s.str());
}
if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
- std::cout << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
+ s.str("");
+ s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
+ PostMessage(s.str());
}
if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
- std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
+ s.str("");
+ s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
+ PostMessage(s.str());
}
if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
- std::cout << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
+ s.str("");
+ s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
+ PostMessage(s.str());
}
std::cout << std::endl;
}
void Interface::PrintLightInfo() {
- std::cout
+ std::stringstream s;
+ s
<< "light level " << world.PlayerChunk().GetLight(world.Player().Position())
<< " at position " << world.Player().Position()
- << std::endl;
+ ;
+ PostMessage(s.str());
}
void Interface::PrintSelectionInfo() {
}
void Interface::Print(const Block &block) {
- std::cout << "type: " << block.type
+ std::stringstream s;
+ s << "type: " << block.type
<< ", face: " << block.GetFace()
<< ", turn: " << block.GetTurn()
- << std::endl;
+ ;
+ PostMessage(s.str());
}
void Interface::ToggleCounter() {
}
+void Interface::PostMessage(const char *msg) {
+ messages.PushLine(msg);
+ msg_timer.Reset();
+ msg_timer.Start();
+ std::cout << msg << std::endl;
+}
+
+
void Interface::Update(int dt) {
ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
ctrl.Update(dt);
+ msg_timer.Update(dt);
place_timer.Update(dt);
remove_timer.Update(dt);
aim = ctrl.Aim();
CheckAim();
+ if (msg_timer.HitOnce()) {
+ msg_timer.Stop();
+ }
+
if (remove_timer.Hit()) {
RemoveBlock();
CheckAim();
counter_text.Render(viewport);
}
+ if (msg_timer.Running()) {
+ messages.Render(viewport);
+ }
+
hud.Render(viewport);
}