Huge code merge. This reflects the current status of my rewrite/restructuring. A...
[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 "globals.h"
17 #include "setup.h"
18 #include "screen.h"
19 #include "level.h"
20
21 /* Load data for this level: */
22
23 void loadlevel(st_level* plevel, char *subset, int level)
24 {
25   int y;
26   FILE * fi;
27   char str[80];
28   char * filename;
29   char * line;
30
31   /* Load data file: */
32
33   filename = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) + 20) + strlen(subset));
34   sprintf(filename, "%s/levels/%s/level%d.dat", DATA_PREFIX, subset, level);
35   fi = fopen(filename, "r");
36   if (fi == NULL)
37     {
38       perror(filename);
39       st_shutdown();
40       free(filename);
41       exit(-1);
42     }
43   free(filename);
44
45
46   /* Load header info: */
47
48
49   /* (Level title) */
50   fgets(str, 20, fi);
51   strcpy(plevel->name, str);
52   plevel->name[strlen(plevel->name)-1] = '\0';
53
54   /* (Level theme) */
55   fgets(str, 20, fi);
56   strcpy(plevel->theme, str);
57   plevel->theme[strlen(plevel->theme)-1] = '\0';
58
59
60
61   /* (Time to beat level) */
62   fgets(str, 10, fi);
63   plevel->time_left = atoi(str);
64
65   /* (Song file for this level) */
66   fgets(str, sizeof(plevel->song_title), fi);
67   strcpy(plevel->song_title, str);
68   plevel->song_title[strlen(plevel->song_title)-1] = '\0';
69
70
71
72   /* (Level background color) */
73   fgets(str, 10, fi);
74   plevel->bkgd_red = atoi(str);
75   fgets(str, 10, fi);
76   plevel->bkgd_green= atoi(str);
77   fgets(str, 10, fi);
78   plevel->bkgd_blue = atoi(str);
79
80   /* (Level width) */
81   fgets(str, 10, fi);
82   plevel->width = atoi(str);
83
84
85   /* Allocate some space for the line-reading! */
86
87   line = (char *) malloc(sizeof(char) * (plevel->width + 5));
88   if (line == NULL)
89     {
90       fprintf(stderr, "Couldn't allocate space to load level data!");
91       fclose(fi);
92       exit(1);
93     }
94
95
96   /* Load the level lines: */
97
98   for (y = 0; y < 15; y++)
99     {
100       if(fgets(line, plevel->width + 5, fi) == NULL)
101         {
102           fprintf(stderr, "Level %s isn't complete!\n",plevel->name);
103           free(line);
104           fclose(fi);
105           exit(1);
106         }
107       line[strlen(line) - 1] = '\0';
108       plevel->tiles[y] = strdup(line);
109     }
110
111   free(line);
112   fclose(fi);
113
114 }
115
116 /* Load graphics: */
117
118 void loadlevelgfx(st_level *plevel)
119 {
120
121   load_level_image(&img_brick[0],plevel->theme,"brick0.png", IGNORE_ALPHA);
122   load_level_image(&img_brick[1],plevel->theme,"brick1.png", IGNORE_ALPHA);
123
124   load_level_image(&img_solid[0],plevel->theme,"solid0.png", USE_ALPHA);
125   load_level_image(&img_solid[1],plevel->theme,"solid1.png", USE_ALPHA);
126   load_level_image(&img_solid[2],plevel->theme,"solid2.png", USE_ALPHA);
127   load_level_image(&img_solid[3],plevel->theme,"solid3.png", USE_ALPHA);
128    
129   load_level_image(&img_bkgd[0][0],plevel->theme,"bkgd-00.png", USE_ALPHA);
130   load_level_image(&img_bkgd[0][1],plevel->theme,"bkgd-01.png", USE_ALPHA);
131   load_level_image(&img_bkgd[0][2],plevel->theme,"bkgd-02.png", USE_ALPHA);
132   load_level_image(&img_bkgd[0][3],plevel->theme,"bkgd-03.png", USE_ALPHA);
133    
134   load_level_image(&img_bkgd[1][0],plevel->theme,"bkgd-10.png", USE_ALPHA);
135   load_level_image(&img_bkgd[1][1],plevel->theme,"bkgd-11.png", USE_ALPHA);
136   load_level_image(&img_bkgd[1][2],plevel->theme,"bkgd-12.png", USE_ALPHA);
137   load_level_image(&img_bkgd[1][3],plevel->theme,"bkgd-13.png", USE_ALPHA);
138 }
139
140 /* Free graphics data for this level: */
141
142 void unloadlevelgfx(void)
143 {
144   int i;
145
146   for (i = 0; i < 2; i++)
147     {
148       texture_free(&img_brick[i]);
149     }
150   for (i = 0; i < 4; i++)
151     {
152       texture_free(&img_solid[i]);
153       texture_free(&img_bkgd[0][i]);
154       texture_free(&img_bkgd[1][i]);
155     }
156 }
157
158 /* Load a level-specific graphic... */
159
160 void load_level_image(texture_type* ptexture, char* theme, char * file, int use_alpha)
161 {
162   char fname[1024];
163
164   snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme, file);
165   if(!faccessible(fname))
166   snprintf(fname, 1024, "%s/images/themes/%s/%s", DATA_PREFIX, theme, file);
167   
168   texture_load(ptexture, fname, use_alpha);
169 }
170
171 /* Edit a piece of the map! */
172
173 void level_change(st_level* plevel, float x, float y, unsigned char c)
174 {
175   int xx, yy;
176
177   yy = (y / 32);
178   xx = (x / 32);
179
180   if (yy >= 0 && yy < 15 && xx >= 0 && xx <= plevel->width)
181     plevel->tiles[yy][xx] = c;
182 }
183