1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-03 00:59:47 +02:00

Use unique_ptr to handle TextureData

This commit is contained in:
Filip Gawin 2019-01-18 03:46:59 +01:00
parent f15f3cefdd
commit 195020df46
15 changed files with 90 additions and 77 deletions

View File

@ -145,10 +145,10 @@ struct Geometry {
struct Texture { struct Texture {
std::string name; std::string name;
std::string alphaName; std::string alphaName;
TextureData::Handle texture; TextureData* texture = nullptr;
template<class String1, class String2> 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)) : name(std::forward<String1>(_name))
, alphaName(std::forward<String2>(_alphaName)) , alphaName(std::forward<String2>(_alphaName))
, texture(_texture) { , texture(_texture) {

View File

@ -4,9 +4,9 @@
#include <gl/gl_core_3_3.h> #include <gl/gl_core_3_3.h>
#include <glm/vec2.hpp> #include <glm/vec2.hpp>
#include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map>
/** /**
* Stores a handle and metadata about a loaded texture. * Stores a handle and metadata about a loaded texture.
@ -33,11 +33,9 @@ public:
return hasAlpha; return hasAlpha;
} }
typedef std::shared_ptr<TextureData> Handle; static auto create(GLuint name, const glm::ivec2& size,
static Handle create(GLuint name, const glm::ivec2& size,
bool transparent) { bool transparent) {
return std::make_shared<TextureData>(name, size, transparent); return std::make_unique<TextureData>(name, size, transparent);
} }
private: private:
@ -45,6 +43,6 @@ private:
glm::ivec2 size; glm::ivec2 size;
bool hasAlpha; bool hasAlpha;
}; };
using TextureArchive = std::map<std::string, TextureData::Handle>; using TextureArchive = std::unordered_map<std::string, std::unique_ptr<TextureData>>;
#endif #endif

View File

@ -363,9 +363,8 @@ void LoaderDFF::readTexture(Geometry::Material &material,
std::transform(name.begin(), name.end(), name.begin(), ::tolower); std::transform(name.begin(), name.end(), name.begin(), ::tolower);
std::transform(alpha.begin(), alpha.end(), alpha.begin(), ::tolower); std::transform(alpha.begin(), alpha.end(), alpha.begin(), ::tolower);
TextureData::Handle textureinst = auto textureInstPtr = textureLookup ? textureLookup(name, alpha) : nullptr;
texturelookup ? texturelookup(name, alpha) : nullptr; material.textures.emplace_back(std::move(name), std::move(alpha), textureInstPtr);
material.textures.emplace_back(std::move(name), std::move(alpha), textureinst);
} }
void LoaderDFF::readGeometryExtension(const GeometryPtr &geom, void LoaderDFF::readGeometryExtension(const GeometryPtr &geom,

View File

@ -25,19 +25,19 @@ public:
class LoaderDFF { class LoaderDFF {
public: public:
using TextureLookupCallback = std::function<TextureData::Handle( using TextureLookupCallback =
const std::string&, const std::string&)>; std::function<TextureData*(const std::string&, const std::string&)>;
using GeometryList = std::vector<GeometryPtr>; using GeometryList = std::vector<GeometryPtr>;
using FrameList = std::vector<ModelFramePtr>; using FrameList = std::vector<ModelFramePtr>;
ClumpPtr loadFromMemory(const FileContentsInfo& file); ClumpPtr loadFromMemory(const FileContentsInfo& file);
void setTextureLookupCallback(const TextureLookupCallback& tlc) { void setTextureLookupCallback(const TextureLookupCallback& tlc) {
texturelookup = tlc; textureLookup = tlc;
} }
private: private:
TextureLookupCallback texturelookup; TextureLookupCallback textureLookup;
FrameList readFrameList(const RWBStream& stream); FrameList readFrameList(const RWBStream& stream);

View File

@ -12,16 +12,18 @@
#include "platform/FileHandle.hpp" #include "platform/FileHandle.hpp"
#include "rw/debug.hpp" #include "rw/debug.hpp"
GLuint gErrorTextureData[] = {0xFFFF00FF, 0xFF000000, 0xFF000000, 0xFFFF00FF}; namespace {
GLuint gDebugTextureData[] = {0xFF0000FF, 0xFF00FF00}; constexpr GLuint gErrorTextureData[] = {0xFFFF00FF, 0xFF000000, 0xFF000000, 0xFFFF00FF};
GLuint gTextureRed[] = {0xFF0000FF}; constexpr GLuint gDebugTextureData[] = {0xFF0000FF, 0xFF00FF00};
GLuint gTextureGreen[] = {0xFF00FF00}; constexpr GLuint gTextureRed[] = {0xFF0000FF};
GLuint gTextureBlue[] = {0xFFFF0000}; constexpr GLuint gTextureGreen[] = {0xFF00FF00};
constexpr GLuint gTextureBlue[] = {0xFFFF0000};
} // namespace
static static
TextureData::Handle getErrorTexture() { std::unique_ptr<TextureData> getErrorTexture() {
static GLuint errTexName = 0; GLuint errTexName = 0;
static TextureData::Handle tex; std::unique_ptr<TextureData> tex = nullptr;
if (errTexName == 0) { if (errTexName == 0) {
glGenTextures(1, &errTexName); glGenTextures(1, &errTexName);
glBindTexture(GL_TEXTURE_2D, errTexName); glBindTexture(GL_TEXTURE_2D, errTexName);
@ -51,9 +53,8 @@ void processPalette(uint32_t* fullColor, RW::BinaryStreamSection& rootSection) {
} }
} }
static static std::unique_ptr<TextureData> createTexture(
TextureData::Handle createTexture(RW::BSTextureNative& texNative, RW::BSTextureNative& texNative, RW::BinaryStreamSection& rootSection) {
RW::BinaryStreamSection& rootSection) {
// TODO: Exception handling. // TODO: Exception handling.
if (texNative.platform != 8) { if (texNative.platform != 8) {
RW_ERROR("Unsupported texture platform " << std::dec 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(name.begin(), name.end(), name.begin(), ::tolower);
std::transform(alpha.begin(), alpha.end(), alpha.begin(), ::tolower); std::transform(alpha.begin(), alpha.end(), alpha.begin(), ::tolower);
auto texture = createTexture(texNative, rootSection); inTextures[name] = createTexture(texNative, rootSection);
inTextures[name] = texture;
} }
return true; return true;

View File

@ -48,13 +48,12 @@ void GameData::load() {
/// @todo cuts.img files should be loaded differently to gta3.img /// @todo cuts.img files should be loaded differently to gta3.img
loadIMG("anim/cuts.img"); loadIMG("anim/cuts.img");
textureslots["particle"] = loadTextureArchive("particle.txd"); textureSlots["particle"] = loadTextureArchive("particle.txd");
textureslots["icons"] = loadTextureArchive("icons.txd"); textureSlots["icons"] = loadTextureArchive("icons.txd");
textureslots["hud"] = loadTextureArchive("hud.txd"); textureSlots["hud"] = loadTextureArchive("hud.txd");
textureslots["fonts"] = loadTextureArchive("fonts.txd"); textureSlots["fonts"] = loadTextureArchive("fonts.txd");
textureslots["generic"] = loadTextureArchive("generic.txd"); textureSlots["generic"] = loadTextureArchive("generic.txd");
auto misc = loadTextureArchive("misc.txd"); loadToTextureArchive("misc.txd", textureSlots["generic"]);
textureslots["generic"].insert(misc.begin(), misc.end());
loadCarcols("data/carcols.dat"); loadCarcols("data/carcols.dat");
loadWeather("data/timecyc.dat"); loadWeather("data/timecyc.dat");
@ -362,12 +361,12 @@ void GameData::loadTXD(const std::string& name) {
currenttextureslot = slot; currenttextureslot = slot;
// Check if this texture slot is loaded already // Check if this texture slot is loaded already
auto slotit = textureslots.find(slot); auto slotit = textureSlots.find(slot);
if (slotit != textureslots.end()) { if (slotit != textureSlots.end()) {
return; return;
} }
textureslots[slot] = loadTextureArchive(name); textureSlots[slot] = loadTextureArchive(name);
} }
TextureArchive GameData::loadTextureArchive(const std::string& name) { TextureArchive GameData::loadTextureArchive(const std::string& name) {
@ -390,6 +389,21 @@ TextureArchive GameData::loadTextureArchive(const std::string& name) {
return textures; 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) { void GameData::getNameAndLod(std::string& name, int& lod) {
auto lodpos = name.rfind("_l"); auto lodpos = name.rfind("_l");
if (lodpos != std::string::npos) { if (lodpos != std::string::npos) {
@ -696,22 +710,21 @@ void GameData::loadSplash(const std::string& name) {
std::string lower(name); std::string lower(name);
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower); std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
auto splashTXD = loadTextureArchive(lower + ".txd"); textureSlots["generic"] = loadTextureArchive(lower + ".txd");
textureslots["generic"].insert(splashTXD.begin(), splashTXD.end());
engine->state->currentSplash = lower; engine->state->currentSplash = lower;
} }
TextureData::Handle GameData::findSlotTexture(const std::string &slot, const std::string &texture) const { TextureData* GameData::findSlotTexture(const std::string &slot, const std::string &texture) const {
auto slotit = textureslots.find(slot); auto slotIt = textureSlots.find(slot);
if (slotit == textureslots.end()) { if (slotIt == textureSlots.end()) {
return nullptr; return nullptr;
} }
auto textureit = slotit->second.find(texture); auto textureIt = slotIt->second.find(texture);
if (textureit == slotit->second.end()) { if (textureIt == slotIt->second.end()) {
return nullptr; return nullptr;
} }
return textureit->second; return textureIt->second.get();
} }
ZoneData *GameData::findZone(const std::string &name) { ZoneData *GameData::findZone(const std::string &name) {

View File

@ -130,6 +130,11 @@ public:
*/ */
TextureArchive loadTextureArchive(const std::string& name); 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. * Converts combined {name}_l{LOD} into name and lod.
*/ */
@ -190,7 +195,7 @@ public:
void loadSplash(const std::string& name); void loadSplash(const std::string& name);
TextureData::Handle findSlotTexture(const std::string& slot, TextureData* findSlotTexture(const std::string& slot,
const std::string& texture) const; const std::string& texture) const;
FileIndex index; FileIndex index;
@ -260,7 +265,7 @@ public:
/** /**
* Texture slots, containing loaded textures. * Texture slots, containing loaded textures.
*/ */
std::map<std::string, TextureArchive> textureslots; std::unordered_map<std::string, TextureArchive> textureSlots;
/** /**
* Texture atlases. * Texture atlases.

View File

@ -86,8 +86,8 @@ void ProjectileObject::explode() {
auto& explosion = engine->createParticleEffect(); auto& explosion = engine->createParticleEffect();
auto tex = engine->data->findSlotTexture("particle", "explo02"); auto texPtr = engine->data->findSlotTexture("particle", "explo02");
explosion.texture = tex; explosion.texture = texPtr;
explosion.size = glm::vec2(exp_size); explosion.size = glm::vec2(exp_size);
explosion.starttime = engine->getGameTime(); explosion.starttime = engine->getGameTime();
explosion.lifetime = 0.5f; explosion.lifetime = 0.5f;

View File

@ -270,9 +270,9 @@ void GameRenderer::renderWorld(GameWorld* world, const ViewCamera& camera,
GLuint splashTexName = 0; GLuint splashTexName = 0;
const auto fc = world->state->fadeColour; const auto fc = world->state->fadeColour;
if ((fc.r + fc.g + fc.b) == 0 && !world->state->currentSplash.empty()) { if ((fc.r + fc.g + fc.b) == 0 && !world->state->currentSplash.empty()) {
auto splash = world->data->findSlotTexture("generic", world->state->currentSplash); auto splashTexPtr = world->data->findSlotTexture("generic", world->state->currentSplash);
if (splash) { if (splashTexPtr) {
splashTexName = splash->getName(); splashTexName = splashTexPtr->getName();
} }
} }

View File

@ -125,8 +125,8 @@ void MapRenderer::draw(GameWorld* world, const MapInfo& mi) {
for (int m = 0; m < MAP_BLOCK_SIZE; ++m) { for (int m = 0; m < MAP_BLOCK_SIZE; ++m) {
std::string num = (m < 10 ? "0" : ""); std::string num = (m < 10 ? "0" : "");
std::string name = "radar" + num + std::to_string(m); std::string name = "radar" + num + std::to_string(m);
auto texture = world->data->findSlotTexture(name, name); auto texturePtr = world->data->findSlotTexture(name, name);
dp.textures = {{texture->getName()}}; dp.textures = {{texturePtr->getName()}};
dp.count = 4; dp.count = 4;
@ -151,9 +151,8 @@ void MapRenderer::draw(GameWorld* world, const MapInfo& mi) {
glDisable(GL_STENCIL_TEST); glDisable(GL_STENCIL_TEST);
// We only need the outer ring if we're clipping. // We only need the outer ring if we're clipping.
glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ONE, GL_ZERO); glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ONE, GL_ZERO);
TextureData::Handle radarDisc = auto radarDiscTexPtr = data->findSlotTexture("hud", "radardisc");
data->findSlotTexture("hud", "radardisc"); dp.textures = {{radarDiscTexPtr->getName()}};
dp.textures = {{radarDisc->getName()}};
glm::mat4 model{1.0f}; glm::mat4 model{1.0f};
model = glm::translate(model, glm::vec3(mi.screenPosition, 0.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; GLuint tex = 0;
if (!texture.empty()) { if (!texture.empty()) {
auto sprite = data->findSlotTexture("hud", texture); auto spriteTexPtr = data->findSlotTexture("hud", texture);
tex = sprite->getName(); tex = spriteTexPtr->getName();
} }
renderer.setUniform(rectProg.get(), "colour", colour); renderer.setUniform(rectProg.get(), "colour", colour);

View File

@ -143,8 +143,8 @@ TextRenderer::TextRenderer(GameRenderer &renderer) : renderer(renderer) {
} }
void TextRenderer::setFontTexture(font_t font, const std::string& textureName) { void TextRenderer::setFontTexture(font_t font, const std::string& textureName) {
auto ftexture = renderer.getData().findSlotTexture("fonts", textureName); auto fTexturePtr = renderer.getData().findSlotTexture("fonts", textureName);
const glm::u32vec2 textureSize = ftexture->getSize(); const glm::u32vec2 textureSize = fTexturePtr->getSize();
glm::u8vec2 glyphOffset{textureSize.x / 16, textureSize.x / 16}; glm::u8vec2 glyphOffset{textureSize.x / 16, textureSize.x / 16};
if (font != FONT_PAGER) { if (font != FONT_PAGER) {
glyphOffset.y += glyphOffset.y / 4; glyphOffset.y += glyphOffset.y / 4;
@ -344,8 +344,8 @@ void TextRenderer::renderText(const TextRenderer::TextInfo& ti,
dp.start = 0; dp.start = 0;
dp.blendMode = BlendMode::BLEND_ALPHA; dp.blendMode = BlendMode::BLEND_ALPHA;
dp.count = gb.getCount(); dp.count = gb.getCount();
auto ftexture = renderer.getData().findSlotTexture("fonts", fontMetaData.textureName); auto fTexturePtr = renderer.getData().findSlotTexture("fonts", fontMetaData.textureName);
dp.textures = {{ftexture->getName()}}; dp.textures = {{fTexturePtr->getName()}};
dp.depthMode = DepthMode::OFF; dp.depthMode = DepthMode::OFF;
renderer.getRenderer().drawArrays(glm::mat4(1.0f), &db, dp); renderer.getRenderer().drawArrays(glm::mat4(1.0f), &db, dp);

View File

@ -1,4 +1,4 @@
#ifndef _RWENGINE_VISUALFX_HPP_ #ifndef _RWENGINE_VISUALFX_HPP_
#define _RWENGINE_VISUALFX_HPP_ #define _RWENGINE_VISUALFX_HPP_
#include <glm/vec2.hpp> #include <glm/vec2.hpp>
@ -50,7 +50,7 @@ struct ParticleFX final : public VisualFX {
float lifetime{-1.f}; float lifetime{-1.f};
/** Texture name */ /** Texture name */
TextureData::Handle texture; TextureData* texture = nullptr;
/** Size of particle */ /** Size of particle */
glm::vec2 size{1.f, 1.f}; glm::vec2 size{1.f, 1.f};

View File

@ -102,9 +102,9 @@ void WaterRenderer::setDataTexture(GLuint fbBinding, GLuint dataTex) {
void WaterRenderer::render(GameRenderer &renderer, GameWorld* world) { void WaterRenderer::render(GameRenderer &renderer, GameWorld* world) {
auto& r = renderer.getRenderer(); auto& r = renderer.getRenderer();
auto waterTex = world->data->findSlotTexture("particle", "water_old"); auto waterTexPtr = world->data->findSlotTexture("particle", "water_old");
RW_CHECK(waterTex != nullptr, "Water texture is null"); RW_CHECK(waterTexPtr != nullptr, "Water texture is null");
if (waterTex == nullptr) { if (waterTexPtr == nullptr) {
// Can't render water if we don't have a texture. // Can't render water if we don't have a texture.
return; return;
} }
@ -147,7 +147,7 @@ void WaterRenderer::render(GameRenderer &renderer, GameWorld* world) {
r.setUniform(waterProg.get(), "inverseVP", ivp); r.setUniform(waterProg.get(), "inverseVP", ivp);
wdp.count = gridGeom.getCount(); wdp.count = gridGeom.getCount();
wdp.textures = {{waterTex->getName(), dataTexture}}; wdp.textures = {{waterTexPtr->getName(), dataTexture}};
r.drawArrays(m, &gridDraw, wdp); r.drawArrays(m, &gridDraw, wdp);

View File

@ -203,12 +203,12 @@ void HUDDrawer::drawPlayerInfo(ai::PlayerController* player, GameWorld* world,
itemTextureName = "detonator"; itemTextureName = "detonator";
} }
TextureData::Handle itemTexture = auto itemTexturePtr =
render.getData().findSlotTexture("hud", itemTextureName); render.getData().findSlotTexture("hud", itemTextureName);
RW_CHECK(itemTexture != nullptr, "Item has 0 texture"); RW_CHECK(itemTexturePtr != nullptr, "Item has 0 texture");
if (itemTexture != nullptr) { if (itemTexturePtr != nullptr) {
RW_CHECK(itemTexture->getName() != 0, "Item has 0 texture"); RW_CHECK(itemTexturePtr->getName() != 0, "Item has 0 texture");
render.drawTexture(itemTexture.get(), render.drawTexture(itemTexturePtr,
glm::vec4(iconX, iconY, hudParameters.uiWeaponSize, glm::vec4(iconX, iconY, hudParameters.uiWeaponSize,
hudParameters.uiWeaponSize)); hudParameters.uiWeaponSize));
} }

View File

@ -44,7 +44,7 @@ void ModelViewer::showObject(uint16_t object) {
LoaderDFF dffLoader; LoaderDFF dffLoader;
dffLoader.setTextureLookupCallback( dffLoader.setTextureLookupCallback(
[&](const std::string& texture, const std::string&) { [&](const std::string& texture, const std::string&) {
return textures.at(texture); return textures.at(texture).get();
}); });
auto file = world()->data->index.openFile(modelName); auto file = world()->data->index.openFile(modelName);