Added inventory scroll + minor refactor

This commit is contained in:
MihailRis 2024-01-04 16:34:52 +03:00
parent ee691f8c3f
commit 374e5654ad
6 changed files with 34 additions and 31 deletions

View File

@ -4,8 +4,6 @@
#include <limits> #include <limits>
#include <stdexcept> #include <stdexcept>
using std::string;
void BinaryWriter::put(ubyte b) { void BinaryWriter::put(ubyte b) {
buffer.push_back(b); buffer.push_back(b);
} }
@ -18,7 +16,7 @@ void BinaryWriter::putCStr(const char* str) {
} }
} }
void BinaryWriter::put(const string& s) { void BinaryWriter::put(const std::string& s) {
size_t len = s.length(); size_t len = s.length();
if (len > INT16_MAX) { if (len > INT16_MAX) {
throw std::domain_error("length > INT16_MAX"); throw std::domain_error("length > INT16_MAX");
@ -27,7 +25,7 @@ void BinaryWriter::put(const string& s) {
put((const ubyte*)s.data(), len); put((const ubyte*)s.data(), len);
} }
void BinaryWriter::putShortStr(const string& s) { void BinaryWriter::putShortStr(const std::string& s) {
size_t len = s.length(); size_t len = s.length();
if (len > 255) { if (len > 255) {
throw std::domain_error("length > 255"); throw std::domain_error("length > 255");
@ -144,22 +142,22 @@ float BinaryReader::getFloat32() {
return value.valfloat; return value.valfloat;
} }
string BinaryReader::getString() { std::string BinaryReader::getString() {
uint16_t length = (uint16_t)getInt16(); uint16_t length = (uint16_t)getInt16();
if (pos+length > size) { if (pos+length > size) {
throw std::underflow_error("unexpected end"); throw std::underflow_error("unexpected end");
} }
pos += length; pos += length;
return string((const char*)(data+pos-length), length); return std::string((const char*)(data+pos-length), length);
} }
string BinaryReader::getShortString() { std::string BinaryReader::getShortString() {
ubyte length = get(); ubyte length = get();
if (pos+length > size) { if (pos+length > size) {
throw std::underflow_error("unexpected end"); throw std::underflow_error("unexpected end");
} }
pos += length; pos += length;
return string((const char*)(data+pos-length), length); return std::string((const char*)(data+pos-length), length);
} }
bool BinaryReader::hasNext() const { bool BinaryReader::hasNext() const {

View File

@ -9,8 +9,6 @@
#include "../coders/toml.h" #include "../coders/toml.h"
#include "../coders/json.h" #include "../coders/json.h"
using std::string;
toml::Wrapper* create_wrapper(EngineSettings& settings) { toml::Wrapper* create_wrapper(EngineSettings& settings) {
std::unique_ptr<toml::Wrapper> wrapper (new toml::Wrapper()); std::unique_ptr<toml::Wrapper> wrapper (new toml::Wrapper());
toml::Section& display = wrapper->add("display"); toml::Section& display = wrapper->add("display");
@ -47,7 +45,7 @@ toml::Wrapper* create_wrapper(EngineSettings& settings) {
return wrapper.release(); return wrapper.release();
} }
string write_controls() { std::string write_controls() {
json::JObject* obj = new json::JObject(); json::JObject* obj = new json::JObject();
for (auto& entry : Events::bindings) { for (auto& entry : Events::bindings) {
const auto& binding = entry.second; const auto& binding = entry.second;
@ -64,7 +62,7 @@ string write_controls() {
return json::stringify(obj, true, " "); return json::stringify(obj, true, " ");
} }
void load_controls(string filename, string source) { void load_controls(std::string filename, std::string source) {
json::JObject* obj = json::parse(filename, source); json::JObject* obj = json::parse(filename, source);
for (auto& entry : Events::bindings) { for (auto& entry : Events::bindings) {
auto& binding = entry.second; auto& binding = entry.second;
@ -73,7 +71,7 @@ void load_controls(string filename, string source) {
if (jentry == nullptr) if (jentry == nullptr)
continue; continue;
inputtype type; inputtype type;
string typestr; std::string typestr;
jentry->str("type", typestr); jentry->str("type", typestr);
if (typestr == "keyboard") { if (typestr == "keyboard") {

View File

@ -11,9 +11,6 @@
#include "../voxels/Block.h" #include "../voxels/Block.h"
#include "ContentGfxCache.h" #include "ContentGfxCache.h"
using glm::vec4;
using glm::vec3;
BlocksPreview::BlocksPreview(Shader* shader, BlocksPreview::BlocksPreview(Shader* shader,
Atlas* atlas, Atlas* atlas,
const ContentGfxCache* cache) const ContentGfxCache* cache)
@ -31,19 +28,21 @@ void BlocksPreview::begin(const Viewport* viewport) {
glm::ortho(0.0f, float(viewport->getWidth()), glm::ortho(0.0f, float(viewport->getWidth()),
0.0f, float(viewport->getHeight()), 0.0f, float(viewport->getHeight()),
-1000.0f, 1000.0f) * -1000.0f, 1000.0f) *
glm::lookAt(vec3(2, 2, 2), vec3(0.0f), vec3(0, 1, 0))); glm::lookAt(glm::vec3(2, 2, 2), glm::vec3(0.0f), glm::vec3(0, 1, 0)));
atlas->getTexture()->bind(); atlas->getTexture()->bind();
} }
/* Draw one block preview at given screen position */ /* Draw one block preview at given screen position */
void BlocksPreview::draw(const Block* def, int x, int y, int size, vec4 tint) { void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tint) {
uint width = viewport->getWidth(); uint width = viewport->getWidth();
uint height = viewport->getHeight(); uint height = viewport->getHeight();
y = height - y - 1; y = height - y - 1;
x += 2; x += 2;
y -= 35; y -= 35;
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), vec3(x/float(width) * 2, y/float(height) * 2, 0.0f)));
glm::vec3 offset (x/float(width) * 2, y/float(height) * 2, 0.0f);
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
blockid_t id = def->rt.id; blockid_t id = def->rt.id;
const UVRegion texfaces[6]{ cache->getRegion(id, 0), cache->getRegion(id, 1), const UVRegion texfaces[6]{ cache->getRegion(id, 0), cache->getRegion(id, 1),
cache->getRegion(id, 2), cache->getRegion(id, 3), cache->getRegion(id, 2), cache->getRegion(id, 3),
@ -54,15 +53,20 @@ void BlocksPreview::draw(const Block* def, int x, int y, int size, vec4 tint) {
// something went wrong... // something went wrong...
break; break;
case BlockModel::block: case BlockModel::block:
batch->blockCube(vec3(size * 0.63f), texfaces, tint, !def->rt.emissive); batch->blockCube(glm::vec3(size * 0.63f), texfaces, tint, !def->rt.emissive);
break; break;
case BlockModel::aabb: case BlockModel::aabb:
batch->blockCube(def->hitbox.size() * vec3(size * 0.63f), texfaces, tint, !def->rt.emissive); batch->blockCube(def->hitbox.size() * glm::vec3(size * 0.63f),
texfaces, tint, !def->rt.emissive);
break; break;
case BlockModel::xsprite: { case BlockModel::xsprite: {
//batch->xSprite(size, size, texfaces[0], tint, !def->rt.emissive); glm::vec3 right = glm::normalize(glm::vec3(1.f, 0.f, -1.f));
vec3 right = glm::normalize(vec3(1.f, 0.f, -1.f)); batch->sprite(right*float(size)*0.43f+glm::vec3(0, size*0.4f, 0),
batch->sprite(right*float(size)*0.43f+vec3(0, size*0.4f, 0), vec3(0.f, 1.f, 0.f), right, size*0.5f, size*0.6f, texfaces[0], tint); glm::vec3(0.f, 1.f, 0.f),
right,
size*0.5f, size*0.6f,
texfaces[0],
tint);
break; break;
} }
} }

View File

@ -7,8 +7,6 @@
#include "../graphics/Atlas.h" #include "../graphics/Atlas.h"
#include "../voxels/Block.h" #include "../voxels/Block.h"
using std::string;
ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) { ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) {
const ContentIndices* contentIds = content->indices; const ContentIndices* contentIds = content->indices;
sideregions = new UVRegion[contentIds->countBlockDefs() * 6]; sideregions = new UVRegion[contentIds->countBlockDefs() * 6];
@ -17,7 +15,7 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) {
for (uint i = 0; i < contentIds->countBlockDefs(); i++) { for (uint i = 0; i < contentIds->countBlockDefs(); i++) {
Block* def = contentIds->getBlockDef(i); Block* def = contentIds->getBlockDef(i);
for (uint side = 0; side < 6; side++) { for (uint side = 0; side < 6; side++) {
string tex = def->textureFaces[side]; std::string tex = def->textureFaces[side];
if (atlas->has(tex)) { if (atlas->has(tex)) {
sideregions[i * 6 + side] = atlas->get(tex); sideregions[i * 6 + side] = atlas->get(tex);
} else { } else {
@ -28,7 +26,6 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) {
} }
} }
ContentGfxCache::~ContentGfxCache() ContentGfxCache::~ContentGfxCache() {
{
delete[] sideregions; delete[] sideregions;
} }

View File

@ -239,6 +239,11 @@ void HudRenderer::drawContentAccess(const GfxContext& ctx, Player* player) {
batch->render(); batch->render();
// blocks & items // blocks & items
if (Events::scroll) {
inventoryScroll -= Events::scroll * (icon_size+interval);
}
inventoryScroll = std::min(inventoryScroll, int(inv_h-viewport.getHeight()));
inventoryScroll = std::max(inventoryScroll, 0);
blocksPreview->begin(&ctx.getViewport()); blocksPreview->begin(&ctx.getViewport());
{ {
Window::clearDepth(); Window::clearDepth();
@ -253,7 +258,7 @@ void HudRenderer::drawContentAccess(const GfxContext& ctx, Player* player) {
if (cblock->hidden) if (cblock->hidden)
continue; continue;
int x = xs + (icon_size+interval) * (index % inv_cols); int x = xs + (icon_size+interval) * (index % inv_cols);
int y = ys + (icon_size+interval) * (index / inv_cols); int y = ys + (icon_size+interval) * (index / inv_cols) - inventoryScroll;
if (mx > x && mx < x + (int)icon_size && my > y && my < y + (int)icon_size) { if (mx > x && mx < x + (int)icon_size && my > y && my < y + (int)icon_size) {
tint.r *= 1.2f; tint.r *= 1.2f;
tint.g *= 1.2f; tint.g *= 1.2f;

View File

@ -37,6 +37,7 @@ class HudRenderer {
int fpsMax = 60; int fpsMax = 60;
std::wstring fpsString; std::wstring fpsString;
bool inventoryOpen = false; bool inventoryOpen = false;
int inventoryScroll = 0;
bool pause = false; bool pause = false;
std::shared_ptr<gui::UINode> debugPanel; std::shared_ptr<gui::UINode> debugPanel;