]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/catalogue.cpp
Split mesh generation from Track3D to TrackType3D
[r2c2.git] / source / libmarklin / catalogue.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/parser.h>
10 #include "catalogue.h"
11 #include "locotype.h"
12 #include "tracktype.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 namespace Marklin {
18
19 Catalogue::Catalogue():
20         scale(1),
21         gauge(1.524)
22 { }
23
24 Catalogue::~Catalogue()
25 {
26         for(map<unsigned, TrackType *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
27                 delete i->second;
28         for(map<unsigned, LocoType *>::iterator i=locos.begin(); i!=locos.end(); ++i)
29                 delete i->second;
30 }
31
32 TrackType &Catalogue::get_track(unsigned art_nr) const
33 {
34         map<unsigned, TrackType *>::const_iterator i=tracks.find(art_nr);
35         if(i==tracks.end())
36                 throw KeyError("Unknown track type");
37
38         return *i->second;
39 }
40
41 LocoType &Catalogue::get_locomotive(unsigned art_nr) const
42 {
43         map<unsigned, LocoType *>::const_iterator i=locos.find(art_nr);
44         if(i==locos.end())
45                 throw KeyError("Unknown locomotive type");
46
47         return *i->second;
48 }
49
50
51 Catalogue::Loader::Loader(Catalogue &c):
52         DataFile::BasicLoader<Catalogue>(c)
53 {
54         add("ballast_profile", &Loader::ballast_profile);
55         add("gauge", &Loader::gauge);
56         add("locomotive", &Loader::locomotive);
57         add("rail_profile", &Loader::rail_profile);
58         add("scale", &Loader::scale);
59         add("track", &Loader::track);
60 }
61
62 void Catalogue::Loader::ballast_profile()
63 {
64         load_sub(obj.ballast_profile);
65 }
66
67 void Catalogue::Loader::gauge(float g)
68 {
69         obj.gauge = g/1000;
70 }
71
72 void Catalogue::Loader::locomotive(unsigned art_nr)
73 {
74         if(obj.locos.count(art_nr))
75                 throw Exception("Duplicate locomotive number");
76
77         RefPtr<LocoType> loco = new LocoType(art_nr);
78         load_sub(*loco);
79         obj.locos[art_nr] = loco.release();
80 }
81
82 void Catalogue::Loader::rail_profile()
83 {
84         load_sub(obj.rail_profile);
85 }
86
87 void Catalogue::Loader::scale(float n, float d)
88 {
89         obj.scale = n/d;
90 }
91
92 void Catalogue::Loader::track(unsigned art_nr)
93 {
94         if(obj.tracks.count(art_nr))
95                 throw Exception("Duplicate track number");
96
97         RefPtr<TrackType> trk = new TrackType(art_nr);
98         load_sub(*trk);
99         obj.tracks[art_nr] = trk.release();
100 }
101
102 } // namespace Marklin