Move content registry, Object/BlockDef to Content namespace

This commit is contained in:
Dorian Wouters 2016-08-11 22:39:03 +02:00
parent 41a326a737
commit dd04f4a968
No known key found for this signature in database
GPG Key ID: 6E9DA8063322434B
10 changed files with 56 additions and 51 deletions

View File

@ -281,7 +281,7 @@ void Chunk::updateClient() {
imcUncompress(); imcUncompress();
#endif #endif
mut.lock(); mut.lock();
ContentRegistry &CR = *G->CR; Content::Registry &CR = *G->CR;
Vertex vertex[CX * CY * CZ * 6 /* faces */ * 4 /* vertices */ / 2 /* face removing (HSR) makes a lower vert max */]; Vertex vertex[CX * CY * CZ * 6 /* faces */ * 4 /* vertices */ / 2 /* face removing (HSR) makes a lower vert max */];
GLushort idxOpaque[CX * CY * CZ * 6 /* faces */ * 6 /* indices */ / 2 /* HSR */], GLushort idxOpaque[CX * CY * CZ * 6 /* faces */ * 6 /* indices */ / 2 /* HSR */],
idxTransp[CX*CY*CZ*6*6/2]; idxTransp[CX*CY*CZ*6*6/2];

View File

@ -19,7 +19,7 @@ Game::Game() :
} }
void Game::init() { void Game::init() {
CR = new ContentRegistry; CR = new Content::Registry;
LS = new Scripting::Lua::State(this); LS = new Scripting::Lua::State(this);
if (GlobalProperties::IsClient) { if (GlobalProperties::IsClient) {
PM = new ProgramManager(*this); PM = new ProgramManager(*this);

View File

@ -11,6 +11,10 @@ using std::shared_ptr;
namespace Diggler { namespace Diggler {
namespace Content {
class Registry;
}
namespace Render { namespace Render {
class Renderer; class Renderer;
} }
@ -26,7 +30,6 @@ class Manager;
} }
class Audio; class Audio;
class ContentRegistry;
class Config; class Config;
class GameWindow; class GameWindow;
class KeyBinds; class KeyBinds;
@ -39,7 +42,7 @@ public:
Net::Host H; Net::Host H;
Universe *U; Universe *U;
PlayerList players; PlayerList players;
ContentRegistry *CR; Content::Registry *CR;
Scripting::Lua::State *LS; Scripting::Lua::State *LS;
// Server // Server

View File

@ -1,5 +1,5 @@
#ifndef DIGGLER_BLOCK_DEF_HPP #ifndef DIGGLER_CONTENT_BLOCK_DEF_HPP
#define DIGGLER_BLOCK_DEF_HPP #define DIGGLER_CONTENT_BLOCK_DEF_HPP
#include <unordered_map> #include <unordered_map>
@ -9,6 +9,7 @@
//#include "../AABB.hpp" //#include "../AABB.hpp"
namespace Diggler { namespace Diggler {
namespace Content {
class BlockDef : public ObjectDef { class BlockDef : public ObjectDef {
public: public:
@ -51,6 +52,7 @@ public:
} phys; } phys;
}; };
}
} }
#endif /* DIGGLER_BLOCK_DEF_HPP */ #endif /* DIGGLER_CONTENT_BLOCK_DEF_HPP */

View File

@ -1,7 +1,7 @@
#ifndef CONTENT_HPP #ifndef DIGGLER_CONTENT_HPP
#define CONTENT_HPP #define DIGGLER_CONTENT_HPP
#include "../Platform.hpp" #include "../platform/Types.hpp"
#include <limits> #include <limits>
#include <type_traits> #include <type_traits>
@ -43,4 +43,4 @@ namespace Content {
} }
} }
#endif #endif /* DIGGLER_CONTENT_HPP */

View File

@ -1,8 +1,9 @@
#ifndef OBJECT_DEF_HPP #ifndef DIGGLER_CONTENT_OBJECT_DEF_HPP
#define OBJECT_DEF_HPP #define DIGGLER_CONTENT_OBJECT_DEF_HPP
#include "../Platform.hpp" #include "../Platform.hpp"
namespace Diggler { namespace Diggler {
namespace Content {
class ObjectDef { class ObjectDef {
public: public:
@ -13,6 +14,7 @@ public:
}; };
}; };
}
} }
#endif #endif /* DIGGLER_CONTENT_OBJECT_DEF_HPP */

View File

@ -5,24 +5,23 @@
#define PRINT_BLOCK_REGISTRATIONS 1 #define PRINT_BLOCK_REGISTRATIONS 1
namespace Diggler { namespace Diggler {
namespace Content {
using CR = ContentRegistry; Registry::BlockRegistration::BlockRegistration(Registry &registry,
const Registry::BlockNameMap::iterator &it) :
CR::BlockRegistration::BlockRegistration(ContentRegistry &registry,
const ContentRegistry::BlockNameMap::iterator &it) :
registry(registry), registry(registry),
it(it), it(it),
state(Uncommitted), state(Uncommitted),
def(it->second->second) { def(it->second->second) {
} }
CR::BlockRegistration::~BlockRegistration() { Registry::BlockRegistration::~BlockRegistration() {
if (state == Uncommitted) { if (state == Uncommitted) {
registry.m_blocks.erase(it->second); registry.m_blocks.erase(it->second);
} }
} }
CR::BlockRegistration::BlockRegistration(CR::BlockRegistration &&o) : Registry::BlockRegistration::BlockRegistration(Registry::BlockRegistration &&o) :
registry(o.registry), registry(o.registry),
it(o.it), it(o.it),
state(o.state), state(o.state),
@ -30,7 +29,7 @@ CR::BlockRegistration::BlockRegistration(CR::BlockRegistration &&o) :
o.state = Moved; o.state = Moved;
} }
BlockId CR::BlockRegistration::commit() { BlockId Registry::BlockRegistration::commit() {
state = Committed; state = Committed;
#if PRINT_BLOCK_REGISTRATIONS #if PRINT_BLOCK_REGISTRATIONS
getDebugStream() << "Registered block " << it->first << " with id " << getDebugStream() << "Registered block " << it->first << " with id " <<
@ -76,14 +75,14 @@ static const DefBlocksInfo DefBlocksInfos[] = {
{"diggler:transp_blue", "Force Field", 0, 0, 25, ANY, "translucent_blue.png"} {"diggler:transp_blue", "Force Field", 0, 0, 25, ANY, "translucent_blue.png"}
}; };
bool ContentRegistry::isTransparent(BlockId id) const { bool Registry::isTransparent(BlockId id) const {
if (id == Content::BlockAirId) if (id == Content::BlockAirId)
return true; return true;
return false; return false;
// TODO return getBlockDef(id).isTransparent; // TODO return getBlockDef(id).isTransparent;
} }
bool ContentRegistry::isFaceVisible(BlockId id1, BlockId id2) const { bool Registry::isFaceVisible(BlockId id1, BlockId id2) const {
// TODO: node mesh/boxes -> not fullblock, faces may not be hidden // TODO: node mesh/boxes -> not fullblock, faces may not be hidden
if (isTransparent(id1)) { if (isTransparent(id1)) {
return (id1 != id2); return (id1 != id2);
@ -92,7 +91,7 @@ bool ContentRegistry::isFaceVisible(BlockId id1, BlockId id2) const {
} }
} }
bool ContentRegistry::canEntityGoThrough(BlockId id/* , Entity& ent*/) const { bool Registry::canEntityGoThrough(BlockId id/* , Entity& ent*/) const {
if (id == Content::BlockAirId) if (id == Content::BlockAirId)
return true; return true;
return false; return false;
@ -103,15 +102,15 @@ bool ContentRegistry::canEntityGoThrough(BlockId id/* , Entity& ent*/) const {
using Coord = TexturePacker::Coord; using Coord = TexturePacker::Coord;
static Coord unk1, unk2, unk3, unk4, unk5, unk6, unk7, unk8; static Coord unk1, unk2, unk3, unk4, unk5, unk6, unk7, unk8;
#define AddTex(b, t) Coord b = m_texturePacker->add(getAssetPath("blocks", t)); #define AddTex(b, t) Coord b = m_texturePacker->add(getAssetPath("blocks", t));
ContentRegistry::ContentRegistry() : Registry::Registry() :
m_atlas(nullptr), m_atlas(nullptr),
m_nextMaxBlockId(Content::BlockUnknownId + 1) { m_nextMaxBlockId(Content::BlockUnknownId + 1) {
{ ContentRegistry::BlockRegistration br(registerBlock(Content::BlockAirId, "air")); { Registry::BlockRegistration br(registerBlock(Content::BlockAirId, "air"));
br.def.appearance.look.type = BlockDef::Appearance::Look::Type::Hidden; br.def.appearance.look.type = BlockDef::Appearance::Look::Type::Hidden;
br.def.phys.hasCollision = false; br.def.phys.hasCollision = false;
br.commit(); br.commit();
} }
{ ContentRegistry::BlockRegistration br(registerBlock(Content::BlockUnknownId, "unknown")); { Registry::BlockRegistration br(registerBlock(Content::BlockUnknownId, "unknown"));
br.def.appearance.look.type = BlockDef::Appearance::Look::Type::Hidden; br.def.appearance.look.type = BlockDef::Appearance::Look::Type::Hidden;
br.def.phys.hasCollision = true; br.def.phys.hasCollision = true;
br.commit(); br.commit();
@ -134,11 +133,11 @@ ContentRegistry::ContentRegistry() :
m_atlas = m_texturePacker->getAtlas(); m_atlas = m_texturePacker->getAtlas();
} }
ContentRegistry::~ContentRegistry() { Registry::~Registry() {
delete m_texturePacker; delete m_texturePacker;
} }
TexturePacker::Coord ContentRegistry::addTexture(const std::string &texName, TexturePacker::Coord Registry::addTexture(const std::string &texName,
const std::string &path) { const std::string &path) {
const TexturePacker::Coord coord = m_texturePacker->add(path); const TexturePacker::Coord coord = m_texturePacker->add(path);
m_textureCoords.emplace(std::piecewise_construct, m_textureCoords.emplace(std::piecewise_construct,
@ -147,7 +146,7 @@ TexturePacker::Coord ContentRegistry::addTexture(const std::string &texName,
return coord; return coord;
} }
const TexturePacker::Coord* ContentRegistry::blockTexCoord(BlockId t, FaceDirection d, const TexturePacker::Coord* Registry::blockTexCoord(BlockId t, FaceDirection d,
const glm::ivec3 &pos) const { const glm::ivec3 &pos) const {
if (t == Content::BlockUnknownId) { if (t == Content::BlockUnknownId) {
const Coord *unk[] = { const Coord *unk[] = {
@ -199,11 +198,11 @@ const TexturePacker::Coord* ContentRegistry::blockTexCoord(BlockId t, FaceDirect
return nullptr; return nullptr;
} }
const Texture* ContentRegistry::getAtlas() const { const Texture* Registry::getAtlas() const {
return m_atlas; return m_atlas;
} }
ContentRegistry::BlockRegistration ContentRegistry::registerBlock(BlockId id, const char *name) { Registry::BlockRegistration Registry::registerBlock(BlockId id, const char *name) {
BlockIdMap::iterator bit = m_blocks.emplace(std::piecewise_construct, BlockIdMap::iterator bit = m_blocks.emplace(std::piecewise_construct,
std::forward_as_tuple(id), std::forward_as_tuple(id),
std::forward_as_tuple()) std::forward_as_tuple())
@ -214,7 +213,7 @@ ContentRegistry::BlockRegistration ContentRegistry::registerBlock(BlockId id, co
.first); .first);
} }
ContentRegistry::BlockRegistration ContentRegistry::registerBlock(const char *name) { Registry::BlockRegistration Registry::registerBlock(const char *name) {
BlockId id = Content::BlockUnknownId; BlockId id = Content::BlockUnknownId;
if (m_freedBlockIds.empty()) { if (m_freedBlockIds.empty()) {
id = m_nextMaxBlockId; id = m_nextMaxBlockId;
@ -227,3 +226,4 @@ ContentRegistry::BlockRegistration ContentRegistry::registerBlock(const char *na
} }
} }
}

View File

@ -1,5 +1,5 @@
#ifndef CONTENT_REGISTRY_HPP #ifndef DIGGLER_CONTENT_REGISTRY_HPP
#define CONTENT_REGISTRY_HPP #define DIGGLER_CONTENT_REGISTRY_HPP
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -22,14 +22,16 @@ enum class FaceDirection : uint8_t {
ZDec = 5 ZDec = 5
}; };
class ContentRegistry { namespace Content {
class Registry {
public: public:
using BlockIdMap = std::unordered_map<BlockId, BlockDef>; using BlockIdMap = std::unordered_map<BlockId, BlockDef>;
using BlockNameMap = std::unordered_map<std::string, BlockIdMap::iterator>; using BlockNameMap = std::unordered_map<std::string, BlockIdMap::iterator>;
class BlockRegistration { class BlockRegistration {
protected: protected:
ContentRegistry &registry; Registry &registry;
const BlockNameMap::iterator it; const BlockNameMap::iterator it;
enum { enum {
Uncommitted, Uncommitted,
@ -39,7 +41,7 @@ public:
public: public:
BlockDef &def; BlockDef &def;
BlockRegistration(ContentRegistry &registry, const BlockNameMap::iterator &it); BlockRegistration(Registry &registry, const BlockNameMap::iterator &it);
~BlockRegistration(); ~BlockRegistration();
BlockRegistration(const BlockRegistration&) = delete; BlockRegistration(const BlockRegistration&) = delete;
@ -66,14 +68,14 @@ private:
std::vector<BlockId> m_freedBlockIds; std::vector<BlockId> m_freedBlockIds;
// No copy // No copy
ContentRegistry(const ContentRegistry&) = delete; Registry(const Registry&) = delete;
ContentRegistry& operator=(const ContentRegistry&) = delete; Registry& operator=(const Registry&) = delete;
BlockRegistration registerBlock(BlockId id, const char *name); BlockRegistration registerBlock(BlockId id, const char *name);
public: public:
ContentRegistry(); Registry();
~ContentRegistry(); ~Registry();
bool isTransparent(BlockId id) const; bool isTransparent(BlockId id) const;
bool isFaceVisible(BlockId id1, BlockId id2) const; bool isFaceVisible(BlockId id1, BlockId id2) const;
@ -89,6 +91,7 @@ public:
const BlockDef& getBlockDef(BlockId); const BlockDef& getBlockDef(BlockId);
}; };
}
} }
#endif #endif /* DIGGLER_CONTENT_REGISTRY_HPP */

View File

@ -66,7 +66,7 @@ bool ClientMessageHandler::handleMessage(InMessage &msg) {
std::string playerName; std::string playerName;
if (cpt.player.display.type == msgpack::type::NIL) { if (cpt.player.display.type == msgpack::type::NIL) {
const Player *blabbermouth = GS.G->players.getByGameId(cpt.player.id); const Player *blabbermouth = GS.G->players.getByGameId(cpt.player.id);
[[likely(true)]] if (blabbermouth != nullptr) { if (blabbermouth != nullptr) {
playerName = blabbermouth->name + "> "; playerName = blabbermouth->name + "> ";
} else { } else {
playerName = "?> "; playerName = "?> ";

View File

@ -8,8 +8,9 @@ using namespace Diggler;
void Diggler_Content_Registry_registerBlock(struct Diggler_Game *cG, void Diggler_Content_Registry_registerBlock(struct Diggler_Game *cG,
const char *name, struct Diggler_Content_BlockDef *cBdef) { const char *name, struct Diggler_Content_BlockDef *cBdef) {
using namespace Content;
Game &G = *reinterpret_cast<Game*>(cG); Game &G = *reinterpret_cast<Game*>(cG);
ContentRegistry::BlockRegistration br(G.CR->registerBlock(name)); Registry::BlockRegistration br(G.CR->registerBlock(name));
{ decltype(cBdef->appearance) &cApp = cBdef->appearance; { decltype(cBdef->appearance) &cApp = cBdef->appearance;
decltype(br.def.appearance) &app = br.def.appearance; decltype(br.def.appearance) &app = br.def.appearance;
std::vector<decltype(app.textures)::iterator> textureIts; std::vector<decltype(app.textures)::iterator> textureIts;
@ -29,9 +30,6 @@ void Diggler_Content_Registry_registerBlock(struct Diggler_Game *cG,
if (cTex.repeatXdiv > 1 || cTex.repeatYdiv > 1) { if (cTex.repeatXdiv > 1 || cTex.repeatYdiv > 1) {
uint16 width = (tex.coord.u - tex.coord.x) / cTex.repeatXdiv, uint16 width = (tex.coord.u - tex.coord.x) / cTex.repeatXdiv,
height = (tex.coord.v - tex.coord.y) / cTex.repeatYdiv; height = (tex.coord.v - tex.coord.y) / cTex.repeatYdiv;
getDebugStream() << "Split " << tex.coord.x << ' ' << tex.coord.y << ' ' << tex.coord.u <<
' ' << tex.coord.v << " into " << static_cast<int>(tex.repeat.xdiv) << 'x' <<
static_cast<int>(tex.repeat.ydiv) << std::endl;
for (int16 y = cTex.repeatYdiv - 1; y >= 0; --y) { for (int16 y = cTex.repeatYdiv - 1; y >= 0; --y) {
for (int16 x = cTex.repeatXdiv - 1; x >= 0; --x) { for (int16 x = cTex.repeatXdiv - 1; x >= 0; --x) {
tex.divCoords.emplace_back(TexturePacker::Coord { tex.divCoords.emplace_back(TexturePacker::Coord {
@ -40,9 +38,6 @@ void Diggler_Content_Registry_registerBlock(struct Diggler_Game *cG,
static_cast<uint16>(tex.coord.x + width * (x + 1)), static_cast<uint16>(tex.coord.x + width * (x + 1)),
static_cast<uint16>(tex.coord.y + height * (y + 1)) static_cast<uint16>(tex.coord.y + height * (y + 1))
}); });
const TexturePacker::Coord &coord = tex.divCoords.back();
getDebugStream() << "> " << coord.x << ' ' << coord.y << ' ' << coord.u <<
' ' << coord.v << std::endl;
} }
} }
} }