Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix #5165
change modelParser from global pointer to function-static reference
s/modelParser/modelLoader to match the class name
  • Loading branch information
rtri committed Mar 12, 2016
1 parent 7e57f7d commit 3a75f32
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 43 deletions.
2 changes: 1 addition & 1 deletion rts/Lua/LuaSyncedCtrl.cpp
Expand Up @@ -441,7 +441,7 @@ static bool ParseProjectileParams(lua_State* L, ProjectileParams& params, const

if (lua_isstring(L, -1)) {
if (key == "model") {
params.model = modelParser->Load3DModel(lua_tostring(L, -1));
params.model = modelLoader.LoadModel(lua_tostring(L, -1));
} else if (key == "cegtag") {
params.cegID = explGenHandler->LoadGeneratorID(lua_tostring(L, -1));
}
Expand Down
4 changes: 2 additions & 2 deletions rts/Lua/LuaUtils.cpp
Expand Up @@ -744,14 +744,14 @@ int LuaUtils::PushFeatureModelDrawType(lua_State* L, const FeatureDef* def)
int LuaUtils::PushModelName(lua_State* L, const SolidObjectDef* def)
{
// redundant with model.path
// lua_pushsstring(L, modelParser->FindModelPath(def->modelName));
// lua_pushsstring(L, modelLoader.FindModelPath(def->modelName));
lua_pushsstring(L, "deprecated! use def.model.path instead!");
return 1;
}


int LuaUtils::PushModelTable(lua_State* L, const SolidObjectDef* def) {
const std::string& modelPath = modelParser->FindModelPath(def->modelName);
const std::string& modelPath = modelLoader.FindModelPath(def->modelName);
const std::string& modelType = StringToLower(FileSystem::GetExtension(modelPath));

const S3DModel* model = def->LoadModel();
Expand Down
2 changes: 1 addition & 1 deletion rts/Lua/LuaWeaponDefs.cpp
Expand Up @@ -267,7 +267,7 @@ static int VisualsTable(lua_State* L, const void* data)
{
const struct WeaponDef::Visuals& v = *static_cast<const struct WeaponDef::Visuals*>(data);
lua_newtable(L);
HSTR_PUSH_STRING(L, "modelName", modelParser->FindModelPath(v.modelName));
HSTR_PUSH_STRING(L, "modelName", modelLoader.FindModelPath(v.modelName));
HSTR_PUSH_NUMBER(L, "colorR", v.color.x);
HSTR_PUSH_NUMBER(L, "colorG", v.color.y);
HSTR_PUSH_NUMBER(L, "colorB", v.color.z);
Expand Down
61 changes: 35 additions & 26 deletions rts/Rendering/Models/IModelParser.cpp
Expand Up @@ -22,10 +22,8 @@
#include "System/maindefines.h"
#include "lib/assimp/include/assimp/Importer.hpp"

C3DModelLoader* modelParser = nullptr;


static void RegisterAssimpModelFormats(C3DModelLoader::FormatMap& formats) {
static void RegisterAssimpModelFormats(CModelLoader::FormatMap& formats) {
std::set<std::string> whitelist;
std::string extension;
std::string extensions;
Expand Down Expand Up @@ -113,11 +111,6 @@ void LoadQueue::Join()
}
}

LoadQueue::~LoadQueue()
{
Join();
}

__FORCE_ALIGN_STACK__
void LoadQueue::Pump()
{
Expand All @@ -131,7 +124,7 @@ void LoadQueue::Pump()
FreeLock();
}

modelParser->Load3DModel(modelName, true);
modelLoader.LoadModel(modelName, true);

{
GrabLock();
Expand Down Expand Up @@ -169,7 +162,7 @@ void LoadQueue::Push(const std::string& modelName)
}


C3DModelLoader::C3DModelLoader()
void CModelLoader::Init()
{
// file-extension should be lowercase
formats["3do"] = MODELTYPE_3DO;
Expand All @@ -189,12 +182,20 @@ C3DModelLoader::C3DModelLoader()
models.push_back(nullptr);
}


C3DModelLoader::~C3DModelLoader()
void CModelLoader::Kill()
{
// thread might be in LoadModel, but it doesn't matter
loadQueue.Join();

// delete model cache
KillModels();
KillParsers();

cache.clear();
formats.clear();
}

void CModelLoader::KillModels()
{
for (unsigned int n = 1; n < models.size(); n++) {
S3DModel* model = models[n];

Expand All @@ -204,22 +205,30 @@ C3DModelLoader::~C3DModelLoader()
model->DeletePieces(model->GetRootPiece());
model->SetRootPiece(nullptr);

delete model;
SafeDelete(model);
}

models.clear();
}

void CModelLoader::KillParsers()
{
for (auto it = parsers.cbegin(); it != parsers.cend(); ++it) {
delete (it->second);
}
parsers.clear();

cache.clear();
parsers.clear();
}

CModelLoader& CModelLoader::GetInstance()
{
static CModelLoader instance;
return instance;
}



std::string C3DModelLoader::FindModelPath(std::string name) const
std::string CModelLoader::FindModelPath(std::string name) const
{
// check for empty string because we can be called
// from Lua*Defs and certain features have no models
Expand Down Expand Up @@ -249,7 +258,7 @@ std::string C3DModelLoader::FindModelPath(std::string name) const



S3DModel* C3DModelLoader::Load3DModel(std::string name, bool preload)
S3DModel* CModelLoader::LoadModel(std::string name, bool preload)
{
// cannot happen except through SpawnProjectile
if (name.empty())
Expand All @@ -262,7 +271,7 @@ S3DModel* C3DModelLoader::Load3DModel(std::string name, bool preload)

// search in cache first
for (unsigned int n = 0; n < 2; n++) {
S3DModel* cachedModel = LoadCached3DModel(*refs[n], preload);
S3DModel* cachedModel = LoadCachedModel(*refs[n], preload);

if (cachedModel != nullptr)
return cachedModel;
Expand All @@ -275,7 +284,7 @@ S3DModel* C3DModelLoader::Load3DModel(std::string name, bool preload)
return (CreateModel(name, path, preload));
}

S3DModel* C3DModelLoader::LoadCached3DModel(const std::string& name, bool preload)
S3DModel* CModelLoader::LoadCachedModel(const std::string& name, bool preload)
{
S3DModel* cachedModel = nullptr;

Expand All @@ -300,7 +309,7 @@ S3DModel* C3DModelLoader::LoadCached3DModel(const std::string& name, bool preloa



S3DModel* C3DModelLoader::CreateModel(
S3DModel* CModelLoader::CreateModel(
const std::string& name,
const std::string& path,
bool preload
Expand All @@ -321,7 +330,7 @@ S3DModel* C3DModelLoader::CreateModel(



IModelParser* C3DModelLoader::GetFormatParser(const std::string& pathExt)
IModelParser* CModelLoader::GetFormatParser(const std::string& pathExt)
{
const auto fi = formats.find(StringToLower(pathExt));

Expand All @@ -331,7 +340,7 @@ IModelParser* C3DModelLoader::GetFormatParser(const std::string& pathExt)
return parsers[fi->second];
}

S3DModel* C3DModelLoader::ParseModel(const std::string& name, const std::string& path)
S3DModel* CModelLoader::ParseModel(const std::string& name, const std::string& path)
{
S3DModel* model = nullptr;
IModelParser* parser = GetFormatParser(FileSystem::GetExtension(path));
Expand All @@ -351,7 +360,7 @@ S3DModel* C3DModelLoader::ParseModel(const std::string& name, const std::string&



void C3DModelLoader::AddModelToCache(
void CModelLoader::AddModelToCache(
S3DModel* model,
const std::string& name,
const std::string& path
Expand All @@ -373,7 +382,7 @@ void C3DModelLoader::AddModelToCache(



void C3DModelLoader::CreateListsNow(S3DModelPiece* o)
void CModelLoader::CreateListsNow(S3DModelPiece* o)
{
o->UploadGeometryVBOs();
o->CreateShatterPieces();
Expand All @@ -385,7 +394,7 @@ void C3DModelLoader::CreateListsNow(S3DModelPiece* o)
}


void C3DModelLoader::CreateLists(S3DModel* model) {
void CModelLoader::CreateLists(S3DModel* model) {
S3DModelPiece* rootPiece = model->GetRootPiece();

if (rootPiece->GetDisplayListID() != 0)
Expand Down
22 changes: 14 additions & 8 deletions rts/Rendering/Models/IModelParser.h
Expand Up @@ -28,7 +28,7 @@ class IModelParser
struct LoadQueue {
public:
LoadQueue(): thread(nullptr) {}
~LoadQueue();
~LoadQueue() { Join(); }

void Pump();
void Push(const std::string& modelName);
Expand All @@ -45,30 +45,36 @@ struct LoadQueue {



class C3DModelLoader
class CModelLoader
{
public:
C3DModelLoader();
~C3DModelLoader();
static CModelLoader& GetInstance();

S3DModel* Load3DModel(std::string name, bool preload = false);
void Init();
void Kill();

S3DModel* LoadModel(std::string name, bool preload = false);

std::string FindModelPath(std::string name) const;

void Preload3DModel(const std::string& name) { loadQueue.Push(name); }
bool IsValid() const { return (!formats.empty()); }
void PreloadModel(const std::string& name) { assert(IsValid()); loadQueue.Push(name); }

public:
typedef std::unordered_map<std::string, unsigned int> ModelMap; // "armflash.3do" --> id
typedef std::unordered_map<std::string, unsigned int> FormatMap; // "3do" --> MODELTYPE_3DO
typedef std::unordered_map<unsigned int, IModelParser*> ParserMap; // MODELTYPE_3DO --> parser

private:
S3DModel* LoadCached3DModel(const std::string& name, bool preload);
S3DModel* LoadCachedModel(const std::string& name, bool preload);
S3DModel* CreateModel(const std::string& name, const std::string& path, bool preload);
S3DModel* ParseModel(const std::string& name, const std::string& path);

IModelParser* GetFormatParser(const std::string& pathExt);

void KillModels();
void KillParsers();

void AddModelToCache(S3DModel* model, const std::string& name, const std::string& path);

void CreateLists(S3DModel* o);
Expand All @@ -85,6 +91,6 @@ class C3DModelLoader
std::vector<S3DModel*> models;
};

extern C3DModelLoader* modelParser;
#define modelLoader (CModelLoader::GetInstance())

#endif /* IMODELPARSER_H */
5 changes: 3 additions & 2 deletions rts/Rendering/WorldDrawer.cpp
Expand Up @@ -60,7 +60,8 @@ CWorldDrawer::~CWorldDrawer()
SafeDelete(featureDrawer);
SafeDelete(unitDrawer); // depends on unitHandler, cubeMapHandler
SafeDelete(projectileDrawer);
SafeDelete(modelParser);

modelLoader.Kill();

SafeDelete(farTextureHandler);
SafeDelete(heightMapTexture);
Expand All @@ -82,7 +83,7 @@ void CWorldDrawer::LoadPre() const
{
// these need to be loaded before featureHandler is created
// (maps with features have their models loaded at startup)
modelParser = new C3DModelLoader();
modelLoader.Init();

loadscreen->SetLoadMessage("Creating Unit Textures");
texturehandler3DO = new C3DOTextureHandler();
Expand Down
4 changes: 2 additions & 2 deletions rts/Sim/Objects/SolidObjectDef.cpp
Expand Up @@ -64,15 +64,15 @@ SolidObjectDef::SolidObjectDef()
void SolidObjectDef::PreloadModel() const
{
if (model == nullptr && !modelName.empty()) {
modelParser->Preload3DModel(modelName);
modelLoader.PreloadModel(modelName);
}
}

S3DModel* SolidObjectDef::LoadModel() const
{
if (model == nullptr) {
if (!modelName.empty()) {
model = modelParser->Load3DModel(modelName);
model = modelLoader.LoadModel(modelName);
} else {
// not useful, too much spam
// LOG_L(L_WARNING, "[SolidObjectDef::%s] object \"%s\" has no model defined", __FUNCTION__, name.c_str());
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Weapons/WeaponDef.cpp
Expand Up @@ -589,7 +589,7 @@ S3DModel* WeaponDef::LoadModel()
{
if (visuals.model == NULL) {
if (!visuals.modelName.empty()) {
visuals.model = modelParser->Load3DModel(visuals.modelName);
visuals.model = modelLoader.LoadModel(visuals.modelName);
} else {
// not useful, too much spam
// LOG_L(L_WARNING, "[WeaponDef::%s] weapon \"%s\" has no model defined", __FUNCTION__, name.c_str());
Expand Down

0 comments on commit 3a75f32

Please sign in to comment.