Merge branch 'main' of https://github.com/MihailRis/VoxelEngine-Cpp
This commit is contained in:
commit
6fba4a7065
51
res/scripts/stdlib.lua
Normal file
51
res/scripts/stdlib.lua
Normal file
@ -0,0 +1,51 @@
|
||||
-- kit of standard functions
|
||||
|
||||
-- Check if given table is an array
|
||||
function is_array(x)
|
||||
if #t > 0 then
|
||||
return true
|
||||
end
|
||||
for k, v in pairs(x) do
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local __cached_scripts = {}
|
||||
local __cached_results = {}
|
||||
|
||||
-- Get entry-point and filename from `entry-point:filename` path
|
||||
function parse_path(path)
|
||||
local index = string.find(path, ':')
|
||||
if index == nil then
|
||||
error("invalid path syntax (':' missing)")
|
||||
end
|
||||
return string.sub(path, 1, index-1), string.sub(path, index+1, -1)
|
||||
end
|
||||
|
||||
-- Load script with caching
|
||||
--
|
||||
-- path - script path `contentpack:filename`.
|
||||
-- Example `base:scripts/tests.lua`
|
||||
--
|
||||
-- nocache - ignore cached script, load anyway
|
||||
function load_script(path, nocache)
|
||||
local packname, filename = parse_path(path)
|
||||
local packpath = pack.get_folder(packname)
|
||||
local fullpath = packpath..filename
|
||||
|
||||
-- __cached_scripts used in condition because cached result may be nil
|
||||
if not nocache and __cached_scripts[fullpath] ~= nil then
|
||||
return __cached_results[fullpath]
|
||||
end
|
||||
local script = loadfile(fullpath)
|
||||
if script == nil then
|
||||
error("script '"..filename.."' not found in '"..packname.."'")
|
||||
end
|
||||
local result = script()
|
||||
if not nocache then
|
||||
__cached_scripts[fullpath] = script
|
||||
__cached_results[fullpath] = result
|
||||
end
|
||||
return result
|
||||
end
|
||||
@ -17,6 +17,8 @@ const int CHUNK_D = 16;
|
||||
const uint VOXEL_USER_BITS = 8;
|
||||
constexpr uint VOXEL_USER_BITS_OFFSET = sizeof(blockstate_t)*8-VOXEL_USER_BITS;
|
||||
|
||||
const int ITEM_ICON_SIZE = 48;
|
||||
|
||||
/* Chunk volume (count of voxels per Chunk) */
|
||||
constexpr int CHUNK_VOL = (CHUNK_W * CHUNK_H * CHUNK_D);
|
||||
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
#include "../coders/json.h"
|
||||
#include "../constants.h"
|
||||
#include "../items/ItemDef.h"
|
||||
#include "../items/Inventory.h"
|
||||
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
@ -488,12 +489,13 @@ void WorldFiles::writeWorldInfo(const World* world) {
|
||||
versionobj.put("major", ENGINE_VERSION_MAJOR);
|
||||
versionobj.put("minor", ENGINE_VERSION_MINOR);
|
||||
|
||||
root.put("name", world->name);
|
||||
root.put("seed", world->seed);
|
||||
root.put("name", world->getName());
|
||||
root.put("seed", world->getSeed());
|
||||
|
||||
auto& timeobj = root.putMap("time");
|
||||
timeobj.put("day-time", world->daytime);
|
||||
timeobj.put("day-time-speed", world->daytimeSpeed);
|
||||
timeobj.put("total-time", world->totalTime);
|
||||
|
||||
files::write_json(getWorldFile(), &root);
|
||||
}
|
||||
@ -506,8 +508,9 @@ bool WorldFiles::readWorldInfo(World* world) {
|
||||
}
|
||||
|
||||
auto root = files::read_json(file);
|
||||
root->str("name", world->name);
|
||||
root->num("seed", world->seed);
|
||||
|
||||
world->setName(root->getStr("name", world->getName()));
|
||||
world->setSeed(root->getInt("seed", world->getSeed()));
|
||||
|
||||
auto verobj = root->map("version");
|
||||
if (verobj) {
|
||||
@ -521,12 +524,13 @@ bool WorldFiles::readWorldInfo(World* world) {
|
||||
if (timeobj) {
|
||||
timeobj->num("day-time", world->daytime);
|
||||
timeobj->num("day-time-speed", world->daytimeSpeed);
|
||||
timeobj->num("total-time", world->totalTime);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WorldFiles::writePlayer(Player* player){
|
||||
void WorldFiles::writePlayer(Player* player) {
|
||||
glm::vec3 position = player->hitbox->position;
|
||||
dynamic::Map root;
|
||||
auto& posarr = root.putList("position");
|
||||
@ -540,6 +544,8 @@ void WorldFiles::writePlayer(Player* player){
|
||||
|
||||
root.put("flight", player->flight);
|
||||
root.put("noclip", player->noclip);
|
||||
root.put("chosen-slot", player->getChosenSlot());
|
||||
root.put("inventory", player->getInventory()->write().release());
|
||||
|
||||
files::write_json(getPlayerFile(), &root);
|
||||
}
|
||||
@ -565,5 +571,11 @@ bool WorldFiles::readPlayer(Player* player) {
|
||||
|
||||
root->flag("flight", player->flight);
|
||||
root->flag("noclip", player->noclip);
|
||||
player->setChosenSlot(root->getInt("chosen-slot", player->getChosenSlot()));
|
||||
|
||||
auto invmap = root->map("inventory");
|
||||
if (invmap) {
|
||||
player->getInventory()->read(invmap);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdexcept>
|
||||
#include "../coders/json.h"
|
||||
#include "../coders/gzip.h"
|
||||
#include "../util/stringutil.h"
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
@ -32,21 +33,21 @@ void files::rafile::read(char* buffer, std::streamsize size) {
|
||||
file.read(buffer, size);
|
||||
}
|
||||
|
||||
bool files::write_bytes(fs::path filename, const char* data, size_t size) {
|
||||
bool files::write_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||
std::ofstream output(filename, std::ios::binary);
|
||||
if (!output.is_open())
|
||||
return false;
|
||||
output.write(data, size);
|
||||
output.write((const char*)data, size);
|
||||
output.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
uint files::append_bytes(fs::path filename, const char* data, size_t size) {
|
||||
uint files::append_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||
std::ofstream output(filename, std::ios::binary | std::ios::app);
|
||||
if (!output.is_open())
|
||||
return 0;
|
||||
uint position = output.tellp();
|
||||
output.write(data, size);
|
||||
output.write((const char*)data, size);
|
||||
output.close();
|
||||
return position;
|
||||
}
|
||||
@ -60,7 +61,7 @@ bool files::read(fs::path filename, char* data, size_t size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
char* files::read_bytes(fs::path filename, size_t& length) {
|
||||
ubyte* files::read_bytes(fs::path filename, size_t& length) {
|
||||
std::ifstream input(filename, std::ios::binary);
|
||||
if (!input.is_open())
|
||||
return nullptr;
|
||||
@ -71,17 +72,17 @@ char* files::read_bytes(fs::path filename, size_t& length) {
|
||||
std::unique_ptr<char> data(new char[length]);
|
||||
input.read(data.get(), length);
|
||||
input.close();
|
||||
return data.release();
|
||||
return (ubyte*)data.release();
|
||||
}
|
||||
|
||||
std::string files::read_string(fs::path filename) {
|
||||
size_t size;
|
||||
std::unique_ptr<char> chars (read_bytes(filename, size));
|
||||
if (chars == nullptr) {
|
||||
std::unique_ptr<ubyte[]> bytes (read_bytes(filename, size));
|
||||
if (bytes == nullptr) {
|
||||
throw std::runtime_error("could not to load file '"+
|
||||
filename.string()+"'");
|
||||
}
|
||||
return std::string(chars.get(), size);
|
||||
return std::string((const char*)bytes.get(), size);
|
||||
}
|
||||
|
||||
bool files::write_string(fs::path filename, const std::string content) {
|
||||
@ -94,27 +95,21 @@ bool files::write_string(fs::path filename, const std::string content) {
|
||||
}
|
||||
|
||||
bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) {
|
||||
// -- binary json tests
|
||||
//return write_binary_json(fs::path(filename.u8string()+".bin"), obj);
|
||||
return files::write_string(filename, json::stringify(obj, nice, " "));
|
||||
}
|
||||
|
||||
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj) {
|
||||
std::vector<ubyte> bytes = json::to_binary(obj);
|
||||
return files::write_bytes(filename, (const char*)bytes.data(), bytes.size());
|
||||
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj, bool compression) {
|
||||
auto bytes = json::to_binary(obj);
|
||||
if (compression) {
|
||||
bytes = gzip::compress(bytes.data(), bytes.size());
|
||||
}
|
||||
return files::write_bytes(filename, bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
std::unique_ptr<dynamic::Map> files::read_json(fs::path filename) {
|
||||
// binary json tests
|
||||
// fs::path binfile = fs::path(filename.u8string()+".bin");
|
||||
// if (fs::is_regular_file(binfile)){
|
||||
// return read_binary_json(binfile);
|
||||
// }
|
||||
|
||||
std::string text = files::read_string(filename);
|
||||
try {
|
||||
auto obj = json::parse(filename.string(), text);
|
||||
//write_binary_json(binfile, obj);
|
||||
return obj;
|
||||
} catch (const parsing_error& error) {
|
||||
std::cerr << error.errorLog() << std::endl;
|
||||
@ -124,8 +119,10 @@ std::unique_ptr<dynamic::Map> files::read_json(fs::path filename) {
|
||||
|
||||
std::unique_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
|
||||
size_t size;
|
||||
std::unique_ptr<char[]> bytes (files::read_bytes(file, size));
|
||||
return std::unique_ptr<dynamic::Map>(json::from_binary((const ubyte*)bytes.get(), size));
|
||||
std::unique_ptr<ubyte[]> bytes (files::read_bytes(file, size));
|
||||
return std::unique_ptr<dynamic::Map>(
|
||||
json::from_binary(bytes.get(), size)
|
||||
);
|
||||
}
|
||||
|
||||
std::vector<std::string> files::read_list(fs::path filename) {
|
||||
|
||||
@ -27,15 +27,33 @@ namespace files {
|
||||
size_t length() const;
|
||||
};
|
||||
|
||||
/* Write bytes array to the file without any extra data */
|
||||
extern bool write_bytes(fs::path, const ubyte* data, size_t size);
|
||||
|
||||
extern bool write_bytes(fs::path, const char* data, size_t size);
|
||||
extern uint append_bytes(fs::path, const char* data, size_t size);
|
||||
/* Append bytes array to the file without any extra data */
|
||||
extern uint append_bytes(fs::path, const ubyte* data, size_t size);
|
||||
|
||||
/* Write string to the file */
|
||||
extern bool write_string(fs::path filename, const std::string content);
|
||||
extern bool write_json(fs::path filename, const dynamic::Map* obj, bool nice=true);
|
||||
extern bool write_binary_json(fs::path filename, const dynamic::Map* obj);
|
||||
|
||||
/* Write dynamic data to the JSON file
|
||||
@param nice if true,
|
||||
human readable format will be used, otherwise minimal */
|
||||
extern bool write_json(
|
||||
fs::path filename,
|
||||
const dynamic::Map* obj,
|
||||
bool nice=true);
|
||||
|
||||
/* Write dynamic data to the binary JSON file
|
||||
(see src/coders/binary_json_spec.md)
|
||||
@param compressed use gzip compression */
|
||||
extern bool write_binary_json(
|
||||
fs::path filename,
|
||||
const dynamic::Map* obj,
|
||||
bool compressed=false);
|
||||
|
||||
extern bool read(fs::path, char* data, size_t size);
|
||||
extern char* read_bytes(fs::path, size_t& length);
|
||||
extern ubyte* read_bytes(fs::path, size_t& length);
|
||||
extern std::string read_string(fs::path filename);
|
||||
extern std::unique_ptr<dynamic::Map> read_json(fs::path file);
|
||||
extern std::unique_ptr<dynamic::Map> read_binary_json(fs::path file);
|
||||
|
||||
@ -8,47 +8,23 @@
|
||||
#include "../graphics/Texture.h"
|
||||
#include "../graphics/Atlas.h"
|
||||
#include "../graphics/Batch3D.h"
|
||||
#include "../graphics/Framebuffer.h"
|
||||
#include "../graphics/GfxContext.h"
|
||||
#include "../window/Window.h"
|
||||
#include "../window/Camera.h"
|
||||
#include "../voxels/Block.h"
|
||||
#include "../content/Content.h"
|
||||
#include "../constants.h"
|
||||
#include "ContentGfxCache.h"
|
||||
|
||||
BlocksPreview::BlocksPreview(Assets* assets, const ContentGfxCache* cache)
|
||||
: shader(assets->getShader("ui3d")),
|
||||
atlas(assets->getAtlas("blocks")),
|
||||
cache(cache) {
|
||||
batch = std::make_unique<Batch3D>(1024);
|
||||
}
|
||||
|
||||
BlocksPreview::~BlocksPreview() {
|
||||
}
|
||||
|
||||
void BlocksPreview::begin(const Viewport* viewport) {
|
||||
this->viewport = viewport;
|
||||
shader->use();
|
||||
shader->uniformMatrix("u_projview",
|
||||
glm::ortho(0.0f, float(viewport->getWidth()),
|
||||
0.0f, float(viewport->getHeight()),
|
||||
-100.0f, 100.0f) *
|
||||
glm::lookAt(glm::vec3(2, 2, 2), glm::vec3(0.0f), glm::vec3(0, 1, 0)));
|
||||
atlas->getTexture()->bind();
|
||||
}
|
||||
|
||||
/* Draw one block preview at given screen position */
|
||||
void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tint) {
|
||||
uint width = viewport->getWidth();
|
||||
uint height = viewport->getHeight();
|
||||
|
||||
y = height - y - 1 - 35 /* magic garbage */;
|
||||
x += 2;
|
||||
|
||||
if (def->model == BlockModel::aabb) {
|
||||
x += (1.0f - def->hitbox.size()).x * size * 0.5f;
|
||||
y += (1.0f - def->hitbox.size()).y * size * 0.25f;
|
||||
}
|
||||
|
||||
glm::vec3 offset (x/float(width) * 2, y/float(height) * 2, 0.0f);
|
||||
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
|
||||
|
||||
ImageData* BlocksPreview::draw(
|
||||
const ContentGfxCache* cache,
|
||||
Framebuffer* fbo,
|
||||
Batch3D* batch,
|
||||
const Block* def,
|
||||
int size
|
||||
){
|
||||
Window::clear();
|
||||
blockid_t id = def->rt.id;
|
||||
const UVRegion texfaces[6]{cache->getRegion(id, 0), cache->getRegion(id, 1),
|
||||
cache->getRegion(id, 2), cache->getRegion(id, 3),
|
||||
@ -59,11 +35,12 @@ void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tin
|
||||
// something went wrong...
|
||||
break;
|
||||
case BlockModel::block:
|
||||
batch->blockCube(glm::vec3(size * 0.63f), texfaces, tint, !def->rt.emissive);
|
||||
batch->blockCube(glm::vec3(size * 0.63f), texfaces,
|
||||
glm::vec4(1.0f), !def->rt.emissive);
|
||||
break;
|
||||
case BlockModel::aabb:
|
||||
batch->blockCube(def->hitbox.size() * glm::vec3(size * 0.63f),
|
||||
texfaces, tint, !def->rt.emissive);
|
||||
texfaces, glm::vec4(1.0f), !def->rt.emissive);
|
||||
break;
|
||||
case BlockModel::custom:
|
||||
case BlockModel::xsprite: {
|
||||
@ -73,10 +50,64 @@ void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tin
|
||||
right,
|
||||
size*0.5f, size*0.6f,
|
||||
texfaces[0],
|
||||
tint);
|
||||
glm::vec4(1.0f));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
batch->flush();
|
||||
return fbo->texture->readData();
|
||||
}
|
||||
|
||||
std::unique_ptr<Atlas> BlocksPreview::build(
|
||||
const ContentGfxCache* cache,
|
||||
Assets* assets,
|
||||
const Content* content
|
||||
) {
|
||||
auto indices = content->getIndices();
|
||||
size_t count = indices->countBlockDefs();
|
||||
size_t iconSize = ITEM_ICON_SIZE;
|
||||
|
||||
Shader* shader = assets->getShader("ui3d");
|
||||
Atlas* atlas = assets->getAtlas("blocks");
|
||||
|
||||
Viewport viewport(iconSize, iconSize);
|
||||
GfxContext pctx(nullptr, viewport, nullptr);
|
||||
GfxContext ctx = pctx.sub();
|
||||
ctx.cullFace(true);
|
||||
ctx.depthTest(true);
|
||||
|
||||
Framebuffer fbo(iconSize, iconSize, true);
|
||||
Batch3D batch(1024);
|
||||
batch.begin();
|
||||
|
||||
shader->use();
|
||||
shader->uniformMatrix("u_projview",
|
||||
|
||||
glm::ortho(0.0f, float(iconSize), 0.0f, float(iconSize),
|
||||
-100.0f, 100.0f) *
|
||||
glm::lookAt(glm::vec3(2, 2, 2),
|
||||
glm::vec3(0.0f),
|
||||
glm::vec3(0, 1, 0)));
|
||||
|
||||
AtlasBuilder builder;
|
||||
Window::viewport(0, 0, iconSize, iconSize);
|
||||
Window::setBgColor(glm::vec4(0.0f));
|
||||
|
||||
fbo.bind();
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
auto def = indices->getBlockDef(i);
|
||||
|
||||
glm::vec3 offset(0.1f, 0.5f, 0.1f);
|
||||
if (def->model == BlockModel::aabb) {
|
||||
offset.y += (1.0f - def->hitbox.size()).y * 0.5f;
|
||||
}
|
||||
atlas->getTexture()->bind();
|
||||
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
|
||||
|
||||
builder.add(def->name, draw(cache, &fbo, &batch, def, iconSize));
|
||||
}
|
||||
fbo.unbind();
|
||||
|
||||
Window::viewport(0, 0, Window::width, Window::height);
|
||||
return std::unique_ptr<Atlas>(builder.build(2));
|
||||
}
|
||||
|
||||
@ -6,25 +6,27 @@
|
||||
#include <memory>
|
||||
|
||||
class Assets;
|
||||
class Viewport;
|
||||
class Shader;
|
||||
class ImageData;
|
||||
class Atlas;
|
||||
class Framebuffer;
|
||||
class Batch3D;
|
||||
class Block;
|
||||
class Content;
|
||||
class ContentGfxCache;
|
||||
|
||||
class BlocksPreview {
|
||||
Shader* shader;
|
||||
Atlas* atlas;
|
||||
std::unique_ptr<Batch3D> batch;
|
||||
const ContentGfxCache* const cache;
|
||||
const Viewport* viewport;
|
||||
public:
|
||||
BlocksPreview(Assets* assets, const ContentGfxCache* cache);
|
||||
~BlocksPreview();
|
||||
static ImageData* draw(
|
||||
const ContentGfxCache* cache,
|
||||
Framebuffer* framebuffer,
|
||||
Batch3D* batch,
|
||||
const Block* block,
|
||||
int size);
|
||||
|
||||
void begin(const Viewport* viewport);
|
||||
void draw(const Block* block, int x, int y, int size, glm::vec4 tint);
|
||||
static std::unique_ptr<Atlas> build(
|
||||
const ContentGfxCache* cache,
|
||||
Assets* assets,
|
||||
const Content* content);
|
||||
};
|
||||
|
||||
#endif // FRONTEND_BLOCKS_PREVIEW_H_
|
||||
|
||||
@ -28,6 +28,10 @@ void InventoryLayout::add(SlotLayout slot) {
|
||||
slots.push_back(slot);
|
||||
}
|
||||
|
||||
void InventoryLayout::add(InventoryPanel panel) {
|
||||
panels.push_back(panel);
|
||||
}
|
||||
|
||||
void InventoryLayout::setSize(glm::vec2 size) {
|
||||
this->size = size;
|
||||
}
|
||||
@ -48,6 +52,10 @@ std::vector<SlotLayout>& InventoryLayout::getSlots() {
|
||||
return slots;
|
||||
}
|
||||
|
||||
std::vector<InventoryPanel>& InventoryLayout::getPanels() {
|
||||
return panels;
|
||||
}
|
||||
|
||||
SlotLayout::SlotLayout(
|
||||
glm::vec2 position,
|
||||
bool background,
|
||||
@ -72,14 +80,17 @@ InventoryBuilder::InventoryBuilder()
|
||||
{}
|
||||
|
||||
void InventoryBuilder::addGrid(
|
||||
int cols, int rows,
|
||||
int cols, int count,
|
||||
glm::vec2 coord,
|
||||
int padding,
|
||||
int padding,
|
||||
bool addpanel,
|
||||
SlotLayout slotLayout)
|
||||
{
|
||||
const int slotSize = InventoryView::SLOT_SIZE;
|
||||
const int interval = InventoryView::SLOT_INTERVAL;
|
||||
|
||||
int rows = ceildiv(count, cols);
|
||||
|
||||
uint width = cols * (slotSize + interval) - interval + padding*2;
|
||||
uint height = rows * (slotSize + interval) - interval + padding*2;
|
||||
|
||||
@ -94,6 +105,8 @@ void InventoryBuilder::addGrid(
|
||||
|
||||
for (int row = 0; row < rows; row++) {
|
||||
for (int col = 0; col < cols; col++) {
|
||||
if (row * cols + col >= count)
|
||||
break;
|
||||
glm::vec2 position (
|
||||
col * (slotSize + interval) + padding,
|
||||
row * (slotSize + interval) + padding
|
||||
@ -103,6 +116,32 @@ void InventoryBuilder::addGrid(
|
||||
layout->add(builtSlot);
|
||||
}
|
||||
}
|
||||
|
||||
if (addpanel) {
|
||||
add(InventoryPanel(
|
||||
coord,
|
||||
glm::vec2(width, height),
|
||||
glm::vec4(0, 0, 0, 0.5f)));
|
||||
}
|
||||
}
|
||||
|
||||
void InventoryBuilder::add(SlotLayout slotLayout) {
|
||||
uint width = InventoryView::SLOT_SIZE;
|
||||
uint height = InventoryView::SLOT_SIZE;
|
||||
|
||||
auto coord = slotLayout.position;
|
||||
auto lsize = layout->getSize();
|
||||
if (coord.x + width > lsize.x) {
|
||||
lsize.x = coord.x + width;
|
||||
}
|
||||
if (coord.y + height > lsize.y) {
|
||||
lsize.y = coord.y + height;
|
||||
}
|
||||
layout->add(slotLayout);
|
||||
}
|
||||
|
||||
void InventoryBuilder::add(InventoryPanel panel) {
|
||||
layout->add(panel);
|
||||
}
|
||||
|
||||
std::unique_ptr<InventoryLayout> InventoryBuilder::build() {
|
||||
@ -149,31 +188,23 @@ void SlotView::draw(Batch2D* batch, Assets* assets) {
|
||||
|
||||
batch->color = glm::vec4(1.0f);
|
||||
|
||||
Shader* uiShader = assets->getShader("ui");
|
||||
Viewport viewport(Window::width, Window::height);
|
||||
GfxContext ctx(nullptr, viewport, batch);
|
||||
|
||||
auto preview = frontend->getBlocksPreview();
|
||||
auto previews = frontend->getBlocksAtlas();
|
||||
auto indices = content->getIndices();
|
||||
|
||||
ItemDef* item = indices->getItemDef(stack.getItemId());
|
||||
ItemDef* item = indices->getItemDef(stack.getItemId());
|
||||
switch (item->iconType) {
|
||||
case item_icon_type::none:
|
||||
break;
|
||||
case item_icon_type::block:
|
||||
batch->render();
|
||||
{
|
||||
GfxContext subctx = ctx.sub();
|
||||
subctx.depthTest(true);
|
||||
subctx.cullFace(true);
|
||||
case item_icon_type::block: {
|
||||
Block* cblock = content->requireBlock(item->icon);
|
||||
batch->texture(previews->getTexture());
|
||||
|
||||
Block* cblock = content->requireBlock(item->icon);
|
||||
preview->begin(&subctx.getViewport());
|
||||
preview->draw(cblock, coord.x, coord.y, slotSize, tint);
|
||||
}
|
||||
uiShader->use();
|
||||
batch->begin();
|
||||
UVRegion region = previews->get(cblock->name);
|
||||
batch->rect(
|
||||
coord.x, coord.y, slotSize, slotSize,
|
||||
0, 0, 0, region, false, true, tint);
|
||||
break;
|
||||
}
|
||||
case item_icon_type::sprite: {
|
||||
size_t index = item->icon.find(':');
|
||||
std::string name = item->icon.substr(index+1);
|
||||
@ -279,14 +310,17 @@ InventoryView::InventoryView(
|
||||
frontend(frontend),
|
||||
interaction(interaction) {
|
||||
size(this->layout->getSize());
|
||||
color(glm::vec4(0, 0, 0, 0.5f));
|
||||
color(glm::vec4(0, 0, 0, 0.0f));
|
||||
}
|
||||
|
||||
InventoryView::~InventoryView() {}
|
||||
|
||||
void InventoryView::build() {
|
||||
int index = 0;
|
||||
size_t index = 0;
|
||||
for (auto& slot : layout->getSlots()) {
|
||||
if (index >= inventory->size())
|
||||
break;
|
||||
|
||||
ItemStack& item = inventory->getSlot(index);
|
||||
|
||||
auto view = std::make_shared<SlotView>(
|
||||
@ -320,15 +354,15 @@ InventoryLayout* InventoryView::getLayout() const {
|
||||
return layout.get();
|
||||
}
|
||||
|
||||
// performance disaster x2
|
||||
void InventoryView::draw(Batch2D* batch, Assets* assets) {
|
||||
Container::draw(batch, assets);
|
||||
Window::clearDepth();
|
||||
}
|
||||
|
||||
void InventoryView::drawBackground(Batch2D* batch, Assets* assets) {
|
||||
glm::vec2 coord = calcCoord();
|
||||
|
||||
batch->texture(nullptr);
|
||||
batch->color = color_;
|
||||
batch->rect(coord.x-1, coord.y-1, size_.x+2, size_.y+2);
|
||||
|
||||
for (auto& panel : layout->getPanels()) {
|
||||
glm::vec2 size = panel.size;
|
||||
glm::vec2 pos = coord + panel.position;
|
||||
batch->color = panel.color;
|
||||
batch->rect(pos.x-1, pos.y-1, size.x+2, size.y+2);
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,10 +53,12 @@ class InventoryLayout {
|
||||
glm::vec2 size;
|
||||
glm::vec2 origin;
|
||||
std::vector<SlotLayout> slots;
|
||||
std::vector<InventoryPanel> panels;
|
||||
public:
|
||||
InventoryLayout(glm::vec2 size);
|
||||
|
||||
void add(SlotLayout slot);
|
||||
void add(InventoryPanel panel);
|
||||
void setSize(glm::vec2 size);
|
||||
void setOrigin(glm::vec2 origin);
|
||||
|
||||
@ -64,6 +66,7 @@ public:
|
||||
glm::vec2 getOrigin() const;
|
||||
|
||||
std::vector<SlotLayout>& getSlots();
|
||||
std::vector<InventoryPanel>& getPanels();
|
||||
};
|
||||
|
||||
class InventoryBuilder {
|
||||
@ -72,10 +75,15 @@ public:
|
||||
InventoryBuilder();
|
||||
|
||||
void addGrid(
|
||||
int cols, int rows,
|
||||
int cols, int count,
|
||||
glm::vec2 coord,
|
||||
int padding,
|
||||
int padding,
|
||||
bool addpanel,
|
||||
SlotLayout slotLayout);
|
||||
|
||||
void add(SlotLayout slotLayout);
|
||||
void add(InventoryPanel panel);
|
||||
|
||||
std::unique_ptr<InventoryLayout> build();
|
||||
};
|
||||
|
||||
@ -126,7 +134,6 @@ public:
|
||||
|
||||
void build();
|
||||
|
||||
virtual void draw(Batch2D* batch, Assets* assets) override;
|
||||
virtual void drawBackground(Batch2D* batch, Assets* assets) override;
|
||||
|
||||
void setInventory(std::shared_ptr<Inventory> inventory);
|
||||
@ -138,7 +145,7 @@ public:
|
||||
void setSelected(int index);
|
||||
|
||||
static const int SLOT_INTERVAL = 4;
|
||||
static const int SLOT_SIZE = 48;
|
||||
static const int SLOT_SIZE = ITEM_ICON_SIZE;
|
||||
};
|
||||
|
||||
class InventoryInteraction {
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include "../world/Level.h"
|
||||
#include "../assets/Assets.h"
|
||||
#include "../graphics/Atlas.h"
|
||||
#include "BlocksPreview.h"
|
||||
#include "ContentGfxCache.h"
|
||||
|
||||
@ -9,8 +10,7 @@ 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())) {
|
||||
|
||||
blocksAtlas(BlocksPreview::build(contentCache.get(), assets, level->content)) {
|
||||
}
|
||||
|
||||
LevelFrontend::~LevelFrontend() {
|
||||
@ -24,10 +24,10 @@ Assets* LevelFrontend::getAssets() const {
|
||||
return assets;
|
||||
}
|
||||
|
||||
BlocksPreview* LevelFrontend::getBlocksPreview() const {
|
||||
return blocksPreview.get();
|
||||
}
|
||||
|
||||
ContentGfxCache* LevelFrontend::getContentGfxCache() const {
|
||||
return contentCache.get();
|
||||
}
|
||||
|
||||
Atlas* LevelFrontend::getBlocksAtlas() const {
|
||||
return blocksAtlas.get();
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Atlas;
|
||||
class Level;
|
||||
class Assets;
|
||||
class BlocksPreview;
|
||||
@ -12,15 +13,15 @@ class LevelFrontend {
|
||||
Level* level;
|
||||
Assets* assets;
|
||||
std::unique_ptr<ContentGfxCache> contentCache;
|
||||
std::unique_ptr<BlocksPreview> blocksPreview;
|
||||
std::unique_ptr<Atlas> blocksAtlas;
|
||||
public:
|
||||
LevelFrontend(Level* level, Assets* assets);
|
||||
~LevelFrontend();
|
||||
|
||||
Level* getLevel() const;
|
||||
Assets* getAssets() const;
|
||||
BlocksPreview* getBlocksPreview() const;
|
||||
ContentGfxCache* getContentGfxCache() const;
|
||||
Atlas* getBlocksAtlas() const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -52,106 +52,106 @@ using glm::vec4;
|
||||
using namespace gui;
|
||||
|
||||
inline std::shared_ptr<Label> create_label(gui::wstringsupplier supplier) {
|
||||
auto label = std::make_shared<Label>(L"-");
|
||||
label->textSupplier(supplier);
|
||||
return label;
|
||||
auto label = std::make_shared<Label>(L"-");
|
||||
label->textSupplier(supplier);
|
||||
return label;
|
||||
}
|
||||
|
||||
void HudRenderer::createDebugPanel(Engine* engine) {
|
||||
auto level = frontend->getLevel();
|
||||
|
||||
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(create_label([this](){ return L"fps: "+this->fpsString;}));
|
||||
panel->add(create_label([this](){
|
||||
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
|
||||
}));
|
||||
panel->add(create_label([=](){
|
||||
auto& settings = engine->getSettings();
|
||||
bool culling = settings.graphics.frustumCulling;
|
||||
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(create_label([=](){
|
||||
auto player = level->player;
|
||||
auto* indices = level->content->getIndices();
|
||||
auto def = indices->getBlockDef(player->selectedVoxel.id);
|
||||
std::wstringstream stream;
|
||||
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(create_label([=](){
|
||||
return L"seed: "+std::to_wstring(level->world->seed);
|
||||
}));
|
||||
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(create_label([this](){ return L"fps: "+this->fpsString;}));
|
||||
panel->add(create_label([this](){
|
||||
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
|
||||
}));
|
||||
panel->add(create_label([=](){
|
||||
auto& settings = engine->getSettings();
|
||||
bool culling = settings.graphics.frustumCulling;
|
||||
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(create_label([=](){
|
||||
auto player = level->player;
|
||||
auto* indices = level->content->getIndices();
|
||||
auto def = indices->getBlockDef(player->selectedVoxel.id);
|
||||
std::wstringstream stream;
|
||||
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(create_label([=](){
|
||||
return L"seed: "+std::to_wstring(level->world->getSeed());
|
||||
}));
|
||||
|
||||
for (int ax = 0; ax < 3; ax++){
|
||||
Panel* sub = new Panel(vec2(10, 27), vec4(0.0f));
|
||||
sub->orientation(Orientation::horizontal);
|
||||
for (int ax = 0; ax < 3; ax++){
|
||||
Panel* sub = new Panel(vec2(10, 27), vec4(0.0f));
|
||||
sub->orientation(Orientation::horizontal);
|
||||
|
||||
std::wstring str = L"x: ";
|
||||
str[0] += ax;
|
||||
Label* label = new Label(str);
|
||||
label->margin(vec4(2, 3, 2, 3));
|
||||
sub->add(label);
|
||||
sub->color(vec4(0.0f));
|
||||
std::wstring str = L"x: ";
|
||||
str[0] += ax;
|
||||
Label* label = new Label(str);
|
||||
label->margin(vec4(2, 3, 2, 3));
|
||||
sub->add(label);
|
||||
sub->color(vec4(0.0f));
|
||||
|
||||
// Coord input
|
||||
TextBox* box = new TextBox(L"");
|
||||
box->textSupplier([=]() {
|
||||
Hitbox* hitbox = level->player->hitbox.get();
|
||||
return util::to_wstring(hitbox->position[ax], 2);
|
||||
});
|
||||
box->textConsumer([=](std::wstring text) {
|
||||
try {
|
||||
vec3 position = level->player->hitbox->position;
|
||||
position[ax] = std::stoi(text);
|
||||
level->player->teleport(position);
|
||||
} catch (std::invalid_argument& _){
|
||||
}
|
||||
});
|
||||
box->setOnEditStart([=](){
|
||||
Hitbox* hitbox = level->player->hitbox.get();
|
||||
box->text(std::to_wstring(int(hitbox->position[ax])));
|
||||
});
|
||||
// Coord input
|
||||
TextBox* box = new TextBox(L"");
|
||||
box->textSupplier([=]() {
|
||||
Hitbox* hitbox = level->player->hitbox.get();
|
||||
return util::to_wstring(hitbox->position[ax], 2);
|
||||
});
|
||||
box->textConsumer([=](std::wstring text) {
|
||||
try {
|
||||
vec3 position = level->player->hitbox->position;
|
||||
position[ax] = std::stoi(text);
|
||||
level->player->teleport(position);
|
||||
} catch (std::invalid_argument& _){
|
||||
}
|
||||
});
|
||||
box->setOnEditStart([=](){
|
||||
Hitbox* hitbox = level->player->hitbox.get();
|
||||
box->text(std::to_wstring(int(hitbox->position[ax])));
|
||||
});
|
||||
|
||||
sub->add(box);
|
||||
panel->add(sub);
|
||||
}
|
||||
panel->add(create_label([=](){
|
||||
int hour, minute, second;
|
||||
timeutil::from_value(level->world->daytime, hour, minute, second);
|
||||
sub->add(box);
|
||||
panel->add(sub);
|
||||
}
|
||||
panel->add(create_label([=](){
|
||||
int 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([=]() {return level->world->daytime;});
|
||||
bar->consumer([=](double val) {level->world->daytime = val;});
|
||||
panel->add(bar);
|
||||
}
|
||||
{
|
||||
TrackBar* bar = new TrackBar(0.0f, 1.0f, 0.0f, 0.005f, 8);
|
||||
bar->supplier([=]() {return WorldRenderer::fog;});
|
||||
bar->consumer([=](double val) {WorldRenderer::fog = val;});
|
||||
panel->add(bar);
|
||||
}
|
||||
{
|
||||
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([=]() {return level->world->daytime;});
|
||||
bar->consumer([=](double val) {level->world->daytime = val;});
|
||||
panel->add(bar);
|
||||
}
|
||||
{
|
||||
TrackBar* bar = new TrackBar(0.0f, 1.0f, 0.0f, 0.005f, 8);
|
||||
bar->supplier([=]() {return WorldRenderer::fog;});
|
||||
bar->consumer([=](double val) {WorldRenderer::fog = val;});
|
||||
panel->add(bar);
|
||||
}
|
||||
{
|
||||
auto checkbox = new FullCheckBox(L"Show Chunk Borders", vec2(400, 32));
|
||||
checkbox->supplier([=]() {
|
||||
return engine->getSettings().debug.showChunkBorders;
|
||||
@ -160,8 +160,8 @@ void HudRenderer::createDebugPanel(Engine* engine) {
|
||||
engine->getSettings().debug.showChunkBorders = checked;
|
||||
});
|
||||
panel->add(checkbox);
|
||||
}
|
||||
panel->refresh();
|
||||
}
|
||||
panel->refresh();
|
||||
}
|
||||
|
||||
std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
|
||||
@ -177,37 +177,25 @@ std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
|
||||
accessInventory->getSlot(id-1).set(ItemStack(id, 1));
|
||||
}
|
||||
|
||||
const int slotSize = InventoryView::SLOT_SIZE;
|
||||
const int interval = InventoryView::SLOT_INTERVAL;
|
||||
int padding = 8;
|
||||
SlotLayout slotLayout(glm::vec2(), false, true,
|
||||
[=](ItemStack& item) {
|
||||
auto copy = ItemStack(item);
|
||||
inventory->move(copy, indices);
|
||||
},
|
||||
[=](ItemStack& item, ItemStack& grabbed) {
|
||||
inventory->getSlot(player->getChosenSlot()).set(item);
|
||||
});
|
||||
|
||||
InventoryBuilder builder;
|
||||
builder.addGrid(8, itemsCount-1, glm::vec2(), 8, true, slotLayout);
|
||||
auto layout = builder.build();
|
||||
|
||||
int columns = 8;
|
||||
int rows = ceildiv(itemsCount-1, columns);
|
||||
uint cawidth = columns * (slotSize + interval) - interval + padding;
|
||||
uint caheight = rows * (slotSize + interval) - interval + padding*2;
|
||||
auto layout = std::make_unique<InventoryLayout>(glm::vec2(cawidth, caheight));
|
||||
for (int i = 0; i < itemsCount-1; i++) {
|
||||
int row = i / columns;
|
||||
int col = i % columns;
|
||||
glm::vec2 position (
|
||||
col * slotSize + (col-1) * interval + padding,
|
||||
row * slotSize + (row-1) * interval + padding
|
||||
);
|
||||
layout->add(SlotLayout(position, false, true,
|
||||
[=](ItemStack& item) {
|
||||
auto copy = ItemStack(item);
|
||||
inventory->move(copy, indices);
|
||||
},
|
||||
[=](ItemStack& item, ItemStack& grabbed) {
|
||||
inventory->getSlot(player->getChosenSlot()).set(item);
|
||||
}));
|
||||
}
|
||||
auto contentAccess = std::make_shared<InventoryView>(
|
||||
content,
|
||||
frontend,
|
||||
interaction.get(),
|
||||
accessInventory,
|
||||
std::move(layout)
|
||||
frontend,
|
||||
interaction.get(),
|
||||
accessInventory,
|
||||
std::move(layout)
|
||||
);
|
||||
contentAccess->build();
|
||||
return contentAccess;
|
||||
@ -219,24 +207,18 @@ std::shared_ptr<InventoryView> HudRenderer::createHotbar() {
|
||||
auto inventory = player->getInventory();
|
||||
auto content = level->content;
|
||||
|
||||
const int slotSize = InventoryView::SLOT_SIZE;
|
||||
const int interval = InventoryView::SLOT_INTERVAL;
|
||||
SlotLayout slotLayout(glm::vec2(), false, false, nullptr, nullptr);
|
||||
InventoryBuilder builder;
|
||||
builder.addGrid(10, 10, glm::vec2(), 4, true, slotLayout);
|
||||
auto layout = builder.build();
|
||||
|
||||
int padding = 4;
|
||||
uint width = 10 * (slotSize + interval) - interval + padding*2;
|
||||
uint height = slotSize + padding * 2;
|
||||
auto layout = std::make_unique<InventoryLayout>(glm::vec2(width, height));
|
||||
for (int i = 0; i < 10; i++) {
|
||||
glm::vec2 position (i * (slotSize + interval) + padding, padding);
|
||||
layout->add(SlotLayout(position, false, false, nullptr, nullptr));
|
||||
}
|
||||
layout->setOrigin(glm::vec2(width / 2, 0));
|
||||
layout->setOrigin(glm::vec2(layout->getSize().x/2, 0));
|
||||
auto view = std::make_shared<InventoryView>(
|
||||
content,
|
||||
frontend,
|
||||
interaction.get(),
|
||||
inventory,
|
||||
std::move(layout)
|
||||
frontend,
|
||||
interaction.get(),
|
||||
inventory,
|
||||
std::move(layout)
|
||||
);
|
||||
view->build();
|
||||
view->setInteractive(false);
|
||||
@ -252,13 +234,9 @@ std::shared_ptr<InventoryView> HudRenderer::createInventory() {
|
||||
SlotLayout slotLayout(glm::vec2(), true, false, [=](ItemStack& stack) {
|
||||
stack.clear();
|
||||
}, nullptr);
|
||||
|
||||
int columns = 10;
|
||||
int rows = ceildiv(inventory->size(), columns);
|
||||
int padding = 4;
|
||||
|
||||
InventoryBuilder builder;
|
||||
builder.addGrid(columns, rows, glm::vec2(), padding, slotLayout);
|
||||
builder.addGrid(10, inventory->size(), glm::vec2(), 4, true, slotLayout);
|
||||
auto layout = builder.build();
|
||||
|
||||
auto view = std::make_shared<InventoryView>(
|
||||
@ -277,13 +255,13 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
||||
gui(engine->getGUI()),
|
||||
frontend(frontend)
|
||||
{
|
||||
auto menu = gui->getMenu();
|
||||
auto menu = gui->getMenu();
|
||||
|
||||
interaction = std::make_unique<InventoryInteraction>();
|
||||
grabbedItemView = std::make_shared<SlotView>(
|
||||
interaction->getGrabbedItem(),
|
||||
frontend,
|
||||
interaction.get(),
|
||||
interaction.get(),
|
||||
frontend->getLevel()->content,
|
||||
SlotLayout(glm::vec2(), false, false, nullptr, nullptr)
|
||||
);
|
||||
@ -301,14 +279,14 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
||||
hotbarView = createHotbar();
|
||||
inventoryView = createInventory();
|
||||
|
||||
uicamera = new Camera(vec3(), 1);
|
||||
uicamera->perspective = false;
|
||||
uicamera->flipped = true;
|
||||
uicamera = new Camera(vec3(), 1);
|
||||
uicamera->perspective = false;
|
||||
uicamera->flipped = true;
|
||||
|
||||
createDebugPanel(engine);
|
||||
menu->reset();
|
||||
|
||||
gui->add(debugPanel);
|
||||
menu->reset();
|
||||
|
||||
gui->add(debugPanel);
|
||||
gui->add(contentAccessPanel);
|
||||
gui->add(hotbarView);
|
||||
gui->add(inventoryView);
|
||||
@ -320,48 +298,48 @@ HudRenderer::~HudRenderer() {
|
||||
gui->remove(inventoryView);
|
||||
gui->remove(hotbarView);
|
||||
gui->remove(contentAccessPanel);
|
||||
gui->remove(debugPanel);
|
||||
delete uicamera;
|
||||
gui->remove(debugPanel);
|
||||
delete uicamera;
|
||||
}
|
||||
|
||||
void HudRenderer::drawDebug(int fps){
|
||||
this->fps = fps;
|
||||
fpsMin = min(fps, fpsMin);
|
||||
fpsMax = max(fps, fpsMax);
|
||||
this->fps = fps;
|
||||
fpsMin = min(fps, fpsMin);
|
||||
fpsMax = max(fps, fpsMax);
|
||||
}
|
||||
|
||||
void HudRenderer::update(bool visible) {
|
||||
auto level = frontend->getLevel();
|
||||
auto player = level->player;
|
||||
auto menu = gui->getMenu();
|
||||
auto menu = gui->getMenu();
|
||||
|
||||
menu->visible(pause);
|
||||
|
||||
if (!visible && inventoryOpen) {
|
||||
inventoryOpen = false;
|
||||
}
|
||||
if (pause && menu->current().panel == nullptr) {
|
||||
pause = false;
|
||||
}
|
||||
if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) {
|
||||
if (pause) {
|
||||
pause = false;
|
||||
menu->reset();
|
||||
} else if (inventoryOpen) {
|
||||
inventoryOpen = false;
|
||||
} else {
|
||||
pause = true;
|
||||
menu->set("pause");
|
||||
}
|
||||
}
|
||||
if (visible && Events::jactive(BIND_HUD_INVENTORY)) {
|
||||
if (!pause) {
|
||||
inventoryOpen = !inventoryOpen;
|
||||
}
|
||||
}
|
||||
if ((pause || inventoryOpen) == Events::_cursor_locked) {
|
||||
Events::toggleCursor();
|
||||
}
|
||||
if (pause && menu->current().panel == nullptr) {
|
||||
pause = false;
|
||||
}
|
||||
if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) {
|
||||
if (pause) {
|
||||
pause = false;
|
||||
menu->reset();
|
||||
} else if (inventoryOpen) {
|
||||
inventoryOpen = false;
|
||||
} else {
|
||||
pause = true;
|
||||
menu->set("pause");
|
||||
}
|
||||
}
|
||||
if (visible && Events::jactive(BIND_HUD_INVENTORY)) {
|
||||
if (!pause) {
|
||||
inventoryOpen = !inventoryOpen;
|
||||
}
|
||||
}
|
||||
if ((pause || inventoryOpen) == Events::_cursor_locked) {
|
||||
Events::toggleCursor();
|
||||
}
|
||||
|
||||
glm::vec2 invSize = contentAccessPanel->size();
|
||||
inventoryView->visible(inventoryOpen);
|
||||
@ -387,10 +365,10 @@ void HudRenderer::update(bool visible) {
|
||||
}
|
||||
|
||||
void HudRenderer::drawOverlay(const GfxContext& ctx) {
|
||||
if (pause) {
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
if (pause) {
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
|
||||
const Viewport& viewport = ctx.getViewport();
|
||||
const uint width = viewport.getWidth();
|
||||
@ -398,48 +376,48 @@ void HudRenderer::drawOverlay(const GfxContext& ctx) {
|
||||
auto batch = ctx.getBatch2D();
|
||||
batch->begin();
|
||||
|
||||
// draw fullscreen dark overlay
|
||||
batch->texture(nullptr);
|
||||
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.5f);
|
||||
batch->rect(0, 0, width, height);
|
||||
// draw fullscreen dark overlay
|
||||
batch->texture(nullptr);
|
||||
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.5f);
|
||||
batch->rect(0, 0, width, height);
|
||||
batch->render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HudRenderer::draw(const GfxContext& ctx){
|
||||
auto level = frontend->getLevel();
|
||||
|
||||
const Viewport& viewport = ctx.getViewport();
|
||||
const uint width = viewport.getWidth();
|
||||
const uint height = viewport.getHeight();
|
||||
const Viewport& viewport = ctx.getViewport();
|
||||
const uint width = viewport.getWidth();
|
||||
const uint height = viewport.getHeight();
|
||||
|
||||
Player* player = level->player;
|
||||
debugPanel->visible(player->debug);
|
||||
Player* player = level->player;
|
||||
debugPanel->visible(player->debug);
|
||||
|
||||
uicamera->setFov(height);
|
||||
uicamera->setFov(height);
|
||||
|
||||
auto batch = ctx.getBatch2D();
|
||||
batch->begin();
|
||||
batch->begin();
|
||||
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
|
||||
// Draw selected item preview
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
|
||||
// Draw selected item preview
|
||||
hotbarView->setCoord(glm::vec2(width/2, height-65));
|
||||
hotbarView->setSelected(player->getChosenSlot());
|
||||
|
||||
// Crosshair
|
||||
batch->begin();
|
||||
if (!pause && Events::_cursor_locked && !level->player->debug) {
|
||||
batch->lineWidth(2);
|
||||
batch->line(width/2, height/2-6, width/2, height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
|
||||
batch->line(width/2+6, height/2, width/2-6, height/2, 0.2f, 0.2f, 0.2f, 1.0f);
|
||||
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);
|
||||
}
|
||||
// Crosshair
|
||||
batch->begin();
|
||||
if (!pause && Events::_cursor_locked && !level->player->debug) {
|
||||
batch->lineWidth(2);
|
||||
batch->line(width/2, height/2-6, width/2, height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
|
||||
batch->line(width/2+6, height/2, width/2-6, height/2, 0.2f, 0.2f, 0.2f, 1.0f);
|
||||
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);
|
||||
}
|
||||
|
||||
if (inventoryOpen) {
|
||||
if (inventoryOpen) {
|
||||
auto caLayout = contentAccess->getLayout();
|
||||
auto invLayout = inventoryView->getLayout();
|
||||
float caWidth = caLayout->getSize().x;
|
||||
@ -452,15 +430,15 @@ void HudRenderer::draw(const GfxContext& ctx){
|
||||
height/2-invSize.y/2
|
||||
));
|
||||
contentAccessPanel->setCoord(glm::vec2(width-caWidth, 0));
|
||||
}
|
||||
}
|
||||
grabbedItemView->setCoord(glm::vec2(Events::cursor));
|
||||
batch->render();
|
||||
batch->render();
|
||||
}
|
||||
|
||||
bool HudRenderer::isInventoryOpen() const {
|
||||
return inventoryOpen;
|
||||
return inventoryOpen;
|
||||
}
|
||||
|
||||
bool HudRenderer::isPause() const {
|
||||
return pause;
|
||||
return pause;
|
||||
}
|
||||
|
||||
@ -3,13 +3,15 @@
|
||||
#include <GL/glew.h>
|
||||
#include "Texture.h"
|
||||
|
||||
Framebuffer::Framebuffer(uint width, uint height) : width(width), height(height) {
|
||||
Framebuffer::Framebuffer(uint width, uint height, bool alpha)
|
||||
: width(width), height(height) {
|
||||
glGenFramebuffers(1, &fbo);
|
||||
bind();
|
||||
GLuint tex;
|
||||
glGenTextures(1, &tex);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
|
||||
GLuint format = alpha ? GL_RGBA : GL_RGB;
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
|
||||
@ -12,7 +12,7 @@ public:
|
||||
uint width;
|
||||
uint height;
|
||||
Texture* texture;
|
||||
Framebuffer(uint width, uint height);
|
||||
Framebuffer(uint width, uint height, bool alpha=false);
|
||||
~Framebuffer();
|
||||
|
||||
void bind();
|
||||
|
||||
@ -1,52 +1,61 @@
|
||||
#include "Texture.h"
|
||||
#include <GL/glew.h>
|
||||
#include <stdexcept>
|
||||
#include <memory>
|
||||
|
||||
#include "ImageData.h"
|
||||
|
||||
Texture::Texture(uint id, int width, int height)
|
||||
: id(id), width(width), height(height) {
|
||||
: id(id), width(width), height(height) {
|
||||
}
|
||||
|
||||
Texture::Texture(ubyte* data, int width, int height, uint format)
|
||||
: width(width), height(height) {
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
|
||||
format, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
: width(width), height(height) {
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
|
||||
format, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
Texture::~Texture() {
|
||||
glDeleteTextures(1, &id);
|
||||
glDeleteTextures(1, &id);
|
||||
}
|
||||
|
||||
void Texture::bind(){
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
}
|
||||
|
||||
void Texture::reload(ubyte* data){
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
ImageData* Texture::readData() {
|
||||
std::unique_ptr<ubyte[]> data (new ubyte[width * height * 4]);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.get());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
return new ImageData(ImageFormat::rgba8888, width, height, data.release());
|
||||
}
|
||||
|
||||
Texture* Texture::from(const ImageData* image) {
|
||||
uint width = image->getWidth();
|
||||
uint height = image->getHeight();
|
||||
uint format;
|
||||
const void* data = image->getData();
|
||||
switch (image->getFormat()) {
|
||||
case ImageFormat::rgb888: format = GL_RGB; break;
|
||||
case ImageFormat::rgba8888: format = GL_RGBA; break;
|
||||
default:
|
||||
throw std::runtime_error("unsupported image data format");
|
||||
}
|
||||
return new Texture((ubyte*)data, width, height, format);
|
||||
}
|
||||
uint width = image->getWidth();
|
||||
uint height = image->getHeight();
|
||||
uint format;
|
||||
const void* data = image->getData();
|
||||
switch (image->getFormat()) {
|
||||
case ImageFormat::rgb888: format = GL_RGB; break;
|
||||
case ImageFormat::rgba8888: format = GL_RGBA; break;
|
||||
default:
|
||||
throw std::runtime_error("unsupported image data format");
|
||||
}
|
||||
return new Texture((ubyte*)data, width, height, format);
|
||||
}
|
||||
|
||||
@ -8,17 +8,19 @@ class ImageData;
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
uint id;
|
||||
int width;
|
||||
int height;
|
||||
Texture(uint id, int width, int height);
|
||||
Texture(ubyte* data, int width, int height, uint format);
|
||||
~Texture();
|
||||
uint id;
|
||||
int width;
|
||||
int height;
|
||||
Texture(uint id, int width, int height);
|
||||
Texture(ubyte* data, int width, int height, uint format);
|
||||
~Texture();
|
||||
|
||||
void bind();
|
||||
void reload(ubyte* data);
|
||||
void bind();
|
||||
void reload(ubyte* data);
|
||||
|
||||
static Texture* from(const ImageData* image);
|
||||
ImageData* readData();
|
||||
|
||||
static Texture* from(const ImageData* image);
|
||||
};
|
||||
|
||||
#endif /* GRAPHICS_TEXTURE_H_ */
|
||||
|
||||
@ -4,7 +4,7 @@ Inventory::Inventory(size_t size) : slots(size) {
|
||||
}
|
||||
|
||||
ItemStack& Inventory::getSlot(size_t index) {
|
||||
return slots[index];
|
||||
return slots.at(index);
|
||||
}
|
||||
|
||||
size_t Inventory::findEmptySlot(size_t begin, size_t end) const {
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
|
||||
class ContentIndices;
|
||||
|
||||
// TODO: items indices fix
|
||||
class Inventory {
|
||||
std::vector<ItemStack> slots;
|
||||
public:
|
||||
|
||||
@ -102,7 +102,11 @@ bool ChunksController::loadVisible(){
|
||||
chunks->putChunk(chunk);
|
||||
|
||||
if (!chunk->isLoaded()) {
|
||||
generator->generate(chunk->voxels, chunk->x, chunk->z, level->world->seed);
|
||||
generator->generate(
|
||||
chunk->voxels,
|
||||
chunk->x, chunk->z,
|
||||
level->world->getSeed()
|
||||
);
|
||||
chunk->setUnsaved(true);
|
||||
}
|
||||
|
||||
|
||||
@ -47,6 +47,11 @@ static const luaL_Reg packlib [] = {
|
||||
};
|
||||
|
||||
/* == world library == */
|
||||
static int l_world_get_total_time(lua_State* L) {
|
||||
lua_pushnumber(L, scripting::level->world->totalTime);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_world_get_day_time(lua_State* L) {
|
||||
lua_pushnumber(L, scripting::level->world->daytime);
|
||||
return 1;
|
||||
@ -59,11 +64,12 @@ static int l_world_set_day_time(lua_State* L) {
|
||||
}
|
||||
|
||||
static int l_world_get_seed(lua_State* L) {
|
||||
lua_pushinteger(L, scripting::level->world->seed);
|
||||
lua_pushinteger(L, scripting::level->world->getSeed());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_Reg worldlib [] = {
|
||||
{"get_total_time", l_world_get_total_time},
|
||||
{"get_day_time", l_world_get_day_time},
|
||||
{"set_day_time", l_world_set_day_time},
|
||||
{"get_seed", l_world_get_seed},
|
||||
@ -261,14 +267,14 @@ static int l_set_block_user_bits(lua_State* L) {
|
||||
int offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET;
|
||||
int bits = lua_tointeger(L, 5);
|
||||
|
||||
uint mask = (1 << bits) - 1;
|
||||
int value = lua_tointeger(L, 6) & mask;
|
||||
uint mask = ((1 << bits) - 1) << offset;
|
||||
int value = (lua_tointeger(L, 6) << offset) & mask;
|
||||
|
||||
voxel* vox = scripting::level->chunks->get(x, y, z);
|
||||
if (vox == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
vox->states = (vox->states & (~mask)) | (value << offset);
|
||||
vox->states = (vox->states & (~mask)) | value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -26,6 +26,10 @@ Level* scripting::level = nullptr;
|
||||
const Content* scripting::content = nullptr;
|
||||
BlocksController* scripting::blocks = nullptr;
|
||||
|
||||
static void handleError(lua_State* L) {
|
||||
std::cerr << "lua error: " << lua_tostring(L,-1) << std::endl;
|
||||
}
|
||||
|
||||
inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
|
||||
lua_pushinteger(L, x);
|
||||
lua_pushinteger(L, y);
|
||||
@ -51,13 +55,24 @@ bool rename_global(lua_State* L, const char* src, const char* dst) {
|
||||
|
||||
int call_func(lua_State* L, int argc, const std::string& name) {
|
||||
if (lua_pcall(L, argc, LUA_MULTRET, 0)) {
|
||||
std::cerr << "Lua error in " << name << ": ";
|
||||
std::cerr << lua_tostring(L,-1) << std::endl;
|
||||
handleError(L);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void load_script(fs::path name) {
|
||||
auto paths = scripting::engine->getPaths();
|
||||
fs::path file = paths->getResources()/fs::path("scripts")/name;
|
||||
|
||||
std::string src = files::read_string(file);
|
||||
if (luaL_loadbuffer(L, src.c_str(), src.length(), file.u8string().c_str())) {
|
||||
handleError(L);
|
||||
return;
|
||||
}
|
||||
call_func(L, 0, file.u8string());
|
||||
}
|
||||
|
||||
void scripting::initialize(Engine* engine) {
|
||||
scripting::engine = engine;
|
||||
|
||||
@ -81,17 +96,15 @@ void scripting::initialize(Engine* engine) {
|
||||
# endif // LUAJIT_VERSION
|
||||
|
||||
apilua::create_funcs(L);
|
||||
|
||||
load_script(fs::path("stdlib.lua"));
|
||||
}
|
||||
|
||||
void scripting::on_world_load(Level* level, BlocksController* blocks) {
|
||||
scripting::level = level;
|
||||
scripting::content = level->content;
|
||||
scripting::blocks = blocks;
|
||||
auto paths = scripting::engine->getPaths();
|
||||
fs::path file = paths->getResources()/fs::path("scripts/world.lua");
|
||||
std::string src = files::read_string(file);
|
||||
luaL_loadbuffer(L, src.c_str(), src.length(), file.string().c_str());
|
||||
call_func(L, 0, "<script>");
|
||||
load_script("world.lua");
|
||||
}
|
||||
|
||||
void scripting::on_world_quit() {
|
||||
@ -165,7 +178,7 @@ void scripting::load_block_script(std::string prefix, fs::path file, block_funcs
|
||||
std::string src = files::read_string(file);
|
||||
std::cout << "loading script " << file.u8string() << std::endl;
|
||||
if (luaL_loadbuffer(L, src.c_str(), src.size(), file.string().c_str())) {
|
||||
std::cerr << "Lua error:" << lua_tostring(L,-1) << std::endl;
|
||||
handleError(L);
|
||||
return;
|
||||
}
|
||||
call_func(L, 0, "<script>");
|
||||
@ -181,7 +194,7 @@ void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_s
|
||||
std::string src = files::read_string(file);
|
||||
std::cout << "loading script " << file.u8string() << std::endl;
|
||||
if (luaL_loadbuffer(L, src.c_str(), src.size(), file.string().c_str())) {
|
||||
std::cerr << "Lua error:" << lua_tostring(L,-1) << std::endl;
|
||||
handleError(L);
|
||||
return;
|
||||
}
|
||||
call_func(L, 0, "<script>");
|
||||
|
||||
@ -179,6 +179,10 @@ void Window::setBgColor(glm::vec3 color) {
|
||||
glClearColor(color.r, color.g, color.b, 1.0f);
|
||||
}
|
||||
|
||||
void Window::setBgColor(glm::vec4 color) {
|
||||
glClearColor(color.r, color.g, color.b, color.a);
|
||||
}
|
||||
|
||||
void Window::viewport(int x, int y, int width, int height){
|
||||
glViewport(x, y, width, height);
|
||||
}
|
||||
|
||||
@ -48,6 +48,7 @@ public:
|
||||
static void clear();
|
||||
static void clearDepth();
|
||||
static void setBgColor(glm::vec3 color);
|
||||
static void setBgColor(glm::vec4 color);
|
||||
static double time();
|
||||
static const char* getClipboardText();
|
||||
static DisplaySettings* getSettings();
|
||||
|
||||
@ -13,101 +13,115 @@
|
||||
#include "../objects/Player.h"
|
||||
#include "../window/Camera.h"
|
||||
|
||||
using glm::vec3;
|
||||
using std::unique_ptr;
|
||||
using std::shared_ptr;
|
||||
using std::string;
|
||||
using std::filesystem::path;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
world_load_error::world_load_error(string message) : std::runtime_error(message) {
|
||||
world_load_error::world_load_error(std::string message)
|
||||
: std::runtime_error(message) {
|
||||
}
|
||||
|
||||
World::World(string name,
|
||||
path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack> packs)
|
||||
: settings(settings),
|
||||
content(content),
|
||||
packs(packs),
|
||||
name(name),
|
||||
seed(seed) {
|
||||
wfile = new WorldFiles(directory, settings.debug);
|
||||
World::World(
|
||||
std::string name,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack> packs)
|
||||
: name(name),
|
||||
seed(seed),
|
||||
settings(settings),
|
||||
content(content),
|
||||
packs(packs) {
|
||||
wfile = new WorldFiles(directory, settings.debug);
|
||||
}
|
||||
|
||||
World::~World(){
|
||||
delete wfile;
|
||||
delete wfile;
|
||||
}
|
||||
|
||||
void World::updateTimers(float delta) {
|
||||
daytime += delta * daytimeSpeed;
|
||||
daytime = fmod(daytime, 1.0f);
|
||||
daytime += delta * daytimeSpeed;
|
||||
daytime = fmod(daytime, 1.0f);
|
||||
totalTime += delta;
|
||||
}
|
||||
|
||||
void World::write(Level* level) {
|
||||
const Content* content = level->content;
|
||||
const Content* content = level->content;
|
||||
|
||||
Chunks* chunks = level->chunks;
|
||||
Chunks* chunks = level->chunks;
|
||||
|
||||
for (size_t i = 0; i < chunks->volume; i++) {
|
||||
shared_ptr<Chunk> chunk = chunks->chunks[i];
|
||||
if (chunk == nullptr || !chunk->isLighted())
|
||||
continue;
|
||||
bool lightsUnsaved = !chunk->isLoadedLights() &&
|
||||
settings.debug.doWriteLights;
|
||||
if (!chunk->isUnsaved() && !lightsUnsaved)
|
||||
continue;
|
||||
wfile->put(chunk.get());
|
||||
}
|
||||
for (size_t i = 0; i < chunks->volume; i++) {
|
||||
auto chunk = chunks->chunks[i];
|
||||
if (chunk == nullptr || !chunk->isLighted())
|
||||
continue;
|
||||
bool lightsUnsaved = !chunk->isLoadedLights() &&
|
||||
settings.debug.doWriteLights;
|
||||
if (!chunk->isUnsaved() && !lightsUnsaved)
|
||||
continue;
|
||||
wfile->put(chunk.get());
|
||||
}
|
||||
|
||||
wfile->write(this, content);
|
||||
wfile->writePlayer(level->player);
|
||||
wfile->write(this, content);
|
||||
wfile->writePlayer(level->player);
|
||||
}
|
||||
|
||||
const float DEF_PLAYER_Y = 100.0f;
|
||||
const float DEF_PLAYER_SPEED = 4.0f;
|
||||
|
||||
Level* World::create(string name,
|
||||
path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
World* world = new World(name, directory, seed, settings, content, packs);
|
||||
Player* player = new Player(vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
|
||||
return new Level(world, content, player, settings);
|
||||
Level* World::create(std::string name,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
World* world = new World(name, directory, seed, settings, content, packs);
|
||||
Player* player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
|
||||
return new Level(world, content, player, settings);
|
||||
}
|
||||
|
||||
ContentLUT* World::checkIndices(const path& directory,
|
||||
const Content* content) {
|
||||
path indicesFile = directory/path("indices.json");
|
||||
if (fs::is_regular_file(indicesFile)) {
|
||||
return ContentLUT::create(indicesFile, content);
|
||||
}
|
||||
return nullptr;
|
||||
ContentLUT* World::checkIndices(const fs::path& directory,
|
||||
const Content* content) {
|
||||
fs::path indicesFile = directory/fs::path("indices.json");
|
||||
if (fs::is_regular_file(indicesFile)) {
|
||||
return ContentLUT::create(indicesFile, content);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Level* World::load(path directory,
|
||||
Level* World::load(fs::path directory,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
unique_ptr<World> world (new World(".", directory, 0, settings, content, packs));
|
||||
auto& wfile = world->wfile;
|
||||
const std::vector<ContentPack>& packs) {
|
||||
auto world = std::make_unique<World>(
|
||||
".", directory, 0, settings, content, packs
|
||||
);
|
||||
auto& wfile = world->wfile;
|
||||
|
||||
if (!wfile->readWorldInfo(world.get())) {
|
||||
throw world_load_error("could not to find world.json");
|
||||
}
|
||||
if (!wfile->readWorldInfo(world.get())) {
|
||||
throw world_load_error("could not to find world.json");
|
||||
}
|
||||
|
||||
Player* player = new Player(vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
|
||||
Level* level = new Level(world.get(), content, player, settings);
|
||||
wfile->readPlayer(player);
|
||||
Player* player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
|
||||
Level* level = new Level(world.get(), content, player, settings);
|
||||
wfile->readPlayer(player);
|
||||
|
||||
world.release();
|
||||
return level;
|
||||
world.release();
|
||||
return level;
|
||||
}
|
||||
|
||||
void World::setName(const std::string& name) {
|
||||
this->name = name;
|
||||
}
|
||||
|
||||
void World::setSeed(uint64_t seed) {
|
||||
this->seed = seed;
|
||||
}
|
||||
|
||||
std::string World::getName() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
uint64_t World::getSeed() const {
|
||||
return seed;
|
||||
}
|
||||
|
||||
const std::vector<ContentPack>& World::getPacks() const {
|
||||
return packs;
|
||||
return packs;
|
||||
}
|
||||
|
||||
@ -18,53 +18,61 @@ class Level;
|
||||
class Player;
|
||||
class ContentLUT;
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class world_load_error : public std::runtime_error {
|
||||
public:
|
||||
world_load_error(std::string message);
|
||||
world_load_error(std::string message);
|
||||
};
|
||||
|
||||
class World {
|
||||
EngineSettings& settings;
|
||||
const Content* const content;
|
||||
std::vector<ContentPack> packs;
|
||||
std::string name;
|
||||
uint64_t seed;
|
||||
EngineSettings& settings;
|
||||
const Content* const content;
|
||||
std::vector<ContentPack> packs;
|
||||
public:
|
||||
std::string name;
|
||||
WorldFiles* wfile;
|
||||
uint64_t seed;
|
||||
WorldFiles* wfile;
|
||||
|
||||
/* Day/night loop timer in range 0..1
|
||||
0.0 - is midnight
|
||||
0.5 - is noon
|
||||
*/
|
||||
float daytime = timeutil::time_value(10, 00, 00);
|
||||
float daytimeSpeed = 1.0f/60.0f/24.0f;
|
||||
/* Day/night loop timer in range 0..1
|
||||
0.0 - is midnight
|
||||
0.5 - is noon
|
||||
*/
|
||||
float daytime = timeutil::time_value(10, 00, 00);
|
||||
float daytimeSpeed = 1.0f/60.0f/24.0f;
|
||||
double totalTime = 0.0;
|
||||
|
||||
World(std::string name,
|
||||
std::filesystem::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
std::vector<ContentPack> packs);
|
||||
~World();
|
||||
World(std::string name,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
std::vector<ContentPack> packs);
|
||||
~World();
|
||||
|
||||
void updateTimers(float delta);
|
||||
void write(Level* level);
|
||||
void updateTimers(float delta);
|
||||
void write(Level* level);
|
||||
|
||||
static ContentLUT* checkIndices(const std::filesystem::path& directory,
|
||||
const Content* content);
|
||||
static ContentLUT* checkIndices(const fs::path& directory,
|
||||
const Content* content);
|
||||
|
||||
static Level* create(std::string name,
|
||||
std::filesystem::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs);
|
||||
static Level* load(std::filesystem::path directory,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs);
|
||||
|
||||
const std::vector<ContentPack>& getPacks() const;
|
||||
static Level* create(std::string name,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs);
|
||||
static Level* load(fs::path directory,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs);
|
||||
|
||||
void setName(const std::string& name);
|
||||
void setSeed(uint64_t seed);
|
||||
|
||||
std::string getName() const;
|
||||
uint64_t getSeed() const;
|
||||
const std::vector<ContentPack>& getPacks() const;
|
||||
};
|
||||
|
||||
#endif /* WORLD_WORLD_H_ */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user