From ffc9347641b5f9c48dc93e1b99d8f64621684ad3 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 21 Jan 2011 01:53:58 +0000 Subject: [PATCH] - bugfixes for G3D viewer (but applies to general texture loading in game also) we now handle better scenarios where binary or model are in different working directories --- source/g3d_viewer/main.cpp | 48 +++++++++++-------- .../shared_lib/include/graphics/FileReader.h | 16 +++---- source/shared_lib/sources/graphics/model.cpp | 33 +++++++++---- 3 files changed, 59 insertions(+), 38 deletions(-) diff --git a/source/g3d_viewer/main.cpp b/source/g3d_viewer/main.cpp index 9beda458..6feae457 100644 --- a/source/g3d_viewer/main.cpp +++ b/source/g3d_viewer/main.cpp @@ -310,7 +310,7 @@ void MainWindow::OnChangeColor(wxCommandEvent &event) { void MainWindow::onMenuFileLoad(wxCommandEvent &event){ try { string fileName; - fileDialog->SetWildcard(wxT("G3D files (*.g3d)|*.g3d")); + fileDialog->SetWildcard(wxT("G3D files (*.g3d)|*.g3d;*.G3D")); fileDialog->SetMessage(wxT("Selecting Glest Model for current view.")); if(fileDialog->ShowModal()==wxID_OK){ @@ -467,27 +467,33 @@ void MainWindow::onMenuFileExit(wxCommandEvent &event) { } void MainWindow::loadModel(string path) { - if(path != "" && fileExists(path) == true) { - this->modelPathList.push_back(path); + try { + if(path != "" && fileExists(path) == true) { + this->modelPathList.push_back(path); + } + + string titlestring=winHeader; + for(unsigned int idx =0; idx < this->modelPathList.size(); idx++) { + string modelPath = this->modelPathList[idx]; + + timer->Stop(); + delete model; + Model *tmpModel= new ModelGl(); + renderer->loadTheModel(tmpModel, modelPath); + model= tmpModel; + + statusbarText = getModelInfo(); + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + timer->Start(100); + titlestring = extractFileFromDirectoryPath(modelPath) + " - "+ titlestring; + } + SetTitle(ToUnicode(titlestring)); + } + catch(std::runtime_error e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); } - - string titlestring=winHeader; - for(unsigned int idx =0; idx < this->modelPathList.size(); idx++) { - string modelPath = this->modelPathList[idx]; - - timer->Stop(); - delete model; - Model *tmpModel= new ModelGl(); - renderer->loadTheModel(tmpModel, modelPath); - model= tmpModel; - - statusbarText = getModelInfo(); - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); - timer->Start(100); - titlestring = extractFileFromDirectoryPath(modelPath) + " - "+ titlestring; - } - SetTitle(ToUnicode(titlestring)); } void MainWindow::loadParticle(string path) { diff --git a/source/shared_lib/include/graphics/FileReader.h b/source/shared_lib/include/graphics/FileReader.h index b186ca64..d025adf6 100644 --- a/source/shared_lib/include/graphics/FileReader.h +++ b/source/shared_lib/include/graphics/FileReader.h @@ -3,9 +3,9 @@ // // Copyright (C) 2001-2010 MartiƱo Figueroa and others // -// You can redistribute this code and/or modify it under -// the terms of the GNU General Public License as published -// by the Free Software Foundation; either version 2 of the +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version // ============================================================== @@ -152,14 +152,14 @@ static inline T* readFromFileReaders(vector const *>* readers, con T* ret = NULL; file.seekg(0, ios::beg); //Set position to first try { - FileReader const * reader = *i; + FileReader const * reader = *i; ret = reader->read(file, filepath); //It is guaranteed that at least the filepath matches ... } catch (...) { //TODO: Specific exceptions continue; } if (ret != NULL) { return ret; - } + } } return NULL; } @@ -169,7 +169,7 @@ static inline T* readFromFileReaders(vector const *>* readers, con //try to assign file ifstream file(filepath.c_str(), ios::in | ios::binary); if (!file.is_open()) { //An error occured; TODO: Which one - throw an exception, print error message? - throw runtime_error("Could not open file " + filepath); + throw runtime_error("Could not open file [" + filepath + "]"); } for (typename vector const *>::const_iterator i = readers->begin(); i != readers->end(); ++i) { T* ret = NULL; @@ -182,7 +182,7 @@ static inline T* readFromFileReaders(vector const *>* readers, con } if (ret != NULL) { return ret; - } + } } return NULL; } @@ -228,7 +228,7 @@ T* FileReader::readPath(const string& filepath, T* object) { T* ret = readFromFileReaders(&(getFileReaders()), filepath, object); //Try all other if (ret == NULL) { std::cerr << "Could not parse filepath: " << filepath << std::endl; - ret = readFromFileReaders(&(getLowPriorityFileReaders()), filepath); //Try to get dummy file + ret = readFromFileReaders(&(getLowPriorityFileReaders()), filepath); //Try to get dummy file if (ret == NULL) { throw runtime_error(string("Could not parse ") + filepath + " as object of type " + typeid(T).name()); } diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index fb12fae7..4fa71fe3 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -18,6 +18,7 @@ #include "interpolation.h" #include "conversion.h" #include "util.h" +#include "platform_common.h" #if defined(ENABLE_VBO_CODE) @@ -28,6 +29,7 @@ #include "leak_dumper.h" using namespace Shared::Platform; +using namespace Shared::PlatformCommon; using namespace std; @@ -212,7 +214,11 @@ void Mesh::loadV2(const string &dir, FILE *f, TextureManager *textureManager,boo //texture if(meshHeader.hasTexture && textureManager!=NULL){ texturePaths[mtDiffuse]= toLower(reinterpret_cast(meshHeader.texName)); - string texPath= dir+"/"+texturePaths[mtDiffuse]; + string texPath= dir; + if(texPath != "") { + texPath += "/"; + } + texPath += texturePaths[mtDiffuse]; textures[mtDiffuse]= static_cast(textureManager->getTexture(texPath)); if(textures[mtDiffuse]==NULL){ @@ -266,7 +272,12 @@ void Mesh::loadV3(const string &dir, FILE *f, TextureManager *textureManager,boo //texture if(!(meshHeader.properties & mp3NoTexture) && textureManager!=NULL){ texturePaths[mtDiffuse]= toLower(reinterpret_cast(meshHeader.texName)); - string texPath= dir+"/"+texturePaths[mtDiffuse]; + + string texPath= dir; + if(texPath != "") { + texPath += "/"; + } + texPath += texturePaths[mtDiffuse]; textures[mtDiffuse]= static_cast(textureManager->getTexture(texPath)); if(textures[mtDiffuse]==NULL){ @@ -327,7 +338,11 @@ void Mesh::load(const string &dir, FILE *f, TextureManager *textureManager,bool readBytes = fread(cMapPath, mapPathSize, 1, f); string mapPath= toLower(reinterpret_cast(cMapPath)); - string mapFullPath= dir + "/" + mapPath; + string mapFullPath= dir; + if(mapFullPath != "") { + mapFullPath += "/"; + } + mapFullPath += mapPath; textures[i]= static_cast(textureManager->getTexture(mapFullPath)); if(textures[i]==NULL){ @@ -573,19 +588,19 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) { try{ FILE *f=fopen(path.c_str(),"rb"); - if (f==NULL){ - throw runtime_error("Error opening 3d model file"); + if (f == NULL) { + printf("In [%s::%s] cannot load file = [%s]\n",__FILE__,__FUNCTION__,path.c_str()); + throw runtime_error("Error opening g3d model file [" + path + "]"); } - string dir= cutLastFile(path); + string dir= extractDirectoryPathFromFile(path); //file header FileHeader fileHeader; size_t readBytes = fread(&fileHeader, sizeof(FileHeader), 1, f); - if(strncmp(reinterpret_cast(fileHeader.id), "G3D", 3)!=0){ - + if(strncmp(reinterpret_cast(fileHeader.id), "G3D", 3) != 0) { printf("In [%s::%s] file = [%s] fileheader.id = [%s][%c]\n",__FILE__,__FUNCTION__,path.c_str(),reinterpret_cast(fileHeader.id),fileHeader.id[0]); - throw runtime_error("Not a valid S3D model"); + throw runtime_error("Not a valid G3D model"); } fileVersion= fileHeader.version;