update Assets container - template-based now
This commit is contained in:
parent
848d121099
commit
d54b6b2e58
@ -1,72 +1,8 @@
|
||||
#include "Assets.hpp"
|
||||
|
||||
#include "../audio/audio.hpp"
|
||||
#include "../graphics/core/Texture.hpp"
|
||||
#include "../graphics/core/Shader.hpp"
|
||||
#include "../graphics/core/Atlas.hpp"
|
||||
#include "../graphics/core/Font.hpp"
|
||||
#include "../frontend/UiDocument.hpp"
|
||||
#include "../logic/scripting/scripting.hpp"
|
||||
|
||||
Assets::~Assets() {
|
||||
}
|
||||
|
||||
Texture* Assets::getTexture(const std::string& name) const {
|
||||
auto found = textures.find(name);
|
||||
if (found == textures.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<Texture> texture, const std::string& name){
|
||||
textures.emplace(name, std::move(texture));
|
||||
}
|
||||
|
||||
|
||||
Shader* Assets::getShader(const std::string& name) const{
|
||||
auto found = shaders.find(name);
|
||||
if (found == shaders.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<Shader> shader, const std::string& name){
|
||||
shaders.emplace(name, std::move(shader));
|
||||
}
|
||||
|
||||
Font* Assets::getFont(const std::string& name) const {
|
||||
auto found = fonts.find(name);
|
||||
if (found == fonts.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<Font> font, const std::string& name){
|
||||
fonts.emplace(name, std::move(font));
|
||||
}
|
||||
|
||||
Atlas* Assets::getAtlas(const std::string& name) const {
|
||||
auto found = atlases.find(name);
|
||||
if (found == atlases.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<Atlas> atlas, const std::string& name){
|
||||
atlases.emplace(name, std::move(atlas));
|
||||
}
|
||||
|
||||
audio::Sound* Assets::getSound(const std::string& name) const {
|
||||
auto found = sounds.find(name);
|
||||
if (found == sounds.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<audio::Sound> sound, const std::string& name) {
|
||||
sounds.emplace(name, std::move(sound));
|
||||
}
|
||||
|
||||
const std::vector<TextureAnimation>& Assets::getAnimations() {
|
||||
return animations;
|
||||
}
|
||||
@ -74,14 +10,3 @@ const std::vector<TextureAnimation>& Assets::getAnimations() {
|
||||
void Assets::store(const TextureAnimation& animation) {
|
||||
animations.emplace_back(animation);
|
||||
}
|
||||
|
||||
UiDocument* Assets::getLayout(const std::string& name) const {
|
||||
auto found = layouts.find(name);
|
||||
if (found == layouts.end())
|
||||
return nullptr;
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
void Assets::store(std::unique_ptr<UiDocument> layout, const std::string& name) {
|
||||
layouts[name] = std::shared_ptr<UiDocument>(std::move(layout));
|
||||
}
|
||||
|
||||
@ -7,57 +7,56 @@
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <typeindex>
|
||||
#include <typeinfo>
|
||||
#include <vector>
|
||||
|
||||
class Texture;
|
||||
class Shader;
|
||||
class Font;
|
||||
class Atlas;
|
||||
class Assets;
|
||||
class UiDocument;
|
||||
|
||||
namespace audio {
|
||||
class Sound;
|
||||
}
|
||||
|
||||
namespace model {
|
||||
struct Model;
|
||||
}
|
||||
|
||||
namespace assetload {
|
||||
/// @brief final work to do in the main thread
|
||||
using postfunc = std::function<void(Assets*)>;
|
||||
}
|
||||
|
||||
class Assets {
|
||||
std::unordered_map<std::string, std::shared_ptr<Texture>> textures;
|
||||
std::unordered_map<std::string, std::shared_ptr<Shader>> shaders;
|
||||
std::unordered_map<std::string, std::shared_ptr<Font>> fonts;
|
||||
std::unordered_map<std::string, std::shared_ptr<Atlas>> atlases;
|
||||
std::unordered_map<std::string, std::shared_ptr<UiDocument>> layouts;
|
||||
std::unordered_map<std::string, std::shared_ptr<audio::Sound>> sounds;
|
||||
std::vector<TextureAnimation> animations;
|
||||
|
||||
using assets_map = std::unordered_map<std::string, std::shared_ptr<void>>;
|
||||
std::unordered_map<std::type_index, assets_map> assets;
|
||||
public:
|
||||
Assets() {}
|
||||
Assets(const Assets&) = delete;
|
||||
~Assets();
|
||||
|
||||
Texture* getTexture(const std::string& name) const;
|
||||
void store(std::unique_ptr<Texture> texture, const std::string& name);
|
||||
|
||||
Shader* getShader(const std::string& name) const;
|
||||
void store(std::unique_ptr<Shader> shader, const std::string& name);
|
||||
|
||||
Font* getFont(const std::string& name) const;
|
||||
void store(std::unique_ptr<Font> font, const std::string& name);
|
||||
|
||||
Atlas* getAtlas(const std::string& name) const;
|
||||
void store(std::unique_ptr<Atlas> atlas, const std::string& name);
|
||||
|
||||
audio::Sound* getSound(const std::string& name) const;
|
||||
void store(std::unique_ptr<audio::Sound> sound, const std::string& name);
|
||||
|
||||
const std::vector<TextureAnimation>& getAnimations();
|
||||
void store(const TextureAnimation& animation);
|
||||
|
||||
UiDocument* getLayout(const std::string& name) const;
|
||||
void store(std::unique_ptr<UiDocument> layout, const std::string& name);
|
||||
template<class T>
|
||||
void store(std::unique_ptr<T> asset, const std::string& name) {
|
||||
assets[typeid(T)][name].reset(asset.release());
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T* get(const std::string& name) const {
|
||||
const auto& mapIter = assets.find(typeid(T));
|
||||
if (mapIter == assets.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
const auto& map = mapIter->second;
|
||||
const auto& found = map.find(name);
|
||||
if (found == map.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<T*>(found->second.get());
|
||||
}
|
||||
};
|
||||
|
||||
#endif // ASSETS_ASSETS_HPP_
|
||||
|
||||
@ -212,7 +212,7 @@ bool AssetsLoader::loadExternalTexture(
|
||||
const std::string& name,
|
||||
const std::vector<std::filesystem::path>& alternatives
|
||||
) {
|
||||
if (assets->getTexture(name) != nullptr) {
|
||||
if (assets->get<Texture>(name) != nullptr) {
|
||||
return true;
|
||||
}
|
||||
for (auto& path : alternatives) {
|
||||
|
||||
@ -15,7 +15,7 @@
|
||||
ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) : content(content) {
|
||||
auto indices = content->getIndices();
|
||||
sideregions = std::make_unique<UVRegion[]>(indices->countBlockDefs() * 6);
|
||||
Atlas* atlas = assets->getAtlas("blocks");
|
||||
auto atlas = assets->get<Atlas>("blocks");
|
||||
|
||||
for (uint i = 0; i < indices->countBlockDefs(); i++) {
|
||||
Block* def = indices->getBlockDef(i);
|
||||
|
||||
@ -30,7 +30,7 @@ LevelFrontend::LevelFrontend(LevelController* controller, Assets* assets)
|
||||
}
|
||||
|
||||
if (type == BlockInteraction::step) {
|
||||
auto sound = assets->getSound(material->stepsSound);
|
||||
auto sound = assets->get<audio::Sound>(material->stepsSound);
|
||||
audio::play(
|
||||
sound,
|
||||
glm::vec3(),
|
||||
@ -45,10 +45,10 @@ LevelFrontend::LevelFrontend(LevelController* controller, Assets* assets)
|
||||
audio::Sound* sound = nullptr;
|
||||
switch (type) {
|
||||
case BlockInteraction::placing:
|
||||
sound = assets->getSound(material->placeSound);
|
||||
sound = assets->get<audio::Sound>(material->placeSound);
|
||||
break;
|
||||
case BlockInteraction::destruction:
|
||||
sound = assets->getSound(material->breakSound);
|
||||
sound = assets->get<audio::Sound>(material->breakSound);
|
||||
break;
|
||||
case BlockInteraction::step:
|
||||
break;
|
||||
|
||||
@ -208,7 +208,7 @@ void Hud::processInput(bool visible) {
|
||||
}
|
||||
}
|
||||
if (!pause && Events::active(BIND_DEVTOOLS_CONSOLE)) {
|
||||
showOverlay(assets->getLayout("core:console"), false);
|
||||
showOverlay(assets->get<UiDocument>("core:console"), false);
|
||||
}
|
||||
if (!Window::isFocused() && !pause && !isInventoryOpen()) {
|
||||
setPause(true);
|
||||
@ -305,7 +305,7 @@ void Hud::openInventory() {
|
||||
|
||||
inventoryOpen = true;
|
||||
auto inventory = player->getInventory();
|
||||
auto inventoryDocument = assets->getLayout("core:inventory");
|
||||
auto inventoryDocument = assets->get<UiDocument>("core:inventory");
|
||||
inventoryView = std::dynamic_pointer_cast<InventoryView>(inventoryDocument->getRoot());
|
||||
inventoryView->bind(inventory, content);
|
||||
add(HudElement(hud_element_mode::inventory_bound, inventoryDocument, inventoryView, false));
|
||||
@ -460,7 +460,7 @@ void Hud::draw(const DrawContext& ctx){
|
||||
auto batch = ctx.getBatch2D();
|
||||
batch->begin();
|
||||
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
auto uishader = assets->get<Shader>("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
|
||||
@ -468,7 +468,7 @@ void Hud::draw(const DrawContext& ctx){
|
||||
if (!pause && !inventoryOpen && !player->debug) {
|
||||
DrawContext chctx = ctx.sub();
|
||||
chctx.setBlendMode(BlendMode::inversion);
|
||||
auto texture = assets->getTexture("gui/crosshair");
|
||||
auto texture = assets->get<Texture>("gui/crosshair");
|
||||
batch->texture(texture);
|
||||
int chsizex = texture != nullptr ? texture->getWidth() : 16;
|
||||
int chsizey = texture != nullptr ? texture->getHeight() : 16;
|
||||
|
||||
@ -35,14 +35,14 @@ void MenuScreen::draw(float delta) {
|
||||
Window::setBgColor(glm::vec3(0.2f));
|
||||
|
||||
uicamera->setFov(Window::height);
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
auto uishader = assets->get<Shader>("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
|
||||
uint width = Window::width;
|
||||
uint height = Window::height;
|
||||
|
||||
auto bg = assets->getTexture("gui/menubg");
|
||||
auto bg = assets->get<Texture>("gui/menubg");
|
||||
batch->begin();
|
||||
batch->texture(bg);
|
||||
batch->rect(
|
||||
|
||||
@ -126,8 +126,8 @@ std::unique_ptr<Atlas> BlocksPreview::build(
|
||||
size_t count = indices->countBlockDefs();
|
||||
size_t iconSize = ITEM_ICON_SIZE;
|
||||
|
||||
Shader* shader = assets->getShader("ui3d");
|
||||
Atlas* atlas = assets->getAtlas("blocks");
|
||||
auto shader = assets->get<Shader>("ui3d");
|
||||
auto atlas = assets->get<Atlas>("blocks");
|
||||
|
||||
Viewport viewport(iconSize, iconSize);
|
||||
DrawContext pctx(nullptr, viewport, nullptr);
|
||||
|
||||
@ -60,7 +60,7 @@ void ModelBatch::draw(const model::Model& model) {
|
||||
Lightmap::extract(light, 3) / 15.0f
|
||||
);
|
||||
for (const auto& mesh : model.meshes) {
|
||||
auto texture = assets->getTexture(mesh.texture);
|
||||
auto texture = assets->get<Texture>(mesh.texture);
|
||||
if (texture) {
|
||||
texture->bind();
|
||||
} else {
|
||||
|
||||
@ -59,7 +59,7 @@ Skybox::~Skybox() {
|
||||
}
|
||||
|
||||
void Skybox::drawBackground(Camera* camera, Assets* assets, int width, int height) {
|
||||
Shader* backShader = assets->getShader("background");
|
||||
auto backShader = assets->get<Shader>("background");
|
||||
backShader->use();
|
||||
backShader->uniformMatrix("u_view", camera->getView(false));
|
||||
backShader->uniform1f("u_zoom", camera->zoom*camera->getFov()/(M_PI*0.5f));
|
||||
@ -107,7 +107,7 @@ void Skybox::draw(
|
||||
DrawContext ctx = pctx.sub();
|
||||
ctx.setBlendMode(BlendMode::addition);
|
||||
|
||||
Shader* shader = assets->getShader("ui3d");
|
||||
auto shader = assets->get<Shader>("ui3d");
|
||||
shader->use();
|
||||
shader->uniformMatrix("u_projview", camera->getProjView(false));
|
||||
shader->uniformMatrix("u_apply", glm::mat4(1.0f));
|
||||
@ -117,7 +117,7 @@ void Skybox::draw(
|
||||
float opacity = glm::pow(1.0f-fog, 7.0f);
|
||||
|
||||
for (auto& sprite : sprites) {
|
||||
batch3d->texture(assets->getTexture(sprite.texture));
|
||||
batch3d->texture(assets->get<Texture>(sprite.texture));
|
||||
|
||||
float sangle = daytime * M_PI*2 + sprite.phase;
|
||||
float distance = sprite.distance;
|
||||
|
||||
@ -68,7 +68,7 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* pl
|
||||
auto assets = engine->getAssets();
|
||||
skybox = std::make_unique<Skybox>(
|
||||
settings.graphics.skyboxResolution.get(),
|
||||
assets->getShader("skybox_gen")
|
||||
assets->get<Shader>("skybox_gen")
|
||||
);
|
||||
}
|
||||
|
||||
@ -154,9 +154,9 @@ void WorldRenderer::renderLevel(
|
||||
Camera* camera,
|
||||
const EngineSettings& settings
|
||||
) {
|
||||
Assets* assets = engine->getAssets();
|
||||
Atlas* atlas = assets->getAtlas("blocks");
|
||||
Shader* shader = assets->getShader("main");
|
||||
auto assets = engine->getAssets();
|
||||
auto atlas = assets->get<Atlas>("blocks");
|
||||
auto shader = assets->get<Shader>("main");
|
||||
|
||||
auto indices = level->content->getIndices();
|
||||
|
||||
@ -292,8 +292,8 @@ void WorldRenderer::draw(
|
||||
const EngineSettings& settings = engine->getSettings();
|
||||
skybox->refresh(pctx, world->daytime, 1.0f+world->fog*2.0f, 4);
|
||||
|
||||
Assets* assets = engine->getAssets();
|
||||
Shader* linesShader = assets->getShader("lines");
|
||||
auto assets = engine->getAssets();
|
||||
auto linesShader = assets->get<Shader>("lines");
|
||||
|
||||
// World render scope with diegetic HUD included
|
||||
{
|
||||
@ -323,7 +323,7 @@ void WorldRenderer::draw(
|
||||
}
|
||||
|
||||
// Rendering fullscreen quad with
|
||||
auto screenShader = assets->getShader("screen");
|
||||
auto screenShader = assets->get<Shader>("screen");
|
||||
screenShader->use();
|
||||
screenShader->uniform1f("u_timer", Window::time());
|
||||
screenShader->uniform1f("u_dayTime", level->getWorld()->daytime);
|
||||
|
||||
@ -205,7 +205,7 @@ void GUI::draw(const DrawContext* pctx, Assets* assets) {
|
||||
menu->setPos((wsize - menu->getSize()) / 2.0f);
|
||||
uicamera->setFov(wsize.y);
|
||||
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
auto uishader = assets->get<Shader>("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
|
||||
|
||||
@ -18,7 +18,7 @@ void Image::draw(const DrawContext* pctx, Assets* assets) {
|
||||
glm::vec2 pos = calcPos();
|
||||
auto batch = pctx->getBatch2D();
|
||||
|
||||
auto texture = assets->getTexture(this->texture);
|
||||
auto texture = assets->get<Texture>(this->texture);
|
||||
if (texture && autoresize) {
|
||||
setSize(glm::vec2(texture->getWidth(), texture->getHeight()));
|
||||
}
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
#include "../../core/Font.hpp"
|
||||
#include "../../core/DrawContext.hpp"
|
||||
#include "../../core/Shader.hpp"
|
||||
#include "../../core/Texture.hpp"
|
||||
#include "../../render/BlocksPreview.hpp"
|
||||
#include "../GUI.hpp"
|
||||
|
||||
@ -155,7 +156,7 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
|
||||
|
||||
batch->setColor(glm::vec4(1.0f));
|
||||
|
||||
auto previews = assets->getAtlas("block-previews");
|
||||
auto previews = assets->get<Atlas>("block-previews");
|
||||
auto indices = content->getIndices();
|
||||
|
||||
ItemDef* item = indices->getItemDef(stack.getItemId());
|
||||
@ -177,10 +178,10 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
|
||||
std::string name = item->icon.substr(index+1);
|
||||
UVRegion region(0.0f, 0.0, 1.0f, 1.0f);
|
||||
if (index == std::string::npos) {
|
||||
batch->texture(assets->getTexture(name));
|
||||
batch->texture(assets->get<Texture>(name));
|
||||
} else {
|
||||
std::string atlasname = item->icon.substr(0, index);
|
||||
Atlas* atlas = assets->getAtlas(atlasname);
|
||||
auto atlas = assets->get<Atlas>(atlasname);
|
||||
if (atlas && atlas->has(name)) {
|
||||
region = atlas->get(name);
|
||||
batch->texture(atlas->getTexture());
|
||||
@ -194,7 +195,7 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
|
||||
}
|
||||
|
||||
if (stack.getCount() > 1) {
|
||||
auto font = assets->getFont("normal");
|
||||
auto font = assets->get<Font>("normal");
|
||||
std::wstring text = std::to_wstring(stack.getCount());
|
||||
|
||||
int x = pos.x+slotSize-text.length()*8;
|
||||
|
||||
@ -158,7 +158,7 @@ uint Label::getLinesNumber() const {
|
||||
|
||||
void Label::draw(const DrawContext* pctx, Assets* assets) {
|
||||
auto batch = pctx->getBatch2D();
|
||||
auto font = assets->getFont(fontName);
|
||||
auto font = assets->get<Font>(fontName);
|
||||
cache.prepare(font, static_cast<size_t>(glm::abs(getSize().x)));
|
||||
|
||||
if (supplier) {
|
||||
|
||||
@ -37,7 +37,7 @@ void Plotter::draw(const DrawContext* pctx, Assets* assets) {
|
||||
}
|
||||
|
||||
int current_point = static_cast<int>(points[index % dmwidth]);
|
||||
auto font = assets->getFont("normal");
|
||||
auto font = assets->get<Font>("normal");
|
||||
for (int y = 0; y < dmheight; y += labelsInterval) {
|
||||
std::wstring string;
|
||||
if (current_point/16 == y/labelsInterval) {
|
||||
|
||||
@ -33,7 +33,7 @@ TextBox::TextBox(std::wstring placeholder, glm::vec4 padding)
|
||||
void TextBox::draw(const DrawContext* pctx, Assets* assets) {
|
||||
Panel::draw(pctx, assets);
|
||||
|
||||
font = assets->getFont(label->getFontName());
|
||||
font = assets->get<Font>(label->getFontName());
|
||||
|
||||
if (!isFocused()) {
|
||||
return;
|
||||
|
||||
@ -32,7 +32,7 @@ inline audio::speakerid_t play_sound(
|
||||
return 0;
|
||||
}
|
||||
auto assets = scripting::engine->getAssets();
|
||||
auto sound = assets->getSound(name);
|
||||
auto sound = assets->get<audio::Sound>(name);
|
||||
if (sound == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -27,7 +27,7 @@ struct DocumentNode {
|
||||
};
|
||||
|
||||
static DocumentNode getDocumentNode(lua::State*, const std::string& name, const std::string& nodeName) {
|
||||
auto doc = engine->getAssets()->getLayout(name);
|
||||
auto doc = engine->getAssets()->get<UiDocument>(name);
|
||||
if (doc == nullptr) {
|
||||
throw std::runtime_error("document '"+name+"' not found");
|
||||
}
|
||||
@ -545,7 +545,7 @@ static int l_gui_setattr(lua::State* L) {
|
||||
|
||||
static int l_gui_get_env(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto doc = engine->getAssets()->getLayout(name);
|
||||
auto doc = engine->getAssets()->get<UiDocument>(name);
|
||||
if (doc == nullptr) {
|
||||
throw std::runtime_error("document '"+std::string(name)+"' not found");
|
||||
}
|
||||
@ -566,7 +566,7 @@ static int l_gui_str(lua::State* L) {
|
||||
|
||||
static int l_gui_reindex(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto doc = engine->getAssets()->getLayout(name);
|
||||
auto doc = engine->getAssets()->get<UiDocument>(name);
|
||||
if (doc == nullptr) {
|
||||
throw std::runtime_error("document '"+std::string(name)+"' not found");
|
||||
}
|
||||
|
||||
@ -51,7 +51,7 @@ static int l_hud_open_block(lua::State* L) {
|
||||
}
|
||||
auto def = content->getIndices()->getBlockDef(vox->id);
|
||||
auto assets = engine->getAssets();
|
||||
auto layout = assets->getLayout(def->uiLayout);
|
||||
auto layout = assets->get<UiDocument>(def->uiLayout);
|
||||
if (layout == nullptr) {
|
||||
throw std::runtime_error("block '"+def->name+"' has no ui layout");
|
||||
}
|
||||
@ -71,7 +71,7 @@ static int l_hud_show_overlay(lua::State* L) {
|
||||
bool playerInventory = lua::toboolean(L, 2);
|
||||
|
||||
auto assets = engine->getAssets();
|
||||
auto layout = assets->getLayout(name);
|
||||
auto layout = assets->get<UiDocument>(name);
|
||||
if (layout == nullptr) {
|
||||
throw std::runtime_error("there is no ui layout "+util::quote(name));
|
||||
}
|
||||
@ -81,7 +81,7 @@ static int l_hud_show_overlay(lua::State* L) {
|
||||
|
||||
static UiDocument* require_layout(const char* name) {
|
||||
auto assets = engine->getAssets();
|
||||
auto layout = assets->getLayout(name);
|
||||
auto layout = assets->get<UiDocument>(name);
|
||||
if (layout == nullptr) {
|
||||
throw std::runtime_error("layout '"+std::string(name)+"' is not found");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user