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
50 SHAPE_FREE
= '0', /* 'air' */
58 SHAPE_LTRAP_FLOOR
= '1', /* trapezoid */
59 SHAPE_RTRAP_FLOOR
= '2',
60 SHAPE_LSLOPE_FLOOR
= '3',
61 SHAPE_RSLOPE_FLOOR
= '4',
62 SHAPE_LTRAP_CEIL
= '5',
63 SHAPE_RTRAP_CEIL
= '6',
64 SHAPE_LSLOPE_CEIL
= '7',
65 SHAPE_RSLOPE_CEIL
= '8',
67 SHAPE_HALF_FLOOR
= 'm',
77 typedef struct stage stage
;
89 char zone_name
[256] = "NO ZONE";
91 stage
* this_stage
= NULL
;
99 DEADLINES AND MILESTONES
101 load zones / stages from new format - May 15
102 display loaded stages - May 22
103 collision algorithm - May 29
109 static void initialize_tiles(int n
, tile
* tiles
){
112 tiles
[i
].shape
= SHAPE_FREE
;
119 static int load_stage(char* gfxpath
, char* stagepath
){
132 reader
* f
= loader_open(stagepath
);
133 char* filename
= path_ending(stagepath
);
137 stage
* s
= malloc(sizeof(stage
));
140 int x
, y
, ox
, oy
, fg
, bg
, shape
;
142 loader_scanline(f
, "%d %d %d %d", &w
, &h
, &ox
, &oy
);
144 loader_scanline(f
, "%s", buf
);
145 strcpy(gfx
, gfxpath
);
147 s
->bgimage
= load_bitmap(gfx
);
149 loader_scanline(f
, "%s", buf
);
150 strcpy(gfx
, gfxpath
);
152 s
->bgtiles
= load_bitmap(gfx
);
154 loader_scanline(f
, "%s", buf
);
155 strcpy(gfx
, gfxpath
);
157 s
->fgtiles
= load_bitmap(gfx
);
159 s
->tiles
= malloc(w
*h
*sizeof(tile
));
160 initialize_tiles(w
*h
, s
->tiles
);
165 strncpy(s
->id
, filename
, 256);
168 while(!loader_feof(f
)){
169 loader_scanline(f
, "%d %d %d %d %c", &x
, &y
, &fg
, &bg
, &shape
);
170 tptr
= s
->tiles
+ (x
+ox
) + (s
->w
* (y
+oy
));
186 int load_zone(string name
){
188 loading a zone from a file
189 a zone consists of one or more stages
190 each stage has its own tileset graphics and background
191 it also has a large array of tiles
193 the stage files are in zones/zonename/
194 the name of the file will be used as the id
199 char stagesdir
[256] = "";
200 char gfxpath
[256] = "";
203 strcat(stagesdir
, "zones/");
204 strcat(stagesdir
, name
);
205 strcat(stagesdir
, "/stages/");
207 strcat(gfxpath
, "zones/");
208 strcat(gfxpath
, name
);
209 strcat(gfxpath
, "/gfx/");
211 strncpy(zone_name
, name
, 256);
212 zone_name
[255] = '\0';
214 dirs
= loader_readdir((string
)stagesdir
);
216 printf("ERROR cant read dirs\n");
222 stagepath
= ptr
->item
;
223 if(load_stage(gfxpath
, stagepath
) < 0){
224 printf("ERROR cant load stage\n");
225 loader_freedirlist(dirs
);
231 loader_freedirlist(dirs
);
245 strcpy(zone_name
, "NO ZONE");
248 void switch_stage(string id
){
251 if(strcmp(id
, ptr
->id
) == 0){
258 printf("ERROR stage not found\n");
262 static void draw_tiles(int gfx
, char layer
, int cx
, int cy
){
263 tile
* tbase
= this_stage
->tiles
;
265 int tw
= this_stage
->w
;
266 int th
= this_stage
->h
;
280 screen_dimensions(&W
, &H
);
282 extra_x
= (W
- 320)/16/2 + 2;
285 extra_y
= (H
- 240)/16/2 + 2;
293 if(io
> tw
-1) return;
294 if(jo
> th
-1) return;
301 for(j
=jo
; j
<j_
; j
++){
302 for(i
=io
; i
<i_
; i
++){
303 t
= tbase
+ i
+ tw
*j
;
304 if(layer
=='b') id
= t
->bg
;
305 else if(layer
=='f') id
= t
->fg
;
309 draw_gfx(gfx
, i
*16-cx
, j
*16-cy
, gx
, gy
, 16, 16);
315 cx, cy is the camera position
316 x, y i think is the offset from 0,0 you should shift
317 w, h i think is the width and height of the screen
319 void stage_draw_bg(int cx
, int cy
){
321 int bw
, bh
, screen_w
, screen_h
;
326 if(this_stage
== NULL
) return;
328 /* draw background vertically centered
329 tile horizontally at fraction of camera x */
330 bgimage
= this_stage
->bgimage
;
331 gfx_dimensions(bgimage
, &bw
, &bh
);
332 screen_dimensions(&screen_w
, &screen_h
);
333 bgshift
= cx
/parallax
/bw
;
334 for(i
=-1; i
<(screen_w
/bw
)+2; i
++){
337 (i
+bgshift
)*bw
-cx
/parallax
,
343 draw_tiles(this_stage
->bgtiles
, 'b', cx
, cy
);
346 void stage_draw_fg(int cx
, int cy
){
347 draw_tiles(this_stage
->bgtiles
, 'f', cx
, cy
);
352 int stage_xcollide(int x
, int y
, int w
, int h
, int v
, int* xx
){
356 int stage_ycollide(int x
, int y
, int w
, int h
, int v
, int* yy
){
384 printf("stage debug:\n\n");
385 printf("zone: %s\n\n", zone_name
);
388 printf("stage: %s\n", ptr
->id
);
389 printf("size: %dx%d\n", ptr
->w
, ptr
->h
);
390 printf("origin: (%d, %d)\n", ptr
->ox
, ptr
->oy
);
391 printf("bgimage: %d\n", ptr
->bgimage
);
392 printf("bgtiles: %d\n", ptr
->bgtiles
);
393 printf("fgtiles: %d\n", ptr
->fgtiles
);
396 for(j
=0; j
<ptr
->h
; j
++){
397 for(i
=0; i
<ptr
->w
; i
++){
398 c
= (ptr
->tiles
+ i
+ ptr
->w
*j
)->fg
;
400 if(isprint(c)) printf("%d", c);