~ubuntu-branches/ubuntu/natty/balder2d/natty

« back to all changes in this revision

Viewing changes to src/menu/widgets/maplistmodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bjørn Hansen
  • Date: 2008-06-15 17:15:38 UTC
  • mfrom: (1.1.1 upstream) (3.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080615171538-e407e07wbtdy0qs8
Tags: 1.0-1
* new upstream release
* update for guichan 8.1 (Closes: #482584)
* use physicsfs to make data/config/map/aiscript loading more flexible
* new skins for menus
* fix typo in control file long description (Closes: #458401)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005 by Bjorn Hansen                                    *
 
3
 *   holomorph@users.sourceforge.net                                       *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
19
 ***************************************************************************/
 
20
 
 
21
#include "log.h"
 
22
#include "menu/widgets/maplistmodel.h"
 
23
#include <physfs.h>
 
24
 
 
25
using namespace Balder;
 
26
 
 
27
MapListModel::MapListModel(): numMaps(0)
 
28
{
 
29
        FindMaps();
 
30
}
 
31
MapListModel::~MapListModel()
 
32
{}
 
33
 
 
34
bool find_file( const std::string map_dir, const std::string & file_name)
 
35
{
 
36
    char **files = PHYSFS_enumerateFiles(map_dir.c_str());
 
37
    char **i;
 
38
    for (i = files; *i != NULL; i++){
 
39
        std::string f = *i;
 
40
        if (file_name == f) {
 
41
            PHYSFS_freeList(files);
 
42
            return true;
 
43
        }
 
44
    }
 
45
    PHYSFS_freeList(files);
 
46
    return false;
 
47
}
 
48
 
 
49
void MapListModel::FindMaps()
 
50
{
 
51
    std::string map_dir("maps/");
 
52
    char **files = PHYSFS_enumerateFiles(map_dir.c_str());
 
53
    char **i;
 
54
    std::string mapname;
 
55
    Log::output("Importing map list...");
 
56
    for (i = files; *i != NULL; i++){
 
57
        mapname = *i;
 
58
        if (PHYSFS_isDirectory((map_dir+mapname).c_str())){
 
59
            // lets try to check that this directory really contains a map
 
60
                if (find_file(map_dir+mapname, "maptags.xml")
 
61
                        && find_file(map_dir+mapname, "geometry.png")
 
62
                        && find_file(map_dir+mapname, "background.png") ) {
 
63
                                mapnames.push_back(mapname);
 
64
                                ++numMaps;
 
65
            }
 
66
        }
 
67
    }
 
68
    PHYSFS_freeList(files);
 
69
}
 
70
 
 
71
std::string Balder::MapListModel::getElementAt (int i)
 
72
{
 
73
  if (i > numMaps - 1)
 
74
  {
 
75
    Log::output("tried to get map at index higher than number of maps");
 
76
    Log::output("numMaps: "+numMaps);
 
77
//    Log::output("i :" +i);
 
78
    return "--Map Not Found--";
 
79
  }
 
80
  return mapnames[i];
 
81
}
 
82
 
 
83
/*!
 
84
    \fn Balder::MapListModel::FindMapIndex(std::string mapname)
 
85
 */
 
86
int Balder::MapListModel::FindMapIndex(std::string mapname)
 
87
{
 
88
    for (int i = 0; i < numMaps; ++i) {
 
89
        if (!mapname.compare(mapnames[i])) return i;
 
90
    }
 
91
    return 0;
 
92
}