]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/catalogue.cpp
Render tracks through GL::Objects
[r2c2.git] / source / libr2c2 / catalogue.cpp
1 /* $Id$
2
3 This file is part of R²C²
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 "tracktype.h"
12 #include "vehicletype.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 namespace R2C2 {
18
19 Catalogue::Catalogue():
20         scale(1),
21         gauge(1.524),
22         layout(*this)
23 { }
24
25 Catalogue::~Catalogue()
26 {
27         for(TrackMap::iterator i=tracks.begin(); i!=tracks.end(); ++i)
28                 delete i->second;
29         for(VehicleMap::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
30                 delete i->second;
31 }
32
33 float Catalogue::get_rail_elevation() const
34 {
35         return ballast_profile.get_height()+rail_profile.get_height();
36 }
37
38 void Catalogue::add_track(TrackType &track)
39 {
40         if(tracks.count(track.get_article_number()))
41                 throw Exception("Duplicate track type");
42
43         tracks[track.get_article_number()] = &track;
44         signal_track_added.emit(track);
45 }
46
47 const TrackType &Catalogue::get_track(const ArticleNumber &art_nr) const
48 {
49         TrackMap::const_iterator i=tracks.find(art_nr);
50         if(i==tracks.end())
51                 throw KeyError("Unknown track type");
52
53         return *i->second;
54 }
55
56 void Catalogue::add_vehicle(VehicleType &veh)
57 {
58         if(vehicles.count(veh.get_article_number()))
59                 throw Exception("Duplicate vehicle type");
60
61         vehicles[veh.get_article_number()] = &veh;
62         signal_vehicle_added.emit(veh);
63 }
64
65 const VehicleType &Catalogue::get_vehicle(const ArticleNumber &art_nr) const
66 {
67         VehicleMap::const_iterator i = vehicles.find(art_nr);
68         if(i==vehicles.end())
69                 throw KeyError("Unknown vehicle type");
70
71         return *i->second;
72 }
73
74
75 Catalogue::Loader::Loader(Catalogue &c):
76         DataFile::BasicLoader<Catalogue>(c)
77 {
78         add("ballast_profile", &Loader::ballast_profile);
79         add("gauge", &Loader::gauge);
80         add("layout", &Loader::layout);
81         add("rail_profile", &Loader::rail_profile);
82         add("scale", &Loader::scale);
83         add("track", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
84         add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
85         add("track_technique", &Catalogue::track_technique);
86         add("vehicle", static_cast<void (Loader::*)(unsigned)>(&Loader::vehicle));
87         add("vehicle", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::vehicle));
88 }
89
90 void Catalogue::Loader::ballast_profile()
91 {
92         load_sub(obj.ballast_profile);
93 }
94
95 void Catalogue::Loader::gauge(float g)
96 {
97         obj.gauge = g/1000;
98         obj.path_profile = Profile();
99         obj.path_profile.append_point(Point(0.1*obj.gauge, 0));
100         obj.path_profile.append_point(Point(-0.1*obj.gauge, 0));
101 }
102
103 void Catalogue::Loader::layout()
104 {
105         load_sub(obj.layout);
106 }
107
108 void Catalogue::Loader::rail_profile()
109 {
110         load_sub(obj.rail_profile);
111 }
112
113 void Catalogue::Loader::scale(float n, float d)
114 {
115         obj.scale = n/d;
116 }
117
118 void Catalogue::Loader::track(unsigned art_nr)
119 {
120         track(ArticleNumber(art_nr));
121 }
122
123 void Catalogue::Loader::track(ArticleNumber art_nr)
124 {
125         if(obj.tracks.count(art_nr))
126                 throw KeyError("Duplicate track type", art_nr.str());
127
128         RefPtr<TrackType> trk = new TrackType(art_nr);
129         load_sub(*trk);
130         obj.add_track(*trk.release());
131 }
132
133 void Catalogue::Loader::vehicle(unsigned art_nr)
134 {
135         vehicle(ArticleNumber(art_nr));
136 }
137
138 void Catalogue::Loader::vehicle(ArticleNumber art_nr)
139 {
140         if(obj.vehicles.count(art_nr))
141                 throw KeyError("Duplicate vehicle type", art_nr.str());
142
143         RefPtr<VehicleType> veh = new VehicleType(art_nr);
144         load_sub(*veh);
145         obj.add_vehicle(*veh.release());
146 }
147
148 } // namespace R2C2