1 |
/** \file IsoMouseMap.cpp |
/** \file IsoMouseMap.cpp |
2 |
* Defines the IsoMouseMap class. |
* Defines the IsoMouseMap class. |
3 |
* |
* |
|
* Revision history: |
|
|
* v1.1 (June 6) - Optimized the mousemap for square maps: |
|
|
* Removed the load function. |
|
|
* Removed the look-up table. |
|
|
* Removed the destroy method, since there is nothing to clen up now. |
|
|
* Simplified the mapMouse function. |
|
|
* |
|
4 |
* @see IsoMouseMap |
* @see IsoMouseMap |
5 |
* |
* |
6 |
* @author <A href="mailto:vovansim@hotmail.com">Vovansim (aka Scorpion)</A> |
* @author <A href="mailto:vovansim@hotmail.com">Vovansim (aka Scorpion)</A> |
7 |
* @version 1.1 |
* @version 1.0 |
8 |
* @date June 6, 2003 |
* @date May 26, 2003 |
9 |
*/ |
*/ |
10 |
|
|
11 |
#include "ImageCanvas.h" |
#include "ImageCanvas.h" |
20 |
setTileWalker(NULL); |
setTileWalker(NULL); |
21 |
|
|
22 |
//Initialize mousemap to zero values |
//Initialize mousemap to zero values |
23 |
|
lookupTable = NULL; |
24 |
width = 0; |
width = 0; |
25 |
height = 0; |
height = 0; |
26 |
|
|
33 |
} |
} |
34 |
|
|
35 |
IsoMouseMap::~IsoMouseMap() { |
IsoMouseMap::~IsoMouseMap() { |
36 |
//Nothing to do... |
//Clean up: |
37 |
|
destroy(); |
38 |
} |
} |
39 |
|
|
40 |
//////////////////////// End Constructors / Destructors /////////////////////// |
//////////////////////// End Constructors / Destructors /////////////////////// |
43 |
|
|
44 |
////////////////////////// Initialization / Clean-up ////////////////////////// |
////////////////////////// Initialization / Clean-up ////////////////////////// |
45 |
|
|
46 |
|
/** Allows to load a mousemap template image for use with isometric and hexagonal maps. */ |
47 |
|
void IsoMouseMap::load(char* filename) { |
48 |
|
//Load the image into a canvas: |
49 |
|
ImageCanvas mapImage; |
50 |
|
mapImage.loadImage(filename); |
51 |
|
|
52 |
|
//Create the mousemap: |
53 |
|
create(mapImage.getWidth(), mapImage.getHeight()); |
54 |
|
|
55 |
|
//Grab the colors of the corners: |
56 |
|
Uint32 colorNE = getPixel(mapImage, width - 1, 0); |
57 |
|
Uint32 colorNW = getPixel(mapImage, 0, 0); |
58 |
|
Uint32 colorSE = getPixel(mapImage, width - 1, height - 1); |
59 |
|
Uint32 colorSW = getPixel(mapImage, 0, height - 1); |
60 |
|
|
61 |
|
//Temporary test point: |
62 |
|
Uint32 testColor; |
63 |
|
|
64 |
|
//Fill in the look-up table: |
65 |
|
for(int r=0; r<height; r++) { |
66 |
|
for(int c=0; c<width; c++) { |
67 |
|
//Grab the current pixel: |
68 |
|
testColor = getPixel(mapImage, c, r); |
69 |
|
|
70 |
|
//check against corner points |
71 |
|
if(testColor == colorNE) { |
72 |
|
lookupTable[c + r * width] = MM_NE; |
73 |
|
} else if(testColor == colorNW) { |
74 |
|
lookupTable[c + r * width] = MM_SE; |
75 |
|
} else if(testColor == colorSE) { |
76 |
|
lookupTable[c + r * width] = MM_NW; |
77 |
|
} else if(testColor == colorSW) { |
78 |
|
lookupTable[c + r * width] = MM_SW; |
79 |
|
} |
80 |
|
} |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
/** Use with rectangular maps. Takes the width and the height of the tile as a parameter. */ |
/** Use with rectangular maps. Takes the width and the height of the tile as a parameter. */ |
85 |
void IsoMouseMap::create(int width, int height) { |
void IsoMouseMap::create(int width, int height) { |
86 |
|
//Clean up if we have something already: |
87 |
|
destroy(); |
88 |
|
|
89 |
//Store the size of the tile: |
//Store the size of the tile: |
90 |
this->width = width; |
this->width = width; |
91 |
this->height = height; |
this->height = height; |
92 |
|
|
93 |
|
//Create the look-up table: |
94 |
|
lookupTable = new MOUSEMAP_DIRECTION[width * height]; |
95 |
|
|
96 |
|
//Initialize the look-up table to the default values: |
97 |
|
for(int i=0; i<width*height; i++) { |
98 |
|
lookupTable[i] = MM_CENTER; |
99 |
|
} |
100 |
|
} |
101 |
|
/** Clean up. */ |
102 |
|
void IsoMouseMap::destroy() { |
103 |
|
if(lookupTable) { |
104 |
|
//We have stuff that needs to be cleaned up: |
105 |
|
delete [] lookupTable; |
106 |
|
width = 0; |
107 |
|
height = 0; |
108 |
|
} |
109 |
} |
} |
110 |
|
|
111 |
//////////////////////// End Initialization / Clean-up //////////////////////// |
//////////////////////// End Initialization / Clean-up //////////////////////// |
114 |
|
|
115 |
/** Returns the coordinates of the tile that was clicked if the coordinates of the mouse are passed in. */ |
/** Returns the coordinates of the tile that was clicked if the coordinates of the mouse are passed in. */ |
116 |
POINT IsoMouseMap::mapMouse(POINT coords) { |
POINT IsoMouseMap::mapMouse(POINT coords) { |
117 |
|
//Check that the coordinates passed in are in the screen space: |
118 |
|
if(!PointInRect(scroller->getScreenSpace(), &coords)) { |
119 |
|
//Mouse out of range: |
120 |
|
POINT result; |
121 |
|
result.x = -1; |
122 |
|
result.y = -1; |
123 |
|
|
124 |
|
return result; |
125 |
|
} |
126 |
|
|
127 |
//Convert the viewspace coordinates to world coordinates |
//Convert the viewspace coordinates to world coordinates |
128 |
//If we have no scroller, then they are the same supposedly |
//If we have no scroller, then they are the same supposedly |
129 |
if(scroller != NULL ) { |
if(scroller != NULL ) { |
135 |
coords.y -= reference.y; |
coords.y -= reference.y; |
136 |
|
|
137 |
//Temporary storage points: |
//Temporary storage points: |
138 |
POINT coordinates; |
POINT coarseCoordinates; |
139 |
|
POINT fineCoordinates; |
140 |
|
|
141 |
//Calculate the coarse coordinates: |
//Calculate the coarse coordinates: |
142 |
coordinates.x = coords.x / getWidth(); |
coarseCoordinates.x = coords.x / getWidth(); |
143 |
coordinates.y = coords.y / getHeight(); |
coarseCoordinates.y = coords.y / getHeight(); |
144 |
|
|
145 |
|
//Calculate the fine coordinates: |
146 |
|
fineCoordinates.x = coords.x % getWidth(); |
147 |
|
fineCoordinates.y = coords.y % getHeight(); |
148 |
|
|
149 |
|
//Check for negative values of fine coords: |
150 |
|
if(fineCoordinates.x < 0) { |
151 |
|
fineCoordinates.x += width; |
152 |
|
coarseCoordinates.x -= 1; |
153 |
|
} |
154 |
|
if(fineCoordinates.y < 0) { |
155 |
|
fineCoordinates.y += height; |
156 |
|
coarseCoordinates.y -= 1; |
157 |
|
} |
158 |
|
|
159 |
|
//Temporary storage point: |
160 |
|
POINT mapPoint; |
161 |
|
mapPoint.x = 0; |
162 |
|
mapPoint.y = 0; |
163 |
|
|
164 |
|
//If there is no tile-walker, map coarse coordinates directly: |
165 |
|
if(tileWalker == NULL) { |
166 |
|
mapPoint = coarseCoordinates; |
167 |
|
} else { |
168 |
|
//Can tilewalk to find the actual coordinates: |
169 |
|
//North: |
170 |
|
while(coarseCoordinates.y < 0) { |
171 |
|
mapPoint = tileWalker->walkTile(mapPoint, IDIR_NORTH); |
172 |
|
coarseCoordinates.y ++; |
173 |
|
} |
174 |
|
//East: |
175 |
|
while(coarseCoordinates.x > 0) |
176 |
|
{ |
177 |
|
mapPoint = tileWalker->walkTile(mapPoint, IDIR_EAST); |
178 |
|
coarseCoordinates.x --; |
179 |
|
} |
180 |
|
//South: |
181 |
|
while(coarseCoordinates.y > 0) |
182 |
|
{ |
183 |
|
mapPoint = tileWalker->walkTile(mapPoint, IDIR_SOUTH); |
184 |
|
coarseCoordinates.y --; |
185 |
|
} |
186 |
|
//West: |
187 |
|
while(coarseCoordinates.x < 0) |
188 |
|
{ |
189 |
|
mapPoint = tileWalker->walkTile(mapPoint, IDIR_WEST); |
190 |
|
coarseCoordinates.x ++; |
191 |
|
} |
192 |
|
|
193 |
|
//Fine tilewalk |
194 |
|
switch(lookupTable[fineCoordinates.x + fineCoordinates.y * getWidth()]) { |
195 |
|
case MM_NW: |
196 |
|
mapPoint = tileWalker->walkTile(mapPoint, IDIR_SOUTHEAST); |
197 |
|
break; |
198 |
|
|
199 |
|
case MM_NE: |
200 |
|
mapPoint = tileWalker->walkTile(mapPoint, IDIR_NORTHEAST); |
201 |
|
break; |
202 |
|
|
203 |
|
case MM_SW: |
204 |
|
mapPoint = tileWalker->walkTile(mapPoint, IDIR_SOUTHWEST); |
205 |
|
break; |
206 |
|
|
207 |
|
case MM_SE: |
208 |
|
mapPoint = tileWalker->walkTile(mapPoint, IDIR_NORTHWEST); |
209 |
|
break; |
210 |
|
} |
211 |
|
} |
212 |
|
|
213 |
return coordinates; |
return mapPoint; |
214 |
} |
} |
215 |
|
|
216 |
|
|