applied leveleditor and multiline-drawtext patches from Ricardo Cruz <rick2@aeiou...
[supertux.git] / src / level.c
1 //
2 // C Implementation: level
3 //
4 // Description: 
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2003
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include "setup.h"
17 #include "screen.h"
18 #include "level.h"
19
20 /* Load data for this level: */
21
22 void loadlevel(st_level* plevel, char *subset, int level)
23 {
24   int y;
25   FILE * fi;
26   char str[80];
27   char * filename;
28   char * line;
29
30   /* Load data file: */
31
32   filename = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) + 20) + strlen(subset));
33   sprintf(filename, "%s/levels/%s/level%d.dat", DATA_PREFIX, subset, level);
34   fi = fopen(filename, "r");
35   if (fi == NULL)
36     {
37       perror(filename);
38       st_shutdown();
39       free(filename);
40       exit(-1);
41     }
42   free(filename);
43
44
45   /* Load header info: */
46
47
48   /* (Level title) */
49   fgets(str, 20, fi);
50   strcpy(plevel->name, str);
51   plevel->name[strlen(plevel->name)-1] = '\0';
52
53   /* (Level theme) */
54   fgets(str, 20, fi);
55   strcpy(plevel->theme, str);
56   plevel->theme[strlen(plevel->theme)-1] = '\0';
57
58
59
60   /* (Time to beat level) */
61   fgets(str, 10, fi);
62   plevel->time_left = atoi(str);
63
64   /* (Song file for this level) */
65   fgets(str, sizeof(plevel->song_title), fi);
66   strcpy(plevel->song_title, str);
67   plevel->song_title[strlen(plevel->song_title)-1] = '\0';
68
69
70
71   /* (Level background color) */
72   fgets(str, 10, fi);
73   plevel->bkgd_red = atoi(str);
74   fgets(str, 10, fi);
75   plevel->bkgd_green= atoi(str);
76   fgets(str, 10, fi);
77   plevel->bkgd_blue = atoi(str);
78
79   /* (Level width) */
80   fgets(str, 10, fi);
81   plevel->width = atoi(str);
82
83
84   /* Allocate some space for the line-reading! */
85
86   line = (char *) malloc(sizeof(char) * (plevel->width + 5));
87   if (line == NULL)
88     {
89       fprintf(stderr, "Couldn't allocate space to load level data!");
90       exit(1);
91     }
92
93
94   /* Load the level lines: */
95
96   for (y = 0; y < 15; y++)
97     {
98       if(fgets(line, plevel->width + 5, fi) == NULL)
99         {
100           fprintf(stderr, "Level %s isn't complete!\n",plevel->name);
101           exit(1);
102         }
103       line[strlen(line) - 1] = '\0';
104       plevel->tiles[y] = strdup(line);
105     }
106
107   fclose(fi);
108   
109 }
110
111 /* Load a level-specific graphic... */
112
113 SDL_Surface * load_level_image(char* theme, char * file, int use_alpha)
114 {
115   char fname[2024];
116
117   snprintf(fname, 21024, "%simages/themes/%s/%s", DATA_PREFIX, theme, file);
118   
119   return(load_image(fname, use_alpha));
120 }