Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
n_iso_engine.c
Go to the documentation of this file.
1
8 #include "n_iso_engine.h"
9
10 #ifndef NOISOENGINE
11
27 int create_empty_map( MAP **map , char *name , int XSIZE , int YSIZE , int TILEW , int TILEH , int nbmaxobjects , int nbmaxgroup , int nbmaxanims , int nbtiles , int nbanims )
28 {
29 int x, y;
30
31 /* only here for warning:unused... avoiding, will be removed further */
32 nbmaxobjects = nbmaxgroup = nbmaxanims = nbtiles = nbanims = 1 ;
33 nbmaxgroup = nbmaxobjects ;
34
35 /* allocating map */
36 Malloc( ( *map ) , MAP , 1 );
37
38 if ( !( *map ) )
39 return FALSE;
40
41 /* allocating grid */
42 Malloc( ( *map ) -> grid , CELL , XSIZE * YSIZE );
43
44 /* allocating name */
45 Malloc( ( *map ) -> name , char , ustrsizez( name ) );
46
47 ustrcpy( ( *map ) -> name , name );
48
49 if ( !( *map ) -> grid || !( *map ) -> name )
50 return FALSE;
51
52 ( *map ) -> XSIZE = XSIZE ;
53 ( *map ) -> YSIZE = YSIZE ;
54
55 for ( x = 0 ; x < XSIZE ; x++ ) {
56 for ( y = 0 ; y < YSIZE ; y++ ) {
57 set_value( ( *map ) , N_TILE , x , y , FALSE );
58 set_value( ( *map ) , N_ABILITY , x , y , FALSE );
59 set_value( ( *map ) , N_MUSIC , x , y , FALSE );
60 set_value( ( *map ) , N_OBJECT , x , y , FALSE );
61 }
62 }
63
64 ( *map ) -> TILEW = TILEW;
65 ( *map ) -> TILEH = TILEH;
66 ( *map ) -> bgcolor = makeacol( 0 , 0 , 0 , 255 );
67 ( *map ) -> wirecolor = makeacol( 255 , 255 , 255 , 255 );
68
69 /* Drawing the mousemap */
70
71 ( *map ) -> mousemap = create_bitmap( ( *map ) -> TILEW , ( *map ) -> TILEH );
72 ( *map ) -> colortile = create_bitmap( ( *map ) -> TILEW , ( *map ) -> TILEH );
73
74 clear_to_color( ( *map ) -> mousemap , makecol( 255 , 255 , 255 ) );
75 clear_to_color( ( *map ) -> colortile , ( *map ) -> bgcolor );
76
77 /* drawing mousemap */
78 line( ( *map ) -> mousemap , 0 , ( TILEH >> 1 ) - 2 , ( TILEW >> 1 ) - 3 , 0 , makecol( 255, 0, 0 ) );
79 floodfill( ( *map ) -> mousemap , 1 , 1 , makecol( 255, 0, 0 ) );
80
81 line( ( *map ) -> mousemap , ( TILEW >> 1 ) - 3 , TILEH - 1 , 0 , ( TILEH >> 1 ) + 1 , makecol( 0 , 255 , 0 ) );
82 floodfill( ( *map ) -> mousemap , 1 , TILEH - 2 , makecol( 0, 255, 0 ) );
83
84 line( ( *map ) -> mousemap , ( TILEW >> 1 ) + 2 , TILEH - 1 , TILEW - 1 , ( TILEH >> 1 ) + 1 , makecol( 0, 0, 255 ) );
85 floodfill( ( *map ) -> mousemap , TILEW - 2 , TILEH - 1 , makecol( 0, 0, 255 ) );
86
87 line( ( *map ) -> mousemap , ( TILEW >> 1 ) + 2 , 0 , TILEW - 1 , ( TILEH >> 1 ) - 2 , makecol( 255, 255 , 0 ) );
88 floodfill( ( *map ) -> mousemap , TILEW - 1 , 0 , makecol( 255, 255, 0 ) );
89
90 /* drawing colortile */
91 line( ( *map ) -> colortile , 0 , ( TILEH >> 1 ) - 2 , ( TILEW >> 1 ) - 3 , 0 , makecol( 255 , 0 , 255 ) );
92 floodfill( ( *map ) -> colortile , 1 , 1 , makecol( 255 , 0 , 255 ) );
93
94 line( ( *map ) -> colortile , ( TILEW >> 1 ) - 3, TILEH - 1 , 0 , ( TILEH >> 1 ) + 1 , makecol( 255 , 0 , 255 ) );
95 floodfill( ( *map ) -> colortile , 1 , TILEH - 2 , makecol( 255 , 0 , 255 ) );
96
97 line( ( *map ) -> colortile , ( TILEW >> 1 ) + 2 , TILEH - 1 , TILEW - 1 , ( TILEH >> 1 ) + 1 , makecol( 255 , 0 , 255 ) );
98 floodfill( ( *map ) -> colortile , TILEW - 2 , TILEH - 1 , makecol( 255 , 0 , 255 ) );
99
100 line( ( *map ) -> colortile , ( TILEW >> 1 ) + 2 , 0 , TILEW - 1 , ( TILEH >> 1 ) - 2 , makecol( 255 , 0 , 255 ) );
101 floodfill( ( *map ) -> colortile , TILEW - 1 , 0 , makecol( 255 , 0 , 255 ) );
102
103 ( *map ) -> ptanchorX = ( *map ) -> X = 0 ;
104 ( *map ) -> ptanchorY = ( *map ) -> Y = 0 ;
105
106 return TRUE;
107 } /* create_empty_map( ... ) */
108
109
110
120 int set_value( MAP *map , int type , int x , int y , int value )
121 {
122 if ( !map || x >= map -> XSIZE || y >= map -> YSIZE )
123 return FALSE;
124
125 if ( type == N_TILE ) {
126
127 map -> grid[ x + y * map -> XSIZE ] . tilenumber = value ;
128
129 return TRUE;
130 }
131
132 if ( type == N_ABILITY ) {
133
134 map -> grid[ x + y * map -> XSIZE ] . ability = value ;
135
136 return TRUE;
137 }
138
139 if ( type == N_MUSIC ) {
140
141 map -> grid[ x + y * map -> XSIZE ] . music = value ;
142
143 return TRUE;
144 }
145
146 if ( type == N_OBJECT ) {
147
148 map -> grid[ x + y * map -> XSIZE ] . objectnumber = value ;
149
150 return TRUE;
151 }
152
153
154 return FALSE;
155 } /* set_value( ... ) */
156
157
158
167 int get_value( MAP *map , int type , int x , int y )
168 {
169
170 if ( !map || x >= map -> XSIZE || y >= map -> YSIZE )
171 return FALSE;
172
173
174 if ( type == N_TILE )
175 return map -> grid[ x + y * map -> XSIZE ] . tilenumber ;
176
177 if ( type == N_OBJECT )
178 return map -> grid[ x + y * map -> XSIZE ] . objectnumber ;
179
180 if ( type == N_MUSIC )
181 return map -> grid[ x + y * map -> XSIZE ] . music ;
182
183 if ( type == N_ABILITY )
184 return map -> grid[ x + y * map -> XSIZE ] . ability ;
185
186 return FALSE;
187
188 } /* get_value( ... ) */
189
190
191
201 int ScreenToMap( int mx, int my, int *Tilex, int *Tiley, BITMAP *mousemap )
202 {
203
204 /* SCREEN TO MAP VARIABLES */
205 int RegionX, RegionY,
206 RegionDX = 0, RegionDY = 0,
207 MouseMapX, MouseMapY,
208 MouseMapWidth, MouseMapHeight, c;
209
210 MouseMapWidth = mousemap -> w;
211 MouseMapHeight = mousemap -> h;
212
213 /* First step find out where we are on the mousemap */
214 RegionX = ( mx / MouseMapWidth );
215 RegionY = ( my / MouseMapHeight ) << 1; /* The multiplying by two is very important */
216
217
218 /* Second Step: Find out WHERE in the mousemap our mouse is, by finding MouseMapX and MouseMapY. */
219
220 MouseMapX = mx % MouseMapWidth;
221 MouseMapY = my % MouseMapHeight;
222
223 /* third step: Find the color in the mouse map */
224 c = getpixel( mousemap, MouseMapX, MouseMapY );
225
226 if ( c == makecol( 255, 0, 0 ) ) {
227 RegionDX = -1;
228 RegionDY = -1;
229 }
230
231 if ( c == makecol( 255, 255, 0 ) ) {
232 RegionDX = 0;
233 RegionDY = -1;
234 }
235
236 if ( c == makecol( 255, 255, 255 ) ) {
237 RegionDX = 0;
238 RegionDY = 0;
239 }
240
241 if ( c == makecol( 0, 255, 0 ) ) {
242 RegionDX = -1;
243 RegionDY = 1;
244 }
245
246 if ( c == makecol( 0, 0, 255 ) ) {
247 RegionDX = 0;
248 RegionDY = 1;
249 }
250
251 *Tilex = ( RegionDX + RegionX );
252 *Tiley = ( RegionDY + RegionY );
253
254
255 return TRUE;
256 } /* ScreenToMap( ... ) */
257
258
259
267 int camera_to_scr( MAP **map , int x , int y )
268 {
269 if ( x < 0 )
270 x = 0;
271
272 if ( y < 0 )
273 y = 0;
274
275 ( *map ) -> X = x % ( *map ) ->TILEW;
276 ( *map ) -> Y = y % ( *map ) ->TILEH;
277 ( *map ) -> ptanchorY = y / ( ( *map ) ->TILEH >> 1 );
278 ( *map ) -> ptanchorY = ( *map ) -> ptanchorY >> 1;
279 ( *map ) -> ptanchorY = ( *map ) -> ptanchorY << 1;
280 ( *map ) -> ptanchorX = ( x + ( ( *map ) -> ptanchorY & 1 ) * ( ( *map ) ->TILEW >> 1 ) ) / ( *map ) ->TILEW;
281
282 return TRUE;
283 } /* camera_to_scr( ... )*/
284
285
286
296 int camera_to_map( MAP **map , int tx , int ty , int x , int y )
297 {
298
299 ( *map ) -> ptanchorX = tx;
300 ( *map ) -> ptanchorY = ty;
301 ( *map ) -> X = x;
302 ( *map ) -> Y = y;
303
304 return TRUE;
305 } /* camera_to_map(...) */
306
307
308
318 int draw_map( MAP *map , BITMAP *bmp , int destx , int desty , int mode )
319 {
320 int a , b , /* iterators */
321 x , y , /* temp store of coordinate */
322 mvx,mvy , /* precomputed offset */
323 tw , th, /* number of tile for one screen */
324 TW2,TH2; /* size of TILE_W /2 and TILE_H/2 */
325
326 /* computing tw&th */
327 tw = 1 + ( ( bmp -> w - destx ) / map -> TILEW ) ;
328 th = 3 + ( ( bmp -> h - desty ) / ( map -> TILEH >> 1 ) ) ;
329
330 TW2 = map -> TILEW>>1 ;
331 TH2 = map -> TILEH>>1 ;
332
333 mvx = destx - TW2 - map -> X;
334 mvy = desty - TH2 - map -> Y;
335
336 for ( a = 0 ; a <= tw ; a++ ) {
337 for ( b = 0 ; b <= th ; b++ ) {
338
339
340 x = ( a * map -> TILEW + ( ( b & 1 ) * TW2 ) ) - ( map -> TILEW >> 1 ) + mvx;
341 y = ( b * TH2 ) + mvy;
342
343 if ( ( a + map -> ptanchorX ) < map->XSIZE && ( b + map -> ptanchorY ) < map->YSIZE ) {
344
345 if ( map -> grid [ a + map -> ptanchorX + ( b + map -> ptanchorY ) * map->XSIZE ] . tilenumber == FALSE ) {
346
347 masked_blit( map -> colortile , bmp , 0 , 0 , x , y , map -> colortile -> w , map -> colortile -> h );
348
349 }
350
351 else {
352
353 /* TODO HERE : add sprite blit */
354 triangle( bmp , x , y + ( map -> TILEH >> 1 ), x + map-> TILEW , y + ( map->TILEH >> 1 ), x + ( map -> TILEW >> 1 ) , y , makecol( 255, 255, 0 ) );
355 triangle( bmp , x , y + ( map -> TILEH >> 1 ), x + map-> TILEW , y + ( map->TILEH >> 1 ), x + ( map -> TILEW >> 1 ) , y + map -> TILEH, makecol( 255, 0, 255 ) );
356
357 } /* if( map -> grid... ) ) */
358
359 if ( mode == 1 ) {
360
361 line( bmp , x + ( map -> TILEW >> 1 ) - 2 , y , x , y + ( map -> TILEH >> 1 ) - 1 , map -> wirecolor );
362 line( bmp , x + ( map -> TILEW >> 1 ) - 2 , y + map -> TILEH - 1 , x , y + ( map -> TILEH >> 1 ) , map -> wirecolor );
363 line( bmp , x + ( map -> TILEW >> 1 ) + 1 , y + map -> TILEH - 1 , x + map -> TILEW - 1 , y + ( map -> TILEH >> 1 ) , map -> wirecolor );
364 line( bmp , x + ( map -> TILEW >> 1 ) + 1 , y , x + map -> TILEW - 1 , y + ( map -> TILEH >> 1 ) - 1 , map -> wirecolor );
365
366 }
367
368 /* TODO HERE : check if object,
369 check if it is already in the library
370 if yes refresh its ttl
371 if no add it with the defaut ttl
372 */
373
374
375 } /* if( (a + ...) ) */
376
377 } /* for( a... ) */
378 } /* for( b... ) */
379
380 /* TODO HERE:
381
382 sort the object list by X and by Y
383 blit all the sprites of the active list
384 */
385
386 return TRUE;
387
388 } /* draw_map( ... ) */
389
390
391
398 int load_map( MAP **map , char *filename )
399 {
400 PACKFILE *loaded;
401
402 char temp_name[1024];
403 MAP temp_map;
404
405
406 int it = 0 , it1 = 0 ;
407
408 loaded = pack_fopen( filename , "rb" );
409
410 if( map )
411 free_map( &(*map) );
412 if( !(*map) )
413 Malloc( (*map) , MAP , 1 );
414
415 /* writting name */
416 it = pack_igetl( loaded );
417
418 pack_fread( temp_name , it , loaded );
419
420 /* writting states */
421 temp_map . XSIZE = pack_igetl( loaded );
422 temp_map . YSIZE = pack_igetl( loaded );
423 temp_map . TILEW = pack_igetl( loaded );
424 temp_map . TILEH = pack_igetl( loaded );
425 temp_map . ptanchorX = pack_igetl( loaded );
426 temp_map . ptanchorY = pack_igetl( loaded );
427 temp_map . X = pack_igetl( loaded );
428 temp_map . Y = pack_igetl( loaded );
429 temp_map . bgcolor = pack_igetl( loaded );
430 temp_map . wirecolor = pack_igetl( loaded );
431
432 create_empty_map( &( *map ) , temp_name ,
433 temp_map . XSIZE , temp_map . YSIZE ,
434 temp_map . TILEW , temp_map . TILEH ,
435 2000 , 2000 , 2000 , 2000 ,2000 );
436 (*map ) -> XSIZE = temp_map . XSIZE ;
437 (*map ) -> YSIZE = temp_map . YSIZE ;
438 (*map ) -> TILEW = temp_map . TILEW ;
439 (*map ) -> TILEH = temp_map . TILEH ;
440 (*map ) -> ptanchorX = temp_map . ptanchorX ;
441 (*map ) -> ptanchorY = temp_map . ptanchorY ;
442 (*map ) -> X = temp_map . X ;
443 (*map ) -> Y = temp_map . Y ;
444 (*map ) ->bgcolor = temp_map . bgcolor ;
445 (*map ) ->wirecolor = temp_map . wirecolor ;
446
447
448 /* saving the grid */
449 for( it = 0 ; it < (*map ) -> XSIZE ; it ++ ) {
450 for( it1 = 0 ; it1 < (*map ) -> YSIZE ; it1 ++ ){
451 (*map ) -> grid[ it + it1 * (*map ) -> XSIZE ] . tilenumber = pack_igetl( loaded );
452 (*map ) -> grid[ it + it1 * (*map ) -> XSIZE ] . ability = pack_igetl( loaded );
453 (*map ) -> grid[ it + it1 * (*map ) -> XSIZE ] . objectnumber = pack_igetl( loaded );
454 (*map ) -> grid[ it + it1 * (*map ) -> XSIZE ] . music = pack_igetl( loaded );
455 }
456 }
457
458 /*load_animlib( loaded , & (*map ) -> libtiles );*/
459 /*load_animlib( loaded , & (*map ) -> libanims );*/
460
461 pack_fclose( loaded );
462
463
464 return TRUE;
465 } /* load_map( ... ) */
466
467
468
475 int save_map( MAP *map , char *filename )
476 {
477
478 PACKFILE *saved;
479
480 int it = 0 , it1 = 0 ;
481
482 saved = pack_fopen( filename , "wb" );
483
484 /* writting name */
485 pack_iputl( ustrsizez( map -> name ) , saved );
486 pack_fwrite( map -> name , ustrsizez( map -> name ) , saved );
487
488 /* writting states */
489 pack_iputl( map -> XSIZE , saved );
490 pack_iputl( map -> YSIZE , saved );
491 pack_iputl( map -> TILEW , saved );
492 pack_iputl( map -> TILEH , saved );
493 pack_iputl( map -> ptanchorX , saved );
494 pack_iputl( map -> ptanchorY , saved );
495 pack_iputl( map -> X , saved );
496 pack_iputl( map -> Y , saved );
497 pack_iputl( map -> bgcolor , saved );
498 pack_iputl( map -> wirecolor , saved );
499
500 /* saving the grid */
501 for( it = 0 ; it < map -> XSIZE ; it ++ ) {
502 for( it1 = 0 ; it1 < map -> YSIZE ; it1 ++ ){
503 pack_iputl( map -> grid[ it + it1 * map -> XSIZE ] . tilenumber , saved );
504 pack_iputl( map -> grid[ it + it1 * map -> XSIZE ] . ability , saved );
505 pack_iputl( map -> grid[ it + it1 * map -> XSIZE ] . objectnumber , saved );
506 pack_iputl( map -> grid[ it + it1 * map -> XSIZE ] . music , saved );
507 }
508 }
509
510 /*save_animlib( saved , map -> libtiles );*/
511 /*save_animlib( saved , map -> libanims );*/
512
513 pack_fclose( saved );
514
515
516 return TRUE;
517 } /* save_map( ... ) */
518
519
520
526 int free_map( MAP **map )
527 {
528
529 if( !(*map) )
530 return TRUE;
531 if( (*map) -> grid )
532 {
533 Free( ( *map ) -> grid );
534 }
535 if( (*map) -> name )
536 {
537 Free( ( *map ) -> name );
538 }
539 if( (*map) -> colortile )
540 destroy_bitmap( ( *map ) -> colortile );
541 if( (*map) -> mousemap )
542 destroy_bitmap( ( *map ) -> mousemap );
543
544 Free( ( *map ) );
545
546 return TRUE;
547 } /* free_map( ... ) */
548
549 #endif /* #ifndef NOISOENGINE */