make it possible to put background images in each layer
[supertux.git] / src / level_subset.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #include <config.h>
22
23 #include <assert.h>
24 #include <unistd.h>
25 #include "app/setup.h"
26 #include "level.h"
27 #include "app/globals.h"
28 #include "video/surface.h"
29 #include "level_subset.h"
30
31 using namespace SuperTux;
32
33 static bool has_suffix(const std::string& data, const std::string& suffix)
34 {
35   if (data.length() >= suffix.length())
36     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
37   else
38     return false;
39 }
40
41 LevelSubset::LevelSubset()
42   : levels(0)
43 {
44 }
45
46 LevelSubset::~LevelSubset()
47 {
48 }
49
50 void LevelSubset::create(const std::string& subset_name)
51 {
52   Level new_lev;
53   LevelSubset new_subset;
54   new_subset.name = subset_name;
55   new_subset.title = "Unknown Title";
56   new_subset.description = "No description so far.";
57   new_subset.hide_from_contribs = false;
58   new_subset.save();
59 }
60
61 void LevelSubset::read_info_file(const std::string& info_file)
62 {
63   lisp_object_t* root_obj = lisp_read_from_file(info_file);
64   if (root_obj == NULL)
65     return;
66   lisp_object_t* cur = lisp_car(root_obj);
67
68   if (lisp_symbol_p(cur) && strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
69     {
70       LispReader reader(lisp_cdr(root_obj));
71
72       reader.read_string("title", title, true);
73       reader.read_string("description", description, true);
74       reader.read_string_vector("levels", levels);
75       hide_from_contribs = false;
76       reader.read_bool("hide-from-contribs", hide_from_contribs);
77     }
78   else
79     {
80       std::cout << "LevelSubset: parse error in info file: " << info_file << std::endl;
81     }
82
83   lisp_free(root_obj);
84 }
85
86 void LevelSubset::load(const std::string& subset)
87 {
88   name = subset;
89   
90   // Check in which directory our subset is located (ie. ~/.supertux/
91   // or SUPERTUX_DATADIR)
92   std::string filename;
93   filename = st_dir + "/levels/" + subset + "/info";
94   if (access(filename.c_str(), R_OK) != 0)
95     {
96       filename = datadir + "/levels/" + subset + "/info";
97       if (access(filename.c_str(), R_OK) != 0)
98         std::cout << "Error: LevelSubset: couldn't find subset: " << subset << std::endl;
99     }
100   
101   read_info_file(filename);
102
103   if (levels.empty())
104     { // Level info file doesn't define any levels, so read the
105       // directory to see what we can find
106       std::set<std::string> files;
107   
108       filename = datadir + "/levels/" + subset + "/";
109       files = FileSystem::read_directory(filename);
110
111       filename = st_dir + "/levels/" + subset + "/";
112       std::set<std::string> user_files = FileSystem::read_directory(filename);
113       files.insert(user_files.begin(), user_files.end());
114   
115       for(std::set<std::string>::iterator i = files.begin(); i != files.end(); ++i)
116         {
117           if (has_suffix(*i, ".stl"))
118             levels.push_back(subset+ "/" + *i);
119         }
120     }
121 }
122
123 void
124 LevelSubset::save()
125 {
126   FILE* fi;
127   std::string filename;
128
129   /* Save data file: */
130   filename = "/levels/" + name + "/";
131
132   FileSystem::fcreatedir(filename.c_str());
133   filename = std::string(st_dir) + "/levels/" + name + "/info";
134   if(!FileSystem::fwriteable(filename.c_str()))
135     filename = datadir + "/levels/" + name + "/info";
136   if(FileSystem::fwriteable(filename.c_str()))
137     {
138       fi = fopen(filename.c_str(), "w");
139       if (fi == NULL)
140         {
141           perror(filename.c_str());
142         }
143
144       /* Write header: */
145       fprintf(fi,";; SuperTux-Level-Subset\n");
146       fprintf(fi,"(supertux-level-subset\n");
147
148       /* Save title info: */
149       fprintf(fi,"  (title \"%s\")\n", title.c_str());
150
151       /* Save the description: */
152       fprintf(fi,"  (description \"%s\")\n", description.c_str());
153
154       /* Save the hide from Contrbis menu boolean: */
155       fprintf(fi,"  (hide-from-contribs %s)\n", hide_from_contribs ? "#t" : "#f");
156
157       fprintf( fi,")");
158       fclose(fi);
159     }
160 }
161
162 void
163 LevelSubset::add_level(const std::string& name)
164 {
165   levels.push_back(name);
166 }
167
168 std::string
169 LevelSubset::get_level_filename(unsigned int num)
170 {
171   assert(num < levels.size());
172   return levels[num];
173 }
174
175 int
176 LevelSubset::get_num_levels() const
177 {
178   return levels.size();
179 }
180
181 /* EOF */