24 |
#include <stdio.h> |
#include <stdio.h> |
25 |
|
|
26 |
#include <Foundation/NSDictionary.h> |
#include <Foundation/NSDictionary.h> |
27 |
|
#include <Foundation/NSData.h> |
28 |
|
#include <Foundation/NSArray.h> |
29 |
#include <Foundation/NSString.h> |
#include <Foundation/NSString.h> |
30 |
#include <Foundation/NSException.h> |
#include <Foundation/NSException.h> |
31 |
#include <Foundation/NSPathUtilities.h> |
#include <Foundation/NSPathUtilities.h> |
32 |
|
#include <Foundation/NSFileManager.h> |
33 |
|
|
34 |
#include "Common/Camera.h" |
#include "Common/Camera.h" |
35 |
#include "Map/MapDisplay.h" |
#include "Map/MapDisplay.h" |
36 |
#include "Map/MapShadows.h" |
#include "C_MapTerrain.h" |
37 |
|
#include "C_MapColors.h" |
38 |
#include "MapRandom.h" |
#include "MapRandom.h" |
39 |
#include "MapModel.h" |
#include "MapModel.h" |
40 |
|
|
43 |
- (id) init |
- (id) init |
44 |
{ |
{ |
45 |
self = [super init]; |
self = [super init]; |
46 |
|
if (self) |
47 |
|
_tmpMapPath = @"Map_default/tmpMap.map"; |
48 |
|
// tempory file for storing all the map parameters with complete path |
49 |
return self; |
return self; |
50 |
} |
} |
51 |
|
|
71 |
|
|
72 |
_map = [[MapDisplay alloc] |
_map = [[MapDisplay alloc] |
73 |
initWithContentsOfFile: mapName]; |
initWithContentsOfFile: mapName]; |
74 |
|
[_map precalculateNormals]; |
75 |
|
|
76 |
[self _initCamera]; |
[self _initCamera]; |
77 |
|
|
78 |
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile: mapName]; |
NSMutableDictionary *paramMapTmp = |
79 |
|
[NSMutableDictionary dictionaryWithContentsOfFile: mapName]; |
80 |
|
|
81 |
NSString *path = [mapName stringByDeletingLastPathComponent]; |
NSString *currentPath = [mapName stringByDeletingLastPathComponent]; |
82 |
if ([[dic objectForKey: @"shadows"] length] != 0) |
|
83 |
_shadowsName = [path stringByAppendingPathComponent: |
NSString *terrainPath = [currentPath stringByAppendingPathComponent: |
84 |
[dic objectForKey: @"shadows"]]; |
[paramMapTmp objectForKey: @"terrain"]]; |
85 |
else |
[paramMapTmp setObject: terrainPath |
86 |
_shadowsName = [dic objectForKey: @"shadows"]; |
forKey: @"terrain"]; |
87 |
|
|
88 |
|
NSString *colorsPath = [currentPath stringByAppendingPathComponent: |
89 |
|
[paramMapTmp objectForKey: @"colors"]]; |
90 |
|
[paramMapTmp setObject: colorsPath |
91 |
|
forKey: @"colors"]; |
92 |
|
|
93 |
|
if ([[paramMapTmp objectForKey: @"shadows"] length] != 0) |
94 |
|
{ |
95 |
|
NSString *shadowsPath = [currentPath stringByAppendingPathComponent: |
96 |
|
[paramMapTmp objectForKey: @"shadows"]]; |
97 |
|
[paramMapTmp setObject: shadowsPath |
98 |
|
forKey: @"shadows"]; |
99 |
|
} |
100 |
|
|
101 |
_shadows = [[MapShadows alloc] initWithShadowsImageName: _shadowsName]; |
[paramMapTmp writeToFile: _tmpMapPath |
102 |
|
atomically: YES]; |
103 |
} |
} |
104 |
|
|
105 |
- (void) openMapWithXLength: (int)xLength |
- (void) openMapWithXLength: (int)xLength |
107 |
andMaxHeight: (int)maxHeight |
andMaxHeight: (int)maxHeight |
108 |
{ |
{ |
109 |
TEST_RELEASE (_map); |
TEST_RELEASE (_map); |
|
printf("open MapRandom \n"); |
|
110 |
|
|
111 |
_map = [[MapDisplay alloc] initWithWidth: xLength |
_map = [[MapDisplay alloc] initWithWidth: xLength |
112 |
height: zLength]; |
height: zLength]; |
114 |
[MapRandom mapRandom: _map |
[MapRandom mapRandom: _map |
115 |
andMaxHeight: maxHeight]; |
andMaxHeight: maxHeight]; |
116 |
|
|
117 |
[self _initCamera]; |
[_map saveHeightFieldToFile: @"Map_default/saveTerrain.tiff"]; |
118 |
|
|
119 |
|
NSMutableDictionary *paramMapTmp = |
120 |
|
[NSMutableDictionary dictionaryWithContentsOfFile: @"Map_default/map1.map"]; |
121 |
|
|
122 |
|
NSFileManager *fileManager = [NSFileManager defaultManager]; |
123 |
|
NSString *currentDir = [fileManager currentDirectoryPath]; |
124 |
|
NSString *colorsPath = |
125 |
|
[currentDir stringByAppendingPathComponent: @"Map_default/caraibes.png"]; |
126 |
|
|
127 |
|
[paramMapTmp setObject: colorsPath |
128 |
|
forKey: @"colors"]; |
129 |
|
[paramMapTmp setObject: @"" |
130 |
|
forKey: @"terrain"]; |
131 |
|
[paramMapTmp setObject: @"" |
132 |
|
forKey: @"shadows"]; |
133 |
|
|
134 |
|
[_map setColors: [paramMapTmp objectForKey: @"colors"]]; |
135 |
|
|
136 |
|
[paramMapTmp writeToFile: _tmpMapPath |
137 |
|
atomically: YES]; |
138 |
|
|
139 |
|
[_map precalculateNormals]; |
140 |
|
|
141 |
_shadowsName = [NSString string]; |
[self _initCamera]; |
|
_shadows = nil; |
|
142 |
} |
} |
143 |
|
|
144 |
- (void) dealloc |
- (void) dealloc |
146 |
printf("dealloc MapModel \n"); |
printf("dealloc MapModel \n"); |
147 |
TEST_RELEASE (_camera); |
TEST_RELEASE (_camera); |
148 |
TEST_RELEASE (_map); |
TEST_RELEASE (_map); |
|
TEST_RELEASE (_shadows); |
|
149 |
|
|
150 |
[super dealloc]; |
[super dealloc]; |
151 |
} |
} |
201 |
|
|
202 |
coord_t eye; |
coord_t eye; |
203 |
eye.x = (double) ([_map width]) / 2.0; |
eye.x = (double) ([_map width]) / 2.0; |
204 |
eye.y = (double) (MAX([_map width],[_map height]) + 550) |
eye.y = (double) (MAX([_map width],[_map height]) + 600) |
205 |
/ tan(DEGTORAD (50.0)); |
/ tan(DEGTORAD ([_camera fovy])); |
206 |
eye.z = (double) ([_map height]) / 2.0; |
eye.z = (double) ([_map height]) / 2.0; |
207 |
double aspect = [_camera aspect]; |
double aspect = [_camera aspect]; |
208 |
|
|
246 |
} |
} |
247 |
} |
} |
248 |
|
|
249 |
|
- (void) setColors: (NSString *)colors |
250 |
|
{ |
251 |
|
NSParameterAssert (_map); |
252 |
|
|
253 |
|
NSMutableDictionary *paramMapTmp = |
254 |
|
[NSMutableDictionary dictionaryWithContentsOfFile: _tmpMapPath]; |
255 |
|
|
256 |
|
[paramMapTmp setObject: colors |
257 |
|
forKey: @"colors"]; |
258 |
|
|
259 |
|
[paramMapTmp writeToFile: _tmpMapPath |
260 |
|
atomically: YES]; |
261 |
|
|
262 |
|
[_map setColors: [paramMapTmp objectForKey: @"colors"]]; |
263 |
|
} |
264 |
|
|
265 |
|
//- (void) setTextures: (NSString *)textures |
266 |
|
// { |
267 |
|
// } |
268 |
|
|
269 |
/** |
/** |
270 |
* Displays or not the shadows. |
* Displays or not the shadows. |
271 |
*/ |
*/ |
272 |
- (void) enableShadows: (BOOL)shadows |
- (void) enableShadows: (BOOL)shadows |
273 |
{ |
{ |
274 |
if ((_shadows != nil) && [_shadowsName length] != 0) |
NSDictionary *paramMapTmp = |
275 |
|
[NSDictionary dictionaryWithContentsOfFile: _tmpMapPath]; |
276 |
|
|
277 |
|
if ([[paramMapTmp objectForKey: @"shadows"] length] != 0) |
278 |
{ |
{ |
|
printf ("enableShadows\n"); |
|
279 |
if (shadows) |
if (shadows) |
280 |
[_map setShadows: _shadowsName]; |
[_map setShadows: [paramMapTmp objectForKey: @"shadows"]]; |
281 |
else |
else |
282 |
[_map setShadows: [NSString string]]; |
[_map setShadows: [NSString string]]; |
283 |
} |
} |
286 |
- (void) computeShadowsWithSun: (coord_t)sun |
- (void) computeShadowsWithSun: (coord_t)sun |
287 |
{ |
{ |
288 |
NSParameterAssert (_map); |
NSParameterAssert (_map); |
|
TEST_RELEASE (_shadows); |
|
|
_shadows = [[MapShadows alloc] initWithLand: _map |
|
|
sun: sun]; |
|
289 |
|
|
290 |
_shadowsName = @"saveShadows.tiff"; |
[_map computeShadowsWithSun: sun]; |
291 |
[_shadows saveWithShadowsImageName: _shadowsName]; |
|
292 |
[_map setShadows: _shadowsName]; |
NSMutableDictionary *paramMapTmp = |
293 |
|
[NSMutableDictionary dictionaryWithContentsOfFile: _tmpMapPath]; |
294 |
|
|
295 |
|
[paramMapTmp setObject: @"Map_default/saveShadows.tiff" |
296 |
|
forKey: @"shadows"]; |
297 |
|
|
298 |
|
[paramMapTmp writeToFile: _tmpMapPath |
299 |
|
atomically: YES]; |
300 |
|
|
301 |
|
[_map saveWithShadowsImageName: [paramMapTmp objectForKey: @"shadows"]]; |
302 |
} |
} |
303 |
|
|
304 |
- (BOOL) saveWithShadowsImageName: (NSString *)shadows |
- (BOOL) saveMapParameterToFile: (NSString *)mapPath |
305 |
{ |
{ |
306 |
NSParameterAssert (_shadows); |
NSParameterAssert (_map); |
307 |
|
NSParameterAssert ([mapPath isAbsolutePath]); |
308 |
|
NSParameterAssert ([mapPath hasSuffix: @".map"]); |
309 |
|
|
310 |
|
NSString *mapPath_ = [NSString stringWithString: mapPath]; |
311 |
|
mapPath_ = [mapPath_ stringByDeletingPathExtension]; |
312 |
|
|
313 |
|
NSString *terrainPath = [mapPath_ stringByAppendingString: @"terrain.tiff"]; |
314 |
|
|
315 |
|
NSMutableDictionary *paramMapTmp = |
316 |
|
[NSMutableDictionary dictionaryWithContentsOfFile: _tmpMapPath]; |
317 |
|
|
318 |
|
[paramMapTmp setObject: terrainPath |
319 |
|
forKey: @"terrain"]; |
320 |
|
|
321 |
|
BOOL terrain = |
322 |
|
[_map saveHeightFieldToFile: [paramMapTmp objectForKey: @"terrain"]]; |
323 |
|
|
324 |
|
BOOL shadows = YES; |
325 |
|
if ([[paramMapTmp objectForKey: @"shadows"] length] != 0) |
326 |
|
{ |
327 |
|
[self enableShadows: YES]; |
328 |
|
|
329 |
|
NSString *shadowsPath = |
330 |
|
[mapPath_ stringByAppendingString: @"shadows.tiff"]; |
331 |
|
|
332 |
|
[paramMapTmp setObject: shadowsPath |
333 |
|
forKey: @"shadows"]; |
334 |
|
|
335 |
|
shadows = |
336 |
|
[_map saveWithShadowsImageName: [paramMapTmp objectForKey: @"shadows"]]; |
337 |
|
} |
338 |
|
|
339 |
|
[paramMapTmp writeToFile: _tmpMapPath |
340 |
|
atomically: YES]; |
341 |
|
|
342 |
|
NSString *localTerrainPath = |
343 |
|
[[paramMapTmp objectForKey: @"terrain"] lastPathComponent]; |
344 |
|
[paramMapTmp setObject: localTerrainPath |
345 |
|
forKey: @"terrain"]; |
346 |
|
|
347 |
|
NSString *localColors = |
348 |
|
[[paramMapTmp objectForKey: @"colors"] lastPathComponent]; |
349 |
|
[paramMapTmp setObject: localColors |
350 |
|
forKey: @"colors"]; |
351 |
|
|
352 |
|
if ([[paramMapTmp objectForKey: @"shadows"] length] != 0) |
353 |
|
{ |
354 |
|
NSString *localShadowsPath = |
355 |
|
[[paramMapTmp objectForKey: @"shadows"] lastPathComponent]; |
356 |
|
[paramMapTmp setObject: localShadowsPath |
357 |
|
forKey: @"shadows"]; |
358 |
|
} |
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
BOOL write = [paramMapTmp writeToFile: mapPath |
363 |
|
atomically: YES]; |
364 |
|
|
365 |
return [_shadows saveWithShadowsImageName: shadows]; |
return (terrain && shadows && write); |
366 |
} |
} |
367 |
|
|
368 |
- (Camera *) getCamera |
- (Camera *) getCamera |