ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

textureset.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/stark/resources/textureset.h"
00024 
00025 #include "engines/stark/debug.h"
00026 
00027 #include "engines/stark/formats/dds.h"
00028 #include "engines/stark/formats/tm.h"
00029 #include "engines/stark/formats/xrc.h"
00030 
00031 #include "engines/stark/gfx/driver.h"
00032 #include "engines/stark/gfx/texture.h"
00033 
00034 #include "engines/stark/services/archiveloader.h"
00035 #include "engines/stark/services/services.h"
00036 #include "engines/stark/services/settings.h"
00037 
00038 #include "common/file.h"
00039 #include "common/unzip.h"
00040 #include "image/png.h"
00041 
00042 namespace Stark {
00043 namespace Resources {
00044 
00045 TextureSet::~TextureSet() {
00046     delete _textureSet;
00047 }
00048 
00049 TextureSet::TextureSet(Object *parent, byte subType, uint16 index, const Common::String &name) :
00050         Object(parent, subType, index, name),
00051         _textureSet(nullptr) {
00052     _type = TYPE;
00053 }
00054 
00055 Gfx::TextureSet *TextureSet::getTexture() {
00056     return _textureSet;
00057 }
00058 
00059 void TextureSet::readData(Formats::XRCReadStream *stream) {
00060     _filename = stream->readString();
00061     _archiveName = stream->getArchiveName();
00062 }
00063 
00064 void TextureSet::onPostRead() {
00065     if (StarkSettings->isAssetsModEnabled()) {
00066         _textureSet = readOverrideDdsArchive();
00067     }
00068 
00069     if (!_textureSet) {
00070         ArchiveReadStream *stream = StarkArchiveLoader->getFile(_filename, _archiveName);
00071 
00072         _textureSet = Formats::TextureSetReader::read(stream);
00073 
00074         delete stream;
00075     }
00076 }
00077 
00078 static Common::String stripExtension(const Common::String &filename) {
00079     if (filename.hasSuffixIgnoreCase(".bmp")) {
00080         return Common::String(filename.c_str(), filename.size() - 4);
00081     }
00082     return filename;
00083 }
00084 
00085 void TextureSet::extractArchive() {
00086     ArchiveReadStream *stream = StarkArchiveLoader->getFile(_filename, _archiveName);
00087     Formats::BiffArchive *archive = Formats::TextureSetReader::readArchive(stream);
00088 
00089     Common::Array<Formats::Texture *> textures = archive->listObjectsRecursive<Formats::Texture>();
00090     for (uint i = 0; i < textures.size(); i++) {
00091         Common::String filename = Common::String::format(
00092                     "dump/%s/%s.png",
00093                     _filename.c_str(),
00094                     stripExtension(textures[i]->getName()).c_str());
00095 
00096         if (Common::File::exists(filename)) {
00097             continue;
00098         }
00099 
00100         Common::DumpFile out;
00101         if (!out.open(filename, true)) {
00102             warning("Unable to open file '%s' for writing", filename.c_str());
00103             return;
00104         }
00105 
00106         Graphics::Surface *surface = textures[i]->getSurface();
00107 
00108         Image::writePNG(out, *surface);
00109 
00110         out.close();
00111 
00112         surface->free();
00113         delete surface;
00114     }
00115 
00116     delete archive;
00117     delete stream;
00118 }
00119 
00120 Gfx::TextureSet *TextureSet::readOverrideDdsArchive() {
00121     Common::String archiveName = _filename + ".zip";
00122 
00123     debugC(kDebugModding, "Attempting to load %s", archiveName.c_str());
00124 
00125     Common::Archive *archive = Common::makeZipArchive(archiveName);
00126     if (!archive) {
00127         return nullptr;
00128     }
00129 
00130     Common::ArchiveMemberList files;
00131     archive->listMatchingMembers(files, "*.dds");
00132     if (files.empty()) {
00133         warning("No DDS files found in archive %s", archiveName.c_str());
00134         delete archive;
00135         return nullptr;
00136     }
00137 
00138     uint loadedCount = 0;
00139     Gfx::TextureSet *textureSet = new Gfx::TextureSet();
00140 
00141     for (Common::ArchiveMemberList::const_iterator it = files.begin(); it != files.end(); it++) {
00142         const Common::String &name = (*it)->getName();
00143 
00144         Common::SeekableReadStream *ddsStream = (*it)->createReadStream();
00145         if (!ddsStream) {
00146             warning("Unable to open %s for reading in %s", (*it)->getName().c_str(), archiveName.c_str());
00147             continue;
00148         }
00149 
00150         Formats::DDS dds;
00151         if (!dds.load(*ddsStream, name + " in " + archiveName)) {
00152             delete ddsStream;
00153             continue;
00154         }
00155 
00156         const Formats::DDS::MipMaps &mipmaps = dds.getMipMaps();
00157         if (mipmaps.empty()) {
00158             warning("No mipmaps in %s", name.c_str());
00159             delete ddsStream;
00160             continue;
00161         }
00162 
00163         Gfx::Texture *texture = StarkGfx->createTexture();
00164         texture->setLevelCount(mipmaps.size());
00165         for (uint i = 0; i < mipmaps.size(); i++) {
00166             texture->addLevel(i, &mipmaps[i]);
00167         }
00168 
00169         // Remove the .dds extension, add .bmp to match the names
00170         // used by the models.
00171         Common::String textureName = Common::String(name.c_str(), name.size() - 4);
00172 
00173         textureSet->addTexture(textureName + ".bmp", texture);
00174 
00175         delete ddsStream;
00176 
00177         loadedCount++;
00178     }
00179 
00180     debugC(kDebugModding, "Loaded %d textures from %s", loadedCount, archiveName.c_str());
00181 
00182     delete archive;
00183 
00184     return textureSet;
00185 }
00186 
00187 void TextureSet::printData() {
00188     debug("filename: %s", _filename.c_str());
00189 }
00190 
00191 } // End of namespace Resources
00192 } // End of namespace Stark


Generated on Sat Dec 7 2019 05:00:39 for ResidualVM by doxygen 1.7.1
curved edge   curved edge