Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
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 int x, y;
29
30 /* only here for warning:unused... avoiding, will be removed further */
31 nbmaxobjects = nbmaxgroup = nbmaxanims = nbtiles = nbanims = 1;
32 nbmaxgroup = nbmaxobjects;
33
34 /* allocating map */
35 Malloc((*map), MAP, 1);
36
37 if (!(*map))
38 return FALSE;
39
40 /* allocating grid */
41 Malloc((*map)->grid, CELL, XSIZE * YSIZE);
42
43 /* allocating name */
44 Malloc((*map)->name, char, ustrsizez(name));
45
46 ustrcpy((*map)->name, name);
47
48 if (!(*map)->grid || !(*map)->name)
49 return FALSE;
50
51 (*map)->XSIZE = XSIZE;
52 (*map)->YSIZE = YSIZE;
53
54 for (x = 0; x < XSIZE; x++) {
55 for (y = 0; y < YSIZE; y++) {
56 set_value((*map), N_TILE, x, y, FALSE);
57 set_value((*map), N_ABILITY, x, y, FALSE);
58 set_value((*map), N_MUSIC, x, y, FALSE);
59 set_value((*map), N_OBJECT, x, y, FALSE);
60 }
61 }
62
63 (*map)->TILEW = TILEW;
64 (*map)->TILEH = TILEH;
65 (*map)->bgcolor = makeacol(0, 0, 0, 255);
66 (*map)->wirecolor = makeacol(255, 255, 255, 255);
67
68 /* Drawing the mousemap */
69
70 (*map)->mousemap = create_bitmap((*map)->TILEW, (*map)->TILEH);
71 (*map)->colortile = create_bitmap((*map)->TILEW, (*map)->TILEH);
72
73 clear_to_color((*map)->mousemap, makecol(255, 255, 255));
74 clear_to_color((*map)->colortile, (*map)->bgcolor);
75
76 /* drawing mousemap */
77 line((*map)->mousemap, 0, (TILEH >> 1) - 2, (TILEW >> 1) - 3, 0, makecol(255, 0, 0));
78 floodfill((*map)->mousemap, 1, 1, makecol(255, 0, 0));
79
80 line((*map)->mousemap, (TILEW >> 1) - 3, TILEH - 1, 0, (TILEH >> 1) + 1, makecol(0, 255, 0));
81 floodfill((*map)->mousemap, 1, TILEH - 2, makecol(0, 255, 0));
82
83 line((*map)->mousemap, (TILEW >> 1) + 2, TILEH - 1, TILEW - 1, (TILEH >> 1) + 1, makecol(0, 0, 255));
84 floodfill((*map)->mousemap, TILEW - 2, TILEH - 1, makecol(0, 0, 255));
85
86 line((*map)->mousemap, (TILEW >> 1) + 2, 0, TILEW - 1, (TILEH >> 1) - 2, makecol(255, 255, 0));
87 floodfill((*map)->mousemap, TILEW - 1, 0, makecol(255, 255, 0));
88
89 /* drawing colortile */
90 line((*map)->colortile, 0, (TILEH >> 1) - 2, (TILEW >> 1) - 3, 0, makecol(255, 0, 255));
91 floodfill((*map)->colortile, 1, 1, makecol(255, 0, 255));
92
93 line((*map)->colortile, (TILEW >> 1) - 3, TILEH - 1, 0, (TILEH >> 1) + 1, makecol(255, 0, 255));
94 floodfill((*map)->colortile, 1, TILEH - 2, makecol(255, 0, 255));
95
96 line((*map)->colortile, (TILEW >> 1) + 2, TILEH - 1, TILEW - 1, (TILEH >> 1) + 1, makecol(255, 0, 255));
97 floodfill((*map)->colortile, TILEW - 2, TILEH - 1, makecol(255, 0, 255));
98
99 line((*map)->colortile, (TILEW >> 1) + 2, 0, TILEW - 1, (TILEH >> 1) - 2, makecol(255, 0, 255));
100 floodfill((*map)->colortile, TILEW - 1, 0, makecol(255, 0, 255));
101
102 (*map)->ptanchorX = (*map)->X = 0;
103 (*map)->ptanchorY = (*map)->Y = 0;
104
105 return TRUE;
106} /* create_empty_map( ... ) */
107
117int set_value(MAP* map, int type, int x, int y, int value) {
118 if (!map || x >= map->XSIZE || y >= map->YSIZE)
119 return FALSE;
120
121 if (type == N_TILE) {
122 map->grid[x + y * map->XSIZE].tilenumber = value;
123
124 return TRUE;
125 }
126
127 if (type == N_ABILITY) {
128 map->grid[x + y * map->XSIZE].ability = value;
129
130 return TRUE;
131 }
132
133 if (type == N_MUSIC) {
134 map->grid[x + y * map->XSIZE].music = value;
135
136 return TRUE;
137 }
138
139 if (type == N_OBJECT) {
140 map->grid[x + y * map->XSIZE].objectnumber = value;
141
142 return TRUE;
143 }
144
145 return FALSE;
146} /* set_value( ... ) */
147
156int get_value(MAP* map, int type, int x, int y) {
157 if (!map || x >= map->XSIZE || y >= map->YSIZE)
158 return FALSE;
159
160 if (type == N_TILE)
161 return map->grid[x + y * map->XSIZE].tilenumber;
162
163 if (type == N_OBJECT)
164 return map->grid[x + y * map->XSIZE].objectnumber;
165
166 if (type == N_MUSIC)
167 return map->grid[x + y * map->XSIZE].music;
168
169 if (type == N_ABILITY)
170 return map->grid[x + y * map->XSIZE].ability;
171
172 return FALSE;
173
174} /* get_value( ... ) */
175
185int ScreenToMap(int mx, int my, int* Tilex, int* Tiley, BITMAP* mousemap) {
186 /* SCREEN TO MAP VARIABLES */
187 int RegionX, RegionY,
188 RegionDX = 0, RegionDY = 0,
189 MouseMapX, MouseMapY,
190 MouseMapWidth, MouseMapHeight, c;
191
192 MouseMapWidth = mousemap->w;
193 MouseMapHeight = mousemap->h;
194
195 /* First step find out where we are on the mousemap */
196 RegionX = (mx / MouseMapWidth);
197 RegionY = (my / MouseMapHeight) << 1; /* The multiplying by two is very important */
198
199 /* Second Step: Find out WHERE in the mousemap our mouse is, by finding MouseMapX and MouseMapY. */
200
201 MouseMapX = mx % MouseMapWidth;
202 MouseMapY = my % MouseMapHeight;
203
204 /* third step: Find the color in the mouse map */
205 c = getpixel(mousemap, MouseMapX, MouseMapY);
206
207 if (c == makecol(255, 0, 0)) {
208 RegionDX = -1;
209 RegionDY = -1;
210 }
211
212 if (c == makecol(255, 255, 0)) {
213 RegionDX = 0;
214 RegionDY = -1;
215 }
216
217 if (c == makecol(255, 255, 255)) {
218 RegionDX = 0;
219 RegionDY = 0;
220 }
221
222 if (c == makecol(0, 255, 0)) {
223 RegionDX = -1;
224 RegionDY = 1;
225 }
226
227 if (c == makecol(0, 0, 255)) {
228 RegionDX = 0;
229 RegionDY = 1;
230 }
231
232 *Tilex = (RegionDX + RegionX);
233 *Tiley = (RegionDY + RegionY);
234
235 return TRUE;
236} /* ScreenToMap( ... ) */
237
245int camera_to_scr(MAP** map, int x, int y) {
246 if (x < 0)
247 x = 0;
248
249 if (y < 0)
250 y = 0;
251
252 (*map)->X = x % (*map)->TILEW;
253 (*map)->Y = y % (*map)->TILEH;
254 (*map)->ptanchorY = y / ((*map)->TILEH >> 1);
255 (*map)->ptanchorY = (*map)->ptanchorY >> 1;
256 (*map)->ptanchorY = (*map)->ptanchorY << 1;
257 (*map)->ptanchorX = (x + ((*map)->ptanchorY & 1) * ((*map)->TILEW >> 1)) / (*map)->TILEW;
258
259 return TRUE;
260} /* camera_to_scr( ... )*/
261
271int camera_to_map(MAP** map, int tx, int ty, int x, int y) {
272 (*map)->ptanchorX = tx;
273 (*map)->ptanchorY = ty;
274 (*map)->X = x;
275 (*map)->Y = y;
276
277 return TRUE;
278} /* camera_to_map(...) */
279
289int draw_map(MAP* map, BITMAP* bmp, int destx, int desty, int mode) {
290 int a, b, /* iterators */
291 x, y, /* temp store of coordinate */
292 mvx, mvy, /* precomputed offset */
293 tw, th, /* number of tile for one screen */
294 TW2, TH2; /* size of TILE_W /2 and TILE_H/2 */
295
296 /* computing tw&th */
297 tw = 1 + ((bmp->w - destx) / map->TILEW);
298 th = 3 + ((bmp->h - desty) / (map->TILEH >> 1));
299
300 TW2 = map->TILEW >> 1;
301 TH2 = map->TILEH >> 1;
302
303 mvx = destx - TW2 - map->X;
304 mvy = desty - TH2 - map->Y;
305
306 for (a = 0; a <= tw; a++) {
307 for (b = 0; b <= th; b++) {
308 x = (a * map->TILEW + ((b & 1) * TW2)) - (map->TILEW >> 1) + mvx;
309 y = (b * TH2) + mvy;
310
311 if ((a + map->ptanchorX) < map->XSIZE && (b + map->ptanchorY) < map->YSIZE) {
312 if (map->grid[a + map->ptanchorX + (b + map->ptanchorY) * map->XSIZE].tilenumber == FALSE) {
313 masked_blit(map->colortile, bmp, 0, 0, x, y, map->colortile->w, map->colortile->h);
314
315 }
316
317 else {
318 /* TODO HERE : add sprite blit */
319 triangle(bmp, x, y + (map->TILEH >> 1), x + map->TILEW, y + (map->TILEH >> 1), x + (map->TILEW >> 1), y, makecol(255, 255, 0));
320 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));
321
322 } /* if( map -> grid... ) ) */
323
324 if (mode == 1) {
325 line(bmp, x + (map->TILEW >> 1) - 2, y, x, y + (map->TILEH >> 1) - 1, map->wirecolor);
326 line(bmp, x + (map->TILEW >> 1) - 2, y + map->TILEH - 1, x, y + (map->TILEH >> 1), map->wirecolor);
327 line(bmp, x + (map->TILEW >> 1) + 1, y + map->TILEH - 1, x + map->TILEW - 1, y + (map->TILEH >> 1), map->wirecolor);
328 line(bmp, x + (map->TILEW >> 1) + 1, y, x + map->TILEW - 1, y + (map->TILEH >> 1) - 1, map->wirecolor);
329 }
330
331 /* TODO HERE : check if object,
332 check if it is already in the library
333 if yes refresh its ttl
334 if no add it with the default ttl
335 */
336
337 } /* if( (a + ...) ) */
338
339 } /* for( a... ) */
340 } /* for( b... ) */
341
342 /* TODO HERE:
343
344 sort the object list by X and by Y
345 blit all the sprites of the active list
346 */
347
348 return TRUE;
349
350} /* draw_map( ... ) */
351
358int load_map(MAP** map, char* filename) {
359 PACKFILE* loaded;
360
361 char temp_name[1024];
362 MAP temp_map;
363
364 int it = 0, it1 = 0;
365
366 loaded = pack_fopen(filename, "rb");
367
368 if (map)
369 free_map((*map));
370 if (!(*map))
371 Malloc((*map), MAP, 1);
372
373 /* writting name */
374 it = pack_igetl(loaded);
375
376 pack_fread(temp_name, it, loaded);
377
378 /* writting states */
379 temp_map.XSIZE = pack_igetl(loaded);
380 temp_map.YSIZE = pack_igetl(loaded);
381 temp_map.TILEW = pack_igetl(loaded);
382 temp_map.TILEH = pack_igetl(loaded);
383 temp_map.ptanchorX = pack_igetl(loaded);
384 temp_map.ptanchorY = pack_igetl(loaded);
385 temp_map.X = pack_igetl(loaded);
386 temp_map.Y = pack_igetl(loaded);
387 temp_map.bgcolor = pack_igetl(loaded);
388 temp_map.wirecolor = pack_igetl(loaded);
389
390 create_empty_map((*map), temp_name,
391 temp_map.XSIZE, temp_map.YSIZE,
392 temp_map.TILEW, temp_map.TILEH,
393 2000, 2000, 2000, 2000, 2000);
394 (*map)->XSIZE = temp_map.XSIZE;
395 (*map)->YSIZE = temp_map.YSIZE;
396 (*map)->TILEW = temp_map.TILEW;
397 (*map)->TILEH = temp_map.TILEH;
398 (*map)->ptanchorX = temp_map.ptanchorX;
399 (*map)->ptanchorY = temp_map.ptanchorY;
400 (*map)->X = temp_map.X;
401 (*map)->Y = temp_map.Y;
402 (*map)->bgcolor = temp_map.bgcolor;
403 (*map)->wirecolor = temp_map.wirecolor;
404
405 /* saving the grid */
406 for (it = 0; it < (*map)->XSIZE; it++) {
407 for (it1 = 0; it1 < (*map)->YSIZE; it1++) {
408 (*map)->grid[it + it1 * (*map)->XSIZE].tilenumber = pack_igetl(loaded);
409 (*map)->grid[it + it1 * (*map)->XSIZE].ability = pack_igetl(loaded);
410 (*map)->grid[it + it1 * (*map)->XSIZE].objectnumber = pack_igetl(loaded);
411 (*map)->grid[it + it1 * (*map)->XSIZE].music = pack_igetl(loaded);
412 }
413 }
414
415 /*load_animlib( loaded , & (*map ) -> libtiles );*/
416 /*load_animlib( loaded , & (*map ) -> libanims );*/
417
418 pack_fclose(loaded);
419
420 return TRUE;
421} /* load_map( ... ) */
422
429int save_map(MAP* map, char* filename) {
430 PACKFILE* saved;
431
432 int it = 0, it1 = 0;
433
434 saved = pack_fopen(filename, "wb");
435
436 /* writting name */
437 pack_iputl(ustrsizez(map->name), saved);
438 pack_fwrite(map->name, ustrsizez(map->name), saved);
439
440 /* writting states */
441 pack_iputl(map->XSIZE, saved);
442 pack_iputl(map->YSIZE, saved);
443 pack_iputl(map->TILEW, saved);
444 pack_iputl(map->TILEH, saved);
445 pack_iputl(map->ptanchorX, saved);
446 pack_iputl(map->ptanchorY, saved);
447 pack_iputl(map->X, saved);
448 pack_iputl(map->Y, saved);
449 pack_iputl(map->bgcolor, saved);
450 pack_iputl(map->wirecolor, saved);
451
452 /* saving the grid */
453 for (it = 0; it < map->XSIZE; it++) {
454 for (it1 = 0; it1 < map->YSIZE; it1++) {
455 pack_iputl(map->grid[it + it1 * map->XSIZE].tilenumber, saved);
456 pack_iputl(map->grid[it + it1 * map->XSIZE].ability, saved);
457 pack_iputl(map->grid[it + it1 * map->XSIZE].objectnumber, saved);
458 pack_iputl(map->grid[it + it1 * map->XSIZE].music, saved);
459 }
460 }
461
462 /*save_animlib( saved , map -> libtiles );*/
463 /*save_animlib( saved , map -> libanims );*/
464
465 pack_fclose(saved);
466
467 return TRUE;
468} /* save_map( ... ) */
469
475int free_map(MAP** map) {
476 if (!(*map))
477 return TRUE;
478 if ((*map)->grid) {
479 Free((*map)->grid);
480 }
481 if ((*map)->name) {
482 Free((*map)->name);
483 }
484 if ((*map)->colortile)
485 destroy_bitmap((*map)->colortile);
486 if ((*map)->mousemap)
487 destroy_bitmap((*map)->mousemap);
488
489 Free((*map));
490
491 return TRUE;
492} /* free_map( ... ) */
493
494#endif /* #ifndef NOISOENGINE */
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:187
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:242
int X
X move in pixel for drawing.
int TILEW
size X of tiles of the map (in pixel)
int ability
ability of the tile (walking, swimming, blocking, killing ?)
int tilenumber
ident of the tile in the MAP->tile library
char * name
Name of the map ( used for linking between two map )
int ptanchorY
Y starting cell for drawing.
int wirecolor
color of wire
int ptanchorX
X starting cell for drawing.
BITMAP * colortile
default full colored background tile
int TILEH
size Y of tiles of the map (in pixel)
CELL * grid
Grid of each cell of the map.
int YSIZE
size Y of the grid (nbYcell)
int XSIZE
size X of the grid (nbXcell)
int music
ident of the music on the tile
int bgcolor
color of the bg
int objectnumber
ident of the object in the MAP->object library
int Y
Y move in pixel for drawing.
int save_map(MAP *map, char *filename)
Save the map to filename.
int get_value(MAP *map, int type, int x, int y)
Get a the tilenumber of a cell item.
int load_map(MAP **map, char *filename)
Load the map from filename.
int camera_to_map(MAP **map, int tx, int ty, int x, int y)
Center Map on given map coordinate, with x & y offset.
#define N_OBJECT
FLAG of object type.
int camera_to_scr(MAP **map, int x, int y)
Center Map on given screen coordinate.
int set_value(MAP *map, int type, int x, int y, int value)
Set the value of a cell in a map.
#define N_MUSIC
FLAG of music type.
#define N_TILE
FLAG of tile type.
#define N_ABILITY
FLAG of ability type.
int draw_map(MAP *map, BITMAP *bmp, int destx, int desty, int mode)
Draw a MAP *map on the BITMAP *bmp.
int ScreenToMap(int mx, int my, int *Tilex, int *Tiley, BITMAP *mousemap)
Convert screen coordinate to map coordinate.
int free_map(MAP **map)
Free the map.
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.
Cell of a MAP.
MAP with objects, tiles, skins.
Map loading, saving, with objects, animations, ...