This commit is contained in:
A-lex-Ra
2024-01-08 12:36:11 +06:00
28 changed files with 540 additions and 218 deletions
+6 -6
View File
@@ -2,6 +2,7 @@
#include <glm/ext.hpp>
#include "../assets/Assets.h"
#include "../graphics/Viewport.h"
#include "../graphics/Shader.h"
#include "../graphics/Texture.h"
@@ -11,10 +12,10 @@
#include "../voxels/Block.h"
#include "ContentGfxCache.h"
BlocksPreview::BlocksPreview(Shader* shader,
Atlas* atlas,
const ContentGfxCache* cache)
: shader(shader), atlas(atlas), cache(cache) {
BlocksPreview::BlocksPreview(Assets* assets, const ContentGfxCache* cache)
: shader(assets->getShader("ui3d")),
atlas(assets->getAtlas("blocks")),
cache(cache) {
batch = std::make_unique<Batch3D>(1024);
}
@@ -37,9 +38,8 @@ void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tin
uint width = viewport->getWidth();
uint height = viewport->getHeight();
y = height - y - 1;
y = height - y - 1 - 35 /* magic garbage */;
x += 2;
y -= 35;
glm::vec3 offset (x/float(width) * 2, y/float(height) * 2, 0.0f);
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
+3 -2
View File
@@ -5,6 +5,7 @@
#include <glm/glm.hpp>
#include <memory>
class Assets;
class Viewport;
class Shader;
class Atlas;
@@ -19,11 +20,11 @@ class BlocksPreview {
const ContentGfxCache* const cache;
const Viewport* viewport;
public:
BlocksPreview(Shader* shader, Atlas* atlas, const ContentGfxCache* cache);
BlocksPreview(Assets* assets, const ContentGfxCache* cache);
~BlocksPreview();
void begin(const Viewport* viewport);
void draw(const Block* block, int x, int y, int size, glm::vec4 tint);
};
#endif // FRONTEND_BLOCKS_PREVIEW_H_
#endif // FRONTEND_BLOCKS_PREVIEW_H_
+1 -1
View File
@@ -19,4 +19,4 @@ public:
}
};
#endif // FRONTEND_BLOCKS_GFX_CACHE_H_
#endif // FRONTEND_BLOCKS_GFX_CACHE_H_
+32 -26
View File
@@ -3,6 +3,7 @@
#include <glm/glm.hpp>
#include "BlocksPreview.h"
#include "LevelFrontend.h"
#include "../window/Events.h"
#include "../assets/Assets.h"
#include "../graphics/Shader.h"
@@ -15,44 +16,45 @@
InventoryView::InventoryView(
int columns,
Player* player,
const Assets* assets,
const ContentIndices* indices,
const ContentGfxCache* cache,
std::vector<blockid_t> blocks)
: player(player),
assets(assets),
indices(indices),
cache(cache),
LevelFrontend* frontend,
std::vector<blockid_t> blocks)
: indices(indices),
blocks(blocks),
invColumns(columns) {
blocksPreview = new BlocksPreview(assets->getShader("ui3d"),
assets->getAtlas("blocks"),
cache);
frontend(frontend),
columns(columns) {
}
InventoryView::~InventoryView() {
}
void InventoryView::setPosition(int x, int y) {
this->invX = x;
this->invY = y;
position.x = x;
position.y = y;
}
int InventoryView::getWidth() const {
return invColumns * iconSize + (invColumns-1) * interval + padX * 2;
return columns * iconSize + (columns-1) * interval + padding.x * 2;
}
int InventoryView::getHeight() const {
uint inv_rows = ceildiv(blocks.size(), invColumns);
return inv_rows * iconSize + (inv_rows-1) * interval + padY * 2;
uint inv_rows = ceildiv(blocks.size(), columns);
return inv_rows * iconSize + (inv_rows-1) * interval + padding.y * 2;
}
void InventoryView::setSlotConsumer(slotconsumer consumer) {
this->consumer = consumer;
}
void InventoryView::actAndDraw(const GfxContext* ctx) {
Assets* assets = frontend->getAssets();
Shader* uiShader = assets->getShader("ui");
auto viewport = ctx->getViewport();
uint inv_w = getWidth();
uint inv_h = getHeight();
int xs = invX + padX;
int ys = invY + padY;
int xs = position.x + padding.x;
int ys = position.y + padding.y;
glm::vec4 tint (1.0f);
int mx = Events::cursor.x;
@@ -62,15 +64,17 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
auto batch = ctx->getBatch2D();
batch->texture(nullptr);
batch->color = glm::vec4(0.0f, 0.0f, 0.0f, 0.5f);
batch->rect(invX, invY, inv_w, inv_h);
batch->rect(position.x, position.y, inv_w, inv_h);
batch->render();
// blocks & items
if (Events::scroll) {
inventoryScroll -= Events::scroll * (iconSize+interval);
scroll -= Events::scroll * (iconSize+interval);
}
inventoryScroll = std::min(inventoryScroll, int(inv_h-viewport.getHeight()));
inventoryScroll = std::max(inventoryScroll, 0);
scroll = std::min(scroll, int(inv_h-viewport.getHeight()));
scroll = std::max(scroll, 0);
auto blocksPreview = frontend->getBlocksPreview();
blocksPreview->begin(&ctx->getViewport());
{
Window::clearDepth();
@@ -80,8 +84,8 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
uint index = 0;
for (uint i = 0; i < blocks.size(); i++) {
Block* cblock = indices->getBlockDef(blocks[i]);
int x = xs + (iconSize+interval) * (index % invColumns);
int y = ys + (iconSize+interval) * (index / invColumns) - inventoryScroll;
int x = xs + (iconSize+interval) * (index % columns);
int y = ys + (iconSize+interval) * (index / columns) - scroll;
if (y < -int(iconSize+interval) || y >= int(viewport.getHeight())) {
index++;
continue;
@@ -91,7 +95,9 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
tint.g *= 1.2f;
tint.b *= 1.2f;
if (Events::jclicked(mousecode::BUTTON_1)) {
player->chosenBlock = blocks[i];
if (consumer) {
consumer(blocks[i]);
}
}
} else {
tint = glm::vec4(1.0f);
+18 -17
View File
@@ -2,44 +2,45 @@
#define FRONTEND_INVENTORY_VIEW_H_
#include <vector>
#include <functional>
#include <glm/glm.hpp>
#include "../typedefs.h"
class Player;
class Assets;
class GfxContext;
class ContentIndices;
class BlocksPreview;
class ContentGfxCache;
class LevelFrontend;
typedef std::function<void(blockid_t)> slotconsumer;
class InventoryView {
Player* player;
const Assets* assets;
const ContentIndices* indices;
const ContentGfxCache* const cache;
std::vector<blockid_t> blocks;
BlocksPreview* blocksPreview;
slotconsumer consumer = nullptr;
LevelFrontend* frontend;
int inventoryScroll = 0;
int invColumns;
int scroll = 0;
int columns;
uint iconSize = 48;
uint interval = 4;
int padX = interval;
int padY = interval;
int invX = 0;
int invY = 0;
glm::ivec2 padding {interval, interval};
glm::ivec2 position {0, 0};
public:
InventoryView(
int columns,
Player* player,
const Assets* assets,
const ContentIndices* indices,
const ContentGfxCache* cache,
LevelFrontend* frontend,
std::vector<blockid_t> blocks);
virtual ~InventoryView();
virtual void actAndDraw(const GfxContext* ctx);
void setPosition(int x, int y);
void actAndDraw(const GfxContext* ctx);
int getWidth() const;
int getHeight() const;
void setSlotConsumer(slotconsumer consumer);
};
#endif // FRONTEND_INVENTORY_VIEW_H_
+33
View File
@@ -0,0 +1,33 @@
#include "LevelFrontend.h"
#include "../world/Level.h"
#include "../assets/Assets.h"
#include "BlocksPreview.h"
#include "ContentGfxCache.h"
LevelFrontend::LevelFrontend(Level* level, Assets* assets)
: level(level),
assets(assets),
contentCache(std::make_unique<ContentGfxCache>(level->content, assets)),
blocksPreview(std::make_unique<BlocksPreview>(assets, contentCache.get())) {
}
LevelFrontend::~LevelFrontend() {
}
Level* LevelFrontend::getLevel() const {
return level;
}
Assets* LevelFrontend::getAssets() const {
return assets;
}
BlocksPreview* LevelFrontend::getBlocksPreview() const {
return blocksPreview.get();
}
ContentGfxCache* LevelFrontend::getContentGfxCache() const {
return contentCache.get();
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef FRONTEND_LEVEL_FRONTEND_H_
#define FRONTEND_LEVEL_FRONTEND_H_
#include <memory>
class Level;
class Assets;
class BlocksPreview;
class ContentGfxCache;
class LevelFrontend {
Level* level;
Assets* assets;
std::unique_ptr<ContentGfxCache> contentCache;
std::unique_ptr<BlocksPreview> blocksPreview;
public:
LevelFrontend(Level* level, Assets* assets);
~LevelFrontend();
Level* getLevel() const;
Assets* getAssets() const;
BlocksPreview* getBlocksPreview() const;
ContentGfxCache* getContentGfxCache() const;
};
#endif // FRONTEND_LEVEL_FRONTEND_H_
+6 -6
View File
@@ -27,7 +27,7 @@
#include "../maths/voxmaths.h"
#include "../settings.h"
#include "../engine.h"
#include "ContentGfxCache.h"
#include "LevelFrontend.h"
#include "graphics/Skybox.h"
using glm::vec3;
@@ -36,14 +36,14 @@ using glm::mat4;
using std::string;
using std::shared_ptr;
WorldRenderer::WorldRenderer(Engine* engine,
Level* level,
const ContentGfxCache* cache)
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend)
: engine(engine),
level(level),
level(frontend->getLevel()),
frustumCulling(new Frustum()),
lineBatch(new LineBatch()),
renderer( new ChunksRenderer(level, cache, engine->getSettings())) {
renderer(new ChunksRenderer(level,
frontend->getContentGfxCache(),
engine->getSettings())) {
auto& settings = engine->getSettings();
level->events->listen(EVT_CHUNK_HIDDEN,
+2 -2
View File
@@ -21,7 +21,7 @@ class Texture;
class Frustum;
class Engine;
class Chunks;
class ContentGfxCache;
class LevelFrontend;
class Skybox;
class WorldRenderer {
@@ -34,7 +34,7 @@ class WorldRenderer {
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);
public:
WorldRenderer(Engine* engine, Level* level, const ContentGfxCache* cache);
WorldRenderer(Engine* engine, LevelFrontend* frontend);
~WorldRenderer();
void draw(const GfxContext& context, Camera* camera);
+64 -68
View File
@@ -39,98 +39,66 @@
#include "WorldRenderer.h"
#include "BlocksPreview.h"
#include "InventoryView.h"
#include "LevelFrontend.h"
#include "../engine.h"
#include "../core_defs.h"
using std::wstring;
using std::shared_ptr;
using glm::vec2;
using glm::vec3;
using glm::vec4;
using namespace gui;
inline Label* create_label(gui::wstringsupplier supplier) {
Label* label = new Label(L"-");
inline std::shared_ptr<Label> create_label(gui::wstringsupplier supplier) {
auto label = std::make_shared<Label>(L"-");
label->textSupplier(supplier);
return label;
}
HudRenderer::HudRenderer(Engine* engine,
Level* level,
const ContentGfxCache* cache)
: level(level),
assets(engine->getAssets()),
gui(engine->getGUI()),
cache(cache) {
auto menu = gui->getMenu();
blocksPreview = new BlocksPreview(assets->getShader("ui3d"),
assets->getAtlas("blocks"),
cache);
auto content = level->content;
auto indices = content->indices;
std::vector<blockid_t> blocks;
for (blockid_t id = 1; id < indices->countBlockDefs(); id++) {
const Block* def = indices->getBlockDef(id);
if (def->hidden)
continue;
blocks.push_back(id);
}
contentAccess.reset(new InventoryView(
8,
level->player,
assets,
indices,
cache,
blocks));
void HudRenderer::createDebugPanel(Engine* engine) {
auto level = frontend->getLevel();
uicamera = new Camera(vec3(), 1);
uicamera->perspective = false;
uicamera->flipped = true;
Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f);
debugPanel = shared_ptr<UINode>(panel);
Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f);
debugPanel = std::shared_ptr<UINode>(panel);
panel->listenInterval(1.0f, [this]() {
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
fpsMin = fps;
fpsMax = fps;
});
panel->setCoord(vec2(10, 10));
panel->add(shared_ptr<Label>(create_label([this](){
return L"fps: "+this->fpsString;
})));
panel->add(shared_ptr<Label>(create_label([this](){
panel->add(create_label([this](){ return L"fps: "+this->fpsString;}));
panel->add(create_label([this](){
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
})));
panel->add(shared_ptr<Label>(create_label([=](){
}));
panel->add(create_label([=](){
auto& settings = engine->getSettings();
bool culling = settings.graphics.frustumCulling;
return L"frustum-culling: "+wstring(culling ? L"on" : L"off");
})));
panel->add(shared_ptr<Label>(create_label([this, level]() {
return L"chunks: "+std::to_wstring(this->level->chunks->chunksCount)+
return L"frustum-culling: "+std::wstring(culling ? L"on" : L"off");
}));
panel->add(create_label([=]() {
return L"chunks: "+std::to_wstring(level->chunks->chunksCount)+
L" visible: "+std::to_wstring(level->chunks->visible);
})));
panel->add(shared_ptr<Label>(create_label([this](){
auto player = this->level->player;
auto indices = this->level->content->indices;
}));
panel->add(create_label([=](){
auto player = level->player;
auto indices = level->content->indices;
auto def = indices->getBlockDef(player->selectedVoxel.id);
std::wstringstream stream;
stream << std::hex << this->level->player->selectedVoxel.states;
stream << std::hex << level->player->selectedVoxel.states;
if (def) {
stream << L" (" << util::str2wstr_utf8(def->name) << L")";
}
return L"block: "+std::to_wstring(player->selectedVoxel.id)+
L" "+stream.str();
})));
panel->add(shared_ptr<Label>(create_label([this](){
return L"seed: "+std::to_wstring(this->level->world->seed);
})));
}));
panel->add(create_label([=](){
return L"seed: "+std::to_wstring(level->world->seed);
}));
for (int ax = 0; ax < 3; ax++){
Panel* sub = new Panel(vec2(10, 27), vec4(0.0f));
sub->orientation(Orientation::horizontal);
wstring str = L"x: ";
std::wstring str = L"x: ";
str[0] += ax;
Label* label = new Label(str);
label->margin(vec4(2, 3, 2, 3));
@@ -139,15 +107,15 @@ HudRenderer::HudRenderer(Engine* engine,
// Coord input
TextBox* box = new TextBox(L"");
box->textSupplier([this, ax]() {
Hitbox* hitbox = this->level->player->hitbox;
box->textSupplier([=]() {
Hitbox* hitbox = level->player->hitbox;
return std::to_wstring(hitbox->position[ax]);
});
box->textConsumer([this, ax](wstring text) {
box->textConsumer([=](std::wstring text) {
try {
vec3 position = this->level->player->hitbox->position;
vec3 position = level->player->hitbox->position;
position[ax] = std::stoi(text);
this->level->player->teleport(position);
level->player->teleport(position);
} catch (std::invalid_argument& _){
}
});
@@ -155,15 +123,15 @@ HudRenderer::HudRenderer(Engine* engine,
sub->add(box);
panel->add(sub);
}
panel->add(shared_ptr<Label>(create_label([this](){
panel->add(create_label([=](){
int hour, minute, second;
timeutil::from_value(this->level->world->daytime, hour, minute, second);
timeutil::from_value(level->world->daytime, hour, minute, second);
std::wstring timeString =
util::lfill(std::to_wstring(hour), 2, L'0') + L":" +
util::lfill(std::to_wstring(minute), 2, L'0');
return L"time: "+timeString;
})));
}));
{
TrackBar* bar = new TrackBar(0.0f, 1.0f, 1.0f, 0.005f, 8);
bar->supplier([=]() {
@@ -203,6 +171,34 @@ HudRenderer::HudRenderer(Engine* engine,
panel->add(checkpanel);
}
panel->refresh();
}
HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
: assets(engine->getAssets()),
gui(engine->getGUI()),
frontend(frontend) {
auto level = frontend->getLevel();
auto menu = gui->getMenu();
auto content = level->content;
auto indices = content->indices;
std::vector<blockid_t> blocks;
for (blockid_t id = 1; id < indices->countBlockDefs(); id++) {
const Block* def = indices->getBlockDef(id);
if (def->hidden)
continue;
blocks.push_back(id);
}
contentAccess.reset(new InventoryView(8, indices, frontend, blocks));
contentAccess->setSlotConsumer([=](blockid_t id) {
level->player->chosenBlock = id;
});
uicamera = new Camera(vec3(), 1);
uicamera->perspective = false;
uicamera->flipped = true;
createDebugPanel(engine);
menu->reset();
gui->add(this->debugPanel);
@@ -210,7 +206,6 @@ HudRenderer::HudRenderer(Engine* engine,
HudRenderer::~HudRenderer() {
gui->remove(debugPanel);
delete blocksPreview;
delete uicamera;
}
@@ -247,6 +242,7 @@ void HudRenderer::update() {
}
void HudRenderer::draw(const GfxContext& ctx){
auto level = frontend->getLevel();
const Content* content = level->content;
const ContentIndices* contentIds = content->indices;
@@ -274,14 +270,14 @@ void HudRenderer::draw(const GfxContext& ctx){
batch->line(width/2-5, height/2-5, width/2+5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
batch->line(width/2+5, height/2-5, width/2-5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
}
Player* player = level->player;
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.75f);
batch->rect(width - 68, height - 68, 68, 68);
batch->color = vec4(1.0f);
batch->render();
auto blocksPreview = frontend->getBlocksPreview();
blocksPreview->begin(&ctx.getViewport());
{
Window::clearDepth();
+5 -8
View File
@@ -15,9 +15,8 @@ class Assets;
class Player;
class Level;
class Engine;
class ContentGfxCache;
class BlocksPreview;
class InventoryView;
class LevelFrontend;
namespace gui {
class GUI;
@@ -25,10 +24,8 @@ namespace gui {
}
class HudRenderer {
Level* level;
Assets* assets;
Camera* uicamera;
BlocksPreview* blocksPreview;
int fps = 60;
int fpsMin = 60;
@@ -40,11 +37,11 @@ class HudRenderer {
std::unique_ptr<InventoryView> contentAccess;
std::shared_ptr<gui::UINode> debugPanel;
gui::GUI* gui;
const ContentGfxCache* const cache;
LevelFrontend* frontend;
void createDebugPanel(Engine* engine);
public:
HudRenderer(Engine* engine,
Level* level,
const ContentGfxCache* cache);
HudRenderer(Engine* engine, LevelFrontend* frontend);
~HudRenderer();
void update();
+9 -15
View File
@@ -28,6 +28,7 @@
#include "WorldRenderer.h"
#include "hud.h"
#include "ContentGfxCache.h"
#include "LevelFrontend.h"
#include "gui/GUI.h"
#include "gui/panels.h"
#include "menu.h"
@@ -47,13 +48,12 @@ MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
menu->reset();
menu->set("main");
uicamera = new Camera(glm::vec3(), Window::height);
uicamera.reset(new Camera(glm::vec3(), Window::height));
uicamera->perspective = false;
uicamera->flipped = true;
}
MenuScreen::~MenuScreen() {
delete uicamera;
}
void MenuScreen::update(float delta) {
@@ -84,26 +84,20 @@ static bool backlight;
LevelScreen::LevelScreen(Engine* engine, Level* level)
: Screen(engine),
level(level) {
level(level),
frontend(std::make_unique<LevelFrontend>(level, engine->getAssets())),
hud(std::make_unique<HudRenderer>(engine, frontend.get())),
worldRenderer(std::make_unique<WorldRenderer>(engine, frontend.get())),
controller(std::make_unique<LevelController>(engine->getSettings(), level)) {
auto& settings = engine->getSettings();
controller = new LevelController(settings, level);
cache = new ContentGfxCache(level->content, engine->getAssets());
worldRenderer = new WorldRenderer(engine, level, cache);
hud = new HudRenderer(engine, level, cache);
backlight = settings.graphics.backlight;
}
LevelScreen::~LevelScreen() {
delete controller;
delete hud;
delete worldRenderer;
delete cache;
std::cout << "-- writing world" << std::endl;
World* world = level->world;
world->write(level);
delete level;
world->write(level.get());
delete world;
}
+8 -8
View File
@@ -11,7 +11,7 @@ class HudRenderer;
class Engine;
class Camera;
class Batch2D;
class ContentGfxCache;
class LevelFrontend;
class LevelController;
/* Screen is a mainloop state */
@@ -27,7 +27,7 @@ public:
};
class MenuScreen : public Screen {
Camera* uicamera;
std::unique_ptr<Camera> uicamera;
public:
MenuScreen(Engine* engine);
~MenuScreen();
@@ -37,11 +37,11 @@ public:
};
class LevelScreen : public Screen {
Level* level;
LevelController* controller;
WorldRenderer* worldRenderer;
HudRenderer* hud;
ContentGfxCache* cache;
std::unique_ptr<Level> level;
std::unique_ptr<LevelFrontend> frontend;
std::unique_ptr<HudRenderer> hud;
std::unique_ptr<WorldRenderer> worldRenderer;
std::unique_ptr<LevelController> controller;
bool hudVisible = true;
void updateHotkeys();
@@ -53,4 +53,4 @@ public:
void draw(float delta) override;
};
#endif // FRONTEND_SCREENS_H_
#endif // FRONTEND_SCREENS_H_