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 "neverhood/microtiles.h"
24
25 namespace Neverhood {
26
MicroTileArray(int16 width,int16 height)27 MicroTileArray::MicroTileArray(int16 width, int16 height) {
28 _tilesW = (width / TileSize) + ((width % TileSize) > 0 ? 1 : 0);
29 _tilesH = (height / TileSize) + ((height % TileSize) > 0 ? 1 : 0);
30 _tiles = new BoundingBox[_tilesW * _tilesH];
31 clear();
32 }
33
~MicroTileArray()34 MicroTileArray::~MicroTileArray() {
35 delete[] _tiles;
36 }
37
addRect(Common::Rect r)38 void MicroTileArray::addRect(Common::Rect r) {
39
40 int ux0, uy0, ux1, uy1;
41 int tx0, ty0, tx1, ty1;
42 int ix0, iy0, ix1, iy1;
43
44 r.clip(Common::Rect(0, 0, 639, 479));
45
46 ux0 = r.left / TileSize;
47 uy0 = r.top / TileSize;
48 ux1 = r.right / TileSize;
49 uy1 = r.bottom / TileSize;
50
51 tx0 = r.left % TileSize;
52 ty0 = r.top % TileSize;
53 tx1 = r.right % TileSize;
54 ty1 = r.bottom % TileSize;
55
56 for (int yc = uy0; yc <= uy1; yc++) {
57 for (int xc = ux0; xc <= ux1; xc++) {
58 ix0 = (xc == ux0) ? tx0 : 0;
59 ix1 = (xc == ux1) ? tx1 : TileSize - 1;
60 iy0 = (yc == uy0) ? ty0 : 0;
61 iy1 = (yc == uy1) ? ty1 : TileSize - 1;
62 updateBoundingBox(_tiles[xc + yc * _tilesW], ix0, iy0, ix1, iy1);
63 }
64 }
65
66 }
67
clear()68 void MicroTileArray::clear() {
69 memset(_tiles, 0, _tilesW * _tilesH * sizeof(BoundingBox));
70 }
71
TileX0(const BoundingBox & boundingBox)72 byte MicroTileArray::TileX0(const BoundingBox &boundingBox) {
73 return (boundingBox >> 24) & 0xFF;
74 }
75
TileY0(const BoundingBox & boundingBox)76 byte MicroTileArray::TileY0(const BoundingBox &boundingBox) {
77 return (boundingBox >> 16) & 0xFF;
78 }
79
TileX1(const BoundingBox & boundingBox)80 byte MicroTileArray::TileX1(const BoundingBox &boundingBox) {
81 return (boundingBox >> 8) & 0xFF;
82 }
83
TileY1(const BoundingBox & boundingBox)84 byte MicroTileArray::TileY1(const BoundingBox &boundingBox) {
85 return boundingBox & 0xFF;
86 }
87
isBoundingBoxEmpty(const BoundingBox & boundingBox)88 bool MicroTileArray::isBoundingBoxEmpty(const BoundingBox &boundingBox) {
89 return boundingBox == EmptyBoundingBox;
90 }
91
isBoundingBoxFull(const BoundingBox & boundingBox)92 bool MicroTileArray::isBoundingBoxFull(const BoundingBox &boundingBox) {
93 return boundingBox == FullBoundingBox;
94 }
95
setBoundingBox(BoundingBox & boundingBox,byte x0,byte y0,byte x1,byte y1)96 void MicroTileArray::setBoundingBox(BoundingBox &boundingBox, byte x0, byte y0, byte x1, byte y1) {
97 boundingBox = (x0 << 24) | (y0 << 16) | (x1 << 8) | y1;
98 }
99
updateBoundingBox(BoundingBox & boundingBox,byte x0,byte y0,byte x1,byte y1)100 void MicroTileArray::updateBoundingBox(BoundingBox &boundingBox, byte x0, byte y0, byte x1, byte y1) {
101 if (!isBoundingBoxEmpty(boundingBox)) {
102 x0 = MIN(TileX0(boundingBox), x0);
103 y0 = MIN(TileY0(boundingBox), y0);
104 x1 = MAX(TileX1(boundingBox), x1);
105 y1 = MAX(TileY1(boundingBox), y1);
106 }
107 setBoundingBox(boundingBox, x0, y0, x1, y1);
108 }
109
getRectangles()110 RectangleList *MicroTileArray::getRectangles() {
111
112 RectangleList *rects = new RectangleList();
113
114 int x, y;
115 int x0, y0, x1, y1;
116 int i = 0;
117
118 for (y = 0; y < _tilesH; ++y) {
119 for (x = 0; x < _tilesW; ++x) {
120 int finish = 0;
121 BoundingBox boundingBox = _tiles[i];
122
123 if (isBoundingBoxEmpty(boundingBox)) {
124 ++i;
125 continue;
126 }
127
128 x0 = (x * TileSize) + TileX0(boundingBox);
129 y0 = (y * TileSize) + TileY0(boundingBox);
130 y1 = (y * TileSize) + TileY1(boundingBox);
131
132 if (TileX1(boundingBox) == TileSize - 1 && x != _tilesW - 1) { // check if the tile continues
133 while (!finish) {
134 ++x;
135 ++i;
136 if (x == _tilesW || i >= _tilesW * _tilesH ||
137 TileY0(_tiles[i]) != TileY0(boundingBox) ||
138 TileY1(_tiles[i]) != TileY1(boundingBox) ||
139 TileX0(_tiles[i]) != 0)
140 {
141 --x;
142 --i;
143 finish = 1;
144 }
145 }
146 }
147
148 x1 = (x * TileSize) + TileX1(_tiles[i]);
149
150 rects->push_back(Common::Rect(x0, y0, x1 + 1, y1 + 1));
151
152 ++i;
153 }
154 }
155
156 return rects;
157 }
158
159 } // End of namespace Neverhood
160