Back to index

supertuxkart  0.5+dfsg1
track_manager.cpp
Go to the documentation of this file.
00001 //  $Id: track_manager.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 SuperTuxKart-Team
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include <stdio.h>
00021 #include <stdexcept>
00022 #include "file_manager.hpp"
00023 #include "string_utils.hpp"
00024 #include "track_manager.hpp"
00025 #include "track.hpp"
00026 #include "sound_manager.hpp"
00027 #include "translation.hpp"
00028 
00029 TrackManager* track_manager = 0;
00030 
00031 TrackManager::TrackManager()
00032 {}   // TrackManager
00033 
00034 //-----------------------------------------------------------------------------
00035 TrackManager::~TrackManager()
00036 {
00037     for(Tracks::iterator i = m_tracks.begin(); i != m_tracks.end(); ++i)
00038         delete *i;
00039 }   // ~TrackManager
00040 
00041 //-----------------------------------------------------------------------------
00042 Track* TrackManager::getTrack(const std::string& ident) const
00043 {
00044     for(Tracks::const_iterator i = m_tracks.begin(); i != m_tracks.end(); ++i)
00045     {
00046         if ((*i)->getIdent() == ident)
00047             return *i;
00048     }
00049 
00050     char msg[MAX_ERROR_MESSAGE_LENGTH];
00051     fprintf(stderr, "TrackManager: Couldn't find track: '%s'", ident.c_str() );
00052     throw std::runtime_error(msg);
00053 }   // getTrack
00054 
00055 //-----------------------------------------------------------------------------
00056 Track* TrackManager::getTrack(size_t id) const
00057 {
00058     return m_tracks[id];
00059 }  // getTrack
00060 
00061 //-----------------------------------------------------------------------------
00062 size_t TrackManager::getTrackCount() const
00063 {
00064     return m_tracks.size();
00065 }   // getTrackCount
00066 
00067 //-----------------------------------------------------------------------------
00068 void TrackManager::loadTrackList ()
00069 {
00070     // Load up a list of tracks - and their names
00071     std::set<std::string> dirs;
00072     file_manager->listFiles(dirs, file_manager->getTrackDir(), /*is_full_path*/ true);
00073     for(std::set<std::string>::iterator dir = dirs.begin(); dir != dirs.end(); dir++)
00074     {
00075         if(*dir=="." || *dir=="..") continue;
00076         std::string config_file;
00077         try
00078         {
00079             // getTrackFile appends dir, so it's opening: *dir/*dir.track
00080             config_file = file_manager->getTrackFile((*dir)+".track");
00081         }
00082         catch (std::exception& e)
00083         {
00084             (void)e;   // remove warning about unused variable
00085             continue;
00086         }
00087         FILE *f=fopen(config_file.c_str(),"r");
00088         if(!f) continue;
00089 
00090         m_tracks.push_back(new Track(config_file));
00091 
00092         // Read music files in that dir as well
00093         sound_manager->loadMusicFromOneDir(*dir);
00094     }
00095 }  // loadTrackList