9
9
using namespace std ;
10
10
using namespace sf ;
11
11
12
+ template <typename T>
12
13
class Grid
13
14
{
14
15
private:
15
- GridValue **gridArr; // the grid
16
+ T * **gridArr; // the grid
16
17
int gridWidth; // the width of the grid
17
18
int gridHeight; // the height of the grid
18
19
int cellSize; // in pixels
19
- int outlineThickness; // in pixels
20
-
21
- Vector2i *startPos;
22
- Vector2i *destPos;
23
20
24
21
public:
25
22
Grid (int width, int height, int cellSize);
26
23
27
- Grid (int width, int height, int cellSize, int outlineThickness);
28
-
29
- GridValue getValueAt (int x, int y);
24
+ T *getValueAt (int x, int y);
30
25
31
26
Vector2f gridToScreen (int x, int y);
32
27
33
28
Vector2i screenToGrid (Vector2i pos);
34
29
35
30
Vector2f centerScreenCoord (Vector2i pos);
36
31
37
- void drawGrid (RenderWindow *window );
32
+ bool setValAt ( int x, int y, T *val );
38
33
39
- bool setValAt (int x, int y, GridValue val);
40
-
41
- bool setValAt (Vector2i pos, GridValue val);
34
+ bool setValAt (Vector2i pos, T *val);
42
35
43
36
int getCellSize ();
44
-
45
- ~Grid ();
46
37
47
- private:
38
+ int getGridWidth ();
39
+
40
+ int getGridHeight ();
41
+
48
42
bool validCoords (int x, int y);
43
+
44
+ ~Grid ();
49
45
};
50
46
47
+ // / <summary>
48
+ // / Get the grid's height
49
+ // / </summary>
50
+ // / <returns>the grid's height</returns>
51
+ template <typename T>
52
+ int Grid<T>::getGridHeight()
53
+ {
54
+ return gridHeight;
55
+ }
56
+
57
+ // / <summary>
58
+ // / Get the grid's width
59
+ // / </summary>
60
+ // / <returns>the grid's width</returns>
61
+ template <typename T>
62
+ int Grid<T>::getGridWidth()
63
+ {
64
+ return gridWidth;
65
+ }
66
+
51
67
// / <summary>
52
68
// / Get the cell size
53
69
// / </summary>
54
70
// / <returns>the cell size in pixels</returns>
55
- int Grid::getCellSize ()
71
+ template <typename T>
72
+ int Grid<T>::getCellSize()
56
73
{
57
74
return cellSize;
58
75
}
@@ -64,7 +81,8 @@ int Grid::getCellSize()
64
81
// / <param name="pos">A position on the screen</param>
65
82
// / <returns>the center screen position of the cell closest to the givest
66
83
// / screen position</returns>
67
- Vector2f Grid::centerScreenCoord (Vector2i pos)
84
+ template <typename T>
85
+ Vector2f Grid<T>::centerScreenCoord(Vector2i pos)
68
86
{
69
87
int posX = (int )floor (pos.x / cellSize) * cellSize;
70
88
int posY = (int )floor (pos.y / cellSize) * cellSize;
@@ -77,66 +95,39 @@ Vector2f Grid::centerScreenCoord(Vector2i pos)
77
95
// / <param name="width">the width of the grid</param>
78
96
// / <param name="height">the height of the grid</param>
79
97
// / <param name="cellSize">the size of each grid square in pixels</param>
80
- Grid::Grid (int width, int height, int cellSize)
98
+ template <typename T>
99
+ Grid<T>::Grid(int width, int height, int cellSize)
81
100
{
82
101
this ->cellSize = cellSize;
83
102
// initialize grid
84
- gridArr = new GridValue *[width];
85
- for (int i = 0 ; i < width; i ++)
103
+ gridArr = new T * *[width];
104
+ for (int x = 0 ; x < width; x ++)
86
105
{
87
- gridArr[i ] = new GridValue [height];
106
+ gridArr[x ] = new T * [height];
88
107
}
89
108
90
109
// initialize instance variables
91
110
gridWidth = width;
92
111
gridHeight = height;
93
112
this ->cellSize = cellSize;
94
- outlineThickness = 1 ;
95
-
96
- startPos = NULL ;
97
- destPos = NULL ;
98
- }
99
-
100
- // / <summary>
101
- // / Create an instance of a grid
102
- // / </summary>
103
- // / <param name="width">the width of the grid</param>
104
- // / <param name="height">the height of the grid</param>
105
- // / <param name="cellSize">the size of each grid square in pixels</param>
106
- Grid::Grid (int width, int height, int cellSize, int outlineThickness)
107
- {
108
- this ->cellSize = cellSize;
109
- // initialize grid
110
- gridArr = new GridValue* [width];
111
- for (int i = 0 ; i < width; i++)
112
- {
113
- gridArr[i] = new GridValue[height];
114
- }
115
-
116
- // initialize instance variables
117
- gridWidth = width;
118
- gridHeight = height;
119
- this ->cellSize = cellSize;
120
- this ->outlineThickness = outlineThickness;
121
-
122
- startPos = NULL ;
123
- destPos = NULL ;
124
113
}
125
114
126
115
// / <summary>
127
116
// / Get the value at the given grid coordinates
128
117
// / </summary>
129
118
// / <param name="x">the row cooridnate</param>
130
119
// / <param name="y">the column coordinate</param>
131
- // / <returns>the value at the given grid coordinates</returns>
132
- GridValue Grid::getValueAt (int x, int y)
120
+ // / <returns>if the coordinates are valid, returns the value at the given grid
121
+ // / coordinates, and returns false otherwise</returns>
122
+ template <typename T>
123
+ T *Grid<T>::getValueAt(int x, int y)
133
124
{
134
125
if (x < gridWidth && y < gridHeight)
135
126
{
136
127
return gridArr[x][y];
137
128
}
138
129
139
- return GridValue::INVALID ;
130
+ return NULL ;
140
131
}
141
132
142
133
// / <summary>
@@ -146,15 +137,23 @@ GridValue Grid::getValueAt(int x, int y)
146
137
// / <param name="y">the y coordinate of the dedired grid space</param>
147
138
// / <returns>a screen position in pixels as a Vector2f of the given cell
148
139
// / in the grid if the given coordinates are valid, otherwise returns NULL</returns>
149
- Vector2f Grid::gridToScreen (int x, int y)
140
+ template <typename T>
141
+ Vector2f Grid<T>::gridToScreen(int x, int y)
150
142
{
151
143
float screenX = x * cellSize;
152
144
float screenY = y * cellSize;
153
145
154
146
return Vector2f (screenX, screenY);
155
147
}
156
148
157
- Vector2i Grid::screenToGrid (Vector2i pos)
149
+
150
+ // / <summary>
151
+ // / Convert the given screen position to a grid position
152
+ // / </summary>
153
+ // / <param name="pos">The screen position to be converted</param>
154
+ // / <returns>The screen position as a grid position</returns>
155
+ template <typename T>
156
+ Vector2i Grid<T>::screenToGrid(Vector2i pos)
158
157
{
159
158
int xPos = (int )floor (pos.x / cellSize);
160
159
int yPos = (int )floor (pos.y / cellSize);
@@ -167,99 +166,24 @@ Vector2i Grid::screenToGrid(Vector2i pos)
167
166
// / <param name="x">the x coordinate of the desired cell</param>
168
167
// / <param name="y"the y coordinate of the desired cell></param>
169
168
// / <returns>true if the cell is valid and false otherwise</returns>
170
- bool Grid::validCoords (int x, int y)
169
+ template <typename T>
170
+ bool Grid<T>::validCoords(int x, int y)
171
171
{
172
172
return (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight);
173
173
}
174
174
175
- // / <summary>
176
- // / Draws this grid in the given window
177
- // / </summary>
178
- // / <param name="window">window to draw into</param>
179
- void Grid::drawGrid (RenderWindow *window)
180
- {
181
- for (int x = 0 ; x < gridWidth; x++)
182
- {
183
- for (int y = 0 ; y < gridHeight; y++)
184
- {
185
- RectangleShape cell = RectangleShape (Vector2f (cellSize, cellSize));
186
- Vector2f pos = gridToScreen (x, y);
187
- cell.setPosition (pos);
188
- cell.setOutlineThickness (outlineThickness);
189
-
190
- GridStateColor color = valToColor (gridArr[x][y]);
191
- cell.setFillColor (Color ((unsigned long )color));
192
- cell.setOutlineColor (Color::White);
193
-
194
- window->draw (cell);
195
- }
196
- }
197
- }
198
-
199
175
// / <summary>
200
176
// / Set the value of the cell at the given coordinates
201
177
// / </summary>
202
178
// / <param name="x">the x coordinate of the cell</param>
203
179
// / <param name="y">the y coordinate of the cell</param>
204
180
// / <param name="val">the new value of the given cell</param>
205
181
// / <returns>true if the cell is valid and false otherwise</returns>
206
- bool Grid::setValAt (int x, int y, GridValue val)
182
+ template <typename T>
183
+ bool Grid<T>::setValAt(int x, int y, T *val)
207
184
{
208
185
if (validCoords (x, y))
209
186
{
210
- // only allow one start and dest cell
211
- if (val == GridValue::START)
212
- {
213
- // set old start to unoccupied
214
- if (startPos == NULL )
215
- {
216
- startPos = new Vector2i (x, y);
217
- }
218
- else
219
- {
220
- // unoccupy old start pos
221
- gridArr[startPos->x ][startPos->y ] = GridValue::UNOCCUPIED;
222
-
223
- // set new start pos
224
- startPos->x = x;
225
- startPos->y = y;
226
- }
227
- }
228
- else if (val == GridValue::DESTINATION)
229
- {
230
- // set old dest to unoccupied
231
- if (destPos == NULL )
232
- {
233
- destPos = new Vector2i (x, y);
234
- }
235
- else
236
- {
237
- // unoccupy old dest pos
238
- gridArr[destPos->x ][destPos->y ] = GridValue::UNOCCUPIED;
239
-
240
- // set new dest pos
241
- destPos->x = x;
242
- destPos->y = y;
243
- }
244
- }
245
- else
246
- {
247
- // make sure that dest/start are removed if the cell is overwritten
248
- GridValue currVal = getValueAt (x, y);
249
- if (startPos != NULL && currVal == GridValue::START)
250
- {
251
- // remove start pos
252
- delete (startPos);
253
- startPos = NULL ;
254
- }
255
- else if (destPos != NULL && currVal == GridValue::DESTINATION)
256
- {
257
- // remove destination pos
258
- delete (destPos);
259
- destPos = NULL ;
260
- }
261
- }
262
-
263
187
// set the value at the cell
264
188
gridArr[x][y] = val;
265
189
@@ -276,7 +200,8 @@ bool Grid::setValAt(int x, int y, GridValue val)
276
200
// / <param name="pos">the screen position of the cell</param>
277
201
// / <param name="val">the new value of the cell</param>
278
202
// / <returns>true if the cell is valid and false otherwise</returns>
279
- bool Grid::setValAt (Vector2i pos, GridValue val)
203
+ template <typename T>
204
+ bool Grid<T>::setValAt(Vector2i pos, T *val)
280
205
{
281
206
Vector2i gridCoords (screenToGrid (pos));
282
207
return setValAt (gridCoords.x , gridCoords.y , val);
@@ -285,28 +210,16 @@ bool Grid::setValAt(Vector2i pos, GridValue val)
285
210
// / <summary>
286
211
// / Destructor for a grid object
287
212
// / </summary>
288
- Grid::~Grid ()
213
+ template <typename T>
214
+ Grid<T>::~Grid ()
289
215
{
290
- cout << " startPos == null: " << (startPos == NULL ) << endl;
291
- cout << " destPos == null: " << (destPos == NULL ) << endl;
292
-
293
216
// free the grid array
294
- for (int i = 0 ; i < gridWidth; i ++)
217
+ for (int x = 0 ; x < gridWidth; x ++)
295
218
{
296
- delete gridArr[i ];
219
+ delete gridArr[x ];
297
220
}
298
221
299
222
delete gridArr;
300
-
301
- // free the start and dest positions
302
- if (startPos != NULL )
303
- {
304
- delete (startPos);
305
- }
306
- if (destPos != NULL )
307
- {
308
- delete (destPos);
309
- }
310
223
}
311
224
312
225
#endif
0 commit comments