2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 2010 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
25 stage module is responsible for modelling the static world.
26 it has three main functions
27 * load zone files into world
28 * draw world background and foreground layers
29 * calculate if a moving rectangle will collide with world
48 SHAPE_FREE
= '0', /* 'air' */
56 SHAPE_LTRAP_FLOOR
= '1', /* trapezoid */
57 SHAPE_RTRAP_FLOOR
= '2',
58 SHAPE_LSLOPE_FLOOR
= '3',
59 SHAPE_RSLOPE_FLOOR
= '4',
60 SHAPE_LTRAP_CEIL
= '5',
61 SHAPE_RTRAP_CEIL
= '6',
62 SHAPE_LSLOPE_CEIL
= '7',
63 SHAPE_RSLOPE_CEIL
= '8',
65 SHAPE_HALF_FLOOR
= 'm',
75 typedef struct stage stage
;
87 char zone_name
[32] = "NO ZONE";
89 stage
* this_stage
= NULL
;
97 DEADLINES AND MILESTONES
99 load zones / stages from new format - May 15
100 display loaded stages - May 22
101 collision algorithm - May 29
107 static void initialize_tiles(int n
, tile
* tiles
){
110 tiles
[i
].shape
= SHAPE_FREE
;
117 static int load_stage(char* gfxpath
, char* stagepath
){
130 reader
* f
= loader_open(stagepath
);
131 char* filename
= path_ending(stagepath
);
135 stage
* s
= malloc(sizeof(stage
));
138 int x
, y
, ox
, oy
, fg
, bg
, shape
;
140 loader_scanline(f
, "%d %d %d %d", &w
, &h
, &ox
, &oy
);
142 loader_scanline(f
, "%s", buf
);
143 strcpy(gfx
, gfxpath
);
145 s
->bgimage
= load_bitmap(gfx
);
147 loader_scanline(f
, "%s", buf
);
148 strcpy(gfx
, gfxpath
);
150 s
->bgtiles
= load_bitmap(gfx
);
152 loader_scanline(f
, "%s", buf
);
153 strcpy(gfx
, gfxpath
);
155 s
->fgtiles
= load_bitmap(gfx
);
157 s
->tiles
= malloc(w
*h
*sizeof(tile
));
158 initialize_tiles(w
*h
, s
->tiles
);
163 strcpy(s
->id
, filename
);
165 while(!loader_feof(f
)){
166 loader_scanline(f
, "%d %d %d %d %c", &x
, &y
, &fg
, &bg
, &shape
);
167 tptr
= s
->tiles
+ x
+ (s
->w
* y
);
183 int load_zone(char* name
){
185 loading a zone from a file
186 a zone consists of one or more stages
187 each stage has its own tileset graphics and background
188 it also has a large array of tiles
190 the stage files are in zones/zonename/
191 the name of the file will be used as the id
196 char stages
[256] = "";
197 char gfxpath
[256] = "";
200 strcat(stages
, "zones/");
201 strcat(stages
, name
);
202 strcat(stages
, "/stages/");
204 strcat(gfxpath
, "zones/");
205 strcat(gfxpath
, name
);
206 strcat(gfxpath
, "/gfx/");
208 strcpy(zone_name
, name
);
209 zone_name
[31] = '\0';
211 dirs
= loader_readdir(stages
);
213 printf("ERROR cant read dirs\n");
219 stagepath
= ptr
->item
;
220 if(load_stage(gfxpath
, stagepath
) < 0){
221 printf("ERROR cant load stage\n");
222 loader_freedirlist(dirs
);
228 loader_freedirlist(dirs
);
242 strcpy(zone_name
, "NO ZONE");
245 void switch_stage(char* id
){
248 if(strcmp(id
, ptr
->id
) == 0){
255 printf("ERROR stage not found\n");
258 void draw_stage_fg(int cx
, int cy
, int x
, int y
, int w
, int h
){
260 /* draw background on tiles where at least
261 the fg or bg tile is partial*/
263 /* draw bg tile where fg is partial */
266 void draw_stage_bg(int cx
, int cy
, int x
, int y
, int w
, int h
){
268 /* calculate water surfaces, draw them */
270 /* draw all fg visible fg tiles */
272 /* draw all decorations */
275 int stage_xcollide(int x
, int y
, int w
, int h
, int v
, int* xx
){
279 int stage_ycollide(int x
, int y
, int w
, int h
, int v
, int* yy
){
307 printf("stage debug:\n\n");
308 printf("zone: %s\n\n", zone_name
);
311 printf("stage: %s\n", ptr
->id
);
312 printf("size: %dx%d\n", ptr
->w
, ptr
->h
);
313 printf("origin: (%d, %d)\n", ptr
->ox
, ptr
->oy
);
314 printf("bgimage: %d\n", ptr
->bgimage
);
315 printf("bgtiles: %d\n", ptr
->bgtiles
);
316 printf("fgtiles: %d\n", ptr
->fgtiles
);
319 for(j
=0; j
<ptr
->h
; j
++){
320 for(i
=0; i
<ptr
->w
; i
++){
321 c
= (ptr
->tiles
+ i
+ ptr
->w
*j
)->shape
;