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

fish.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/effects/fish.h"
00024 
00025 #include "common/random.h"
00026 #include "common/tokenizer.h"
00027 
00028 #include "graphics/surface.h"
00029 
00030 #include "engines/stark/gfx/driver.h"
00031 #include "engines/stark/gfx/surfacerenderer.h"
00032 #include "engines/stark/gfx/texture.h"
00033 
00034 #include "engines/stark/services/global.h"
00035 #include "engines/stark/services/services.h"
00036 #include "engines/stark/services/settings.h"
00037 
00038 namespace Stark {
00039 
00040 VisualEffectFish::VisualEffectFish(Gfx::Driver *gfx, const Common::Point &size) :
00041         VisualEffect(TYPE, size, gfx),
00042         _fishCount(10),
00043         _currentFrame(0),
00044         _numFrames(20),
00045         _maxRadius(100),
00046         _fishPathWidthRatio(20),
00047         _fishPathHeightRatio(30),
00048         _fishPathWidth(0),
00049         _fishPathHeight(0),
00050         _masterPathWidth(0),
00051         _masterPathHeight(0),
00052         _mainColorR(44),
00053         _mainColorG(97),
00054         _mainColorB(133),
00055         _mainColor(0),
00056         _otherColor(0) {
00057 }
00058 
00059 VisualEffectFish::~VisualEffectFish() {
00060 }
00061 
00062 void VisualEffectFish::render(const Common::Point &position) {
00063     // Stop rendering if special effect is off
00064     if (!StarkSettings->getBoolSetting(Settings::kSpecialFX)) return;
00065 
00066     _timeRemainingUntilNextUpdate -= StarkGlobal->getMillisecondsPerGameloop();
00067     if (_timeRemainingUntilNextUpdate <= 0) {
00068         update();
00069         _timeRemainingUntilNextUpdate = _timeBetweenTwoUpdates;
00070     }
00071 
00072     // Fill with transparent color
00073     _surface->fillRect(Common::Rect(_surface->w, _surface->h), 0);
00074 
00075     for (uint i = 0; i < _fishList.size(); i++) {
00076         drawFish(_fishList[i]);
00077     }
00078 
00079     _texture->update(_surface);
00080     _surfaceRenderer->render(_texture, position);
00081 }
00082 
00083 void VisualEffectFish::setParams(const Common::String &params) {
00084     // Example input: GFX_Fish( 2, 30, 20, 30, (20, 50, 70), 20, 100 )
00085     Common::StringTokenizer tokenizer(params, "(), ");
00086 
00087     int index = 0;
00088     while (!tokenizer.empty()) {
00089         Common::String token = tokenizer.nextToken();
00090         switch (index) {
00091         case 0:
00092             if (token != "GFX_Fish") {
00093                 error("Unexpected effect type '%s'", token.c_str());
00094             }
00095             break;
00096         case 1:
00097             _timeBetweenTwoUpdates = CLIP<uint>(strtol(token.c_str(), nullptr, 10), 0, 1000) * 33;
00098             break;
00099         case 2:
00100             _fishCount = CLIP<uint>(strtol(token.c_str(), nullptr, 10), 1, 300);
00101             break;
00102         case 3:
00103             _fishPathWidthRatio = CLIP<int>(strtol(token.c_str(), nullptr, 10), 0, 100);
00104             break;
00105         case 4:
00106             _fishPathHeightRatio = CLIP<int>(strtol(token.c_str(), nullptr, 10), 0, 100);
00107             break;
00108         case 5:
00109             _mainColorR = strtol(token.c_str(), nullptr, 10);
00110             break;
00111         case 6:
00112             _mainColorG = strtol(token.c_str(), nullptr, 10);
00113             break;
00114         case 7:
00115             _mainColorB = strtol(token.c_str(), nullptr, 10);
00116             break;
00117         case 8:
00118             _numFrames = CLIP<uint>(strtol(token.c_str(), nullptr, 10), 10, 100);
00119             break;
00120         case 9:
00121             _maxRadius = CLIP<uint>(strtol(token.c_str(), nullptr, 10), 30, 200);
00122             break;
00123         default:
00124             warning("Unexpected parameter %d: %s", index, token.c_str());
00125         }
00126 
00127         index++;
00128     }
00129 
00130     _mainColor = _surface->format.RGBToColor(_mainColorR, _mainColorG, _mainColorB);
00131     _otherColor = _surface->format.RGBToColor(
00132             _mainColorR + (255 - _mainColorR) / 4,
00133             _mainColorG + (255 - _mainColorG) / 4,
00134             _mainColorB + (255 - _mainColorB) / 3
00135     );
00136 
00137     _fishPathWidth =  _size.x * _fishPathWidthRatio  / 100;
00138     _fishPathHeight = _size.y * _fishPathHeightRatio / 100;
00139     _masterPathWidth =  _size.x - _fishPathWidth;
00140     _masterPathHeight = _size.y - _fishPathHeight;
00141 
00142     _numFrames = 20; // The original also ignores the parameter
00143     _frames.resize(_numFrames);
00144     for (uint i = 0; i < _frames.size(); i++) {
00145         Frame &frame = _frames[i];
00146 
00147         // Barycentric coordinates
00148         float t = (i + 1) / (float)_frames.size();
00149         frame.weight1 = (1.0f - t) * (1.0f - t) * (1.0f - t) / 6.0f;
00150         frame.weight2 = (t * t * t * 3.0f - t * t * 6.0f + 4.0f) / 6.0f;
00151         frame.weight3 = (((3.0f - t * 3.0f) * t + 3.0f) * t + 1.0f) / 6.0f;
00152         frame.weight4 = t * t * t / 6.0f;
00153     }
00154 
00155     _fishList.resize(_fishCount);
00156     for (uint i = 0; i < _fishList.size(); i++) {
00157         Fish &fish = _fishList[i];
00158 
00159         fish.point1.x = StarkRandomSource->getRandomNumber(_fishPathWidth - 1);
00160         fish.point1.y = StarkRandomSource->getRandomNumber(_fishPathHeight - 1);
00161         fish.point2.x = StarkRandomSource->getRandomNumber(_fishPathWidth - 1);
00162         fish.point2.y = StarkRandomSource->getRandomNumber(_fishPathHeight - 1);
00163         fish.point3.x = StarkRandomSource->getRandomNumber(_fishPathWidth - 1);
00164         fish.point3.y = StarkRandomSource->getRandomNumber(_fishPathHeight - 1);
00165         fish.point4.x = StarkRandomSource->getRandomNumber(_fishPathWidth - 1);
00166         fish.point4.y = StarkRandomSource->getRandomNumber(_fishPathHeight - 1);
00167     }
00168 
00169     _masterPath.point1.x = StarkRandomSource->getRandomNumber(_masterPathWidth - 1);
00170     _masterPath.point1.y = StarkRandomSource->getRandomNumber(_masterPathHeight - 1);
00171     _masterPath.point2 = _masterPath.point1;
00172     _masterPath.point3 = _masterPath.point1;
00173     _masterPath.point4 = _masterPath.point1;
00174 }
00175 
00176 void VisualEffectFish::update() {
00177     _currentFrame++;
00178     if (_currentFrame >= _frames.size()) {
00179         _currentFrame %= _frames.size();
00180 
00181         for (uint i = 0; i < _fishList.size(); i++) {
00182             Fish &fish = _fishList[i];
00183             fish.point1 = fish.point2;
00184             fish.point2 = fish.point3;
00185             fish.point3 = fish.point4;
00186             fish.point4.x = StarkRandomSource->getRandomNumber(_fishPathWidth - 1);
00187             fish.point4.y = StarkRandomSource->getRandomNumber(_fishPathHeight - 1);
00188         }
00189 
00190         _masterPath.point1 = _masterPath.point2;
00191         _masterPath.point2 = _masterPath.point3;
00192         _masterPath.point3 = _masterPath.point4;
00193 
00194         uint radius;
00195         do {
00196             _masterPath.point4.x = StarkRandomSource->getRandomNumber(_masterPathWidth - 1);
00197             _masterPath.point4.y = StarkRandomSource->getRandomNumber(_masterPathHeight - 1);
00198 
00199             int deltaX = _masterPath.point4.x - _masterPath.point3.x;
00200             int deltaY = _masterPath.point4.y - _masterPath.point3.y;
00201             radius = sqrt(deltaX * deltaX + deltaY * deltaY);
00202         } while (radius > _maxRadius);
00203     }
00204 
00205     const Frame &frame = _frames[_currentFrame];
00206 
00207     _masterPath.currentPosition.x = _masterPath.point1.x * frame.weight1 + _masterPath.point2.x * frame.weight2
00208                                   + _masterPath.point3.x * frame.weight3 + _masterPath.point4.x * frame.weight4;
00209     _masterPath.currentPosition.y = _masterPath.point1.y * frame.weight1 + _masterPath.point2.y * frame.weight2
00210                                   + _masterPath.point3.y * frame.weight3 + _masterPath.point4.y * frame.weight4;
00211 
00212     for (uint i = 0; i < _fishList.size(); i++) {
00213         Fish &fish = _fishList[i];
00214 
00215         fish.previousPosition = fish.currentPosition;
00216         fish.currentPosition.x = fish.point1.x * frame.weight1 + fish.point2.x * frame.weight2
00217                                + fish.point3.x * frame.weight3 + fish.point4.x * frame.weight4;
00218         fish.currentPosition.y = fish.point1.y * frame.weight1 + fish.point2.y * frame.weight2
00219                                + fish.point3.y * frame.weight3 + fish.point4.y * frame.weight4;
00220 
00221         fish.currentPosition += _masterPath.currentPosition;
00222     }
00223 }
00224 
00225 void VisualEffectFish::drawFish(const Fish &fish) {
00226     if (fish.currentPosition.x < 0 || fish.currentPosition.x >= _surface->w
00227         || fish.currentPosition.y < 0 || fish.currentPosition.y >= _surface->h) {
00228         return;
00229     }
00230 
00231     if (fish.previousPosition.x < 0 || fish.previousPosition.x >= _surface->w
00232         || fish.previousPosition.y < 0 || fish.previousPosition.y >= _surface->h) {
00233         return;
00234     }
00235 
00236     int dot = (fish.previousPosition.x - fish.currentPosition.x)
00237             * (fish.previousPosition.x - fish.currentPosition.x)
00238             + (fish.previousPosition.y - fish.currentPosition.y)
00239             * (fish.previousPosition.y - fish.currentPosition.y);
00240 
00241     uint color;
00242     if (dot >= 4) {
00243         color = _mainColor;
00244     } else {
00245         color = _otherColor;
00246     }
00247     _surface->drawLine(fish.previousPosition.x, fish.previousPosition.y,
00248                        fish.currentPosition.x,  fish.currentPosition.y, color);
00249 }
00250 
00251 } // End of namespace Stark


Generated on Sat May 18 2019 05:01:02 for ResidualVM by doxygen 1.7.1
curved edge   curved edge