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

visual/image.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/visual/image.h"
00024 
00025 #include "graphics/surface.h"
00026 #include "image/png.h"
00027 
00028 #include "engines/stark/formats/xmg.h"
00029 #include "engines/stark/gfx/driver.h"
00030 #include "engines/stark/gfx/surfacerenderer.h"
00031 #include "engines/stark/gfx/texture.h"
00032 #include "engines/stark/services/services.h"
00033 #include "engines/stark/services/settings.h"
00034 
00035 namespace Stark {
00036 
00037 VisualImageXMG::VisualImageXMG(Gfx::Driver *gfx) :
00038         Visual(TYPE),
00039         _gfx(gfx),
00040         _texture(nullptr),
00041         _surface(nullptr),
00042         _originalWidth(0),
00043         _originalHeight(0) {
00044     _surfaceRenderer = _gfx->createSurfaceRenderer();
00045 }
00046 
00047 VisualImageXMG::~VisualImageXMG() {
00048     if (_surface) {
00049         _surface->free();
00050     }
00051     delete _surface;
00052     delete _texture;
00053     delete _surfaceRenderer;
00054 }
00055 
00056 void VisualImageXMG::setHotSpot(const Common::Point &hotspot) {
00057     _hotspot = hotspot;
00058 }
00059 
00060 void VisualImageXMG::load(Common::ReadStream *stream) {
00061     assert(!_surface && !_texture);
00062 
00063     // Decode the XMG
00064     _surface = Formats::XMGDecoder::decode(stream);
00065     _texture = _gfx->createTexture(_surface);
00066     _texture->setSamplingFilter(StarkSettings->getImageSamplingFilter());
00067 
00068     _originalWidth  = _surface->w;
00069     _originalHeight = _surface->h;
00070 }
00071 
00072 void VisualImageXMG::readOriginalSize(Common::ReadStream *stream) {
00073     Formats::XMGDecoder::readSize(stream, _originalWidth, _originalHeight);
00074 }
00075 
00076 bool VisualImageXMG::loadPNG(Common::SeekableReadStream *stream) {
00077     assert(!_surface && !_texture);
00078 
00079     // Decode the XMG
00080     Image::PNGDecoder pngDecoder;
00081     if (!pngDecoder.loadStream(*stream)) {
00082         return false;
00083     }
00084 
00085     if (pngDecoder.getPalette()) {
00086         warning("Indexed colors PNG images are not supported");
00087         return false;
00088     }
00089 
00090     if (StarkSettings->shouldPreMultiplyReplacementPNGs()) {
00091         // We can do alpha pre-multiplication when loading for
00092         // convenience when testing modded graphics.
00093         _surface = multiplyColorWithAlpha(pngDecoder.getSurface());
00094     } else {
00095         _surface = pngDecoder.getSurface()->convertTo(Gfx::Driver::getRGBAPixelFormat());
00096     }
00097 
00098     _texture = _gfx->createTexture(_surface);
00099     _texture->setSamplingFilter(StarkSettings->getImageSamplingFilter());
00100 
00101     return true;
00102 }
00103 
00104 Graphics::Surface *VisualImageXMG::multiplyColorWithAlpha(const Graphics::Surface *source) {
00105     assert(source->format == Gfx::Driver::getRGBAPixelFormat());
00106 
00107     Graphics::Surface *dest = new Graphics::Surface();
00108     dest->create(source->w, source->h, Gfx::Driver::getRGBAPixelFormat());
00109 
00110     for (uint y = 0; y < source->h; y++) {
00111         const uint8 *src = (const uint8 *) source->getBasePtr(0, y);
00112         uint8 *dst = (uint8 *) dest->getBasePtr(0, y);
00113 
00114         for (uint x = 0; x < source->w; x++) {
00115             uint8 a, r, g, b;
00116             r = *src++;
00117             g = *src++;
00118             b = *src++;
00119             a = *src++;
00120 
00121             if (a != 0xFF) {
00122                 r = (int) r * a / 255;
00123                 g = (int) g * a / 255;
00124                 b = (int) b * a / 255;
00125             }
00126 
00127             *dst++ = r;
00128             *dst++ = g;
00129             *dst++ = b;
00130             *dst++ = a;
00131         }
00132     }
00133 
00134     return dest;
00135 }
00136 
00137 void VisualImageXMG::render(const Common::Point &position, bool useOffset) {
00138     render(position, useOffset, true);
00139 }
00140 
00141 void VisualImageXMG::render(const Common::Point &position, bool useOffset, bool unscaled) {
00142     Common::Point drawPos = useOffset ? position - _hotspot : position;
00143 
00144     if (!unscaled) {
00145         uint width = _gfx->scaleWidthOriginalToCurrent(_originalWidth);
00146         uint height = _gfx->scaleHeightOriginalToCurrent(_originalHeight);
00147         _surfaceRenderer->render(_texture, drawPos, width, height);
00148     } else {
00149         _surfaceRenderer->render(_texture, drawPos, _originalWidth, _originalHeight);
00150     }
00151 }
00152 
00153 void VisualImageXMG::setFadeLevel(float fadeLevel) {
00154     _surfaceRenderer->setFadeLevel(fadeLevel);
00155 }
00156 
00157 bool VisualImageXMG::isPointSolid(const Common::Point &point) const {
00158     assert(_surface);
00159 
00160     if (_originalWidth < 32 || _originalHeight < 32) {
00161         return true; // Small images are always solid
00162     }
00163 
00164     Common::Point scaledPoint;
00165     scaledPoint.x = point.x * _surface->w / _originalWidth;
00166     scaledPoint.y = point.y * _surface->h / _originalHeight;
00167     scaledPoint.x = CLIP<uint16>(scaledPoint.x, 0, _surface->w);
00168     scaledPoint.y = CLIP<uint16>(scaledPoint.y, 0, _surface->h);
00169 
00170     // Maybe implement this method in some other way to avoid having to keep the surface in memory
00171     const byte *ptr = (const byte *) _surface->getBasePtr(scaledPoint.x, scaledPoint.y);
00172     return *(ptr + 3) == 0xFF;
00173 }
00174 
00175 int VisualImageXMG::getWidth() const {
00176     return _originalWidth;
00177 }
00178 
00179 int VisualImageXMG::getHeight() const {
00180     return _originalHeight;
00181 }
00182 
00183 const Graphics::Surface *VisualImageXMG::getSurface() const {
00184     assert(_surface);
00185     return _surface;
00186 }
00187 
00188 } // End of namespace Stark


Generated on Sat May 25 2019 05:00:47 for ResidualVM by doxygen 1.7.1
curved edge   curved edge