]> git.tdb.fi Git - r2c2.git/blob - source/3d/path.cpp
Fix a segfault in removing vehicles when creating a new train
[r2c2.git] / source / 3d / path.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gl/matrix.h>
9 #include "libmarklin/tracktype.h"
10 #include "layout.h"
11 #include "path.h"
12 #include "track.h"
13 #include "tracktype.h"
14
15 using namespace Msp;
16
17 namespace Marklin {
18
19 Path3D::Path3D(const Track3D &t):
20         track(t),
21         paths(0),
22         automatic(true),
23         z_offs(0)
24 {
25         track.get_layout().get_path_scene().add(*this);
26 }
27
28 Path3D::~Path3D()
29 {
30         track.get_layout().get_path_scene().remove(*this);
31 }
32
33 void Path3D::set_automatic()
34 {
35         automatic = true;
36 }
37
38 void Path3D::set_path(unsigned p)
39 {
40         if(!(track.get_track().get_type().get_paths()&(1<<p)))
41                 throw InvalidParameterValue("Invalid path");
42         automatic = false;
43         paths = 1<<p;
44 }
45
46 void Path3D::set_mask(unsigned p)
47 {
48         if(p&~track.get_track().get_type().get_paths())
49                 throw InvalidParameterValue("Invalid path mask");
50         automatic = false;
51         paths = p;
52 }
53
54 void Path3D::set_color(const GL::Color &c)
55 {
56         color = c;
57 }
58
59 void Path3D::set_layer(float l)
60 {
61         z_offs = l*track.get_track().get_layout().get_catalogue().get_gauge()*0.01;
62 }
63
64 void Path3D::render(const GL::Tag &tag) const
65 {
66         if(tag=="unlit")
67         {
68                 unsigned mask = (automatic ? 1<<track.get_track().get_active_path() : paths);
69                 mask &= track.get_track().get_type().get_paths();
70                 if(!mask)
71                         return;
72
73                 GL::PushMatrix push_mat;
74                 track.apply_matrix();
75                 GL::translate(0, 0, z_offs);
76
77                 glColor4f(color.r, color.g, color.b, color.a);
78                 for(unsigned i=0; mask; ++i, mask>>=1)
79                         if(mask&1)
80                                 track.get_type().get_path_mesh(i).draw();
81         }
82 }
83
84 } // namespace Marklin