16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-21 20:39:18 +02:00

replaced basic array in the model manager with stl containers

This commit is contained in:
tmj-fstate
2017-08-05 18:28:46 +02:00
parent 238ea7547d
commit b783cd38a9
8 changed files with 81 additions and 165 deletions

View File

@@ -17,64 +17,45 @@ http://mozilla.org/MPL/2.0/.
#include "MdlMngr.h"
#include "Globals.h"
#include "Logs.h"
#include "McZapkie/mctools.h"
//#define SeekFiles std::string("*.t3d")
// wczytanie modelu do kontenerka
TModel3d *
TMdlContainer::LoadModel(std::string const &Name, bool const Dynamic) {
TModel3d * TMdlContainer::LoadModel(std::string const &NewName, bool dynamic)
{ // wczytanie modelu do kontenerka
SafeDelete(Model);
Name = NewName;
Model = new TModel3d();
if (!Model->LoadFromFile(Name, dynamic)) // np. "models\\pkp/head1-y.t3d"
SafeDelete(Model);
return Model;
};
TMdlContainer *TModelsManager::Models;
int TModelsManager::Count;
int const MAX_MODELS = 1000;
void TModelsManager::Init()
{
Models = new TMdlContainer[MAX_MODELS];
Count = 0;
}
/*
TModelsManager::TModelsManager()
{
// Models= NULL;
Models= new TMdlContainer[MAX_MODELS];
Count= 0;
};
TModelsManager::~TModelsManager()
{
Free();
};
*/
void TModelsManager::Free()
{
delete[] Models;
Models = nullptr;
}
TModel3d * TModelsManager::LoadModel(std::string const &Name, bool dynamic)
{ // wczytanie modelu do tablicy
TModel3d *mdl = NULL;
if (Count >= MAX_MODELS)
Error("FIXME: Too many models, program will now crash :)");
else
{
mdl = Models[Count].LoadModel(Name, dynamic);
if (mdl)
Count++; // jeśli błąd wczytania modelu, to go nie wliczamy
Model = std::make_shared<TModel3d>();
if( true == Model->LoadFromFile( Name, Dynamic ) ) {
m_name = Name;
return Model.get();
}
else {
m_name.clear();
Model = nullptr;
return nullptr;
}
};
TModelsManager::modelcontainer_sequence TModelsManager::m_models;
TModelsManager::stringmodelcontainerindex_map TModelsManager::m_modelsmap;
// wczytanie modelu do tablicy
TModel3d *
TModelsManager::LoadModel(std::string const &Name, bool dynamic) {
m_models.emplace_back();
auto model = m_models.back().LoadModel( Name, dynamic );
if( model != nullptr ) {
m_modelsmap.emplace( Name, m_models.size() - 1 );
return model;
}
else {
m_models.pop_back();
return nullptr;
}
return mdl;
}
TModel3d * TModelsManager::GetModel(std::string const &Name, bool dynamic)
TModel3d *
TModelsManager::GetModel(std::string const &Name, bool const Dynamic)
{ // model może być we wpisie "node...model" albo "node...dynamic", a także być dodatkowym w dynamic
// (kabina, wnętrze, ładunek)
// dla "node...dynamic" mamy podaną ścieżkę w "\dynamic\" i musi być co najmniej 1 poziom, zwkle
@@ -97,53 +78,10 @@ TModel3d * TModelsManager::GetModel(std::string const &Name, bool dynamic)
// - niebo animowane, ścieżka brana ze wpisu, tekstury nieokreślone
// - wczytanie modelu animowanego - Init() - sprawdzić
std::string buf;
std::string buftp = Global::asCurrentTexturePath; // zapamiętanie aktualnej ścieżki do tekstur,
// bo będzie tyczmasowo zmieniana
/*
// Ra: niby tak jest lepiej, ale działa gorzej, więc przywrócone jest oryginalne
//nawet jeśli model będzie pobrany z tablicy, to trzeba ustalić ścieżkę dla tekstur
if (dynamic) //na razie tak, bo nie wiadomo, jaki może mieć wpływ na pozostałe modele
{//dla pojazdów podana jest zawsze pełna ścieżka do modelu
strcpy(buf,Name);
if (strchr(Name,'/')!=NULL)
{//pobieranie tekstur z katalogu, w którym jest model
Global::asCurrentTexturePath=Global::asCurrentTexturePath+AnsiString(Name);
Global::asCurrentTexturePath.Delete(Global::asCurrentTexturePath.Pos("/")+1,Global::asCurrentTexturePath.Length());
}
}
else
{//dla modeli scenerii trzeba ustalić ścieżkę
if (strchr(Name,'\\')==NULL)
{//jeśli nie ma lewego ukośnika w ścieżce, a jest prawy, to zmienić ścieżkę dla tekstur na tę
z modelem
strcpy(buf,"models\\"); //Ra: było by lepiej katalog dodać w parserze
//strcpy(buf,"scenery\\"); //Ra: było by lepiej katalog dodać w parserze
strcat(buf,Name);
if (strchr(Name,'/')!=NULL)
{//jeszcze musi być prawy ukośnik
Global::asCurrentTexturePath=Global::asCurrentTexturePath+AnsiString(Name);
Global::asCurrentTexturePath.Delete(Global::asCurrentTexturePath.Pos("/")+1,Global::asCurrentTexturePath.Length());
}
}
else
{//jeśli jest lewy ukośnik, to ścieżkę do tekstur zmienić tylko dla pojazdów
strcpy(buf,Name);
}
}
StrLower(buf);
for (int i=0;i<Count;i++)
{//bezsensowne przeszukanie tabeli na okoliczność wystąpienia modelu
if (strcmp(buf,Models[i].Name)==0)
{
Global::asCurrentTexturePath=buftp; //odtworzenie ścieżki do tekstur
return (Models[i].Model); //model znaleziony
}
};
*/
std::string const buftp = Global::asCurrentTexturePath; // zapamiętanie aktualnej ścieżki do tekstur,
if( Name.find('\\') == std::string::npos )
{
buf = "models\\"; // Ra: było by lepiej katalog dodać w parserze
buf.append( Name );
buf = "models\\" + Name; // Ra: było by lepiej katalog dodać w parserze
if( Name.find( '/') != std::string::npos)
{
Global::asCurrentTexturePath = Global::asCurrentTexturePath + Name;
@@ -154,43 +92,27 @@ TModel3d * TModelsManager::GetModel(std::string const &Name, bool dynamic)
else
{
buf = Name;
if (dynamic) // na razie tak, bo nie wiadomo, jaki może mieć wpływ na pozostałe modele
if (Name.find( '/') != std::string::npos)
{ // pobieranie tekstur z katalogu, w którym jest model
if( Dynamic ) {
// na razie tak, bo nie wiadomo, jaki może mieć wpływ na pozostałe modele
if( Name.find( '/' ) != std::string::npos ) { // pobieranie tekstur z katalogu, w którym jest model
Global::asCurrentTexturePath = Global::asCurrentTexturePath + Name;
Global::asCurrentTexturePath.erase(Global::asCurrentTexturePath.find("/") + 1,
Global::asCurrentTexturePath.length() - 1);
Global::asCurrentTexturePath.erase(
Global::asCurrentTexturePath.find( "/" ) + 1,
Global::asCurrentTexturePath.length() - 1 );
}
}
}
buf = ToLower( buf );
for (int i = 0; i < Count; ++i)
{
if ( buf == Models[i].Name )
{
Global::asCurrentTexturePath = buftp;
return (Models[i].Model);
}
};
TModel3d *tmpModel = LoadModel(buf, dynamic); // model nie znaleziony, to wczytać
auto const lookup = m_modelsmap.find( buf );
if( lookup != m_modelsmap.end() ) {
Global::asCurrentTexturePath = buftp;
return ( m_models[ lookup->second ].Model.get() );
}
auto model = LoadModel(buf, Dynamic); // model nie znaleziony, to wczytać
Global::asCurrentTexturePath = buftp; // odtworzenie ścieżki do tekstur
return (tmpModel); // NULL jeśli błąd
return model; // NULL jeśli błąd
};
/*
TModel3d TModelsManager::GetModel(char *Name, AnsiString asReplacableTexture)
{
GLuint ReplacableTextureID= 0;
TModel3d NewModel;
NewModel= *GetNextModel(Name);
if (asReplacableTexture!=AnsiString("none"))
ReplacableTextureID= TTexturesManager::GetTextureID(asReplacableTexture.c_str());
NewModel.ReplacableSkinID=ReplacableTextureID;
return NewModel;
};
*/
//---------------------------------------------------------------------------