~ubuntu-branches/ubuntu/saucy/lordsawar/saucy

« back to all changes in this revision

Viewing changes to src/File.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese
  • Date: 2008-12-20 13:52:12 UTC
  • mfrom: (1.1.6 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20081220135212-noeb2w3y98ebo7o9
Tags: 0.1.4-1
[ Barry deFreese ]
* New upstream release.
* Move 0.0.8-2.1 changelog entry to correct point in changelog.
* Make lordsawar-data suggest lordsawar.
* Update my e-mail address.
* Add build-depends on intltool, uuid-dev, and libboost-dev.
* Don't install locales since there are no translations currently.
* Add simple man page for new lordsawar-pbm binary.
* Drop gcc4.3 patches as they have been fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
20
20
//  02110-1301, USA.
21
21
 
22
 
#include <config.h>
 
22
#include "config.h"
23
23
 
24
24
#include <iostream>
 
25
#include <algorithm>
25
26
#include <glibmm/fileutils.h>
26
27
#include <glibmm/ustring.h>
27
28
#include <glibmm/convert.h>
28
29
 
29
30
#include "File.h"
30
 
#include "defs.h"
31
31
#include <SDL_image.h>
32
32
#include "Configuration.h"
33
 
 
34
 
using namespace std;
35
 
 
36
 
#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
 
33
#include "Campaign.h"
 
34
#include "defs.h"
 
35
 
 
36
#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
37
37
//#define debug(x)
38
38
 
39
39
namespace
47
47
        for (Glib::Dir::iterator i = dir.begin(), end = dir.end();
48
48
             i != end; ++i)
49
49
        {
50
 
            string entry = path + *i;
 
50
            std::string entry = path + *i;
51
51
            if (Glib::file_test(entry, Glib::FILE_TEST_IS_DIR))
52
52
                dirlist.push_back(entry);
53
53
        }
64
64
    
65
65
        for (Glib::Dir::iterator i = dir.begin(), end = dir.end(); i != end; ++i)
66
66
        {
67
 
            string entry = *i;
68
 
            string::size_type idx = entry.find(".xml");
69
 
            if (idx != string::npos)
 
67
            std::string entry = *i;
 
68
            std::string::size_type idx = entry.find(".xml");
 
69
            if (idx != std::string::npos)
70
70
            {
71
71
                entry.replace(idx, 4, "");  //substitute the ".xml" with ""
72
72
                retlist.push_back(Glib::filename_to_utf8(entry));
91
91
    }
92
92
}
93
93
 
94
 
SDL_Surface* File::loadImage(string filename, bool alpha)
95
 
{
96
 
    SDL_Surface* tmp = IMG_Load(filename.c_str());
97
 
    if (!tmp)
98
 
    {
99
 
        cerr << _("ERROR: Couldn't load image ") << filename << endl;
100
 
        return 0;
101
 
    }
102
 
 
103
 
    SDL_Surface* convertedImage;
104
 
    // 1:1 copy
105
 
    if (alpha)
106
 
        convertedImage = SDL_DisplayFormatAlpha(tmp);
107
 
    // strip alpha channel if any
108
 
    else
109
 
        convertedImage = SDL_DisplayFormat(tmp);
110
 
    SDL_FreeSurface(tmp);
111
 
 
112
 
    return convertedImage;
113
 
 
 
94
std::string getArmysetDir()
 
95
{
 
96
  return Configuration::s_dataPath + "/" + ARMYSETDIR + "/";
 
97
}
 
98
 
 
99
std::string getTilesetDir()
 
100
{
 
101
  return Configuration::s_dataPath + "/" + TILESETDIR + "/";
 
102
}
 
103
 
 
104
std::string getCitysetDir()
 
105
{
 
106
  return Configuration::s_dataPath + "/" + CITYSETDIR + "/";
 
107
}
 
108
 
 
109
std::string getShieldsetDir()
 
110
{
 
111
  return Configuration::s_dataPath + "/" + SHIELDSETDIR + "/";
 
112
}
 
113
 
 
114
std::string getCampaignDir()
 
115
{
 
116
  return Configuration::s_dataPath + "/" + CAMPAIGNDIR + "/";
114
117
}
115
118
 
116
119
std::list<std::string> File::scanArmysets()
117
120
{
118
 
    string path = Configuration::s_dataPath + "/army/";
119
 
 
120
 
    std::list<std::string> retlist = get_xml_files_in_immediate_subdirs(path);
 
121
    std::list<std::string> retlist = 
 
122
      get_xml_files_in_immediate_subdirs(getArmysetDir());
121
123
 
122
124
    if (retlist.empty())
123
125
    {
124
 
        cerr << _("Couldn't find any armysets!") << endl;
125
 
        cerr << _("Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc") << endl;
126
 
        cerr << _("Exiting!") << endl;
127
 
        exit(-1);
 
126
      std::cerr << "Couldn't find any armysets!" << std::endl;
 
127
      std::cerr << "Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc" << std::endl;
 
128
      std::cerr << "Exiting!" << std::endl;
 
129
      exit(-1);
128
130
    }
129
131
 
130
132
    return retlist;
131
133
}
132
134
 
133
 
 
134
 
string File::getArmyset(string armysetname)
135
 
{
136
 
    return Configuration::s_dataPath + "/army/" + armysetname + "/" + 
137
 
      armysetname + ".xml";
138
 
}
139
 
 
140
 
SDL_Surface* File::getArmyPicture(string armysetname, string pic)
141
 
{
142
 
    return loadImage(Configuration::s_dataPath + "/army/" + armysetname + "/" + pic);
143
 
}
144
 
 
145
 
 
146
 
string File::getTileset(string tilesetname)
147
 
{
148
 
    return Configuration::s_dataPath + "/tilesets/" + tilesetname + "/" + tilesetname + ".xml";
149
 
}
150
 
 
151
 
std::string File::getTilesetFile(string tilesetname, string picname)
152
 
{
153
 
    return Configuration::s_dataPath + "/tilesets/" + tilesetname + "/" + picname;
154
 
}
155
 
 
156
 
SDL_Surface* File::getTilesetPicture(string tilesetname, string picname)
157
 
{
158
 
    return loadImage(getTilesetFile(tilesetname, picname));
159
 
}
160
 
 
161
 
string File::getMiscFile(string filename)
162
 
{
163
 
    return Configuration::s_dataPath + "/" + filename;
164
 
}
165
 
 
166
 
string File::getCityset(string citysetname)
167
 
{
168
 
    return Configuration::s_dataPath + "/citysets/" + citysetname + "/" + citysetname + ".xml";
169
 
}
170
 
 
171
 
std::string File::getCitysetFile(string citysetname, string picname)
172
 
{
173
 
    return Configuration::s_dataPath + "/citysets/" + citysetname + "/" + picname;
174
 
}
175
 
 
176
 
SDL_Surface* File::getCitysetPicture(string citysetname, string picname)
177
 
{
178
 
    return loadImage(getCitysetFile(citysetname, picname));
179
 
}
180
 
 
181
 
SDL_Surface* File::getMiscPicture(string picname, bool alpha)
182
 
{
183
 
    return loadImage(Configuration::s_dataPath + "/various/" + picname,alpha);
184
 
}
185
 
 
186
 
 
187
 
SDL_Surface* File::getItemPicture(string picname)
188
 
{
189
 
    return loadImage(Configuration::s_dataPath + "/various/items/" + picname + ".png");
190
 
}
191
 
 
192
 
string File::getItemDescription()
193
 
{
194
 
    return Configuration::s_dataPath + "/various/items/items.xml";
195
 
}
196
 
 
 
135
std::string File::getArmyset(std::string armysetsubdir)
 
136
{
 
137
  return getArmysetDir() + armysetsubdir + "/" + armysetsubdir + ".xml";
 
138
}
 
139
 
 
140
std::string File::getTileset(std::string tilesetsubdir)
 
141
{
 
142
  return getTilesetDir() + tilesetsubdir + "/" + tilesetsubdir + ".xml";
 
143
}
 
144
 
 
145
std::string File::getTilesetFile(std::string tilesetsubdir, std::string picname)
 
146
{
 
147
  return getTilesetDir() + tilesetsubdir + "/" + picname;
 
148
}
 
149
 
 
150
std::string File::getShieldsetFile(std::string shieldsetsubdir, std::string picname)
 
151
{
 
152
  return getShieldsetDir() + shieldsetsubdir + "/" + picname;
 
153
}
 
154
 
 
155
std::string File::getMiscFile(std::string filename)
 
156
{
 
157
  return Configuration::s_dataPath + "/" + filename;
 
158
}
 
159
 
 
160
std::string File::getCityset(std::string citysetdir)
 
161
{
 
162
  return getCitysetDir() + citysetdir + "/" + citysetdir + ".xml";
 
163
}
 
164
 
 
165
std::string File::getCitysetFile(std::string citysetsubdir, std::string picname)
 
166
{
 
167
  return getCitysetDir() + citysetsubdir + "/" + picname;
 
168
}
 
169
 
 
170
std::string File::getItemDescription()
 
171
{
 
172
  return Configuration::s_dataPath + "/various/items/items.xml";
 
173
}
197
174
 
198
175
std::string File::getEditorFile(std::string filename)
199
176
{
200
 
    return Configuration::s_dataPath + "/various/editor/" + filename + ".png";
201
 
}
202
 
 
203
 
SDL_Surface* File::getEditorPic(std::string filename)
204
 
{
205
 
    return loadImage(getEditorFile(filename));
 
177
  return Configuration::s_dataPath + "/various/editor/" + filename + ".png";
206
178
}
207
179
 
208
180
std::string File::getMusicFile(std::string filename)
209
181
{
210
 
    return std::string(Configuration::s_dataPath + "/music/" + filename.c_str());
211
 
}
212
 
 
213
 
string File::getSavePath()
 
182
  return std::string(Configuration::s_dataPath + "/music/" + filename.c_str());
 
183
}
 
184
 
 
185
std::string File::getCampaignFile(std::string filename)
 
186
{
 
187
  return getCampaignDir() + "/" + filename;
 
188
}
 
189
 
 
190
std::string File::getSavePath()
214
191
{
215
192
    return Configuration::s_savePath + "/";
216
193
}
217
194
 
218
 
list<string> File::scanTilesets()
219
 
{
220
 
    string path = Configuration::s_dataPath + "/tilesets/";
221
 
    std::list<std::string> retlist = get_xml_files_in_immediate_subdirs(path);
222
 
    
223
 
    if (retlist.empty())
224
 
    {
225
 
        cerr << _("Couldn't find any tilesets!") << endl;
226
 
        cerr << _("Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc") << endl;
227
 
        cerr << _("Exiting!") << endl;
228
 
        exit(-1);
229
 
    }
230
 
    
231
 
    return retlist;
232
 
}
233
 
 
234
 
list<string> File::scanCitysets()
235
 
{
236
 
    string path = Configuration::s_dataPath + "/citysets/";
237
 
    std::list<std::string> retlist = get_xml_files_in_immediate_subdirs(path);
238
 
    
239
 
    if (retlist.empty())
240
 
    {
241
 
        cerr << _("Couldn't find any citysets!") << endl;
242
 
        cerr << _("Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc") << endl;
243
 
        cerr << _("Exiting!") << endl;
244
 
        exit(-1);
245
 
    }
246
 
    
247
 
    return retlist;
248
 
}
249
 
 
250
 
list<string> File::scanMaps()
251
 
{
252
 
    string path = Configuration::s_dataPath + "/map/";
253
 
    
254
 
    std::list<std::string> retlist;
255
 
    Glib::Dir dir(path);
256
 
    
257
 
    for (Glib::Dir::iterator i = dir.begin(), end = dir.end(); i != end; ++i)
258
 
    {
259
 
        string entry = *i;
260
 
        string::size_type idx = entry.find(".map");
261
 
        if (idx != string::npos)
262
 
        {
263
 
            retlist.push_back(Glib::filename_to_utf8(entry));
264
 
        }
265
 
    }
266
 
    
267
 
    if (retlist.empty())
268
 
    {
269
 
        cerr << _("Couldn't find a single map!") << endl;
270
 
        cerr << _("Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc") << endl;
271
 
    }
272
 
 
273
 
    return retlist;
274
 
}
275
 
 
 
195
std::list<std::string> File::scanTilesets()
 
196
{
 
197
    std::list<std::string> retlist = 
 
198
      get_xml_files_in_immediate_subdirs(getTilesetDir());
 
199
    
 
200
    if (retlist.empty())
 
201
    {
 
202
      std::cerr << "Couldn't find any tilesets!" << std::endl;
 
203
      std::cerr << "Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc" << std::endl;
 
204
      std::cerr << "Exiting!" << std::endl;
 
205
      exit(-1);
 
206
    }
 
207
    
 
208
    return retlist;
 
209
}
 
210
 
 
211
std::list<std::string> File::scanCitysets()
 
212
{
 
213
    std::list<std::string> retlist = 
 
214
      get_xml_files_in_immediate_subdirs(getCitysetDir());
 
215
    
 
216
    if (retlist.empty())
 
217
    {
 
218
      std::cerr << "Couldn't find any citysets!" << std::endl;
 
219
      std::cerr << "Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc" << std::endl;
 
220
      std::cerr << "Exiting!" << std::endl;
 
221
        exit(-1);
 
222
    }
 
223
    
 
224
    return retlist;
 
225
}
 
226
 
 
227
std::list<std::string> File::scanCampaigns()
 
228
{
 
229
    std::string campaign;
 
230
    std::string path = getCampaignDir();
 
231
    std::list<std::string> retlist;
 
232
    Glib::Dir dir(path);
 
233
    
 
234
    for (Glib::Dir::iterator i = dir.begin(), end = dir.end(); i != end; ++i)
 
235
    {
 
236
        std::string entry = *i;
 
237
        std::string::size_type idx = entry.find(".map");
 
238
        if (idx != std::string::npos)
 
239
        {
 
240
            retlist.push_back(Glib::filename_to_utf8(entry));
 
241
        }
 
242
    }
 
243
    
 
244
    if (retlist.empty())
 
245
    {
 
246
      std::cerr << "Couldn't find a single campaign!" << std::endl;
 
247
      std::cerr << "Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc" << std::endl;
 
248
    }
 
249
 
 
250
    //now we find the ones that are pointed to, and remove them
 
251
    for (std::list<std::string>::iterator it = retlist.begin(); 
 
252
         it != retlist.end();)
 
253
      {
 
254
        campaign = Campaign::get_campaign_from_scenario_file(path + "/" + *it);
 
255
        std::list<std::string>::iterator cit;
 
256
        cit = find (retlist.begin(), retlist.end(), campaign);
 
257
        if (cit != retlist.end())
 
258
          {
 
259
            retlist.erase (cit);
 
260
            continue;
 
261
          }
 
262
        it++;
 
263
      }
 
264
 
 
265
    return retlist;
 
266
}
 
267
 
 
268
std::list<std::string> File::scanMaps()
 
269
{
 
270
  std::string path = Configuration::s_dataPath + "/" + MAPDIR + "/";
 
271
    
 
272
    std::list<std::string> retlist;
 
273
    Glib::Dir dir(path);
 
274
    
 
275
    for (Glib::Dir::iterator i = dir.begin(), end = dir.end(); i != end; ++i)
 
276
    {
 
277
      std::string entry = *i;
 
278
      std::string::size_type idx = entry.find(".map");
 
279
      if (idx != std::string::npos)
 
280
        {
 
281
            retlist.push_back(Glib::filename_to_utf8(entry));
 
282
        }
 
283
    }
 
284
    
 
285
    if (retlist.empty())
 
286
    {
 
287
      std::cerr << "Couldn't find a single map!" << std::endl;
 
288
      std::cerr << "Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc" << std::endl;
 
289
    }
 
290
 
 
291
    return retlist;
 
292
}
276
293
 
277
294
std::list<std::string> File::scanShieldsets()
278
295
{
279
 
    string path = Configuration::s_dataPath + "/shield/";
280
 
 
281
 
    std::list<std::string> retlist = get_xml_files_in_immediate_subdirs(path);
 
296
    std::list<std::string> retlist = 
 
297
      get_xml_files_in_immediate_subdirs(getShieldsetDir());
282
298
 
283
299
    if (retlist.empty())
284
300
    {
285
 
        cerr << _("Couldn't find any shieldsets!") << endl;
286
 
        cerr << _("Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc") << endl;
287
 
        cerr << _("Exiting!") << endl;
288
 
        exit(-1);
 
301
      std::cerr << "Couldn't find any shieldsets!" << std::endl;
 
302
      std::cerr << "Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc" << std::endl;
 
303
      std::cerr << "Exiting!" << std::endl;
 
304
      exit(-1);
289
305
    }
290
306
 
291
307
    return retlist;
292
308
}
293
309
 
294
 
 
295
 
string File::getShieldset(string shieldsetname)
296
 
{
297
 
    return Configuration::s_dataPath + "/shield/" + shieldsetname + "/" + 
298
 
      shieldsetname + ".xml";
299
 
}
300
 
 
301
 
SDL_Surface* File::getShieldPicture(string shieldsetname, string pic)
302
 
{
303
 
    return loadImage(Configuration::s_dataPath + "/shield/" + shieldsetname + "/" + pic);
 
310
std::string File::getShieldset(std::string shieldsetsubdir)
 
311
{
 
312
  return getShieldsetDir() + shieldsetsubdir + "/" + shieldsetsubdir + ".xml";
304
313
}
305
314
 
306
315
// End of file