2012-01-06 23:29:45 +01:00
|
|
|
/* ResidualVM - A 3D game interpreter
|
2008-06-13 14:57:47 +00:00
|
|
|
*
|
2012-01-06 23:29:45 +01:00
|
|
|
* ResidualVM is the legal property of its developers, whose names
|
2011-04-16 14:12:44 +02:00
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
2008-06-13 14:57:47 +00:00
|
|
|
* file distributed with this source distribution.
|
2006-04-02 14:20:45 +00:00
|
|
|
*
|
2012-12-19 23:15:43 +01:00
|
|
|
* This program is free software; you can redistribute it 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.
|
2014-04-05 18:18:42 +02:00
|
|
|
*
|
2012-12-19 23:15:43 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2006-04-02 14:20:45 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-12-19 23:15:43 +01:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-04-05 18:18:42 +02:00
|
|
|
*
|
2012-12-19 23:15:43 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-04-02 14:20:45 +00:00
|
|
|
*
|
|
|
|
*/
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2008-06-12 12:08:15 +00:00
|
|
|
#include "common/endian.h"
|
2014-04-05 18:18:42 +02:00
|
|
|
#include "image/tga.h"
|
2015-01-28 20:44:27 -08:00
|
|
|
#include "image/png.h"
|
2012-10-22 19:06:56 +02:00
|
|
|
#include "graphics/surface.h"
|
2008-01-26 11:47:23 +00:00
|
|
|
|
2011-08-13 14:04:32 +02:00
|
|
|
#include "engines/grim/grim.h"
|
2011-07-23 15:37:14 +02:00
|
|
|
#include "engines/grim/debug.h"
|
2009-05-24 19:13:58 +00:00
|
|
|
#include "engines/grim/material.h"
|
|
|
|
#include "engines/grim/gfx_base.h"
|
2011-07-18 22:16:40 +02:00
|
|
|
#include "engines/grim/colormap.h"
|
2011-07-27 18:07:35 +02:00
|
|
|
#include "engines/grim/resource.h"
|
2011-08-14 19:45:11 +02:00
|
|
|
#include "engines/grim/textsplit.h"
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2009-05-25 06:49:57 +00:00
|
|
|
namespace Grim {
|
|
|
|
|
2014-05-29 15:35:46 -07:00
|
|
|
Common::List<MaterialData *> *MaterialData::_materials = nullptr;
|
2011-07-18 22:16:40 +02:00
|
|
|
|
2011-12-19 14:49:31 +01:00
|
|
|
MaterialData::MaterialData(const Common::String &filename, Common::SeekableReadStream *data, CMap *cmap) :
|
2016-07-23 14:14:03 +02:00
|
|
|
_fname(filename), _cmap(cmap), _refCount(1), _textures(nullptr) {
|
2011-07-18 22:16:40 +02:00
|
|
|
|
2011-08-13 14:04:32 +02:00
|
|
|
if (g_grim->getGameType() == GType_MONKEY4) {
|
2012-02-04 18:07:24 -08:00
|
|
|
initEMI(data);
|
2011-08-13 14:04:32 +02:00
|
|
|
} else {
|
2012-04-24 17:21:18 -07:00
|
|
|
initGrim(data);
|
2011-08-13 14:04:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 20:44:27 -08:00
|
|
|
void loadPNG(Common::SeekableReadStream *data, Texture *t) {
|
2015-01-29 21:18:58 +01:00
|
|
|
Image::PNGDecoder *pngDecoder = new Image::PNGDecoder();
|
|
|
|
pngDecoder->loadStream(*data);
|
2015-01-28 20:44:27 -08:00
|
|
|
|
2015-02-02 22:30:59 +01:00
|
|
|
Graphics::Surface *pngSurface =pngDecoder->getSurface()->convertTo(Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24), pngDecoder->getPalette());
|
2021-05-04 11:45:03 +03:00
|
|
|
|
2015-01-29 21:18:58 +01:00
|
|
|
t->_width = pngSurface->w;
|
|
|
|
t->_height = pngSurface->h;
|
2015-01-28 20:44:27 -08:00
|
|
|
t->_texture = nullptr;
|
|
|
|
|
2015-01-29 21:18:58 +01:00
|
|
|
int bpp = pngSurface->format.bytesPerPixel;
|
2015-01-28 20:44:27 -08:00
|
|
|
assert(bpp == 4); // Assure we have 32 bpp
|
|
|
|
|
2015-01-28 20:54:07 -08:00
|
|
|
t->_colorFormat = BM_RGBA;
|
2015-01-28 20:44:27 -08:00
|
|
|
t->_bpp = 4;
|
|
|
|
t->_hasAlpha = true;
|
|
|
|
|
|
|
|
|
|
|
|
// Allocate room for the texture.
|
|
|
|
t->_data = new uint8[t->_width * t->_height * (bpp)];
|
|
|
|
|
|
|
|
// Copy the texture data, as the decoder owns the current copy.
|
2015-01-29 21:18:58 +01:00
|
|
|
memcpy(t->_data, pngSurface->getPixels(), t->_width * t->_height * (bpp));
|
2015-01-28 20:44:27 -08:00
|
|
|
|
2015-01-29 21:18:58 +01:00
|
|
|
pngSurface->free();
|
|
|
|
delete pngSurface;
|
|
|
|
delete pngDecoder;
|
2015-01-28 20:44:27 -08:00
|
|
|
}
|
|
|
|
|
2012-04-24 17:21:18 -07:00
|
|
|
void MaterialData::initGrim(Common::SeekableReadStream *data) {
|
2015-01-28 20:44:27 -08:00
|
|
|
if (_fname.hasSuffix(".png")) {
|
|
|
|
_numImages = 1;
|
|
|
|
_textures = new Texture*[1];
|
|
|
|
_textures[0] = new Texture();
|
|
|
|
loadPNG(data, _textures[0]);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-19 14:49:31 +01:00
|
|
|
uint32 tag = data->readUint32BE();
|
|
|
|
if (tag != MKTAG('M','A','T',' '))
|
2013-02-04 21:25:25 +01:00
|
|
|
error("Invalid header for texture %s. Expected 'MAT ', got '%c%c%c%c'", _fname.c_str(),
|
|
|
|
(tag >> 24) & 0xFF, (tag >> 16) & 0xFF, (tag >> 8) & 0xFF, tag & 0xFF);
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2016-07-23 14:14:03 +02:00
|
|
|
data->seek(12, SEEK_SET);
|
2011-12-19 14:49:31 +01:00
|
|
|
_numImages = data->readUint32LE();
|
2014-06-30 22:46:50 +02:00
|
|
|
_textures = new Texture*[_numImages];
|
2016-07-23 14:14:03 +02:00
|
|
|
/* Discovered by diffing orange.mat with pink.mat and blue.mat .
|
|
|
|
* Actual meaning unknown, so I prefer to use it as an enum-ish
|
|
|
|
* at the moment, to detect unexpected values.
|
|
|
|
*/
|
|
|
|
data->seek(0x4c, SEEK_SET);
|
|
|
|
uint32 offset = data->readUint32LE();
|
|
|
|
if (offset == 0x8)
|
|
|
|
offset = 16;
|
|
|
|
else if (offset != 0)
|
|
|
|
error("Unknown offset: %d", offset);
|
|
|
|
|
|
|
|
data->seek(60 + _numImages * 40 + offset, SEEK_SET);
|
2011-07-28 22:21:16 +02:00
|
|
|
for (int i = 0; i < _numImages; ++i) {
|
2014-06-30 22:46:50 +02:00
|
|
|
Texture *t = _textures[i] = new Texture();
|
2016-07-23 14:14:03 +02:00
|
|
|
t->_width = data->readUint32LE();
|
|
|
|
t->_height = data->readUint32LE();
|
2011-12-19 14:49:31 +01:00
|
|
|
t->_hasAlpha = data->readUint32LE();
|
2014-05-29 15:35:46 -07:00
|
|
|
t->_texture = nullptr;
|
2012-01-02 14:00:05 +01:00
|
|
|
t->_colorFormat = BM_RGBA;
|
2014-05-29 15:35:46 -07:00
|
|
|
t->_data = nullptr;
|
2011-07-28 22:21:16 +02:00
|
|
|
if (t->_width == 0 || t->_height == 0) {
|
2011-10-07 23:05:20 +02:00
|
|
|
Debug::warning(Debug::Materials, "skip load texture: bad texture size (%dx%d) for texture %d of material %s",
|
2013-07-09 21:12:55 +02:00
|
|
|
t->_width, t->_height, i, _fname.c_str());
|
2011-07-28 22:21:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-07-24 15:37:11 +02:00
|
|
|
t->_data = new uint8[t->_width * t->_height];
|
2016-07-23 14:14:03 +02:00
|
|
|
data->seek(12, SEEK_CUR);
|
2011-12-19 14:49:31 +01:00
|
|
|
data->read(t->_data, t->_width * t->_height);
|
2011-07-28 22:21:16 +02:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 14:00:05 +01:00
|
|
|
void loadTGA(Common::SeekableReadStream *data, Texture *t) {
|
2014-04-05 18:18:42 +02:00
|
|
|
Image::TGADecoder *tgaDecoder = new Image::TGADecoder();
|
2012-10-22 19:06:56 +02:00
|
|
|
tgaDecoder->loadStream(*data);
|
|
|
|
const Graphics::Surface *tgaSurface = tgaDecoder->getSurface();
|
|
|
|
|
|
|
|
t->_width = tgaSurface->w;
|
|
|
|
t->_height = tgaSurface->h;
|
2014-05-29 15:35:46 -07:00
|
|
|
t->_texture = nullptr;
|
2012-10-22 19:06:56 +02:00
|
|
|
|
|
|
|
int bpp = tgaSurface->format.bytesPerPixel;
|
|
|
|
if (bpp == 4) {
|
2012-11-18 09:47:23 -08:00
|
|
|
t->_colorFormat = BM_BGRA;
|
2012-01-02 14:00:05 +01:00
|
|
|
t->_bpp = 4;
|
2012-08-12 15:05:32 +02:00
|
|
|
t->_hasAlpha = true;
|
2012-01-02 14:00:05 +01:00
|
|
|
} else {
|
2012-01-10 06:39:04 +01:00
|
|
|
t->_colorFormat = BM_BGR888;
|
2012-01-02 14:00:05 +01:00
|
|
|
t->_bpp = 3;
|
2012-08-12 15:05:32 +02:00
|
|
|
t->_hasAlpha = false;
|
2012-01-02 14:00:05 +01:00
|
|
|
}
|
2012-10-22 19:06:56 +02:00
|
|
|
|
|
|
|
assert(bpp == 3 || bpp == 4); // Assure we have 24/32 bpp
|
|
|
|
|
|
|
|
// Allocate room for the texture.
|
2014-07-24 15:37:11 +02:00
|
|
|
t->_data = new uint8[t->_width * t->_height * (bpp)];
|
2012-10-22 19:06:56 +02:00
|
|
|
|
|
|
|
// Copy the texture data, as the decoder owns the current copy.
|
2013-10-13 11:30:34 +02:00
|
|
|
memcpy(t->_data, tgaSurface->getPixels(), t->_width * t->_height * (bpp));
|
2012-10-22 19:06:56 +02:00
|
|
|
|
|
|
|
delete tgaDecoder;
|
2012-01-02 14:00:05 +01:00
|
|
|
}
|
2013-07-09 21:12:55 +02:00
|
|
|
|
2012-02-04 18:07:24 -08:00
|
|
|
void MaterialData::initEMI(Common::SeekableReadStream *data) {
|
2011-08-14 19:45:11 +02:00
|
|
|
|
2012-02-04 18:07:24 -08:00
|
|
|
if (_fname.hasSuffix(".sur")) { // This expects that we want all the materials in the sur-file
|
2014-06-30 22:46:50 +02:00
|
|
|
Common::Array<Common::String> texFileNames;
|
2014-01-07 12:29:41 +01:00
|
|
|
char readFileName[64];
|
2013-02-04 21:36:38 +01:00
|
|
|
TextSplitter *ts = new TextSplitter(_fname, data);
|
2012-01-02 14:00:05 +01:00
|
|
|
ts->setLineNumber(2); // Skip copyright-line
|
|
|
|
ts->expectString("version\t1.0");
|
|
|
|
if (ts->checkString("name:"))
|
2012-02-21 18:17:37 -08:00
|
|
|
ts->scanString("name:%s", 1, readFileName);
|
2013-07-09 21:12:55 +02:00
|
|
|
|
|
|
|
while (!ts->checkString("END_OF_SECTION")) {
|
2012-02-21 18:17:37 -08:00
|
|
|
ts->scanString("tex:%s", 1, readFileName);
|
2011-08-14 19:45:11 +02:00
|
|
|
Common::String mFileName(readFileName);
|
2012-08-12 18:00:23 +02:00
|
|
|
texFileNames.push_back(ResourceLoader::fixFilename(mFileName, false));
|
2011-08-14 19:45:11 +02:00
|
|
|
}
|
2014-06-30 22:46:50 +02:00
|
|
|
_textures = new Texture*[texFileNames.size()];
|
2011-08-14 19:45:11 +02:00
|
|
|
for (uint i = 0; i < texFileNames.size(); i++) {
|
2014-06-30 22:46:50 +02:00
|
|
|
Common::String name = texFileNames[i];
|
|
|
|
if (name.hasPrefix("specialty")) {
|
|
|
|
_textures[i] = g_driver->getSpecialtyTexturePtr(name);
|
|
|
|
} else {
|
|
|
|
_textures[i] = new Texture();
|
|
|
|
Common::SeekableReadStream *texData = g_resourceloader->openNewStreamFile(texFileNames[i].c_str(), true);
|
|
|
|
if (!texData) {
|
|
|
|
warning("Couldn't find tex-file: %s", texFileNames[i].c_str());
|
|
|
|
_textures[i]->_width = 0;
|
|
|
|
_textures[i]->_height = 0;
|
|
|
|
_textures[i]->_texture = new int(1); // HACK to avoid initializing.
|
|
|
|
_textures[i]->_data = nullptr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
loadTGA(texData, _textures[i]);
|
|
|
|
delete texData;
|
2012-01-02 14:00:05 +01:00
|
|
|
}
|
2011-08-14 19:45:11 +02:00
|
|
|
}
|
|
|
|
_numImages = texFileNames.size();
|
2012-01-26 06:51:10 +01:00
|
|
|
delete ts;
|
2012-01-02 14:00:05 +01:00
|
|
|
return;
|
2013-07-09 21:12:55 +02:00
|
|
|
} else if (_fname.hasSuffix(".tga")) {
|
2011-08-14 19:45:11 +02:00
|
|
|
_numImages = 1;
|
2014-06-30 22:46:50 +02:00
|
|
|
_textures = new Texture*[1];
|
|
|
|
_textures[0] = new Texture();
|
|
|
|
loadTGA(data, _textures[0]);
|
2012-01-02 14:00:05 +01:00
|
|
|
return;
|
2014-06-30 22:46:50 +02:00
|
|
|
} else if (_fname.hasPrefix("specialty")) {
|
|
|
|
_numImages = 1;
|
|
|
|
_textures = new Texture*[1];
|
|
|
|
_textures[0] = g_driver->getSpecialtyTexturePtr(_fname);
|
2011-08-14 19:45:11 +02:00
|
|
|
} else {
|
2012-02-04 18:07:24 -08:00
|
|
|
warning("Unknown material-format: %s", _fname.c_str());
|
2011-08-13 14:04:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-18 22:16:40 +02:00
|
|
|
MaterialData::~MaterialData() {
|
|
|
|
_materials->remove(this);
|
|
|
|
if (_materials->empty()) {
|
|
|
|
delete _materials;
|
2014-05-29 15:35:46 -07:00
|
|
|
_materials = nullptr;
|
2011-07-18 22:16:40 +02:00
|
|
|
}
|
|
|
|
|
2011-07-28 22:21:16 +02:00
|
|
|
for (int i = 0; i < _numImages; ++i) {
|
2014-06-30 22:46:50 +02:00
|
|
|
Texture *t = _textures[i];
|
|
|
|
if (!t) continue;
|
|
|
|
if (t->_isShared) continue; // don't delete specialty textures
|
2011-11-02 17:26:31 +01:00
|
|
|
if (t->_width && t->_height && t->_texture)
|
2014-06-30 22:13:40 +02:00
|
|
|
g_driver->destroyTexture(t);
|
2011-11-02 17:26:31 +01:00
|
|
|
delete[] t->_data;
|
2014-06-30 22:46:50 +02:00
|
|
|
delete t;
|
2011-07-28 22:21:16 +02:00
|
|
|
}
|
|
|
|
delete[] _textures;
|
2011-07-18 22:16:40 +02:00
|
|
|
}
|
|
|
|
|
2011-12-19 14:49:31 +01:00
|
|
|
MaterialData *MaterialData::getMaterialData(const Common::String &filename, Common::SeekableReadStream *data, CMap *cmap) {
|
2011-07-18 22:16:40 +02:00
|
|
|
if (!_materials) {
|
|
|
|
_materials = new Common::List<MaterialData *>();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Common::List<MaterialData *>::iterator i = _materials->begin(); i != _materials->end(); ++i) {
|
|
|
|
MaterialData *m = *i;
|
2012-01-02 14:00:05 +01:00
|
|
|
if (m->_fname == filename && g_grim->getGameType() == GType_MONKEY4) {
|
|
|
|
++m->_refCount;
|
|
|
|
return m;
|
|
|
|
}
|
2015-01-31 13:36:01 -08:00
|
|
|
// We need to allow null cmaps for remastered overlays
|
|
|
|
if (m->_fname == filename && (!(m->_cmap || cmap) || m->_cmap->getFilename() == cmap->getFilename())) {
|
2011-07-18 22:16:40 +02:00
|
|
|
++m->_refCount;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 14:49:31 +01:00
|
|
|
MaterialData *m = new MaterialData(filename, data, cmap);
|
2011-07-18 22:16:40 +02:00
|
|
|
_materials->push_back(m);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2014-06-17 21:58:21 -04:00
|
|
|
Material::Material(const Common::String &filename, Common::SeekableReadStream *data, CMap *cmap, bool clamp) :
|
2011-07-18 22:16:40 +02:00
|
|
|
Object(), _currImage(0) {
|
2011-12-19 14:49:31 +01:00
|
|
|
_data = MaterialData::getMaterialData(filename, data, cmap);
|
2014-06-17 21:58:21 -04:00
|
|
|
_clampTexture = clamp;
|
2011-07-18 22:16:40 +02:00
|
|
|
}
|
|
|
|
|
2012-04-12 17:53:03 -07:00
|
|
|
Material::Material() :
|
2015-05-12 08:24:51 +02:00
|
|
|
Object(), _currImage(0), _data(nullptr), _clampTexture(false) {
|
2012-04-12 17:53:03 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 18:07:35 +02:00
|
|
|
void Material::reload(CMap *cmap) {
|
|
|
|
Common::String fname = _data->_fname;
|
|
|
|
--_data->_refCount;
|
|
|
|
if (_data->_refCount < 1) {
|
|
|
|
delete _data;
|
|
|
|
}
|
|
|
|
|
2014-06-17 21:58:21 -04:00
|
|
|
Material *m = g_resourceloader->loadMaterial(fname, cmap, _clampTexture);
|
2011-07-27 18:07:35 +02:00
|
|
|
// Steal the data from the new material and discard it.
|
|
|
|
_data = m->_data;
|
|
|
|
++_data->_refCount;
|
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
|
2003-08-15 18:00:22 +00:00
|
|
|
void Material::select() const {
|
2014-06-30 22:46:50 +02:00
|
|
|
Texture *t = _data->_textures[_currImage];
|
|
|
|
if (t && t->_width && t->_height) {
|
2011-11-02 17:26:31 +01:00
|
|
|
if (!t->_texture) {
|
2014-07-24 15:37:11 +02:00
|
|
|
g_driver->createTexture(t, (uint8 *)t->_data, _data->_cmap, _clampTexture);
|
2011-11-02 17:26:31 +01:00
|
|
|
delete[] t->_data;
|
2014-05-29 15:35:46 -07:00
|
|
|
t->_data = nullptr;
|
2011-11-02 17:26:31 +01:00
|
|
|
}
|
2014-06-30 22:13:40 +02:00
|
|
|
g_driver->selectTexture(t);
|
2014-06-30 22:46:50 +02:00
|
|
|
} else {
|
|
|
|
warning("Can't select material: %s", getFilename().c_str());
|
2011-11-02 17:26:31 +01:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Material::~Material() {
|
2012-04-12 17:53:03 -07:00
|
|
|
if (_data) {
|
|
|
|
--_data->_refCount;
|
|
|
|
if (_data->_refCount < 1) {
|
|
|
|
delete _data;
|
|
|
|
}
|
2011-07-18 22:16:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-18 18:46:20 +02:00
|
|
|
void Material::setActiveTexture(int n) {
|
|
|
|
_currImage = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Material::getNumTextures() const {
|
2011-07-18 22:16:40 +02:00
|
|
|
return _data->_numImages;
|
|
|
|
}
|
|
|
|
|
2011-09-18 18:46:20 +02:00
|
|
|
int Material::getActiveTexture() const {
|
2011-07-18 22:16:40 +02:00
|
|
|
return _currImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Common::String &Material::getFilename() const {
|
|
|
|
return _data->_fname;
|
|
|
|
}
|
|
|
|
|
|
|
|
MaterialData *Material::getData() const {
|
|
|
|
return _data;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
2009-05-25 06:49:57 +00:00
|
|
|
|
|
|
|
} // end of namespace Grim
|