Nilorea Library
C utilities for networking, threading, graphics
n_iso_engine.c
Go to the documentation of this file.
1
9
10#ifndef NOISOENGINE
11
27int 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 {
57 for ( y = 0 ; y < YSIZE ; y++ )
58 {
59 set_value( ( *map ), N_TILE, x, y, FALSE );
60 set_value( ( *map ), N_ABILITY, x, y, FALSE );
61 set_value( ( *map ), N_MUSIC, x, y, FALSE );
62 set_value( ( *map ), N_OBJECT, x, y, FALSE );
63 }
64 }
65
66 ( *map ) -> TILEW = TILEW;
67 ( *map ) -> TILEH = TILEH;
68 ( *map ) -> bgcolor = makeacol( 0, 0, 0, 255 );
69 ( *map ) -> wirecolor = makeacol( 255, 255, 255, 255 );
70
71 /* Drawing the mousemap */
72
73 ( *map ) -> mousemap = create_bitmap( ( *map ) -> TILEW, ( *map ) -> TILEH );
74 ( *map ) -> colortile = create_bitmap( ( *map ) -> TILEW, ( *map ) -> TILEH );
75
76 clear_to_color( ( *map ) -> mousemap, makecol( 255, 255, 255 ) );
77 clear_to_color( ( *map ) -> colortile, ( *map ) -> bgcolor );
78
79 /* drawing mousemap */
80 line( ( *map ) -> mousemap, 0, ( TILEH >> 1 ) - 2, ( TILEW >> 1 ) - 3, 0, makecol( 255, 0, 0 ) );
81 floodfill( ( *map ) -> mousemap, 1, 1, makecol( 255, 0, 0 ) );
82
83 line( ( *map ) -> mousemap, ( TILEW >> 1 ) - 3, TILEH - 1, 0, ( TILEH >> 1 ) + 1, makecol( 0, 255, 0 ) );
84 floodfill( ( *map ) -> mousemap, 1, TILEH - 2, makecol( 0, 255, 0 ) );
85
86 line( ( *map ) -> mousemap, ( TILEW >> 1 ) + 2, TILEH - 1, TILEW - 1, ( TILEH >> 1 ) + 1, makecol( 0, 0, 255 ) );
87 floodfill( ( *map ) -> mousemap, TILEW - 2, TILEH - 1, makecol( 0, 0, 255 ) );
88
89 line( ( *map ) -> mousemap, ( TILEW >> 1 ) + 2, 0, TILEW - 1, ( TILEH >> 1 ) - 2, makecol( 255, 255, 0 ) );
90 floodfill( ( *map ) -> mousemap, TILEW - 1, 0, makecol( 255, 255, 0 ) );
91
92 /* drawing colortile */
93 line( ( *map ) -> colortile, 0, ( TILEH >> 1 ) - 2, ( TILEW >> 1 ) - 3, 0, makecol( 255, 0, 255 ) );
94 floodfill( ( *map ) -> colortile, 1, 1, makecol( 255, 0, 255 ) );
95
96 line( ( *map ) -> colortile, ( TILEW >> 1 ) - 3, TILEH - 1, 0, ( TILEH >> 1 ) + 1, makecol( 255, 0, 255 ) );
97 floodfill( ( *map ) -> colortile, 1, TILEH - 2, makecol( 255, 0, 255 ) );
98
99 line( ( *map ) -> colortile, ( TILEW >> 1 ) + 2, TILEH - 1, TILEW - 1, ( TILEH >> 1 ) + 1, makecol( 255, 0, 255 ) );
100 floodfill( ( *map ) -> colortile, TILEW - 2, TILEH - 1, makecol( 255, 0, 255 ) );
101
102 line( ( *map ) -> colortile, ( TILEW >> 1 ) + 2, 0, TILEW - 1, ( TILEH >> 1 ) - 2, makecol( 255, 0, 255 ) );
103 floodfill( ( *map ) -> colortile, TILEW - 1, 0, makecol( 255, 0, 255 ) );
104
105 ( *map ) -> ptanchorX = ( *map ) -> X = 0 ;
106 ( *map ) -> ptanchorY = ( *map ) -> Y = 0 ;
107
108 return TRUE;
109} /* create_empty_map( ... ) */
110
111
112
122int set_value( MAP *map, int type, int x, int y, int value )
123{
124 if ( !map || x >= map -> XSIZE || y >= map -> YSIZE )
125 return FALSE;
126
127 if ( type == N_TILE )
128 {
129
130 map -> grid[ x + y * map -> XSIZE ] . tilenumber = value ;
131
132 return TRUE;
133 }
134
135 if ( type == N_ABILITY )
136 {
137
138 map -> grid[ x + y * map -> XSIZE ] . ability = value ;
139
140 return TRUE;
141 }
142
143 if ( type == N_MUSIC )
144 {
145
146 map -> grid[ x + y * map -> XSIZE ] . music = value ;
147
148 return TRUE;
149 }
150
151 if ( type == N_OBJECT )
152 {
153
154 map -> grid[ x + y * map -> XSIZE ] . objectnumber = value ;
155
156 return TRUE;
157 }
158
159
160 return FALSE;
161} /* set_value( ... ) */
162
163
164
173int get_value( MAP *map, int type, int x, int y )
174{
175
176 if ( !map || x >= map -> XSIZE || y >= map -> YSIZE )
177 return FALSE;
178
179
180 if ( type == N_TILE )
181 return map -> grid[ x + y * map -> XSIZE ] . tilenumber ;
182
183 if ( type == N_OBJECT )
184 return map -> grid[ x + y * map -> XSIZE ] . objectnumber ;
185
186 if ( type == N_MUSIC )
187 return map -> grid[ x + y * map -> XSIZE ] . music ;
188
189 if ( type == N_ABILITY )
190 return map -> grid[ x + y * map -> XSIZE ] . ability ;
191
192 return FALSE;
193
194} /* get_value( ... ) */
195
196
197
207int ScreenToMap( int mx, int my, int *Tilex, int *Tiley, BITMAP *mousemap )
208{
209
210 /* SCREEN TO MAP VARIABLES */
211 int RegionX, RegionY,
212 RegionDX = 0, RegionDY = 0,
213 MouseMapX, MouseMapY,
214 MouseMapWidth, MouseMapHeight, c;
215
216 MouseMapWidth = mousemap -> w;
217 MouseMapHeight = mousemap -> h;
218
219 /* First step find out where we are on the mousemap */
220 RegionX = ( mx / MouseMapWidth );
221 RegionY = ( my / MouseMapHeight ) << 1; /* The multiplying by two is very important */
222
223
224 /* Second Step: Find out WHERE in the mousemap our mouse is, by finding MouseMapX and MouseMapY. */
225
226 MouseMapX = mx % MouseMapWidth;
227 MouseMapY = my % MouseMapHeight;
228
229 /* third step: Find the color in the mouse map */
230 c = getpixel( mousemap, MouseMapX, MouseMapY );
231
232 if ( c == makecol( 255, 0, 0 ) )
233 {
234 RegionDX = -1;
235 RegionDY = -1;
236 }
237
238 if ( c == makecol( 255, 255, 0 ) )
239 {
240 RegionDX = 0;
241 RegionDY = -1;
242 }
243
244 if ( c == makecol( 255, 255, 255 ) )
245 {
246 RegionDX = 0;
247 RegionDY = 0;
248 }
249
250 if ( c == makecol( 0, 255, 0 ) )
251 {
252 RegionDX = -1;
253 RegionDY = 1;
254 }
255
256 if ( c == makecol( 0, 0, 255 ) )
257 {
258 RegionDX = 0;
259 RegionDY = 1;
260 }
261
262 *Tilex = ( RegionDX + RegionX );
263 *Tiley = ( RegionDY + RegionY );
264
265
266 return TRUE;
267} /* ScreenToMap( ... ) */
268
269
270
278int camera_to_scr( MAP **map, int x, int y )
279{
280 if ( x < 0 )
281 x = 0;
282
283 if ( y < 0 )
284 y = 0;
285
286 ( *map ) -> X = x % ( *map ) ->TILEW;
287 ( *map ) -> Y = y % ( *map ) ->TILEH;
288 ( *map ) -> ptanchorY = y / ( ( *map ) ->TILEH >> 1 );
289 ( *map ) -> ptanchorY = ( *map ) -> ptanchorY >> 1;
290 ( *map ) -> ptanchorY = ( *map ) -> ptanchorY << 1;
291 ( *map ) -> ptanchorX = ( x + ( ( *map ) -> ptanchorY & 1 ) * ( ( *map ) ->TILEW >> 1 ) ) / ( *map ) ->TILEW;
292
293 return TRUE;
294} /* camera_to_scr( ... )*/
295
296
297
307int camera_to_map( MAP **map, int tx, int ty, int x, int y )
308{
309
310 ( *map ) -> ptanchorX = tx;
311 ( *map ) -> ptanchorY = ty;
312 ( *map ) -> X = x;
313 ( *map ) -> Y = y;
314
315 return TRUE;
316} /* camera_to_map(...) */
317
318
319
329int draw_map( MAP *map, BITMAP *bmp, int destx, int desty, int mode )
330{
331 int a, b, /* iterators */
332 x, y, /* temp store of coordinate */
333 mvx,mvy, /* precomputed offset */
334 tw, th, /* number of tile for one screen */
335 TW2,TH2; /* size of TILE_W /2 and TILE_H/2 */
336
337 /* computing tw&th */
338 tw = 1 + ( ( bmp -> w - destx ) / map -> TILEW ) ;
339 th = 3 + ( ( bmp -> h - desty ) / ( map -> TILEH >> 1 ) ) ;
340
341 TW2 = map -> TILEW>>1 ;
342 TH2 = map -> TILEH>>1 ;
343
344 mvx = destx - TW2 - map -> X;
345 mvy = desty - TH2 - map -> Y;
346
347 for ( a = 0 ; a <= tw ; a++ )
348 {
349 for ( b = 0 ; b <= th ; b++ )
350 {
351
352
353 x = ( a * map -> TILEW + ( ( b & 1 ) * TW2 ) ) - ( map -> TILEW >> 1 ) + mvx;
354 y = ( b * TH2 ) + mvy;
355
356 if ( ( a + map -> ptanchorX ) < map->XSIZE && ( b + map -> ptanchorY ) < map->YSIZE )
357 {
358
359 if ( map -> grid [ a + map -> ptanchorX + ( b + map -> ptanchorY ) * map->XSIZE ] . tilenumber == FALSE )
360 {
361
362 masked_blit( map -> colortile, bmp, 0, 0, x, y, map -> colortile -> w, map -> colortile -> h );
363
364 }
365
366 else
367 {
368
369 /* TODO HERE : add sprite blit */
370 triangle( bmp, x, y + ( map -> TILEH >> 1 ), x + map-> TILEW, y + ( map->TILEH >> 1 ), x + ( map -> TILEW >> 1 ), y, makecol( 255, 255, 0 ) );
371 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 ) );
372
373 } /* if( map -> grid... ) ) */
374
375 if ( mode == 1 )
376 {
377
378 line( bmp, x + ( map -> TILEW >> 1 ) - 2, y, x, y + ( map -> TILEH >> 1 ) - 1, map -> wirecolor );
379 line( bmp, x + ( map -> TILEW >> 1 ) - 2, y + map -> TILEH - 1, x, y + ( map -> TILEH >> 1 ), map -> wirecolor );
380 line( bmp, x + ( map -> TILEW >> 1 ) + 1, y + map -> TILEH - 1, x + map -> TILEW - 1, y + ( map -> TILEH >> 1 ), map -> wirecolor );
381 line( bmp, x + ( map -> TILEW >> 1 ) + 1, y, x + map -> TILEW - 1, y + ( map -> TILEH >> 1 ) - 1, map -> wirecolor );
382
383 }
384
385 /* TODO HERE : check if object,
386 check if it is already in the library
387 if yes refresh its ttl
388 if no add it with the defaut ttl
389 */
390
391
392 } /* if( (a + ...) ) */
393
394 } /* for( a... ) */
395 } /* for( b... ) */
396
397 /* TODO HERE:
398
399 sort the object list by X and by Y
400 blit all the sprites of the active list
401 */
402
403 return TRUE;
404
405} /* draw_map( ... ) */
406
407
408
415int load_map( MAP **map, char *filename )
416{
417 PACKFILE *loaded;
418
419 char temp_name[1024];
420 MAP temp_map;
421
422
423 int it = 0, it1 = 0 ;
424
425 loaded = pack_fopen( filename, "rb" );
426
427 if( map )
428 free_map( (*map) );
429 if( !(*map) )
430 Malloc( (*map), MAP, 1 );
431
432 /* writting name */
433 it = pack_igetl( loaded );
434
435 pack_fread( temp_name, it, loaded );
436
437 /* writting states */
438 temp_map . XSIZE = pack_igetl( loaded );
439 temp_map . YSIZE = pack_igetl( loaded );
440 temp_map . TILEW = pack_igetl( loaded );
441 temp_map . TILEH = pack_igetl( loaded );
442 temp_map . ptanchorX = pack_igetl( loaded );
443 temp_map . ptanchorY = pack_igetl( loaded );
444 temp_map . X = pack_igetl( loaded );
445 temp_map . Y = pack_igetl( loaded );
446 temp_map . bgcolor = pack_igetl( loaded );
447 temp_map . wirecolor = pack_igetl( loaded );
448
449 create_empty_map( ( *map ), temp_name,
450 temp_map . XSIZE, temp_map . YSIZE,
451 temp_map . TILEW, temp_map . TILEH,
452 2000, 2000, 2000, 2000,2000 );
453 (*map ) -> XSIZE = temp_map . XSIZE ;
454 (*map ) -> YSIZE = temp_map . YSIZE ;
455 (*map ) -> TILEW = temp_map . TILEW ;
456 (*map ) -> TILEH = temp_map . TILEH ;
457 (*map ) -> ptanchorX = temp_map . ptanchorX ;
458 (*map ) -> ptanchorY = temp_map . ptanchorY ;
459 (*map ) -> X = temp_map . X ;
460 (*map ) -> Y = temp_map . Y ;
461 (*map ) ->bgcolor = temp_map . bgcolor ;
462 (*map ) ->wirecolor = temp_map . wirecolor ;
463
464
465 /* saving the grid */
466 for( it = 0 ; it < (*map ) -> XSIZE ; it ++ )
467 {
468 for( it1 = 0 ; it1 < (*map ) -> YSIZE ; it1 ++ )
469 {
470 (*map ) -> grid[ it + it1 * (*map ) -> XSIZE ] . tilenumber = pack_igetl( loaded );
471 (*map ) -> grid[ it + it1 * (*map ) -> XSIZE ] . ability = pack_igetl( loaded );
472 (*map ) -> grid[ it + it1 * (*map ) -> XSIZE ] . objectnumber = pack_igetl( loaded );
473 (*map ) -> grid[ it + it1 * (*map ) -> XSIZE ] . music = pack_igetl( loaded );
474 }
475 }
476
477 /*load_animlib( loaded , & (*map ) -> libtiles );*/
478 /*load_animlib( loaded , & (*map ) -> libanims );*/
479
480 pack_fclose( loaded );
481
482
483 return TRUE;
484} /* load_map( ... ) */
485
486
487
494int save_map( MAP *map, char *filename )
495{
496
497 PACKFILE *saved;
498
499 int it = 0, it1 = 0 ;
500
501 saved = pack_fopen( filename, "wb" );
502
503 /* writting name */
504 pack_iputl( ustrsizez( map -> name ), saved );
505 pack_fwrite( map -> name, ustrsizez( map -> name ), saved );
506
507 /* writting states */
508 pack_iputl( map -> XSIZE, saved );
509 pack_iputl( map -> YSIZE, saved );
510 pack_iputl( map -> TILEW, saved );
511 pack_iputl( map -> TILEH, saved );
512 pack_iputl( map -> ptanchorX, saved );
513 pack_iputl( map -> ptanchorY, saved );
514 pack_iputl( map -> X, saved );
515 pack_iputl( map -> Y, saved );
516 pack_iputl( map -> bgcolor, saved );
517 pack_iputl( map -> wirecolor, saved );
518
519 /* saving the grid */
520 for( it = 0 ; it < map -> XSIZE ; it ++ )
521 {
522 for( it1 = 0 ; it1 < map -> YSIZE ; it1 ++ )
523 {
524 pack_iputl( map -> grid[ it + it1 * map -> XSIZE ] . tilenumber, saved );
525 pack_iputl( map -> grid[ it + it1 * map -> XSIZE ] . ability, saved );
526 pack_iputl( map -> grid[ it + it1 * map -> XSIZE ] . objectnumber, saved );
527 pack_iputl( map -> grid[ it + it1 * map -> XSIZE ] . music, saved );
528 }
529 }
530
531 /*save_animlib( saved , map -> libtiles );*/
532 /*save_animlib( saved , map -> libanims );*/
533
534 pack_fclose( saved );
535
536
537 return TRUE;
538} /* save_map( ... ) */
539
540
541
547int free_map( MAP **map )
548{
549
550 if( !(*map) )
551 return TRUE;
552 if( (*map) -> grid )
553 {
554 Free( ( *map ) -> grid );
555 }
556 if( (*map) -> name )
557 {
558 Free( ( *map ) -> name );
559 }
560 if( (*map) -> colortile )
561 destroy_bitmap( ( *map ) -> colortile );
562 if( (*map) -> mousemap )
563 destroy_bitmap( ( *map ) -> mousemap );
564
565 Free( ( *map ) );
566
567 return TRUE;
568} /* free_map( ... ) */
569
570#endif /* #ifndef NOISOENGINE */
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition: n_common.h:183
#define Free(__ptr)
Free Handler to get errors.
Definition: n_common.h:256
int wirecolor
color of wire
Definition: n_iso_engine.h:90
int TILEH
size Y of tiles of the map (in pixel)
Definition: n_iso_engine.h:78
int YSIZE
size Y of the grid (nbYcell)
Definition: n_iso_engine.h:74
int XSIZE
size X of the grid (nbXcell)
Definition: n_iso_engine.h:72
int bgcolor
color of the bg
Definition: n_iso_engine.h:88
int save_map(MAP *map, char *filename)
Save the map to filename.
Definition: n_iso_engine.c:494
int get_value(MAP *map, int type, int x, int y)
Get a the tilenumber of a cell item.
Definition: n_iso_engine.c:173
int load_map(MAP **map, char *filename)
Load the map from filename.
Definition: n_iso_engine.c:415
int camera_to_map(MAP **map, int tx, int ty, int x, int y)
Center Map on given map coordinate, with x & y offset.
Definition: n_iso_engine.c:307
#define N_OBJECT
FLAG of object type.
Definition: n_iso_engine.h:33
int camera_to_scr(MAP **map, int x, int y)
Center Map on given screen coordinate.
Definition: n_iso_engine.c:278
int set_value(MAP *map, int type, int x, int y, int value)
Set the value of a cell in a map.
Definition: n_iso_engine.c:122
#define N_MUSIC
FLAG of music type.
Definition: n_iso_engine.h:31
#define N_TILE
FLAG of tile type.
Definition: n_iso_engine.h:27
#define N_ABILITY
FLAG of ability type.
Definition: n_iso_engine.h:29
int draw_map(MAP *map, BITMAP *bmp, int destx, int desty, int mode)
Draw a MAP *map on the BITMAP *bmp.
Definition: n_iso_engine.c:329
int ScreenToMap(int mx, int my, int *Tilex, int *Tiley, BITMAP *mousemap)
Convert screen coordinate to map coordinate.
Definition: n_iso_engine.c:207
int free_map(MAP **map)
Free the map.
Definition: n_iso_engine.c:547
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)
Create an empty map.
Definition: n_iso_engine.c:27
Cell of a MAP.
Definition: n_iso_engine.h:44
MAP with objects, tiles, skins.
Definition: n_iso_engine.h:58
Map loading, saving, with objects, animations, ...