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

tiledsurface.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 COPYRIGHT
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 "common/textconsole.h"
00024 
00025 #if defined(USE_OPENGL)
00026 
00027 #include "graphics/opengl/tiledsurface.h"
00028 
00029 #include "graphics/opengl/surfacerenderer.h"
00030 #include "graphics/opengl/texture.h"
00031 
00032 namespace OpenGL {
00033 
00034 TiledSurface::TiledSurface(uint width, uint height, const Graphics::PixelFormat &pixelFormat) {
00035     _backingSurface.create(width, height, pixelFormat);
00036 
00037     for (uint y = 0; y < height; y += maxTextureSize) {
00038         for (uint x = 0; x < width; x += maxTextureSize) {
00039             uint textureWidth = (x + maxTextureSize >= width) ? (width - x) : maxTextureSize;
00040             uint textureHeight = (y + maxTextureSize >= height) ? (height - y) : maxTextureSize;
00041 
00042             _tiles.push_back(Tile());
00043 
00044             Tile &tile = _tiles.back();
00045             tile.rect = Common::Rect(textureWidth, textureHeight);
00046             tile.rect.translate(x, y);
00047             tile.texture = nullptr;
00048             tile.dirty = true;
00049         }
00050     }
00051 }
00052 
00053 TiledSurface::~TiledSurface() {
00054     for (uint i = 0; i < _tiles.size(); i++) {
00055         delete _tiles[i].texture;
00056     }
00057     _backingSurface.free();
00058 }
00059 
00060 void TiledSurface::copyRectToSurface(const void *src, int srcPitch, int x, int y, int w, int h) {
00061     _backingSurface.copyRectToSurface(src, srcPitch, x, y, w, h);
00062 
00063     Common::Rect destRect = Common::Rect(w, h);
00064     destRect.translate(x, y);
00065 
00066     for (uint i = 0; i < _tiles.size(); i++) {
00067         if (_tiles[i].rect.intersects(destRect) || _tiles[i].rect.contains(destRect)) {
00068             _tiles[i].dirty = true;
00069         }
00070     }
00071 }
00072 
00073 void TiledSurface::update() {
00074     for (uint i = 0; i < _tiles.size(); i++) {
00075         Tile &tile = _tiles[i];
00076         if (tile.dirty) {
00077             Graphics::Surface subSurface = _backingSurface.getSubArea(tile.rect);
00078 
00079             delete tile.texture;
00080             tile.texture = new Texture(subSurface);
00081 
00082             tile.dirty = false;
00083         }
00084     }
00085 }
00086 
00087 void TiledSurface::draw(SurfaceRenderer *surfaceRenderer) const {
00088     for (uint i = 0; i < _tiles.size(); i++) {
00089         const Tile &tile = _tiles[i];
00090 
00091         assert(tile.texture);
00092         assert(!tile.dirty);
00093 
00094         Math::Vector2d topLeft = Math::Vector2d(tile.rect.left / (float)_backingSurface.w, tile.rect.top / (float)_backingSurface.h);
00095         Math::Vector2d bottomRight = Math::Vector2d(tile.rect.right / (float)_backingSurface.w, tile.rect.bottom / (float)_backingSurface.h);
00096 
00097         surfaceRenderer->render(tile.texture, Math::Rect2d(topLeft, bottomRight));
00098     }
00099 }
00100 
00101 void TiledSurface::fill(uint32 color) {
00102     Common::Rect rect = Common::Rect(_backingSurface.w, _backingSurface.h);
00103     _backingSurface.fillRect(rect, color);
00104 
00105     invalidateAllTiles();
00106 }
00107 
00108 void TiledSurface::invalidateAllTiles() {
00109     for (uint i = 0; i < _tiles.size(); i++) {
00110         _tiles[i].dirty = true;
00111     }
00112 }
00113 
00114 Graphics::Surface *TiledSurface::getBackingSurface() {
00115     invalidateAllTiles();
00116     return &_backingSurface;
00117 }
00118 
00119 const Graphics::Surface *TiledSurface::getBackingSurface() const {
00120     return &_backingSurface;
00121 }
00122 
00123 } // End of namespace OpenGL
00124 
00125 #endif


Generated on Sat Feb 23 2019 05:01:19 for ResidualVM by doxygen 1.7.1
curved edge   curved edge