refactor textures access
This commit is contained in:
parent
673f235ff9
commit
4f377b2056
24
src/assets/assets_util.cpp
Normal file
24
src/assets/assets_util.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include "assets_util.hpp"
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "graphics/core/Atlas.hpp"
|
||||
#include "graphics/core/Texture.hpp"
|
||||
|
||||
util::TextureRegion util::getTextureRegion(
|
||||
const Assets& assets, const std::string& name, const std::string& fallback
|
||||
) {
|
||||
size_t sep = name.find(':');
|
||||
if (sep == std::string::npos) {
|
||||
return {assets.get<Texture>(name), UVRegion(0,0,1,1)};
|
||||
} else {
|
||||
auto atlas = assets.get<Atlas>(name.substr(0, sep));
|
||||
if (atlas) {
|
||||
if (auto reg = atlas->getIf(name.substr(sep+1))) {
|
||||
return {atlas->getTexture(), *reg};
|
||||
} else if (!fallback.empty()){
|
||||
return util::getTextureRegion(assets, fallback, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
return {nullptr, UVRegion()};
|
||||
}
|
||||
21
src/assets/assets_util.hpp
Normal file
21
src/assets/assets_util.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "maths/UVRegion.hpp"
|
||||
|
||||
class Assets;
|
||||
class Texture;
|
||||
|
||||
namespace util {
|
||||
struct TextureRegion {
|
||||
const Texture* texture;
|
||||
UVRegion region;
|
||||
};
|
||||
|
||||
TextureRegion getTextureRegion(
|
||||
const Assets& assets,
|
||||
const std::string& name,
|
||||
const std::string& fallback
|
||||
);
|
||||
}
|
||||
@ -75,7 +75,7 @@ void Batch2D::vertex(
|
||||
buffer[index++] = a;
|
||||
}
|
||||
|
||||
void Batch2D::texture(Texture* new_texture){
|
||||
void Batch2D::texture(const Texture* new_texture){
|
||||
if (currentTexture == new_texture) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ class Batch2D : public Flushable {
|
||||
std::unique_ptr<Texture> blank;
|
||||
size_t index;
|
||||
glm::vec4 color;
|
||||
Texture* currentTexture;
|
||||
const Texture* currentTexture;
|
||||
DrawPrimitive primitive = DrawPrimitive::triangle;
|
||||
UVRegion region {0.0f, 0.0f, 1.0f, 1.0f};
|
||||
|
||||
@ -40,7 +40,7 @@ public:
|
||||
~Batch2D();
|
||||
|
||||
void begin();
|
||||
void texture(Texture* texture);
|
||||
void texture(const Texture* texture);
|
||||
void untexture();
|
||||
void setRegion(UVRegion region);
|
||||
void sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint);
|
||||
|
||||
@ -30,10 +30,10 @@ Cubemap::Cubemap(uint width, uint height, ImageFormat imageFormat)
|
||||
}
|
||||
}
|
||||
|
||||
void Cubemap::bind(){
|
||||
void Cubemap::bind() const {
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
|
||||
}
|
||||
|
||||
void Cubemap::unbind() {
|
||||
void Cubemap::unbind() const {
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
}
|
||||
|
||||
@ -7,6 +7,6 @@ class Cubemap : public GLTexture {
|
||||
public:
|
||||
Cubemap(uint width, uint height, ImageFormat format);
|
||||
|
||||
virtual void bind() override;
|
||||
virtual void unbind() override;
|
||||
virtual void bind() const override;
|
||||
virtual void unbind() const override;
|
||||
};
|
||||
|
||||
@ -33,11 +33,11 @@ GLTexture::~GLTexture() {
|
||||
glDeleteTextures(1, &id);
|
||||
}
|
||||
|
||||
void GLTexture::bind(){
|
||||
void GLTexture::bind() const {
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
}
|
||||
|
||||
void GLTexture::unbind() {
|
||||
void GLTexture::unbind() const {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
|
||||
@ -10,8 +10,8 @@ public:
|
||||
GLTexture(const ubyte* data, uint width, uint height, ImageFormat format);
|
||||
virtual ~GLTexture();
|
||||
|
||||
virtual void bind() override;
|
||||
virtual void unbind() override;
|
||||
virtual void bind() const override;
|
||||
virtual void unbind() const override;
|
||||
virtual void reload(const ubyte* data);
|
||||
|
||||
void setNearestFilter();
|
||||
|
||||
@ -17,8 +17,8 @@ public:
|
||||
|
||||
virtual ~Texture() {}
|
||||
|
||||
virtual void bind() = 0;
|
||||
virtual void unbind() = 0;
|
||||
virtual void bind() const = 0;
|
||||
virtual void unbind() const = 0;
|
||||
|
||||
virtual void reload(const ImageData& image) = 0;
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#include "ModelBatch.hpp"
|
||||
|
||||
#include "assets/assets_util.hpp"
|
||||
#include "graphics/core/Mesh.hpp"
|
||||
#include "graphics/core/Model.hpp"
|
||||
#include "graphics/core/Atlas.hpp"
|
||||
@ -144,7 +145,7 @@ void ModelBatch::setLightsOffset(const glm::vec3& offset) {
|
||||
|
||||
void ModelBatch::setTexture(const std::string& name,
|
||||
const texture_names_map* varTextures) {
|
||||
if (name.at(0) == '$') {
|
||||
if (varTextures && name.at(0) == '$') {
|
||||
const auto& found = varTextures->find(name);
|
||||
if (found == varTextures->end()) {
|
||||
return setTexture(nullptr);
|
||||
@ -152,25 +153,13 @@ void ModelBatch::setTexture(const std::string& name,
|
||||
return setTexture(found->second, varTextures);
|
||||
}
|
||||
}
|
||||
size_t sep = name.find(':');
|
||||
if (sep == std::string::npos) {
|
||||
setTexture(assets->get<Texture>(name));
|
||||
} else {
|
||||
auto atlas = assets->get<Atlas>(name.substr(0, sep));
|
||||
if (atlas == nullptr) {
|
||||
setTexture(nullptr);
|
||||
} else {
|
||||
setTexture(atlas->getTexture());
|
||||
if (auto reg = atlas->getIf(name.substr(sep+1))) {
|
||||
region = *reg;
|
||||
} else {
|
||||
setTexture("blocks:notfound", varTextures);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto textureRegion = util::getTextureRegion(*assets, name, "blocks:notfound");
|
||||
setTexture(textureRegion.texture);
|
||||
region = textureRegion.region;
|
||||
}
|
||||
|
||||
void ModelBatch::setTexture(Texture* texture) {
|
||||
void ModelBatch::setTexture(const Texture* texture) {
|
||||
if (texture == nullptr) {
|
||||
texture = blank.get();
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ class ModelBatch {
|
||||
|
||||
Assets* assets;
|
||||
Chunks* chunks;
|
||||
Texture* texture = nullptr;
|
||||
const Texture* texture = nullptr;
|
||||
UVRegion region {0.0f, 0.0f, 1.0f, 1.0f};
|
||||
const EngineSettings* settings;
|
||||
glm::vec3 lightsOffset {};
|
||||
@ -72,7 +72,7 @@ class ModelBatch {
|
||||
bool backlight);
|
||||
void setTexture(const std::string& name,
|
||||
const texture_names_map* varTextures);
|
||||
void setTexture(Texture* texture);
|
||||
void setTexture(const Texture* texture);
|
||||
void flush();
|
||||
|
||||
struct DrawEntry {
|
||||
|
||||
@ -376,13 +376,12 @@ void WorldRenderer::renderHands(const Camera& camera, const Assets& assets) {
|
||||
matrix = matrix * glm::translate(glm::mat4(1.0f), offset);
|
||||
|
||||
// render
|
||||
texture_names_map map = {};
|
||||
modelBatch->setLightsOffset(camera.position);
|
||||
modelBatch->draw(
|
||||
matrix,
|
||||
glm::vec3(1.0f),
|
||||
assets.get<model::Model>(def.modelName),
|
||||
&map
|
||||
nullptr
|
||||
);
|
||||
Window::clearDepth();
|
||||
setupWorldShader(entityShader, hudcam, engine->getSettings(), 0.0f);
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include "InventoryView.hpp"
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "assets/assets_util.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "frontend/LevelFrontend.hpp"
|
||||
#include "frontend/locale.hpp"
|
||||
@ -174,22 +175,13 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
|
||||
break;
|
||||
}
|
||||
case ItemIconType::SPRITE: {
|
||||
size_t index = item.icon.find(':');
|
||||
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->get<Texture>(name));
|
||||
} else {
|
||||
std::string atlasname = item.icon.substr(0, index);
|
||||
auto atlas = assets->get<Atlas>(atlasname);
|
||||
if (atlas && atlas->has(name)) {
|
||||
region = atlas->get(name);
|
||||
batch->texture(atlas->getTexture());
|
||||
}
|
||||
}
|
||||
auto textureRegion =
|
||||
util::getTextureRegion(*assets, item.icon, "blocks:notfound");
|
||||
|
||||
batch->texture(textureRegion.texture);
|
||||
batch->rect(
|
||||
pos.x, pos.y, slotSize, slotSize,
|
||||
0, 0, 0, region, false, true, tint);
|
||||
0, 0, 0, textureRegion.region, false, true, tint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user