Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_iso_engine.c
Go to the documentation of this file.
1
10
11#ifndef NOISOENGINE
12
28int 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) {
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
118int set_value(MAP* map, int type, int x, int y, int value) {
119 if (!map || x >= map->XSIZE || y >= map->YSIZE)
120 return FALSE;
121
122 if (type == N_TILE) {
123 map->grid[x + y * map->XSIZE].tilenumber = value;
124
125 return TRUE;
126 }
127
128 if (type == N_ABILITY) {
129 map->grid[x + y * map->XSIZE].ability = value;
130
131 return TRUE;
132 }
133
134 if (type == N_MUSIC) {
135 map->grid[x + y * map->XSIZE].music = value;
136
137 return TRUE;
138 }
139
140 if (type == N_OBJECT) {
141 map->grid[x + y * map->XSIZE].objectnumber = value;
142
143 return TRUE;
144 }
145
146 return FALSE;
147} /* set_value( ... ) */
148
157int get_value(MAP* map, int type, int x, int y) {
158 if (!map || x >= map->XSIZE || y >= map->YSIZE)
159 return FALSE;
160
161 if (type == N_TILE)
162 return map->grid[x + y * map->XSIZE].tilenumber;
163
164 if (type == N_OBJECT)
165 return map->grid[x + y * map->XSIZE].objectnumber;
166
167 if (type == N_MUSIC)
168 return map->grid[x + y * map->XSIZE].music;
169
170 if (type == N_ABILITY)
171 return map->grid[x + y * map->XSIZE].ability;
172
173 return FALSE;
174
175} /* get_value( ... ) */
176
186int ScreenToMap(int mx, int my, int* Tilex, int* Tiley, BITMAP* mousemap) {
187 /* SCREEN TO MAP VARIABLES */
188 int RegionX, RegionY,
189 RegionDX = 0, RegionDY = 0,
190 MouseMapX, MouseMapY,
191 MouseMapWidth, MouseMapHeight, c;
192
193 MouseMapWidth = mousemap->w;
194 MouseMapHeight = mousemap->h;
195
196 /* First step find out where we are on the mousemap */
197 RegionX = (mx / MouseMapWidth);
198 RegionY = (my / MouseMapHeight) << 1; /* The multiplying by two is very important */
199
200 /* Second Step: Find out WHERE in the mousemap our mouse is, by finding MouseMapX and MouseMapY. */
201
202 MouseMapX = mx % MouseMapWidth;
203 MouseMapY = my % MouseMapHeight;
204
205 /* third step: Find the color in the mouse map */
206 c = getpixel(mousemap, MouseMapX, MouseMapY);
207
208 if (c == makecol(255, 0, 0)) {
209 RegionDX = -1;
210 RegionDY = -1;
211 }
212
213 if (c == makecol(255, 255, 0)) {
214 RegionDX = 0;
215 RegionDY = -1;
216 }
217
218 if (c == makecol(255, 255, 255)) {
219 RegionDX = 0;
220 RegionDY = 0;
221 }
222
223 if (c == makecol(0, 255, 0)) {
224 RegionDX = -1;
225 RegionDY = 1;
226 }
227
228 if (c == makecol(0, 0, 255)) {
229 RegionDX = 0;
230 RegionDY = 1;
231 }
232
233 *Tilex = (RegionDX + RegionX);
234 *Tiley = (RegionDY + RegionY);
235
236 return TRUE;
237} /* ScreenToMap( ... ) */
238
246int camera_to_scr(MAP** map, int x, int y) {
247 if (x < 0)
248 x = 0;
249
250 if (y < 0)
251 y = 0;
252
253 (*map)->X = x % (*map)->TILEW;
254 (*map)->Y = y % (*map)->TILEH;
255 (*map)->ptanchorY = y / ((*map)->TILEH >> 1);
256 (*map)->ptanchorY = (*map)->ptanchorY >> 1;
257 (*map)->ptanchorY = (*map)->ptanchorY << 1;
258 (*map)->ptanchorX = (x + ((*map)->ptanchorY & 1) * ((*map)->TILEW >> 1)) / (*map)->TILEW;
259
260 return TRUE;
261} /* camera_to_scr( ... )*/
262
272int camera_to_map(MAP** map, int tx, int ty, int x, int y) {
273 (*map)->ptanchorX = tx;
274 (*map)->ptanchorY = ty;
275 (*map)->X = x;
276 (*map)->Y = y;
277
278 return TRUE;
279} /* camera_to_map(...) */
280
290int draw_map(MAP* map, BITMAP* bmp, int destx, int desty, int mode) {
291 int a, b, /* iterators */
292 x, y, /* temp store of coordinate */
293 mvx, mvy, /* precomputed offset */
294 tw, th, /* number of tile for one screen */
295 TW2, TH2; /* size of TILE_W /2 and TILE_H/2 */
296
297 /* computing tw&th */
298 tw = 1 + ((bmp->w - destx) / map->TILEW);
299 th = 3 + ((bmp->h - desty) / (map->TILEH >> 1));
300
301 TW2 = map->TILEW >> 1;
302 TH2 = map->TILEH >> 1;
303
304 mvx = destx - TW2 - map->X;
305 mvy = desty - TH2 - map->Y;
306
307 for (a = 0; a <= tw; a++) {
308 for (b = 0; b <= th; b++) {
309 x = (a * map->TILEW + ((b & 1) * TW2)) - (map->TILEW >> 1) + mvx;
310 y = (b * TH2) + mvy;
311
312 if ((a + map->ptanchorX) < map->XSIZE && (b + map->ptanchorY) < map->YSIZE) {
313 if (map->grid[a + map->ptanchorX + (b + map->ptanchorY) * map->XSIZE].tilenumber == FALSE) {
314 masked_blit(map->colortile, bmp, 0, 0, x, y, map->colortile->w, map->colortile->h);
315
316 }
317
318 else {
319 /* TODO HERE : add sprite blit */
320 triangle(bmp, x, y + (map->TILEH >> 1), x + map->TILEW, y + (map->TILEH >> 1), x + (map->TILEW >> 1), y, makecol(255, 255, 0));
321 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));
322
323 } /* if( map -> grid... ) ) */
324
325 if (mode == 1) {
326 line(bmp, x + (map->TILEW >> 1) - 2, y, x, y + (map->TILEH >> 1) - 1, map->wirecolor);
327 line(bmp, x + (map->TILEW >> 1) - 2, y + map->TILEH - 1, x, y + (map->TILEH >> 1), map->wirecolor);
328 line(bmp, x + (map->TILEW >> 1) + 1, y + map->TILEH - 1, x + map->TILEW - 1, y + (map->TILEH >> 1), map->wirecolor);
329 line(bmp, x + (map->TILEW >> 1) + 1, y, x + map->TILEW - 1, y + (map->TILEH >> 1) - 1, map->wirecolor);
330 }
331
332 /* TODO HERE : check if object,
333 check if it is already in the library
334 if yes refresh its ttl
335 if no add it with the default ttl
336 */
337
338 } /* if( (a + ...) ) */
339
340 } /* for( a... ) */
341 } /* for( b... ) */
342
343 /* TODO HERE:
344
345 sort the object list by X and by Y
346 blit all the sprites of the active list
347 */
348
349 return TRUE;
350
351} /* draw_map( ... ) */
352
359int load_map(MAP** map, char* filename) {
360 PACKFILE* loaded;
361
362 char temp_name[1024];
363 MAP temp_map;
364
365 int it = 0, it1 = 0;
366
367 loaded = pack_fopen(filename, "rb");
368
369 if (map)
370 free_map((*map));
371 if (!(*map))
372 Malloc((*map), MAP, 1);
373
374 /* writting name */
375 it = pack_igetl(loaded);
376
377 pack_fread(temp_name, it, loaded);
378
379 /* writting states */
380 temp_map.XSIZE = pack_igetl(loaded);
381 temp_map.YSIZE = pack_igetl(loaded);
382 temp_map.TILEW = pack_igetl(loaded);
383 temp_map.TILEH = pack_igetl(loaded);
384 temp_map.ptanchorX = pack_igetl(loaded);
385 temp_map.ptanchorY = pack_igetl(loaded);
386 temp_map.X = pack_igetl(loaded);
387 temp_map.Y = pack_igetl(loaded);
388 temp_map.bgcolor = pack_igetl(loaded);
389 temp_map.wirecolor = pack_igetl(loaded);
390
391 create_empty_map((*map), temp_name,
392 temp_map.XSIZE, temp_map.YSIZE,
393 temp_map.TILEW, temp_map.TILEH,
394 2000, 2000, 2000, 2000, 2000);
395 (*map)->XSIZE = temp_map.XSIZE;
396 (*map)->YSIZE = temp_map.YSIZE;
397 (*map)->TILEW = temp_map.TILEW;
398 (*map)->TILEH = temp_map.TILEH;
399 (*map)->ptanchorX = temp_map.ptanchorX;
400 (*map)->ptanchorY = temp_map.ptanchorY;
401 (*map)->X = temp_map.X;
402 (*map)->Y = temp_map.Y;
403 (*map)->bgcolor = temp_map.bgcolor;
404 (*map)->wirecolor = temp_map.wirecolor;
405
406 /* saving the grid */
407 for (it = 0; it < (*map)->XSIZE; it++) {
408 for (it1 = 0; it1 < (*map)->YSIZE; it1++) {
409 (*map)->grid[it + it1 * (*map)->XSIZE].tilenumber = pack_igetl(loaded);
410 (*map)->grid[it + it1 * (*map)->XSIZE].ability = pack_igetl(loaded);
411 (*map)->grid[it + it1 * (*map)->XSIZE].objectnumber = pack_igetl(loaded);
412 (*map)->grid[it + it1 * (*map)->XSIZE].music = pack_igetl(loaded);
413 }
414 }
415
416 /*load_animlib( loaded , & (*map ) -> libtiles );*/
417 /*load_animlib( loaded , & (*map ) -> libanims );*/
418
419 pack_fclose(loaded);
420
421 return TRUE;
422} /* load_map( ... ) */
423
430int save_map(MAP* map, char* filename) {
431 PACKFILE* saved;
432
433 int it = 0, it1 = 0;
434
435 saved = pack_fopen(filename, "wb");
436
437 /* writting name */
438 pack_iputl(ustrsizez(map->name), saved);
439 pack_fwrite(map->name, ustrsizez(map->name), saved);
440
441 /* writting states */
442 pack_iputl(map->XSIZE, saved);
443 pack_iputl(map->YSIZE, saved);
444 pack_iputl(map->TILEW, saved);
445 pack_iputl(map->TILEH, saved);
446 pack_iputl(map->ptanchorX, saved);
447 pack_iputl(map->ptanchorY, saved);
448 pack_iputl(map->X, saved);
449 pack_iputl(map->Y, saved);
450 pack_iputl(map->bgcolor, saved);
451 pack_iputl(map->wirecolor, saved);
452
453 /* saving the grid */
454 for (it = 0; it < map->XSIZE; it++) {
455 for (it1 = 0; it1 < map->YSIZE; it1++) {
456 pack_iputl(map->grid[it + it1 * map->XSIZE].tilenumber, saved);
457 pack_iputl(map->grid[it + it1 * map->XSIZE].ability, saved);
458 pack_iputl(map->grid[it + it1 * map->XSIZE].objectnumber, saved);
459 pack_iputl(map->grid[it + it1 * map->XSIZE].music, saved);
460 }
461 }
462
463 /*save_animlib( saved , map -> libtiles );*/
464 /*save_animlib( saved , map -> libanims );*/
465
466 pack_fclose(saved);
467
468 return TRUE;
469} /* save_map( ... ) */
470
476int free_map(MAP** map) {
477 if (!(*map))
478 return TRUE;
479 if ((*map)->grid) {
480 Free((*map)->grid);
481 }
482 if ((*map)->name) {
483 Free((*map)->name);
484 }
485 if ((*map)->colortile)
486 destroy_bitmap((*map)->colortile);
487 if ((*map)->mousemap)
488 destroy_bitmap((*map)->mousemap);
489
490 Free((*map));
491
492 return TRUE;
493} /* free_map( ... ) */
494
495#endif /* #ifndef NOISOENGINE */
int mode
Network for managing conenctions.
Definition ex_network.c:22
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:185
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:240
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, ...