2009-05-26 14:13:08 +00:00
|
|
|
/* Residual - A 3D game interpreter
|
2008-06-13 14:57:47 +00:00
|
|
|
*
|
|
|
|
* Residual 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
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*
|
|
|
|
*/
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2008-06-12 12:08:15 +00:00
|
|
|
#include "common/endian.h"
|
2008-01-26 11:47:23 +00:00
|
|
|
|
2009-06-18 11:52:26 +00:00
|
|
|
#include "engines/grim/grim.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"
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2009-05-25 06:49:57 +00:00
|
|
|
namespace Grim {
|
|
|
|
|
2011-07-18 22:16:40 +02:00
|
|
|
Common::List<MaterialData *> *MaterialData::_materials = NULL;
|
|
|
|
|
|
|
|
MaterialData::MaterialData(const Common::String &filename, const char *data, int len, CMap *cmap) :
|
|
|
|
_fname(filename), _cmap(cmap), _refCount(1) {
|
|
|
|
|
2004-02-24 21:09:53 +00:00
|
|
|
if (len < 4 || memcmp(data, "MAT ", 4) != 0)
|
2008-09-28 15:23:15 +00:00
|
|
|
error("invalid magic loading texture");
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-12-10 07:26:03 +00:00
|
|
|
_numImages = READ_LE_UINT32(data + 12);
|
2011-05-04 14:10:17 +08: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.
|
|
|
|
*/
|
|
|
|
uint32 offset = READ_LE_UINT32(data + 0x4c);
|
|
|
|
if (offset == 0x8)
|
2011-07-18 22:16:40 +02:00
|
|
|
data += 16;
|
2011-05-04 14:10:17 +08:00
|
|
|
else if (offset != 0)
|
2011-07-18 22:16:40 +02:00
|
|
|
error("Unknown offset: %d", offset);
|
2011-05-04 14:10:17 +08:00
|
|
|
_width = READ_LE_UINT32(data + 60 + _numImages * 40);
|
|
|
|
_height = READ_LE_UINT32(data + 64 + _numImages * 40);
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2008-07-30 07:04:32 +00:00
|
|
|
if (_width == 0 || _height == 0) {
|
2009-05-25 19:21:58 +00:00
|
|
|
if (gDebugLevel == DEBUG_WARN || gDebugLevel == DEBUG_ALL)
|
2011-05-22 12:02:20 +08:00
|
|
|
warning("skip load texture: bad texture size (%dx%d) for texture %s", _width, _height, _fname.c_str());
|
2005-01-13 20:21:12 +00:00
|
|
|
return;
|
2004-02-24 21:09:53 +00:00
|
|
|
}
|
2003-08-23 18:39:15 +00:00
|
|
|
|
2011-05-04 14:10:17 +08:00
|
|
|
data += 84 + _numImages * 40;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2009-06-26 16:13:11 +00:00
|
|
|
g_driver->createMaterial(this, data, cmap);
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2011-07-18 22:16:40 +02:00
|
|
|
MaterialData::~MaterialData() {
|
|
|
|
_materials->remove(this);
|
|
|
|
if (_materials->empty()) {
|
|
|
|
delete _materials;
|
|
|
|
_materials = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_width && _height)
|
|
|
|
g_driver->destroyMaterial(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
MaterialData *MaterialData::getMaterialData(const Common::String &filename, const char *data, int len, CMap *cmap) {
|
|
|
|
if (!_materials) {
|
|
|
|
_materials = new Common::List<MaterialData *>();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Common::List<MaterialData *>::iterator i = _materials->begin(); i != _materials->end(); ++i) {
|
|
|
|
MaterialData *m = *i;
|
|
|
|
if (m->_fname == filename && *m->_cmap == *cmap) {
|
|
|
|
++m->_refCount;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MaterialData *m = new MaterialData(filename, data, len, cmap);
|
|
|
|
_materials->push_back(m);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
Material::Material(const Common::String &filename, const char *data, int len, CMap *cmap) :
|
|
|
|
Object(), _currImage(0) {
|
|
|
|
_data = MaterialData::getMaterialData(filename, data, len, cmap);
|
|
|
|
}
|
|
|
|
|
2003-08-15 18:00:22 +00:00
|
|
|
void Material::select() const {
|
2011-07-18 22:16:40 +02:00
|
|
|
if (_data->_width && _data->_height)
|
|
|
|
g_driver->selectMaterial(this);
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Material::~Material() {
|
2011-07-18 22:16:40 +02:00
|
|
|
--_data->_refCount;
|
|
|
|
if (_data->_refCount < 1) {
|
|
|
|
delete _data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Material::getNumImages() const {
|
|
|
|
return _data->_numImages;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Material::getCurrentImage() const {
|
|
|
|
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
|