-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapController.cpp
More file actions
173 lines (116 loc) · 3.6 KB
/
MapController.cpp
File metadata and controls
173 lines (116 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* MapController.cpp
Robert Kirk DeLisle
25 August 2006
Purpose: Serves as an interface between MapModel, MapView and other classes
Modification History:
*/
#include "MapController.h"
MapController::MapController()
{
/* Purpose: Consturct the Map MVC triad
Parameters: hInst - handle to the application instance, used for some bitmap operations
Return: none
Exceptions:
*/
//set up the MVC connections
m_View.Connect(&m_Model);
return;
}
void MapController::LoadMap(const string MapFile)
{
/* Purpose: Specifies a map file to be loaded into the model
Parameters: MapFile - Location of a CSV file containing a map.
Return: none
Exceptions: caught and thrown from m_Model->LoadMap()
*/
//tell the model to load a map file
try
{
m_Model.LoadMap(MapFile);
}
catch(ExceptionRKD &ex)
{
throw ex;
}
return;
}
long MapController::GetPixelPosition(const long X, const long Y) const
{
/* Purpose: get the value of the tile at pixel postion (X,Y)
requires conversion between pixels and tiles
Parameters: X,Y - Pixel positions to check
Return: none
Exceptions:
*/
//validate pixels
// if ( (X<0 || Y<0) || ( X>=m_Model.Width() || Y <=m_Model.Height() ) )
//out of bounds
long tileX, tileY;
tileX = (X/m_View.GetTileWidth());
tileY = (Y/m_View.GetTileHeight());
return m_Model.GetPosition(tileX, tileY);
}
bool MapController::CheckMapByPixels(const long MinX, const long MaxX, const long MinY, const long MaxY, long &ret) const
{
/* Purpose: check the content of the map within the given rectangle.
Parameters: MinX, MaxX, MinY, MaxY - defines the rectangle to check in pixels
Return: true if the rectangle contains only 0s
false if the rectangle contains only 1s
Exceptions:
*/
long x, y; //counter indices
bool bSuccess = true; //optimistic outlook
ret = 0;
//convert pixels to tiles
long MinTileX = MinX/m_View.GetTileWidth();
long MaxTileX = MaxX/m_View.GetTileWidth();
long MinTileY = MinY/m_View.GetTileHeight();
long MaxTileY = MaxY/m_View.GetTileHeight();
if (MaxTileX >= m_Model.Width())
MaxTileX = m_Model.Width()-1;
if (MaxTileY >= m_Model.Height())
MaxTileY = m_Model.Height()-1;
for (x=MinTileX; x<=MaxTileX; x++)
{
for (y=MinTileY; y<=MaxTileY; y++)
{
if ( m_Model.GetPosition(x,y) != 0 )
{
//found something - set the flag
bSuccess = false;
ret = m_Model.GetPosition(x,y);
}
}
}
return bSuccess;
}
long MapController::ModifyTileByPixel(const long MinX, const long MaxX, const long MinY, const long MaxY)
{
/* Purpose: Change the value of the tile(s) occuring within X,Y pixels
Parameters: X,Y - pixel
Return: none
Exceptions:
*/
long x, y; //counter indices
long mods=0;
//convert pixels to tiles
long MinTileX = MinX/m_View.GetTileWidth();
long MaxTileX = MaxX/m_View.GetTileWidth();
long MinTileY = MinY/m_View.GetTileHeight();
long MaxTileY = MaxY/m_View.GetTileHeight();
if (MaxTileX >= m_Model.Width())
MaxTileX = m_Model.Width()-1;
if (MaxTileY >= m_Model.Height())
MaxTileY = m_Model.Height()-1;
for (x=MinTileX; x<=MaxTileX; x++)
{
for (y=MinTileY; y<=MaxTileY; y++)
{
if ( m_Model.CheckPosition(x,y) )
mods++;
m_Model.ModifyTile(x,y,0);
m_View.EraseTile(x,y);
}
}
return mods;
}