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.bytesPerPixel == 4);
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 uint32 *srcPixel = (const uint32 *) source->getBasePtr(0, y);
00112         uint32 *dstPixel = (uint32 *) dest->getBasePtr(0, y);
00113 
00114         for (uint x = 0; x < source->w; x++) {
00115             byte a, r, g, b;
00116             source->format.colorToARGB(*srcPixel++, a, r, g, b);
00117 
00118             if (a != 0xFF) {
00119                 r = (int) r * a / 255;
00120                 g = (int) g * a / 255;
00121                 b = (int) b * a / 255;
00122             }
00123 
00124             *dstPixel++ = dest->format.ARGBToColor(a, r, g, b);
00125         }
00126     }
00127 
00128     return dest;
00129 }
00130 
00131 void VisualImageXMG::render(const Common::Point &position, bool useOffset) {
00132     render(position, useOffset, true);
00133 }
00134 
00135 void VisualImageXMG::render(const Common::Point &position, bool useOffset, bool unscaled) {
00136     Common::Point drawPos = useOffset ? position - _hotspot : position;
00137 
00138     if (!unscaled) {
00139         uint width = _gfx->scaleWidthOriginalToCurrent(_originalWidth);
00140         uint height = _gfx->scaleHeightOriginalToCurrent(_originalHeight);
00141         _surfaceRenderer->render(_texture, drawPos, width, height);
00142     } else {
00143         _surfaceRenderer->render(_texture, drawPos, _originalWidth, _originalHeight);
00144     }
00145 }
00146 
00147 void VisualImageXMG::setFadeLevel(float fadeLevel) {
00148     _surfaceRenderer->setFadeLevel(fadeLevel);
00149 }
00150 
00151 bool VisualImageXMG::isPointSolid(const Common::Point &point) const {
00152     assert(_surface);
00153 
00154     if (_originalWidth < 32 || _originalHeight < 32) {
00155         return true; // Small images are always solid
00156     }
00157 
00158     Common::Point scaledPoint;
00159     scaledPoint.x = point.x * _surface->w / _originalWidth;
00160     scaledPoint.y = point.y * _surface->h / _originalHeight;
00161     scaledPoint.x = CLIP<uint16>(scaledPoint.x, 0, _surface->w);
00162     scaledPoint.y = CLIP<uint16>(scaledPoint.y, 0, _surface->h);
00163 
00164     // Maybe implement this method in some other way to avoid having to keep the surface in memory
00165     const byte *ptr = (const byte *) _surface->getBasePtr(scaledPoint.x, scaledPoint.y);
00166     return *(ptr + 3) == 0xFF;
00167 }
00168 
00169 int VisualImageXMG::getWidth() const {
00170     return _originalWidth;
00171 }
00172 
00173 int VisualImageXMG::getHeight() const {
00174     return _originalHeight;
00175 }
00176 
00177 const Graphics::Surface *VisualImageXMG::getSurface() const {
00178     assert(_surface);
00179     return _surface;
00180 }
00181 
00182 } // End of namespace Stark


Generated on Sat Mar 16 2019 05:01:37 for ResidualVM by doxygen 1.7.1
curved edge   curved edge