Menu screen prototype, refactor

This commit is contained in:
MihailRis 2023-11-16 03:00:05 +03:00
parent 5af04e20d1
commit b03bcb5191
14 changed files with 267 additions and 52 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ Debug/voxel_engine
/build /build
/world /world
/worlds
.vscode .vscode

View File

@ -59,12 +59,15 @@ Engine::Engine(const EngineSettings& settings_) {
std::cout << "-- initializing finished" << std::endl; std::cout << "-- initializing finished" << std::endl;
std::cout << "-- loading world" << std::endl; std::cout << "-- loading world" << std::endl;
vec3 playerPosition = vec3(0, 64, 0);
Camera* camera = new Camera(playerPosition, radians(90.0f));
World* world = new World("world-1", enginefs::get_worlds_folder()/"world", 42, settings);
Player* player = new Player(playerPosition, 4.0f, camera);
setScreen(new LevelScreen(this, world->loadLevel(player, settings)));
//World* world = new World("world-1", enginefs::get_worlds_folder()/"world", 42, settings);
//vec3 playerPosition = vec3(0, 64, 0);
//Camera* camera = new Camera(playerPosition, radians(90.0f));
//Player* player = new Player(playerPosition, 4.0f, camera);
//setScreen(new LevelScreen(this, world->loadLevel(player, settings)));
setScreen(new MenuScreen(this));
} }
void Engine::updateTimers() { void Engine::updateTimers() {

View File

@ -3,33 +3,35 @@
#include "panels.h" #include "panels.h"
#include <iostream> #include <iostream>
#include <algorithm>
#include "../../assets/Assets.h" #include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h" #include "../../graphics/Batch2D.h"
#include "../../graphics/Shader.h"
#include "../../window/Events.h" #include "../../window/Events.h"
#include "../../window/input.h" #include "../../window/input.h"
#include "../../window/Camera.h"
using std::shared_ptr; using std::shared_ptr;
using namespace gui; using namespace gui;
GUI::GUI() { GUI::GUI() {
container = new Container(vec2(0, 0), vec2(Window::width, Window::height)); container = new Container(vec2(0, 0), vec2(Window::width, Window::height));
uicamera = new Camera(vec3(), Window::height);
uicamera->perspective = false;
uicamera->flipped = true;
} }
GUI::~GUI() { GUI::~GUI() {
delete uicamera;
delete container; delete container;
} }
void GUI::act(float delta) { void GUI::act(float delta) {
for (IntervalEvent& event : intervalEvents) {
event.timer += delta;
if (event.timer > event.interval) {
event.callback();
event.timer = fmod(event.timer, event.interval);
}
}
container->size(vec2(Window::width, Window::height)); container->size(vec2(Window::width, Window::height));
container->act(delta);
int mx = Events::x; int mx = Events::x;
int my = Events::y; int my = Events::y;
@ -77,6 +79,12 @@ void GUI::act(float delta) {
} }
void GUI::draw(Batch2D* batch, Assets* assets) { void GUI::draw(Batch2D* batch, Assets* assets) {
uicamera->fov = Window::height;
Shader* uishader = assets->getShader("ui");
uishader->use();
uishader->uniformMatrix("u_projview", uicamera->getProjection()*uicamera->getView());
batch->begin(); batch->begin();
container->draw(batch, assets); container->draw(batch, assets);
} }
@ -93,6 +101,6 @@ void GUI::add(shared_ptr<UINode> panel) {
container->add(panel); container->add(panel);
} }
void GUI::interval(float interval, ontimeout callback) { void GUI::remove(std::shared_ptr<UINode> panel) {
intervalEvents.push_back({callback, interval, 0.0f}); container->remove(panel);
} }

View File

@ -8,6 +8,7 @@
class Batch2D; class Batch2D;
class Assets; class Assets;
class Camera;
/* /*
Some info about padding and margin. Some info about padding and margin.
@ -44,19 +45,12 @@ namespace gui {
class UINode; class UINode;
class Container; class Container;
typedef std::function<void()> ontimeout;
struct IntervalEvent {
ontimeout callback;
float interval;
float timer;
};
class GUI { class GUI {
Container* container; Container* container;
std::shared_ptr<UINode> hover = nullptr; std::shared_ptr<UINode> hover = nullptr;
std::shared_ptr<UINode> pressed = nullptr; std::shared_ptr<UINode> pressed = nullptr;
std::shared_ptr<UINode> focus = nullptr; std::shared_ptr<UINode> focus = nullptr;
std::vector<IntervalEvent> intervalEvents; Camera* uicamera;
public: public:
GUI(); GUI();
~GUI(); ~GUI();
@ -67,8 +61,7 @@ namespace gui {
void act(float delta); void act(float delta);
void draw(Batch2D* batch, Assets* assets); void draw(Batch2D* batch, Assets* assets);
void add(std::shared_ptr<UINode> panel); void add(std::shared_ptr<UINode> panel);
void remove(std::shared_ptr<UINode> panel);
void interval(float interval, ontimeout callback);
}; };
} }

View File

@ -35,6 +35,7 @@ namespace gui {
UINode(glm::vec2 coord, glm::vec2 size); UINode(glm::vec2 coord, glm::vec2 size);
public: public:
virtual ~UINode(); virtual ~UINode();
virtual void act(float delta) {};
virtual void draw(Batch2D* batch, Assets* assets) = 0; virtual void draw(Batch2D* batch, Assets* assets) = 0;
virtual void visible(bool flag); virtual void visible(bool flag);

View File

@ -29,6 +29,20 @@ shared_ptr<UINode> Container::getAt(vec2 pos, shared_ptr<UINode> self) {
return UINode::getAt(pos, self); return UINode::getAt(pos, self);
} }
void Container::act(float delta) {
for (IntervalEvent& event : intervalEvents) {
event.timer += delta;
if (event.timer > event.interval) {
event.callback();
event.timer = fmod(event.timer, event.interval);
}
}
for (auto node : nodes) {
if (node->visible())
node->act(delta);
}
}
void Container::draw(Batch2D* batch, Assets* assets) { void Container::draw(Batch2D* batch, Assets* assets) {
vec2 coord = calcCoord(); vec2 coord = calcCoord();
vec2 size = this->size(); vec2 size = this->size();
@ -50,6 +64,18 @@ void Container::add(shared_ptr<UINode> node) {
refresh(); refresh();
} }
void Container::remove(shared_ptr<UINode> selected) {
selected->setParent(nullptr);
nodes.erase(std::remove_if(nodes.begin(), nodes.end(), [selected](const shared_ptr<UINode> node) {
return node == selected;
}), nodes.end());
refresh();
}
void Container::listenInterval(float interval, ontimeout callback) {
intervalEvents.push_back({callback, interval, 0.0f});
}
Panel::Panel(vec2 size, glm::vec4 padding, float interval, bool resizing) Panel::Panel(vec2 size, glm::vec4 padding, float interval, bool resizing)
: Container(vec2(), size), padding(padding), interval(interval), resizing_(resizing) { : Container(vec2(), size), padding(padding), interval(interval), resizing_(resizing) {
color_ = vec4(0.0f, 0.0f, 0.0f, 0.75f); color_ = vec4(0.0f, 0.0f, 0.0f, 0.75f);

View File

@ -10,18 +10,29 @@ class Batch2D;
class Assets; class Assets;
namespace gui { namespace gui {
typedef std::function<void()> ontimeout;
struct IntervalEvent {
ontimeout callback;
float interval;
float timer;
};
enum class Orientation { vertical, horizontal }; enum class Orientation { vertical, horizontal };
class Container : public UINode { class Container : public UINode {
protected: protected:
std::vector<std::shared_ptr<UINode>> nodes; std::vector<std::shared_ptr<UINode>> nodes;
std::vector<IntervalEvent> intervalEvents;
public: public:
Container(glm::vec2 coord, glm::vec2 size); Container(glm::vec2 coord, glm::vec2 size);
virtual void act(float delta) override;
virtual void drawBackground(Batch2D* batch, Assets* assets) {}; virtual void drawBackground(Batch2D* batch, Assets* assets) {};
virtual void draw(Batch2D* batch, Assets* assets); virtual void draw(Batch2D* batch, Assets* assets) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override; virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void add(std::shared_ptr<UINode> node); virtual void add(std::shared_ptr<UINode> node);
virtual void remove(std::shared_ptr<UINode> node);
void listenInterval(float interval, ontimeout callback);
}; };
class Panel : public Container { class Panel : public Container {

View File

@ -28,6 +28,8 @@
#include "gui/panels.h" #include "gui/panels.h"
#include "gui/UINode.h" #include "gui/UINode.h"
#include "gui/GUI.h" #include "gui/GUI.h"
#include "screens.h"
#include "../engine.h"
using std::wstring; using std::wstring;
using std::shared_ptr; using std::shared_ptr;
@ -42,18 +44,18 @@ inline Label* create_label(gui::wstringsupplier supplier) {
return label; return label;
} }
HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level), assets(assets), guiController(gui) { HudRenderer::HudRenderer(Engine* engine, Level* level) : level(level), assets(engine->getAssets()), guiController(engine->getGUI()) {
batch = new Batch2D(1024); batch = new Batch2D(1024);
uicamera = new Camera(vec3(), Window::height); uicamera = new Camera(vec3(), Window::height);
uicamera->perspective = false; uicamera->perspective = false;
uicamera->flipped = true; uicamera->flipped = true;
gui->interval(1.0f, [this]() { Panel* panel = new Panel(vec2(200, 200), vec4(5.0f), 1.0f);
panel->listenInterval(1.0f, [this]() {
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin); fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
fpsMin = fps; fpsMin = fps;
fpsMax = fps; fpsMax = fps;
}); });
Panel* panel = new Panel(vec2(200, 200), vec4(5.0f), 1.0f);
panel->setCoord(vec2(10, 10)); panel->setCoord(vec2(10, 10));
panel->add(shared_ptr<Label>(create_label([this](){ panel->add(shared_ptr<Label>(create_label([this](){
return L"chunks: "+std::to_wstring(this->level->chunks->chunksCount); return L"chunks: "+std::to_wstring(this->level->chunks->chunksCount);
@ -102,9 +104,9 @@ HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level),
panel->add(shared_ptr<UINode>(sub)); panel->add(shared_ptr<UINode>(sub));
} }
panel->refresh(); panel->refresh();
debugPanel = panel; debugPanel = shared_ptr<UINode>(panel);
pauseMenu = new Panel(vec2(350, 200)); Panel* pauseMenu = new Panel(vec2(350, 200));
pauseMenu->color(vec4(0.0f)); pauseMenu->color(vec4(0.0f));
{ {
Button* button = new Button(L"Continue", vec4(12.0f, 10.0f, 12.0f, 12.0f)); Button* button = new Button(L"Continue", vec4(12.0f, 10.0f, 12.0f, 12.0f));
@ -114,17 +116,21 @@ HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level),
pauseMenu->add(shared_ptr<UINode>(button)); pauseMenu->add(shared_ptr<UINode>(button));
} }
{ {
Button* button = new Button(L"Save and Quit", vec4(12.0f, 10.0f, 12.0f, 12.0f)); Button* button = new Button(L"Save and Quit to Menu", vec4(12.0f, 10.0f, 12.0f, 12.0f));
button->listenAction([this](GUI*){ button->listenAction([this, engine](GUI*){
Window::setShouldClose(true); this->pauseMenu->visible(false);
engine->setScreen(new MenuScreen(engine));
}); });
pauseMenu->add(shared_ptr<UINode>(button)); pauseMenu->add(shared_ptr<UINode>(button));
} }
guiController->add(shared_ptr<UINode>(debugPanel)); this->pauseMenu = std::shared_ptr<gui::UINode>(pauseMenu);
guiController->add(shared_ptr<UINode>(pauseMenu)); guiController->add(this->debugPanel);
guiController->add(this->pauseMenu);
} }
HudRenderer::~HudRenderer() { HudRenderer::~HudRenderer() {
guiController->remove(debugPanel);
guiController->remove(pauseMenu);
delete batch; delete batch;
delete uicamera; delete uicamera;
} }
@ -216,7 +222,7 @@ void HudRenderer::drawInventory(Player* player) {
void HudRenderer::draw(){ void HudRenderer::draw(){
debugPanel->visible(level->player->debug); debugPanel->visible(level->player->debug);
pauseMenu->visible(pause); pauseMenu->visible(pause);
pauseMenu->setCoord(vec2(Window::width/2.0f, Window::height/2.0f) - pauseMenu->size() / 2.0f); pauseMenu->setCoord((Window::size() - pauseMenu->size()) / 2.0f);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);

View File

@ -2,6 +2,7 @@
#define SRC_HUD_H_ #define SRC_HUD_H_
#include <string> #include <string>
#include <memory>
class Batch2D; class Batch2D;
class Camera; class Camera;
@ -9,10 +10,11 @@ class Level;
class Assets; class Assets;
class Player; class Player;
class Level; class Level;
class Engine;
namespace gui { namespace gui {
class GUI; class GUI;
class Panel; class UINode;
} }
class HudRenderer { class HudRenderer {
@ -27,13 +29,13 @@ class HudRenderer {
std::wstring fpsString; std::wstring fpsString;
bool occlusion; bool occlusion;
bool inventoryOpen = false; bool inventoryOpen = false;
bool pause = true; bool pause = false;
gui::Panel* debugPanel; std::shared_ptr<gui::UINode> debugPanel;
gui::Panel* pauseMenu; std::shared_ptr<gui::UINode> pauseMenu;
gui::GUI* guiController; gui::GUI* guiController;
public: public:
HudRenderer(gui::GUI* gui, Level* level, Assets* assets); HudRenderer(Engine* engine, Level* level);
~HudRenderer(); ~HudRenderer();
void drawInventory(Player* player); void drawInventory(Player* player);
void draw(); void draw();

View File

@ -2,10 +2,13 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <glm/glm.hpp>
#include <filesystem>
#include "../window/Camera.h" #include "../window/Camera.h"
#include "../window/Events.h" #include "../window/Events.h"
#include "../window/input.h" #include "../window/input.h"
#include "../graphics/Shader.h"
#include "../assets/Assets.h" #include "../assets/Assets.h"
#include "../world/Level.h" #include "../world/Level.h"
#include "../world/World.h" #include "../world/World.h"
@ -16,13 +19,84 @@
#include "world_render.h" #include "world_render.h"
#include "hud.h" #include "hud.h"
#include "gui/GUI.h" #include "gui/GUI.h"
#include "gui/panels.h"
#include "gui/controls.h"
#include "../engine.h" #include "../engine.h"
#include "../files/engine_files.h"
#include "../util/stringutil.h"
using std::wstring;
using glm::vec3;
using glm::vec4;
using std::shared_ptr; using std::shared_ptr;
using std::filesystem::directory_iterator;
using namespace gui;
MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f);
panel->color(vec4(0.0f));
panel->setCoord(vec2(10, 10));
{
Button* button = new Button(L"Continue", vec4(12.0f, 10.0f, 12.0f, 10.0f));
button->listenAction([this, panel](GUI*) {
EngineSettings& settings = engine->getSettings();
World* world = new World("world", enginefs::get_worlds_folder()/"world", 42, settings);
vec3 playerPosition = vec3(0, 64, 0);
Camera* camera = new Camera(playerPosition, radians(90.0f));
Player* player = new Player(playerPosition, 4.0f, camera);
engine->setScreen(new LevelScreen(engine, world->loadLevel(player, settings)));
});
panel->add(shared_ptr<UINode>(button));
}
// ATTENTION: FUNCTIONALITY INCOMPLETE ZONE
/*Panel* worldsPanel = new Panel(vec2(390, 200), vec4(5.0f));
worldsPanel->color(vec4(0.1f));
for (auto const& entry : directory_iterator(enginefs::get_worlds_folder())) {
std::string name = entry.path().filename();
Button* button = new Button(util::str2wstr_utf8(name), vec4(10.0f, 8.0f, 10.0f, 8.0f));
button->color(vec4(0.5f));
button->listenAction([this, panel, name](GUI*) {
EngineSettings& settings = engine->getSettings();
World* world = new World(name, enginefs::get_worlds_folder()/name, 42, settings);
vec3 playerPosition = vec3(0, 64, 0);
Camera* camera = new Camera(playerPosition, radians(90.0f));
Player* player = new Player(playerPosition, 4.0f, camera);
engine->setScreen(new LevelScreen(engine, world->loadLevel(player, settings)));
});
worldsPanel->add(shared_ptr<UINode>(button));
}
panel->add(shared_ptr<UINode>(worldsPanel));*/
{
Button* button = new Button(L"Quit", vec4(12.0f, 10.0f, 12.0f, 10.0f));
button->listenAction([this](GUI*) {
Window::setShouldClose(true);
});
panel->add(shared_ptr<UINode>(button));
}
this->panel = shared_ptr<UINode>(panel);
engine->getGUI()->add(this->panel);
}
MenuScreen::~MenuScreen() {
engine->getGUI()->remove(panel);
}
void MenuScreen::update(float delta) {
}
void MenuScreen::draw(float delta) {
panel->setCoord((Window::size() - panel->size()) / 2.0f);
Window::clear();
Window::setBgColor(vec3(0.2f, 0.2f, 0.2f));
}
LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine), level(level) { LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine), level(level) {
worldRenderer = new WorldRenderer(level, engine->getAssets()); worldRenderer = new WorldRenderer(level, engine->getAssets());
hud = new HudRenderer(engine->getGUI(), level, engine->getAssets()); hud = new HudRenderer(engine, level);
} }
LevelScreen::~LevelScreen() { LevelScreen::~LevelScreen() {

View File

@ -1,6 +1,7 @@
#ifndef FRONTEND_SCREENS_H_ #ifndef FRONTEND_SCREENS_H_
#define FRONTEND_SCREENS_H_ #define FRONTEND_SCREENS_H_
#include <memory>
#include "../settings.h" #include "../settings.h"
class Assets; class Assets;
@ -8,6 +9,11 @@ class Level;
class WorldRenderer; class WorldRenderer;
class HudRenderer; class HudRenderer;
class Engine; class Engine;
class Camera;
namespace gui {
class UINode;
}
/* Screen is a mainloop state */ /* Screen is a mainloop state */
@ -21,6 +27,16 @@ public:
virtual void draw(float delta) = 0; virtual void draw(float delta) = 0;
}; };
class MenuScreen : public Screen {
std::shared_ptr<gui::UINode> panel;
public:
MenuScreen(Engine* engine);
~MenuScreen();
void update(float delta) override;
void draw(float delta) override;
};
class LevelScreen : public Screen { class LevelScreen : public Screen {
Level* level; Level* level;
WorldRenderer* worldRenderer; WorldRenderer* worldRenderer;

View File

@ -3,6 +3,7 @@
#include <vector> #include <vector>
#include <locale> #include <locale>
#include <sstream> #include <sstream>
#include <stdexcept>
using std::vector; using std::vector;
using std::string; using std::string;
@ -10,7 +11,7 @@ using std::stringstream;
using std::wstring; using std::wstring;
using std::wstringstream; using std::wstringstream;
wstring lfill(wstring s, uint length, wchar_t c) { wstring util::lfill(wstring s, uint length, wchar_t c) {
if (s.length() >= length) { if (s.length() >= length) {
return s; return s;
} }
@ -22,7 +23,7 @@ wstring lfill(wstring s, uint length, wchar_t c) {
return ss.str(); return ss.str();
} }
wstring rfill(wstring s, uint length, wchar_t c) { wstring util::rfill(wstring s, uint length, wchar_t c) {
if (s.length() >= length) { if (s.length() >= length) {
return s; return s;
} }
@ -34,7 +35,7 @@ wstring rfill(wstring s, uint length, wchar_t c) {
return ss.str(); return ss.str();
} }
uint encode_utf8(uint c, ubyte* bytes) { uint util::encode_utf8(uint32_t c, ubyte* bytes) {
if (c < 0x80) { if (c < 0x80) {
bytes[0] = c >> 0 & 0x7F | 0x00; bytes[0] = c >> 0 & 0x7F | 0x00;
return 1; return 1;
@ -56,7 +57,52 @@ uint encode_utf8(uint c, ubyte* bytes) {
} }
} }
string wstr2str_utf8(const wstring ws) { struct utf_t {
char mask;
char lead;
uint32_t beg;
uint32_t end;
int bits_stored;
};
const utf_t utf[] = {
/* mask lead beg end bits */
{(char)0b00111111, (char)0b10000000, 0, 0, 6},
{(char)0b01111111, (char)0b00000000, 0000, 0177, 7},
{(char)0b00011111, (char)0b11000000, 0200, 03777, 5},
{(char)0b00001111, (char)0b11100000, 04000, 0177777, 4},
{(char)0b00000111, (char)0b11110000, 0200000, 04177777, 3},
{0, 0, 0, 0, 0},
};
inline uint utf8_len(ubyte cp) {
uint len = 0;
for (const utf_t* u = utf; u->mask; ++u) {
if((cp >= u->beg) && (cp <= u->end)) {
break;
}
++len;
}
if(len > 4) /* Out of bounds */
throw std::runtime_error("utf-8 decode error");
return len;
}
extern uint32_t util::decode_utf8(uint& size, const char* chr) {
size = utf8_len(*chr);
int shift = utf[0].bits_stored * (size - 1);
uint32_t code = (*chr++ & utf[size].mask) << shift;
for(uint i = 1; i < size; ++i, ++chr) {
shift -= utf[0].bits_stored;
code |= ((char)*chr & utf[0].mask) << shift;
}
return code;
}
string util::wstr2str_utf8(const wstring ws) {
vector<char> chars; vector<char> chars;
char buffer[4]; char buffer[4];
for (wchar_t wc : ws) { for (wchar_t wc : ws) {
@ -66,4 +112,23 @@ string wstr2str_utf8(const wstring ws) {
} }
} }
return string(chars.data(), chars.size()); return string(chars.data(), chars.size());
}
wstring util::str2wstr_utf8(const string s) {
vector<wchar_t> chars;
size_t pos = 0;
uint size = 0;
while (pos < s.length()) {
chars.push_back(decode_utf8(size, &s.at(pos)));
pos += size;
}
return wstring(chars.data(), chars.size());
}
bool util::is_integer(string text) {
for (char c : text) {
if (c < '0' || c > '9')
return false;
}
return true;
} }

View File

@ -4,10 +4,15 @@
#include <string> #include <string>
#include "../typedefs.h" #include "../typedefs.h"
extern std::wstring lfill(std::wstring s, uint length, wchar_t c); namespace util {
extern std::wstring rfill(std::wstring s, uint length, wchar_t c); extern std::wstring lfill(std::wstring s, uint length, wchar_t c);
extern std::wstring rfill(std::wstring s, uint length, wchar_t c);
extern uint encode_utf8(uint c, ubyte* bytes); extern uint encode_utf8(uint32_t c, ubyte* bytes);
extern std::string wstr2str_utf8(const std::wstring ws); extern uint32_t decode_utf8(uint& size, const char* bytes);
extern std::string wstr2str_utf8(const std::wstring ws);
extern std::wstring str2wstr_utf8(const std::string s);
extern bool is_integer(std::string text);
}
#endif // UTIL_STRINGUTIL_H_ #endif // UTIL_STRINGUTIL_H_

View File

@ -37,6 +37,10 @@ public:
static void setBgColor(glm::vec3 color); static void setBgColor(glm::vec3 color);
static double time(); static double time();
static glm::vec2 size() {
return glm::vec2(width, height);
}
static ImageData* takeScreenshot(); static ImageData* takeScreenshot();
}; };