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

layer.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/resources/layer.h"
00024 
00025 #include "engines/stark/formats/xrc.h"
00026 
00027 #include "engines/stark/resources/camera.h"
00028 #include "engines/stark/resources/item.h"
00029 #include "engines/stark/resources/light.h"
00030 
00031 #include "engines/stark/services/services.h"
00032 #include "engines/stark/services/stateprovider.h"
00033 
00034 #include "engines/stark/scene.h"
00035 
00036 #include "common/debug.h"
00037 
00038 namespace Stark {
00039 namespace Resources {
00040 
00041 Object *Layer::construct(Object *parent, byte subType, uint16 index, const Common::String &name) {
00042     switch (subType) {
00043     case kLayer2D:
00044         return new Layer2D(parent, subType, index, name);
00045     case kLayer3D:
00046         return new Layer3D(parent, subType, index, name);
00047     default:
00048         error("Unknown layer subtype %d", subType);
00049     }
00050 }
00051 
00052 Layer::~Layer() {
00053 }
00054 
00055 Layer::Layer(Object *parent, byte subType, uint16 index, const Common::String &name) :
00056         Object(parent, subType, index, name),
00057         _scrollScale(1.0),
00058         _enabled(true) {
00059     _type = TYPE;
00060 }
00061 
00062 void Layer::readData(Formats::XRCReadStream *stream) {
00063     _scrollScale = stream->readFloatLE();
00064     if (_scrollScale > 10.0 || _scrollScale < -1.0)
00065         _scrollScale = 0;
00066 }
00067 
00068 void Layer::printData() {
00069     debug("scrollScale: %f", _scrollScale);
00070     debug("enabled: %d", _enabled);
00071 }
00072 
00073 void Layer::setScrollPosition(const Common::Point &position) {
00074     // The location scroll position is scaled to create a parallax effect
00075     _scroll.x = (_scrollScale + 1.0) * (float) position.x;
00076     _scroll.y = (_scrollScale + 1.0) * (float) position.y;
00077 }
00078 
00079 Common::Point Layer::getScroll() const {
00080     return _scroll;
00081 }
00082 
00083 void Layer::setScroll(const Common::Point &scroll) {
00084     _scroll = scroll;
00085 }
00086 
00087 bool Layer::isEnabled() const {
00088     return _enabled;
00089 }
00090 
00091 void Layer::enable(bool enabled) {
00092     _enabled = enabled;
00093 }
00094 
00095 Gfx::LightEntryArray Layer::listLightEntries() {
00096     Common::Array<Light *> lights = listChildren<Light>();
00097 
00098     Gfx::LightEntryArray lightEntries;
00099     for (uint i = 0; i < lights.size(); i++) {
00100         lightEntries.push_back(lights[i]->getLightEntry());
00101     }
00102 
00103     return lightEntries;
00104 }
00105 
00106 void Layer::saveLoad(ResourceSerializer *serializer) {
00107     serializer->syncAsSint32LE(_enabled);
00108 }
00109 
00110 void Layer::saveLoadCurrent(ResourceSerializer *serializer) {
00111     serializer->syncAsSint32LE(_scroll.x);
00112     serializer->syncAsSint32LE(_scroll.y);
00113 }
00114 
00115 Layer2D::~Layer2D() {
00116 }
00117 
00118 Layer2D::Layer2D(Object *parent, byte subType, uint16 index, const Common::String &name) :
00119         Layer(parent, subType, index, name) {
00120 }
00121 
00122 void Layer2D::readData(Formats::XRCReadStream *stream) {
00123     Layer::readData(stream);
00124 
00125     uint32 itemsCount = stream->readUint32LE();
00126     for (uint i = 0; i < itemsCount; i++) {
00127         uint32 itemIndex = stream->readUint32LE();
00128         _itemIndices.push_back(itemIndex);
00129     }
00130 
00131     _enabled = stream->readBool();
00132 }
00133 
00134 void Layer2D::onEnterLocation() {
00135     Layer::onEnterLocation();
00136 
00137     Common::Array<Item *> items = listChildren<Item>();
00138 
00139     // Build the item list in the appropriate order
00140     _items.clear();
00141     for (uint i = 0; i < _itemIndices.size(); i++) {
00142         for (uint j = 0; j < items.size(); j++) {
00143             if (items[j]->getIndex() == _itemIndices[i]) {
00144                 _items.push_back(items[j]);
00145                 break;
00146             }
00147         }
00148     }
00149 }
00150 
00151 void Layer2D::onExitLocation() {
00152     Layer::onExitLocation();
00153 
00154     _items.clear();
00155 }
00156 
00157 Gfx::RenderEntryArray Layer2D::listRenderEntries() {
00158     Gfx::RenderEntryArray renderEntries;
00159     for (uint i = 0; i < _items.size(); i++) {
00160         Item *item = _items[i];
00161 
00162         Gfx::RenderEntry *renderEntry = item->getRenderEntry(_scroll);
00163 
00164         if (!renderEntry) {
00165             // warning("No render entry for item '%s'", item->getName().c_str());
00166             continue;
00167         }
00168 
00169         renderEntries.push_back(renderEntry);
00170     }
00171 
00172     return renderEntries;
00173 }
00174 
00175 void Layer2D::printData() {
00176     Layer::printData();
00177 }
00178 
00179 Layer3D::~Layer3D() {
00180 }
00181 
00182 Layer3D::Layer3D(Object *parent, byte subType, uint16 index, const Common::String &name) :
00183         Layer(parent, subType, index, name),
00184         _shouldRenderShadows(true),
00185         _maxShadowLength(75),
00186         _nearClipPlane(100.0),
00187         _farClipPlane(64000.0),
00188         _backgroundItem(nullptr) {
00189 }
00190 
00191 void Layer3D::readData(Formats::XRCReadStream *stream) {
00192     Layer::readData(stream);
00193 
00194     _shouldRenderShadows = stream->readBool();
00195     _nearClipPlane = stream->readFloatLE();
00196     _farClipPlane = stream->readFloatLE();
00197     if (stream->isDataLeft()) {
00198         _maxShadowLength = stream->readUint32LE();
00199     }
00200 }
00201 
00202 void Layer3D::onAllLoaded() {
00203     Layer::onAllLoaded();
00204 
00205     _items = listChildren<Item>();
00206     _backgroundItem = findChildWithSubtype<Item>(Item::kItemBackground);
00207 
00208     Camera *camera = findChild<Camera>();
00209     camera->setClipPlanes(_nearClipPlane, _farClipPlane);
00210 }
00211 
00212 void Layer3D::onEnterLocation() {
00213     Layer::onEnterLocation();
00214 
00215     StarkScene->setupShadows(_shouldRenderShadows, _maxShadowLength / 1000.0f);
00216 }
00217 
00218 Gfx::RenderEntry *Layer3D::getBackgroundRenderEntry() {
00219     if (!_backgroundItem) {
00220         return nullptr;
00221     }
00222 
00223     return _backgroundItem->getRenderEntry(_scroll);
00224 }
00225 
00226 Gfx::RenderEntryArray Layer3D::listRenderEntries() {
00227     // Sort the items by distance to the camera
00228     Gfx::RenderEntryArray itemEntries;
00229     for (uint i = 0; i < _items.size(); i++) {
00230         Item *item = _items[i];
00231 
00232         if (item->getSubType() != Item::kItemBackground) {
00233             Gfx::RenderEntry *renderEntry = item->getRenderEntry(_scroll);
00234 
00235             if (!renderEntry) {
00236                 // warning("No render entry for item '%s'", item->getName().c_str());
00237                 continue;
00238             }
00239 
00240             itemEntries.push_back(renderEntry);
00241         }
00242     }
00243     Common::sort(itemEntries.begin(), itemEntries.end(), Gfx::RenderEntry::compare);
00244 
00245     Gfx::RenderEntryArray renderEntries;
00246 
00247     // Add the background render entry to the list first
00248     Gfx::RenderEntry *backgroundRenderEntry = getBackgroundRenderEntry();
00249     if (backgroundRenderEntry) {
00250         renderEntries.push_back(backgroundRenderEntry);
00251     }
00252 
00253     // Add the other items
00254     renderEntries.push_back(itemEntries);
00255 
00256     return renderEntries;
00257 }
00258 
00259 void Layer3D::printData() {
00260     Layer::printData();
00261 
00262     debug("shouldRenderShadows: %d", _shouldRenderShadows);
00263     debug("maxShadowLength: %d", _maxShadowLength);
00264     debug("nearClipPlane: %f", _nearClipPlane);
00265     debug("farClipPlane: %f", _farClipPlane);
00266 }
00267 
00268 } // End of namespace Resources
00269 } // End of namespace Stark


Generated on Sat Jul 4 2020 05:01:17 for ResidualVM by doxygen 1.7.1
curved edge   curved edge