Save as dialog, and confirm save dialog.
[cantaveria.git] / stage.c
blob68bade815a581244a973d9043ee261f292c1450b
1 /*
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
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include <util.h>
39 #include <list.h>
40 #include <loader.h>
41 #include <graphics.h>
43 #include <stage.h>
47 enum tile_shape {
48 SHAPE_FREE = '0', /* 'air' */
49 SHAPE_SQUARE = 'M',
51 SHAPE_TRI_NE = 'a',
52 SHAPE_TRI_NW = 'b',
53 SHAPE_TRI_SE = 'c',
54 SHAPE_TRI_SW = 'd',
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',
66 SHAPE_HALF_CEIL = 'w'
69 typedef struct {
70 unsigned char bg;
71 unsigned char fg;
72 unsigned char shape;
73 } tile;
75 typedef struct stage stage;
76 struct stage {
77 char id[32];
78 int w, h;
79 tile* tiles;
80 int bgimage;
81 int bgtiles;
82 int fgtiles;
83 int ox, oy;
84 stage* next;
87 char zone_name[32] = "NO ZONE";
88 stage* stages = NULL;
89 stage* this_stage = NULL;
91 /***/
95 STAGE MODULE
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){
108 int i;
109 for(i=0; i<n; i++){
110 tiles[i].shape = SHAPE_FREE;
111 tiles[i].fg = 0;
112 tiles[i].bg = 0;
117 static int load_stage(char* gfxpath, char* stagepath){
119 [stage file]
120 width height
121 bgimage
122 dectiles
123 bgtiles
124 fgtiles
125 x y fg bg shape
126 x y fg bg shape
130 reader* f = loader_open(stagepath);
131 char* filename = path_ending(stagepath);
132 char buf[256];
133 char gfx[256];
134 tile* tptr;
135 stage* s = malloc(sizeof(stage));
136 int w = 20;
137 int h = 15;
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);
144 strcat(gfx, buf);
145 s->bgimage = load_bitmap(gfx);
147 loader_scanline(f, "%s", buf);
148 strcpy(gfx, gfxpath);
149 strcat(gfx, buf);
150 s->bgtiles = load_bitmap(gfx);
152 loader_scanline(f, "%s", buf);
153 strcpy(gfx, gfxpath);
154 strcat(gfx, buf);
155 s->fgtiles = load_bitmap(gfx);
157 s->tiles = malloc(w*h*sizeof(tile));
158 initialize_tiles(w*h, s->tiles);
159 s->w = w;
160 s->h = h;
161 s->ox = ox;
162 s->oy = oy;
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);
168 tptr->fg = fg;
169 tptr->bg = bg;
170 tptr->shape = shape;
173 s->next = stages;
174 stages = s;
176 loader_close(f);
177 return 0;
180 /***/
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
194 list* dirs;
195 list* ptr;
196 char stages[256] = "";
197 char gfxpath[256] = "";
198 char* stagepath;
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);
212 if(dirs == NULL){
213 printf("ERROR cant read dirs\n");
214 return -1;
217 ptr = dirs->next;
218 while(ptr){
219 stagepath = ptr->item;
220 if(load_stage(gfxpath, stagepath) < 0){
221 printf("ERROR cant load stage\n");
222 loader_freedirlist(dirs);
223 return -1;
225 ptr = ptr->next;
228 loader_freedirlist(dirs);
229 return 0;
232 void unload_zone(){
233 stage* ptr = stages;
234 stage* prev;
235 while(ptr){
236 free(ptr->tiles);
237 prev = ptr;
238 ptr = ptr->next;
239 free(prev);
242 strcpy(zone_name, "NO ZONE");
245 void switch_stage(char* id){
246 stage* ptr = stages;
247 while(ptr){
248 if(strcmp(id, ptr->id) == 0){
249 this_stage = ptr;
250 return;
252 ptr = ptr->next;
255 printf("ERROR stage not found\n");
258 void draw_stage_fg(int cx, int cy, int x, int y, int w, int h){
259 //draw background
260 /* draw background on tiles where at least
261 the fg or bg tile is partial*/
262 //draw bg tiles
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){
267 //draw water
268 /* calculate water surfaces, draw them */
269 //fg tiles
270 /* draw all fg visible fg tiles */
271 //draw decorations
272 /* draw all decorations */
275 int stage_xcollide(int x, int y, int w, int h, int v, int* xx){
276 return 0;
279 int stage_ycollide(int x, int y, int w, int h, int v, int* yy){
280 return 0;
283 void stage_init(){
284 /* does nothing */
291 /*** debug ***/
293 void stage_debug(){
294 stage* ptr = stages;
295 int i, j;
296 char c;
298 struct stage {
299 char id[32];
300 int w, h;
301 tile* tiles;
302 int bgimage;
303 int bgtiles;
304 int fgtiles;
305 stage* next;
306 };*/
307 printf("stage debug:\n\n");
308 printf("zone: %s\n\n", zone_name);
310 while(ptr){
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);
317 printf("tiles:\n");
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;
322 printf("%c", c);
324 printf("\n");
328 printf("\n");
329 ptr = ptr->next;