]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/vehicletype.cpp
Don't crash if a train has no router
[r2c2.git] / source / 3d / vehicletype.cpp
index 162b2c42110a4cd1d67100e43c5fe04e01124ee2..34f2be630a9bc587680aaf2b4691888b6941eaf0 100644 (file)
@@ -1,10 +1,4 @@
-/* $Id$
-
-This file is part of R²C²
-Copyright © 2010-2011  Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
+#include <msp/gl/material.h>
 #include <msp/gl/meshbuilder.h>
 #include <msp/gl/technique.h>
 #include <msp/gl/texture2d.h>
@@ -32,25 +26,19 @@ T get(const map<string, string> &params, const string &key, T def = T())
 namespace R2C2 {
 
 VehicleType3D::VehicleType3D(Catalogue3D &c, const VehicleType &t):
-       catalogue(c),
+       ObjectType3D(c),
        type(t),
-       body_object(0),
-       axle_objects(1)
+       body_object(0)
 {
        body_object = get_object(type.get_object());
 
-       const vector<VehicleType::Axle> &axles = type.get_fixed_axles();
+       const vector<VehicleType::Axle> &axles = type.get_axles();
        for(vector<VehicleType::Axle>::const_iterator i=axles.begin(); i!=axles.end(); ++i)
-               axle_objects[0].push_back(get_object(i->object));
+               axle_objects.push_back(get_object(i->object));
 
        const vector<VehicleType::Bogie> &bogies = type.get_bogies();
        for(vector<VehicleType::Bogie>::const_iterator i=bogies.begin(); i!=bogies.end(); ++i)
-       {
                bogie_objects.push_back(get_object(i->object));
-               axle_objects.push_back(vector<GL::Object *>());
-               for(vector<VehicleType::Axle>::const_iterator j=i->axles.begin(); j!=i->axles.end(); ++j)
-                       axle_objects.back().push_back(get_object(j->object));
-       }
 
        const vector<VehicleType::Rod> &rods = type.get_rods();
        for(vector<VehicleType::Rod>::const_iterator i=rods.begin(); i!=rods.end(); ++i)
@@ -63,33 +51,24 @@ VehicleType3D::~VehicleType3D()
                delete i->second;
 }
 
-const GL::Object *VehicleType3D::get_fixed_axle_object(unsigned i) const
+const GL::Object *VehicleType3D::get_axle_object(unsigned i) const
 {
-       if(i>=axle_objects[0].size())
-               throw InvalidParameterValue("Axle index out of range");
-       return axle_objects[0][i];
+       if(i>=axle_objects.size())
+               throw out_of_range("VehicleType3D::get_fixed_axle_object");
+       return axle_objects[i];
 }
 
 const GL::Object *VehicleType3D::get_bogie_object(unsigned i) const
 {
        if(i>=bogie_objects.size())
-               throw InvalidParameterValue("Bogie index out of range");
+               throw out_of_range("VehicleType3D::get_bogie_object");
        return bogie_objects[i];
 }
 
-const GL::Object *VehicleType3D::get_bogie_axle_object(unsigned i, unsigned j) const
-{
-       if(i>=bogie_objects.size())
-               throw InvalidParameterValue("Bogie index out of range");
-       if(j>=axle_objects[i+1].size())
-               throw InvalidParameterValue("Axle index out of range");
-       return axle_objects[i+1][j];
-}
-
 const GL::Object *VehicleType3D::get_rod_object(unsigned i) const
 {
        if(i>=rod_objects.size())
-               throw InvalidParameterValue("Rod index out of range");
+               throw out_of_range("VehicleType3D::get_rod_object");
        return rod_objects[i];
 }
 
@@ -107,9 +86,9 @@ GL::Object *VehicleType3D::get_object(const string &name)
                        string kind = name.substr(1, colon-1);
 
                        map<string, string> params;
-                       params["length"] = lexical_cast(type.get_length()*1000);
-                       params["width"] = lexical_cast(type.get_width()*1000);
-                       params["height"] = lexical_cast(type.get_height()*1000);
+                       params["length"] = lexical_cast<string>(type.get_length()*1000);
+                       params["width"] = lexical_cast<string>(type.get_width()*1000);
+                       params["height"] = lexical_cast<string>(type.get_height()*1000);
                        if(colon!=string::npos)
                        {
                                string::size_type start = colon+1;
@@ -144,7 +123,7 @@ GL::Object *VehicleType3D::get_object(const string &name)
                        }
                }
                else
-                       return catalogue.get<GL::Object>(name);
+                       return &catalogue.get<GL::Object>(name);
        }
        return ptr;
 }
@@ -165,7 +144,9 @@ GL::Technique *VehicleType3D::create_technique(const map<string, string> &params
        tex->storage(GL::RGB, 2, 1);
        tex->set_min_filter(GL::NEAREST);
        tex->set_mag_filter(GL::NEAREST);
-       unsigned char data[6] = { color>>16, color>>8, color, color2>>16, color2>>8, color2 };
+       unsigned char data[6];
+       data[0] = color>>16;  data[1] = color>>8;  data[2] = color;
+       data[3] = color2>>16; data[4] = color2>>8; data[5] = color2;
        tex->image(0, GL::RGB, GL::UNSIGNED_BYTE, data);
        pass.set_texture(0, tex);