2 // C Implementation: level
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2003
9 // Copyright: See COPYING file that comes with this distribution
23 #include "lispreader.h"
27 texture_type img_bkgd, img_bkgd_tile[2][4], img_solid[4], img_brick[2];
29 st_subset::st_subset()
34 void st_subset::create(const std::string& subset_name)
38 new_subset.name = subset_name;
39 new_subset.title = "Unknown Title";
40 new_subset.description = "No description so far.";
42 level_default(&new_lev);
43 level_save(&new_lev,subset_name.c_str(),1);
46 void st_subset::parse (lisp_object_t* cursor)
48 while(!lisp_nil_p(cursor))
50 lisp_object_t* cur = lisp_car(cursor);
53 if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
59 if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
61 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
66 else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
68 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
74 cursor = lisp_cdr (cursor);
78 void st_subset::load(char *subset)
84 lisp_object_t* root_obj = 0;
88 snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
89 if(!faccessible(filename))
90 snprintf(filename, 1024, "%s/levels/%s/info", DATA_PREFIX, subset);
91 if(faccessible(filename))
93 fi = fopen(filename, "r");
99 lisp_stream_init_file (&stream, fi);
100 root_obj = lisp_read (&stream);
102 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
104 printf("World: Parse Error in file %s", filename);
107 lisp_object_t* cur = lisp_car(root_obj);
109 if (!lisp_symbol_p (cur))
111 printf("World: Read error in %s",filename);
114 if (strcmp(lisp_symbol(cur), "level-subset") == 0)
116 parse(lisp_cdr(root_obj));
122 snprintf(str, 1024, "%s.png", filename);
125 texture_load(&image,str,IGNORE_ALPHA);
129 snprintf(filename, 1024, "%s/images/status/level-subset-info.png", DATA_PREFIX);
130 texture_load(&image,filename,IGNORE_ALPHA);
134 for(i=1; i != -1; ++i)
136 /* Get the number of levels in this subset */
137 snprintf(filename, 1024, "%s/levels/%s/level%d.dat", st_dir, subset,i);
138 if(!faccessible(filename))
140 snprintf(filename, 1024, "%s/levels/%s/level%d.dat", DATA_PREFIX, subset,i);
141 if(!faccessible(filename))
148 void st_subset::save()
153 /* Save data file: */
154 filename = "/levels/" + name + "/";
156 fcreatedir(filename.c_str());
157 filename = string(st_dir) + "/levels/" + name + "/info";
158 if(!fwriteable(filename.c_str()))
159 filename = string(DATA_PREFIX) + "/levels/" + name + "/info";
160 if(fwriteable(filename.c_str()))
162 fi = fopen(filename.c_str(), "w");
165 perror(filename.c_str());
169 fprintf(fi,";SuperTux-Level-Subset\n");
170 fprintf(fi,"(level-subset\n");
172 /* Save title info: */
173 fprintf(fi," (title \"%s\")\n", title.c_str());
175 /* Save the description: */
176 fprintf(fi," (description \"%s\")\n", description.c_str());
184 void st_subset::free()
189 texture_free(&image);
193 void level_default(st_level* plevel)
196 plevel->name = "UnNamed";
197 plevel->theme = "antarctica";
198 plevel->song_title = "Mortimers_chipdisko.mod";
199 plevel->bkgd_image = "arctis.png";
201 plevel->time_left = 100;
202 plevel->gravity = 10.;
203 plevel->bkgd_red = 0;
204 plevel->bkgd_green = 0;
205 plevel->bkgd_blue = 0;
207 for(i = 0; i < 15; ++i)
209 plevel->tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
210 plevel->tiles[i][plevel->width] = (unsigned int) '\0';
211 for(y = 0; y < plevel->width; ++y)
212 plevel->tiles[i][y] = (unsigned int) '.';
213 plevel->tiles[i][plevel->width] = (unsigned int) '\0';
217 /* Load data for this level: */
218 /* Returns -1, if the loading of the level failed. */
219 int level_load(st_level* plevel,const char *subset, int level)
223 /* Load data file: */
225 snprintf(filename, 1024, "%s/levels/%s/level%d.dat", st_dir, subset, level);
226 if(!faccessible(filename))
227 snprintf(filename, 1024, "%s/levels/%s/level%d.dat", DATA_PREFIX, subset, level);
229 return level_load(plevel, filename);
232 int level_load(st_level* plevel, const char* filename)
236 lisp_object_t* root_obj = 0;
237 fi = fopen(filename, "r");
244 lisp_stream_t stream;
245 lisp_stream_init_file (&stream, fi);
246 root_obj = lisp_read (&stream);
248 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
250 printf("World: Parse Error in file %s", filename);
256 if (strcmp(lisp_symbol(lisp_car(root_obj)), "level") == 0)
258 LispReader reader(lisp_cdr(root_obj));
261 reader.read_int("width", &plevel->width);
262 reader.read_int("time", &plevel->time_left);
263 reader.read_int("bkgd_red", &plevel->bkgd_red);
264 reader.read_int("bkgd_green", &plevel->bkgd_green);
265 reader.read_int("bkgd_blue", &plevel->bkgd_blue);
266 reader.read_float("gravity", &plevel->gravity);
267 reader.read_string("name", &plevel->name);
268 reader.read_string("theme", &plevel->theme);
269 reader.read_string("music", &plevel->song_title);
270 reader.read_string("background", &plevel->bkgd_image);
271 reader.read_int_vector("tilemap", &vi);
277 for( i = 0; i < 15; ++i)
278 plevel->tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
282 for(vector<int>::iterator it = vi.begin(); it != vi.end(); ++it, ++i)
285 plevel->tiles[j][i] = (*it);
286 if(i == plevel->width)
294 /* Set the global gravity to the latest loaded level's gravity */
295 gravity = plevel->gravity;
297 /* Mark the end position of this level! - Is a bit wrong here thought */
299 for (y = 0; y < 15; ++y)
301 for (x = 0; x < plevel->width; ++x)
303 if(plevel->tiles[y][x] == '|')
315 /* Save data for level: */
317 void level_save(st_level* plevel,const char * subset, int level)
324 /* Save data file: */
325 sprintf(str, "/levels/%s/", subset);
327 snprintf(filename, 1024, "%s/levels/%s/level%d.dat", st_dir, subset, level);
328 if(!fwriteable(filename))
329 snprintf(filename, 1024, "%s/levels/%s/level%d.dat", DATA_PREFIX, subset, level);
331 fi = fopen(filename, "w");
341 fprintf(fi,";SuperTux-Level\n");
342 fprintf(fi,"(level\n");
344 fprintf(fi," (name \"%s\")\n", plevel->name.c_str());
345 fprintf(fi," (theme \"%s\")\n", plevel->theme.c_str());
346 fprintf(fi," (music \"%s\")\n", plevel->song_title.c_str());
347 fprintf(fi," (background \"%s\")\n", plevel->bkgd_image.c_str());
348 fprintf(fi," (bkgd_red %d)\n", plevel->bkgd_red);
349 fprintf(fi," (bkgd_green %d)\n", plevel->bkgd_green);
350 fprintf(fi," (bkgd_blue %d)\n", plevel->bkgd_blue);
351 fprintf(fi," (time %d)\n", plevel->time_left);
352 fprintf(fi," (width %d)\n", plevel->width);
353 fprintf(fi," (gravity %2.1f)\n", plevel->gravity);
354 fprintf(fi," (tilemap ");
356 for(y = 0; y < 15; ++y)
358 for(i = 0; i < plevel->width; ++i)
359 fprintf(fi," %d ", plevel->tiles[y][i]);
369 /* Unload data for this level: */
371 void level_free(st_level* plevel)
374 for(i=0; i < 15; ++i)
375 free(plevel->tiles[i]);
377 plevel->name.clear();
378 plevel->theme.clear();
379 plevel->song_title.clear();
380 plevel->bkgd_image.clear();
385 void level_load_gfx(st_level *plevel)
387 level_load_image(&img_brick[0],plevel->theme,"brick0.png", IGNORE_ALPHA);
388 level_load_image(&img_brick[1],plevel->theme,"brick1.png", IGNORE_ALPHA);
390 level_load_image(&img_solid[0],plevel->theme,"solid0.png", USE_ALPHA);
391 level_load_image(&img_solid[1],plevel->theme,"solid1.png", USE_ALPHA);
392 level_load_image(&img_solid[2],plevel->theme,"solid2.png", USE_ALPHA);
393 level_load_image(&img_solid[3],plevel->theme,"solid3.png", USE_ALPHA);
395 level_load_image(&img_bkgd_tile[0][0],plevel->theme,"bkgd-00.png", USE_ALPHA);
396 level_load_image(&img_bkgd_tile[0][1],plevel->theme,"bkgd-01.png", USE_ALPHA);
397 level_load_image(&img_bkgd_tile[0][2],plevel->theme,"bkgd-02.png", USE_ALPHA);
398 level_load_image(&img_bkgd_tile[0][3],plevel->theme,"bkgd-03.png", USE_ALPHA);
400 level_load_image(&img_bkgd_tile[1][0],plevel->theme,"bkgd-10.png", USE_ALPHA);
401 level_load_image(&img_bkgd_tile[1][1],plevel->theme,"bkgd-11.png", USE_ALPHA);
402 level_load_image(&img_bkgd_tile[1][2],plevel->theme,"bkgd-12.png", USE_ALPHA);
403 level_load_image(&img_bkgd_tile[1][3],plevel->theme,"bkgd-13.png", USE_ALPHA);
405 if(!plevel->bkgd_image.empty())
408 snprintf(fname, 1024, "%s/background/%s", st_dir, plevel->bkgd_image.c_str());
409 if(!faccessible(fname))
410 snprintf(fname, 1024, "%s/images/background/%s", DATA_PREFIX, plevel->bkgd_image.c_str());
411 texture_load(&img_bkgd, fname, IGNORE_ALPHA);
415 /* Quick hack to make sure an image is loaded, when we are freeing it afterwards. */#
416 level_load_image(&img_bkgd, plevel->theme,"solid0.png", IGNORE_ALPHA);
420 /* Free graphics data for this level: */
422 void level_free_gfx(void)
426 for (i = 0; i < 2; i++)
428 texture_free(&img_brick[i]);
430 for (i = 0; i < 4; i++)
432 texture_free(&img_solid[i]);
433 texture_free(&img_bkgd_tile[0][i]);
434 texture_free(&img_bkgd_tile[1][i]);
437 texture_free(&img_bkgd);
440 /* Load a level-specific graphic... */
442 void level_load_image(texture_type* ptexture, string theme,const char * file, int use_alpha)
446 snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
447 if(!faccessible(fname))
448 snprintf(fname, 1024, "%s/images/themes/%s/%s", DATA_PREFIX, theme.c_str(), file);
450 texture_load(ptexture, fname, use_alpha);
453 /* Edit a piece of the map! */
455 void level_change(st_level* plevel, float x, float y, unsigned char c)
462 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= plevel->width)
463 plevel->tiles[yy][xx] = c;
466 /* Free music data for this level: */
468 void level_free_song(void)
470 free_music(level_song);
471 free_music(level_song_fast);
476 void level_load_song(st_level* plevel)
480 char * song_subtitle;
482 song_path = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) +
483 strlen(plevel->song_title.c_str()) + 8));
484 sprintf(song_path, "%s/music/%s", DATA_PREFIX, plevel->song_title.c_str());
485 level_song = load_song(song_path);
489 song_path = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) +
490 strlen(plevel->song_title.c_str()) + 8 + 5));
491 song_subtitle = strdup(plevel->song_title.c_str());
492 strcpy(strstr(song_subtitle, "."), "\0");
493 sprintf(song_path, "%s/music/%s-fast%s", DATA_PREFIX, song_subtitle, strstr(plevel->song_title.c_str(), "."));
494 level_song_fast = load_song(song_path);