1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "sludge/allfiles.h"
24 #include "sludge/backdrop.h"
25 #include "sludge/event.h"
26 #include "sludge/graphics.h"
27 #include "sludge/moreio.h"
28 #include "sludge/newfatal.h"
29 #include "sludge/objtypes.h"
30 #include "sludge/region.h"
31 #include "sludge/sludge.h"
32 #include "sludge/sludger.h"
33
34 namespace Sludge {
35
RegionManager(SludgeEngine * vm)36 RegionManager::RegionManager(SludgeEngine *vm)
37 {
38 _vm = vm;
39 _allScreenRegions = new ScreenRegionList;
40 _allScreenRegions->clear();
41 _lastRegion = nullptr;
42 _overRegion = nullptr;
43 }
44
~RegionManager()45 RegionManager::~RegionManager() {
46 kill();
47
48 delete _allScreenRegions;
49 _allScreenRegions = nullptr;
50 }
51
showBoxes()52 void RegionManager::showBoxes() {
53 for (ScreenRegionList::iterator it = _allScreenRegions->begin(); it != _allScreenRegions->end(); ++it) {
54 g_sludge->_gfxMan->drawVerticalLine((*it)->x1, (*it)->y1, (*it)->y2);
55 g_sludge->_gfxMan->drawVerticalLine((*it)->x2, (*it)->y1, (*it)->y2);
56 g_sludge->_gfxMan->drawHorizontalLine((*it)->x1, (*it)->y1, (*it)->x2);
57 g_sludge->_gfxMan->drawHorizontalLine((*it)->x1, (*it)->y2, (*it)->x2);
58 }
59 }
60
removeScreenRegion(int objectNum)61 void RegionManager::removeScreenRegion(int objectNum) {
62 for (ScreenRegionList::iterator it = _allScreenRegions->begin(); it != _allScreenRegions->end(); ++it) {
63 if ((*it)->thisType->objectNum == objectNum) {
64 ScreenRegion *killMe = *it;
65 g_sludge->_objMan->removeObjectType(killMe->thisType);
66 if (killMe == _overRegion)
67 _overRegion = nullptr;
68 delete killMe;
69 killMe = nullptr;
70 it = _allScreenRegions->reverse_erase(it);
71 }
72 }
73 }
74
saveRegions(Common::WriteStream * stream)75 void RegionManager::saveRegions(Common::WriteStream *stream) {
76 uint numRegions = _allScreenRegions->size();
77 stream->writeUint16BE(numRegions);
78 for (ScreenRegionList::iterator it = _allScreenRegions->begin(); it != _allScreenRegions->end(); ++it) {
79 stream->writeUint16BE((*it)->x1);
80 stream->writeUint16BE((*it)->y1);
81 stream->writeUint16BE((*it)->x2);
82 stream->writeUint16BE((*it)->y2);
83 stream->writeUint16BE((*it)->sX);
84 stream->writeUint16BE((*it)->sY);
85 stream->writeUint16BE((*it)->di);
86 g_sludge->_objMan->saveObjectRef((*it)->thisType, stream);
87 }
88 }
89
loadRegions(Common::SeekableReadStream * stream)90 void RegionManager::loadRegions(Common::SeekableReadStream *stream) {
91 int numRegions = stream->readUint16BE();
92 while (numRegions--) {
93 ScreenRegion *newRegion = new ScreenRegion;
94 _allScreenRegions->push_back(newRegion);
95 newRegion->x1 = stream->readUint16BE();
96 newRegion->y1 = stream->readUint16BE();
97 newRegion->x2 = stream->readUint16BE();
98 newRegion->y2 = stream->readUint16BE();
99 newRegion->sX = stream->readUint16BE();
100 newRegion->sY = stream->readUint16BE();
101 newRegion->di = stream->readUint16BE();
102 newRegion->thisType = g_sludge->_objMan->loadObjectRef(stream);
103 }
104 }
105
kill()106 void RegionManager::kill() {
107 for (ScreenRegionList::iterator it = _allScreenRegions->begin(); it != _allScreenRegions->end(); ++it) {
108 ScreenRegion *killRegion = (*it);
109 g_sludge->_objMan->removeObjectType(killRegion->thisType);
110 delete killRegion;
111 }
112 _allScreenRegions->clear();
113 _overRegion = nullptr;
114 _lastRegion = nullptr;
115 }
116
addScreenRegion(int x1,int y1,int x2,int y2,int sX,int sY,int di,int objectNum)117 bool RegionManager::addScreenRegion(int x1, int y1, int x2, int y2, int sX, int sY, int di,
118 int objectNum) {
119 ScreenRegion *newRegion = new ScreenRegion;
120 if (!checkNew(newRegion))
121 return false;
122 newRegion->di = di;
123 newRegion->x1 = x1;
124 newRegion->y1 = y1;
125 newRegion->x2 = x2;
126 newRegion->y2 = y2;
127 newRegion->sX = sX;
128 newRegion->sY = sY;
129 newRegion->thisType = g_sludge->_objMan->loadObjectType(objectNum);
130 _allScreenRegions->push_front(newRegion);
131 return (bool) (newRegion->thisType != nullptr);
132 }
133
updateOverRegion()134 void RegionManager::updateOverRegion() {
135 int cameraX = g_sludge->_gfxMan->getCamX();
136 int cameraY = g_sludge->_gfxMan->getCamY();
137 for (ScreenRegionList::iterator it = _allScreenRegions->begin(); it != _allScreenRegions->end(); ++it) {
138 if ((g_sludge->_evtMan->mouseX() >= (*it)->x1 - cameraX)
139 && (g_sludge->_evtMan->mouseY() >= (*it)->y1 - cameraY)
140 && (g_sludge->_evtMan->mouseX() <= (*it)->x2 - cameraX)
141 && (g_sludge->_evtMan->mouseY() <= (*it)->y2 - cameraY)) {
142 _overRegion = (*it);
143 return;
144 }
145 }
146 _overRegion = nullptr;
147 return;
148 }
149
getRegionForObject(int obj)150 ScreenRegion *RegionManager::getRegionForObject(int obj) {
151 for (ScreenRegionList::iterator it = _allScreenRegions->begin(); it != _allScreenRegions->end(); ++it) {
152 if (obj == (*it)->thisType->objectNum) {
153 return (*it);
154 }
155 }
156
157 return nullptr;
158 }
159
freeze(FrozenStuffStruct * frozenStuff)160 void RegionManager::freeze(FrozenStuffStruct *frozenStuff) {
161 frozenStuff->allScreenRegions = _allScreenRegions;
162 _allScreenRegions = new ScreenRegionList;
163 _overRegion = nullptr;
164 }
165
resotre(FrozenStuffStruct * frozenStuff)166 void RegionManager::resotre(FrozenStuffStruct *frozenStuff) {
167 // kill
168 kill();
169 delete _allScreenRegions;
170 _allScreenRegions = nullptr;
171
172 // restore
173 _allScreenRegions = frozenStuff->allScreenRegions;
174 _overRegion = nullptr;
175 }
176
177 } // End of namespace Sludge
178