mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-22 02:12:45 +01:00
Use unique_ptr to handle TextureData
This commit is contained in:
parent
f15f3cefdd
commit
195020df46
@ -145,10 +145,10 @@ struct Geometry {
|
||||
struct Texture {
|
||||
std::string name;
|
||||
std::string alphaName;
|
||||
TextureData::Handle texture;
|
||||
TextureData* texture = nullptr;
|
||||
|
||||
template<class String1, class String2>
|
||||
Texture(String1&& _name, String2&& _alphaName, const TextureData::Handle &_texture)
|
||||
Texture(String1&& _name, String2&& _alphaName, TextureData* _texture)
|
||||
: name(std::forward<String1>(_name))
|
||||
, alphaName(std::forward<String2>(_alphaName))
|
||||
, texture(_texture) {
|
||||
|
@ -4,9 +4,9 @@
|
||||
#include <gl/gl_core_3_3.h>
|
||||
#include <glm/vec2.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
/**
|
||||
* Stores a handle and metadata about a loaded texture.
|
||||
@ -33,11 +33,9 @@ public:
|
||||
return hasAlpha;
|
||||
}
|
||||
|
||||
typedef std::shared_ptr<TextureData> Handle;
|
||||
|
||||
static Handle create(GLuint name, const glm::ivec2& size,
|
||||
static auto create(GLuint name, const glm::ivec2& size,
|
||||
bool transparent) {
|
||||
return std::make_shared<TextureData>(name, size, transparent);
|
||||
return std::make_unique<TextureData>(name, size, transparent);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -45,6 +43,6 @@ private:
|
||||
glm::ivec2 size;
|
||||
bool hasAlpha;
|
||||
};
|
||||
using TextureArchive = std::map<std::string, TextureData::Handle>;
|
||||
using TextureArchive = std::unordered_map<std::string, std::unique_ptr<TextureData>>;
|
||||
|
||||
#endif
|
||||
|
@ -363,9 +363,8 @@ void LoaderDFF::readTexture(Geometry::Material &material,
|
||||
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
|
||||
std::transform(alpha.begin(), alpha.end(), alpha.begin(), ::tolower);
|
||||
|
||||
TextureData::Handle textureinst =
|
||||
texturelookup ? texturelookup(name, alpha) : nullptr;
|
||||
material.textures.emplace_back(std::move(name), std::move(alpha), textureinst);
|
||||
auto textureInstPtr = textureLookup ? textureLookup(name, alpha) : nullptr;
|
||||
material.textures.emplace_back(std::move(name), std::move(alpha), textureInstPtr);
|
||||
}
|
||||
|
||||
void LoaderDFF::readGeometryExtension(const GeometryPtr &geom,
|
||||
|
@ -25,19 +25,19 @@ public:
|
||||
|
||||
class LoaderDFF {
|
||||
public:
|
||||
using TextureLookupCallback = std::function<TextureData::Handle(
|
||||
const std::string&, const std::string&)>;
|
||||
using TextureLookupCallback =
|
||||
std::function<TextureData*(const std::string&, const std::string&)>;
|
||||
using GeometryList = std::vector<GeometryPtr>;
|
||||
using FrameList = std::vector<ModelFramePtr>;
|
||||
|
||||
ClumpPtr loadFromMemory(const FileContentsInfo& file);
|
||||
|
||||
void setTextureLookupCallback(const TextureLookupCallback& tlc) {
|
||||
texturelookup = tlc;
|
||||
textureLookup = tlc;
|
||||
}
|
||||
|
||||
private:
|
||||
TextureLookupCallback texturelookup;
|
||||
TextureLookupCallback textureLookup;
|
||||
|
||||
FrameList readFrameList(const RWBStream& stream);
|
||||
|
||||
|
@ -12,16 +12,18 @@
|
||||
#include "platform/FileHandle.hpp"
|
||||
#include "rw/debug.hpp"
|
||||
|
||||
GLuint gErrorTextureData[] = {0xFFFF00FF, 0xFF000000, 0xFF000000, 0xFFFF00FF};
|
||||
GLuint gDebugTextureData[] = {0xFF0000FF, 0xFF00FF00};
|
||||
GLuint gTextureRed[] = {0xFF0000FF};
|
||||
GLuint gTextureGreen[] = {0xFF00FF00};
|
||||
GLuint gTextureBlue[] = {0xFFFF0000};
|
||||
namespace {
|
||||
constexpr GLuint gErrorTextureData[] = {0xFFFF00FF, 0xFF000000, 0xFF000000, 0xFFFF00FF};
|
||||
constexpr GLuint gDebugTextureData[] = {0xFF0000FF, 0xFF00FF00};
|
||||
constexpr GLuint gTextureRed[] = {0xFF0000FF};
|
||||
constexpr GLuint gTextureGreen[] = {0xFF00FF00};
|
||||
constexpr GLuint gTextureBlue[] = {0xFFFF0000};
|
||||
} // namespace
|
||||
|
||||
static
|
||||
TextureData::Handle getErrorTexture() {
|
||||
static GLuint errTexName = 0;
|
||||
static TextureData::Handle tex;
|
||||
std::unique_ptr<TextureData> getErrorTexture() {
|
||||
GLuint errTexName = 0;
|
||||
std::unique_ptr<TextureData> tex = nullptr;
|
||||
if (errTexName == 0) {
|
||||
glGenTextures(1, &errTexName);
|
||||
glBindTexture(GL_TEXTURE_2D, errTexName);
|
||||
@ -51,9 +53,8 @@ void processPalette(uint32_t* fullColor, RW::BinaryStreamSection& rootSection) {
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
TextureData::Handle createTexture(RW::BSTextureNative& texNative,
|
||||
RW::BinaryStreamSection& rootSection) {
|
||||
static std::unique_ptr<TextureData> createTexture(
|
||||
RW::BSTextureNative& texNative, RW::BinaryStreamSection& rootSection) {
|
||||
// TODO: Exception handling.
|
||||
if (texNative.platform != 8) {
|
||||
RW_ERROR("Unsupported texture platform " << std::dec
|
||||
@ -191,9 +192,7 @@ bool TextureLoader::loadFromMemory(const FileContentsInfo& file,
|
||||
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
|
||||
std::transform(alpha.begin(), alpha.end(), alpha.begin(), ::tolower);
|
||||
|
||||
auto texture = createTexture(texNative, rootSection);
|
||||
|
||||
inTextures[name] = texture;
|
||||
inTextures[name] = createTexture(texNative, rootSection);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -48,13 +48,12 @@ void GameData::load() {
|
||||
/// @todo cuts.img files should be loaded differently to gta3.img
|
||||
loadIMG("anim/cuts.img");
|
||||
|
||||
textureslots["particle"] = loadTextureArchive("particle.txd");
|
||||
textureslots["icons"] = loadTextureArchive("icons.txd");
|
||||
textureslots["hud"] = loadTextureArchive("hud.txd");
|
||||
textureslots["fonts"] = loadTextureArchive("fonts.txd");
|
||||
textureslots["generic"] = loadTextureArchive("generic.txd");
|
||||
auto misc = loadTextureArchive("misc.txd");
|
||||
textureslots["generic"].insert(misc.begin(), misc.end());
|
||||
textureSlots["particle"] = loadTextureArchive("particle.txd");
|
||||
textureSlots["icons"] = loadTextureArchive("icons.txd");
|
||||
textureSlots["hud"] = loadTextureArchive("hud.txd");
|
||||
textureSlots["fonts"] = loadTextureArchive("fonts.txd");
|
||||
textureSlots["generic"] = loadTextureArchive("generic.txd");
|
||||
loadToTextureArchive("misc.txd", textureSlots["generic"]);
|
||||
|
||||
loadCarcols("data/carcols.dat");
|
||||
loadWeather("data/timecyc.dat");
|
||||
@ -362,12 +361,12 @@ void GameData::loadTXD(const std::string& name) {
|
||||
currenttextureslot = slot;
|
||||
|
||||
// Check if this texture slot is loaded already
|
||||
auto slotit = textureslots.find(slot);
|
||||
if (slotit != textureslots.end()) {
|
||||
auto slotit = textureSlots.find(slot);
|
||||
if (slotit != textureSlots.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
textureslots[slot] = loadTextureArchive(name);
|
||||
textureSlots[slot] = loadTextureArchive(name);
|
||||
}
|
||||
|
||||
TextureArchive GameData::loadTextureArchive(const std::string& name) {
|
||||
@ -390,6 +389,21 @@ TextureArchive GameData::loadTextureArchive(const std::string& name) {
|
||||
return textures;
|
||||
}
|
||||
|
||||
void GameData::loadToTextureArchive(const std::string& name,
|
||||
TextureArchive& archive) {
|
||||
RW_PROFILE_COUNTER_ADD("loadTextureArchive", 1);
|
||||
/// @todo refactor loadTXD to use correct file locations
|
||||
auto file = index.openFile(name);
|
||||
if (!file.data) {
|
||||
logger->error("Data", "Failed to open txd: " + name);
|
||||
}
|
||||
|
||||
TextureLoader l;
|
||||
if (!l.loadFromMemory(file, archive)) {
|
||||
logger->error("Data", "Error loading txd: " + name);
|
||||
}
|
||||
}
|
||||
|
||||
void GameData::getNameAndLod(std::string& name, int& lod) {
|
||||
auto lodpos = name.rfind("_l");
|
||||
if (lodpos != std::string::npos) {
|
||||
@ -696,22 +710,21 @@ void GameData::loadSplash(const std::string& name) {
|
||||
std::string lower(name);
|
||||
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
|
||||
|
||||
auto splashTXD = loadTextureArchive(lower + ".txd");
|
||||
textureslots["generic"].insert(splashTXD.begin(), splashTXD.end());
|
||||
textureSlots["generic"] = loadTextureArchive(lower + ".txd");
|
||||
|
||||
engine->state->currentSplash = lower;
|
||||
}
|
||||
|
||||
TextureData::Handle GameData::findSlotTexture(const std::string &slot, const std::string &texture) const {
|
||||
auto slotit = textureslots.find(slot);
|
||||
if (slotit == textureslots.end()) {
|
||||
TextureData* GameData::findSlotTexture(const std::string &slot, const std::string &texture) const {
|
||||
auto slotIt = textureSlots.find(slot);
|
||||
if (slotIt == textureSlots.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
auto textureit = slotit->second.find(texture);
|
||||
if (textureit == slotit->second.end()) {
|
||||
auto textureIt = slotIt->second.find(texture);
|
||||
if (textureIt == slotIt->second.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return textureit->second;
|
||||
return textureIt->second.get();
|
||||
}
|
||||
|
||||
ZoneData *GameData::findZone(const std::string &name) {
|
||||
|
@ -130,6 +130,11 @@ public:
|
||||
*/
|
||||
TextureArchive loadTextureArchive(const std::string& name);
|
||||
|
||||
/**
|
||||
* Loads to named a texture archive from the game data
|
||||
*/
|
||||
void loadToTextureArchive(const std::string& name, TextureArchive& archive);
|
||||
|
||||
/**
|
||||
* Converts combined {name}_l{LOD} into name and lod.
|
||||
*/
|
||||
@ -190,7 +195,7 @@ public:
|
||||
|
||||
void loadSplash(const std::string& name);
|
||||
|
||||
TextureData::Handle findSlotTexture(const std::string& slot,
|
||||
TextureData* findSlotTexture(const std::string& slot,
|
||||
const std::string& texture) const;
|
||||
|
||||
FileIndex index;
|
||||
@ -260,7 +265,7 @@ public:
|
||||
/**
|
||||
* Texture slots, containing loaded textures.
|
||||
*/
|
||||
std::map<std::string, TextureArchive> textureslots;
|
||||
std::unordered_map<std::string, TextureArchive> textureSlots;
|
||||
|
||||
/**
|
||||
* Texture atlases.
|
||||
|
@ -86,8 +86,8 @@ void ProjectileObject::explode() {
|
||||
|
||||
auto& explosion = engine->createParticleEffect();
|
||||
|
||||
auto tex = engine->data->findSlotTexture("particle", "explo02");
|
||||
explosion.texture = tex;
|
||||
auto texPtr = engine->data->findSlotTexture("particle", "explo02");
|
||||
explosion.texture = texPtr;
|
||||
explosion.size = glm::vec2(exp_size);
|
||||
explosion.starttime = engine->getGameTime();
|
||||
explosion.lifetime = 0.5f;
|
||||
|
@ -270,9 +270,9 @@ void GameRenderer::renderWorld(GameWorld* world, const ViewCamera& camera,
|
||||
GLuint splashTexName = 0;
|
||||
const auto fc = world->state->fadeColour;
|
||||
if ((fc.r + fc.g + fc.b) == 0 && !world->state->currentSplash.empty()) {
|
||||
auto splash = world->data->findSlotTexture("generic", world->state->currentSplash);
|
||||
if (splash) {
|
||||
splashTexName = splash->getName();
|
||||
auto splashTexPtr = world->data->findSlotTexture("generic", world->state->currentSplash);
|
||||
if (splashTexPtr) {
|
||||
splashTexName = splashTexPtr->getName();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -125,8 +125,8 @@ void MapRenderer::draw(GameWorld* world, const MapInfo& mi) {
|
||||
for (int m = 0; m < MAP_BLOCK_SIZE; ++m) {
|
||||
std::string num = (m < 10 ? "0" : "");
|
||||
std::string name = "radar" + num + std::to_string(m);
|
||||
auto texture = world->data->findSlotTexture(name, name);
|
||||
dp.textures = {{texture->getName()}};
|
||||
auto texturePtr = world->data->findSlotTexture(name, name);
|
||||
dp.textures = {{texturePtr->getName()}};
|
||||
|
||||
dp.count = 4;
|
||||
|
||||
@ -151,9 +151,8 @@ void MapRenderer::draw(GameWorld* world, const MapInfo& mi) {
|
||||
glDisable(GL_STENCIL_TEST);
|
||||
// We only need the outer ring if we're clipping.
|
||||
glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ONE, GL_ZERO);
|
||||
TextureData::Handle radarDisc =
|
||||
data->findSlotTexture("hud", "radardisc");
|
||||
dp.textures = {{radarDisc->getName()}};
|
||||
auto radarDiscTexPtr = data->findSlotTexture("hud", "radardisc");
|
||||
dp.textures = {{radarDiscTexPtr->getName()}};
|
||||
|
||||
glm::mat4 model{1.0f};
|
||||
model = glm::translate(model, glm::vec3(mi.screenPosition, 0.0f));
|
||||
@ -265,8 +264,8 @@ void MapRenderer::prepareBlip(const glm::vec2& coord, const glm::mat4& view,
|
||||
|
||||
GLuint tex = 0;
|
||||
if (!texture.empty()) {
|
||||
auto sprite = data->findSlotTexture("hud", texture);
|
||||
tex = sprite->getName();
|
||||
auto spriteTexPtr = data->findSlotTexture("hud", texture);
|
||||
tex = spriteTexPtr->getName();
|
||||
}
|
||||
renderer.setUniform(rectProg.get(), "colour", colour);
|
||||
|
||||
|
@ -143,8 +143,8 @@ TextRenderer::TextRenderer(GameRenderer &renderer) : renderer(renderer) {
|
||||
}
|
||||
|
||||
void TextRenderer::setFontTexture(font_t font, const std::string& textureName) {
|
||||
auto ftexture = renderer.getData().findSlotTexture("fonts", textureName);
|
||||
const glm::u32vec2 textureSize = ftexture->getSize();
|
||||
auto fTexturePtr = renderer.getData().findSlotTexture("fonts", textureName);
|
||||
const glm::u32vec2 textureSize = fTexturePtr->getSize();
|
||||
glm::u8vec2 glyphOffset{textureSize.x / 16, textureSize.x / 16};
|
||||
if (font != FONT_PAGER) {
|
||||
glyphOffset.y += glyphOffset.y / 4;
|
||||
@ -344,8 +344,8 @@ void TextRenderer::renderText(const TextRenderer::TextInfo& ti,
|
||||
dp.start = 0;
|
||||
dp.blendMode = BlendMode::BLEND_ALPHA;
|
||||
dp.count = gb.getCount();
|
||||
auto ftexture = renderer.getData().findSlotTexture("fonts", fontMetaData.textureName);
|
||||
dp.textures = {{ftexture->getName()}};
|
||||
auto fTexturePtr = renderer.getData().findSlotTexture("fonts", fontMetaData.textureName);
|
||||
dp.textures = {{fTexturePtr->getName()}};
|
||||
dp.depthMode = DepthMode::OFF;
|
||||
|
||||
renderer.getRenderer().drawArrays(glm::mat4(1.0f), &db, dp);
|
||||
|
@ -1,4 +1,4 @@
|
||||
#ifndef _RWENGINE_VISUALFX_HPP_
|
||||
#ifndef _RWENGINE_VISUALFX_HPP_
|
||||
#define _RWENGINE_VISUALFX_HPP_
|
||||
|
||||
#include <glm/vec2.hpp>
|
||||
@ -50,7 +50,7 @@ struct ParticleFX final : public VisualFX {
|
||||
float lifetime{-1.f};
|
||||
|
||||
/** Texture name */
|
||||
TextureData::Handle texture;
|
||||
TextureData* texture = nullptr;
|
||||
|
||||
/** Size of particle */
|
||||
glm::vec2 size{1.f, 1.f};
|
||||
|
@ -102,9 +102,9 @@ void WaterRenderer::setDataTexture(GLuint fbBinding, GLuint dataTex) {
|
||||
void WaterRenderer::render(GameRenderer &renderer, GameWorld* world) {
|
||||
auto& r = renderer.getRenderer();
|
||||
|
||||
auto waterTex = world->data->findSlotTexture("particle", "water_old");
|
||||
RW_CHECK(waterTex != nullptr, "Water texture is null");
|
||||
if (waterTex == nullptr) {
|
||||
auto waterTexPtr = world->data->findSlotTexture("particle", "water_old");
|
||||
RW_CHECK(waterTexPtr != nullptr, "Water texture is null");
|
||||
if (waterTexPtr == nullptr) {
|
||||
// Can't render water if we don't have a texture.
|
||||
return;
|
||||
}
|
||||
@ -147,7 +147,7 @@ void WaterRenderer::render(GameRenderer &renderer, GameWorld* world) {
|
||||
r.setUniform(waterProg.get(), "inverseVP", ivp);
|
||||
|
||||
wdp.count = gridGeom.getCount();
|
||||
wdp.textures = {{waterTex->getName(), dataTexture}};
|
||||
wdp.textures = {{waterTexPtr->getName(), dataTexture}};
|
||||
|
||||
r.drawArrays(m, &gridDraw, wdp);
|
||||
|
||||
|
@ -203,12 +203,12 @@ void HUDDrawer::drawPlayerInfo(ai::PlayerController* player, GameWorld* world,
|
||||
itemTextureName = "detonator";
|
||||
}
|
||||
|
||||
TextureData::Handle itemTexture =
|
||||
auto itemTexturePtr =
|
||||
render.getData().findSlotTexture("hud", itemTextureName);
|
||||
RW_CHECK(itemTexture != nullptr, "Item has 0 texture");
|
||||
if (itemTexture != nullptr) {
|
||||
RW_CHECK(itemTexture->getName() != 0, "Item has 0 texture");
|
||||
render.drawTexture(itemTexture.get(),
|
||||
RW_CHECK(itemTexturePtr != nullptr, "Item has 0 texture");
|
||||
if (itemTexturePtr != nullptr) {
|
||||
RW_CHECK(itemTexturePtr->getName() != 0, "Item has 0 texture");
|
||||
render.drawTexture(itemTexturePtr,
|
||||
glm::vec4(iconX, iconY, hudParameters.uiWeaponSize,
|
||||
hudParameters.uiWeaponSize));
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ void ModelViewer::showObject(uint16_t object) {
|
||||
LoaderDFF dffLoader;
|
||||
dffLoader.setTextureLookupCallback(
|
||||
[&](const std::string& texture, const std::string&) {
|
||||
return textures.at(texture);
|
||||
return textures.at(texture).get();
|
||||
});
|
||||
|
||||
auto file = world()->data->index.openFile(modelName);
|
||||
|
Loading…
Reference in New Issue
Block a user