Layout3D::~Layout3D()
{
- for(Track3DSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ for(list<Track3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
delete *i;
}
void Layout3D::set_quality(unsigned q)
{
quality=q;
- for(Track3DSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ for(list<Track3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
(*i)->set_quality(quality);
}
-void Layout3D::render(bool endpoints)
+void Layout3D::render(bool endpoints) const
{
GL::Texture::unbind();
glEnable(GL_DEPTH_TEST);
- for(Track3DSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
(*i)->render();
if(endpoints)
{
glDepthMask(false);
- for(Track3DSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
(*i)->render_endpoints();
glDepthMask(true);
}
}
-Track3D *Layout3D::get_track(const Track *t)
+Track3D &Layout3D::get_track(const Track &t) const
{
- for(Track3DSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
- if(&(*i)->get_track()==t)
- return *i;
-
- return 0;
+ for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ if(&(*i)->get_track()==&t)
+ return **i;
+
+ throw KeyError("Unknown track");
}
-Track3D *Layout3D::pick_track(float x, float y, float size)
+Track3D *Layout3D::pick_track(float x, float y, float size) const
{
vector<GL::SelectRecord> select_buf;
GL::select_buffer(select_buf);
void Layout3D::track_removed(Track &t)
{
- for(Track3DSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ for(list<Track3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
if(&(*i)->get_track()==&t)
{
delete *i;
class Layout3D
{
+private:
+ Layout &layout;
+ std::list<Track3D *> tracks;
+ unsigned quality;
+
public:
Layout3D(Layout &);
~Layout3D();
void set_quality(unsigned);
- const Track3DSeq &get_tracks() const { return tracks; }
- void render(bool =false);
- Track3D *get_track(const Track *);
- Track3D *pick_track(float, float, float);
+ const std::list<Track3D *> &get_tracks() const { return tracks; }
+ void render(bool =false) const;
+ Track3D &get_track(const Track &) const;
+ Track3D *pick_track(float, float, float) const;
private:
- Layout &layout;
- Track3DSeq tracks;
- unsigned quality;
-
void track_added(Track &);
void track_removed(Track &);
};
+++ /dev/null
-#ifndef MARKLIN3D_MISC_H_
-#define MARKLIN3D_MISC_H_
-
-namespace Marklin {
-
-struct Color
-{
- float r, g, b;
-
- Color(float r_, float g_, float b_): r(r_), g(g_), b(b_) { }
-};
-
-} // namespace Marklin
-
-#endif
}
}
-void Track3D::render()
+void Track3D::render() const
{
prepare_render();
- glPushName((unsigned)this);
+ glPushName(reinterpret_cast<unsigned>(this));
varray.apply();
glColor4f(0.25*color.r, 0.25*color.g, 0.25*color.b, 1);
glPopMatrix();
}
-void Track3D::render_endpoints()
+void Track3D::render_endpoints() const
{
prepare_render();
glPopMatrix();
}
-void Track3D::render_route(int route)
+void Track3D::render_route(int route) const
{
prepare_render();
glPopMatrix();
}
-void Track3D::prepare_render()
+void Track3D::prepare_render() const
{
const Point &pos=track.get_position();
float rot=track.get_rotation();
unsigned nsegs;
if(radius)
{
- nsegs=(unsigned)(part.length*(1<<quality))+1;
+ nsegs=static_cast<unsigned>(part.length*(1<<quality))+1;
Point center(x-sin(dir)*radius, y+cos(dir)*radius, 0);
float r=fabs(radius);
float start=((radius<0)?M_PI:0)+dir;
#define MARKLIN3D_TRACK_H_
#include <list>
+#include <msp/gl/color.h>
#include <msp/gl/vertexarray.h>
#include <msp/gl/vertexarraybuilder.h>
#include "libmarklin/track.h"
#include "libmarklin/trackpart.h"
-#include "misc.h"
namespace Marklin {
class Track3D
{
-public:
- Track3D(Track &, unsigned);
- void set_color(const Color &c) { color=c; }
- void set_quality(unsigned);
- void get_bounds(float, Point &, Point &) const;
- Track &get_track() const { return track; }
- void render();
- void render_endpoints();
- void render_route(int);
private:
Track &track;
- Color color;
+ Msp::GL::Color color;
std::vector<Point> border;
Msp::GL::VertexArray varray;
std::vector<unsigned> base_seq;
std::vector<std::vector<unsigned> > route_seq;
unsigned quality;
- void prepare_render();
+public:
+ Track3D(Track &, unsigned);
+
+ void set_color(const Msp::GL::Color &c) { color=c; }
+ void set_quality(unsigned);
+ void get_bounds(float, Point &, Point &) const;
+ Track &get_track() const { return track; }
+ void render() const;
+ void render_endpoints() const;
+ void render_route(int) const;
+private:
+ void prepare_render() const;
void build_object();
void build_part(const TrackPart &, Msp::GL::VertexArrayBuilder &, unsigned &);
};
-typedef std::list<Track3D *> Track3DSeq;
} // namespace Marklin
if(argc>1)
{
layout->load(argv[1]);
- const Track3DSeq <racks=layout_3d->get_tracks();
- for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
+ const list<Track3D *> <racks=layout_3d->get_tracks();
+ for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
{
if((*i)->get_track().get_sensor_id())
- (*i)->set_color(Color(1, 1, 0.5));
+ (*i)->set_color(GL::Color(1, 1, 0.5));
else if((*i)->get_track().get_turnout_id())
- (*i)->set_color(Color(0.5, 1, 1));
+ (*i)->set_color(GL::Color(0.5, 1, 1));
else if((*i)->get_track().get_flex())
- (*i)->set_color(Color(1, 0.5, 1));
+ (*i)->set_color(GL::Color(1, 0.5, 1));
}
}
measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
}
+Designer::~Designer()
+{
+ delete manipulator;
+ delete selection;
+ delete layout;
+ delete layout_3d;
+ delete cat_layout;
+ delete cat_layout_3d;
+ delete measure;
+}
+
int Designer::main()
{
setenv("__GL_SYNC_TO_VBLANK", "1", 0);
float uy=sin_yaw*-sin_pitch*0.41421;
float uz=cos_pitch*0.41421;
- float xf=(float)x*2/screen_w-1;
- float yf=1-(float)y*2/screen_h;
+ float xf=static_cast<float>(x)*2/screen_w-1;
+ float yf=1-static_cast<float>(y)*2/screen_h;
float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
SDL_Event event;
while(SDL_PollEvent(&event))
{
- float gx,gy;
+ float gx, gy;
switch(event.type)
{
case SDL_MOUSEBUTTONDOWN:
SDL_GL_SwapBuffers();
}
-Designer::~Designer()
-{
- delete manipulator;
- delete selection;
- delete layout;
- delete layout_3d;
- delete cat_layout;
- delete cat_layout_3d;
- delete measure;
-}
-
/*** private ***/
void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
if(*j!=*i)
(*i)->snap_to(**j, true);
- Track3D *t3d=layout_3d->get_track(*i);
+ Track3D &t3d=layout_3d->get_track(**i);
if((*i)->get_flex())
- t3d->set_color(Color(1, 0.5, 1));
+ t3d.set_color(GL::Color(1, 0.5, 1));
else
- t3d->set_color(Color(1, 1, 1));
+ t3d.set_color(GL::Color(1, 1, 1));
}
}
else if(key==SDLK_f)
measure->render();
}
- /*glBegin(GL_LINES);
- glColor4f(1, 0, 0, 1);
- glVertex3f(0, 0, 0);
- glVertex3f(0.5, 0, 0);
- glColor4f(0, 1, 0, 1);
- glVertex3f(0, 0, 0);
- glVertex3f(0, 0.5, 0);
- glColor4f(0, 0, 1, 1);
- glVertex3f(0, 0, 0);
- glVertex3f(0, 0, 0.5);
- glEnd();*/
-
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, screen_w, 0, screen_h, 0, 1);
if(mode==CATALOGUE)
l=cat_layout_3d;
- float xx=((float)(x-(int)screen_w/2)/screen_h)*0.82843;
- float yy=((float)y/screen_h-0.5)*0.82843;
- float size=(float)4/screen_h*0.82843;
+ float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
+ float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
+ float size=4.0/screen_h*0.82843;
project_3d();
apply_camera();
void Designer::move_tooltip(int x, int y)
{
- int w=(int)(font->get_string_width(tooltip)*20);
- tooltip_x=max(min((int)screen_w-w, x), 0);
- tooltip_y=max(min((int)screen_h-20, y), 0);
+ int w=static_cast<int>(font->get_string_width(tooltip)*20);
+ tooltip_x=max(min(static_cast<int>(screen_w)-w, x), 0);
+ tooltip_y=max(min(static_cast<int>(screen_h)-20, y), 0);
}
void Designer::save_accept()
unsigned id=lexical_cast<unsigned>(input->get_text());
track->set_turnout_id(id);
- Track3D *t3d=layout_3d->get_track(track);
+ Track3D &t3d=layout_3d->get_track(*track);
if(id)
- t3d->set_color(Color(0.5, 1, 1));
+ t3d.set_color(GL::Color(0.5, 1, 1));
else
- t3d->set_color(Color(1, 1, 1));
+ t3d.set_color(GL::Color(1, 1, 1));
input_dismiss();
}
unsigned id=lexical_cast<unsigned>(input->get_text());
track->set_sensor_id(id);
- Track3D *t3d=layout_3d->get_track(track);
+ Track3D &t3d=layout_3d->get_track(*track);
if(id)
- t3d->set_color(Color(1, 1, 0.5));
+ t3d.set_color(GL::Color(1, 1, 0.5));
else
- t3d->set_color(Color(1, 1, 1));
+ t3d.set_color(GL::Color(1, 1, 1));
input_dismiss();
}
class Designer: public Msp::Application
{
-public:
- Designer(int, char **);
- Marklin::Layout *get_layout() { return layout; }
- Marklin::Layout3D *get_layout_3d() { return layout_3d; }
- Msp::GL::Font &get_font() { return *font; }
- int main();
- void map_pointer_coords(int, int, float &, float &);
- ~Designer();
private:
enum Mode
{
Msp::Time::TimeStamp tooltip_timeout;
Msp::Time::TimeStamp last_tick;
+public:
+ Designer(int, char **);
+ ~Designer();
+
+ Marklin::Layout *get_layout() { return layout; }
+ Marklin::Layout3D *get_layout_3d() { return layout_3d; }
+ Msp::GL::Font &get_font() { return *font; }
+ int main();
+ void map_pointer_coords(int, int, float &, float &);
+private:
void tick();
void key_press(unsigned, unsigned, wchar_t);
void key_release(unsigned, unsigned);
void Manipulator::update_wrap()
{
wrap.clear();
- float min_x=0,max_x=0;
- float min_y=0,max_y=0;
+ float min_x=0, max_x=0;
+ float min_y=0, max_y=0;
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- Track3D *t3d=designer.get_layout_3d()->get_track(i->track);
+ Track3D &t3d=designer.get_layout_3d()->get_track(*i->track);
TrackWrap tw;
float min_area=100;
for(float a=0; a<M_PI; a+=0.01)
{
- Point minp,maxp;
- t3d->get_bounds(a, minp, maxp);
+ Point minp, maxp;
+ t3d.get_bounds(a, minp, maxp);
float area=(maxp.x-minp.x)*(maxp.y-minp.y);
if(area<min_area)
{
class Manipulator
{
-public:
- sigc::signal<void, const std::string &> signal_status;
- sigc::signal<void, bool> signal_done;
-
- Manipulator(Designer &);
- void set_selection(Selection *);
- void start_move();
- void start_rotate();
- void start_elevate();
- void duplicate();
- void flatten();
- void even_slope(bool =false);
- void cancel();
- void button_press(int, int, float, float, unsigned);
- void pointer_motion(int, int, float, float);
- void render();
private:
enum Mode
{
std::set<Marklin::Track *> neighbors;
sigc::connection selection_changed_conn;
+public:
+ sigc::signal<void, const std::string &> signal_status;
+ sigc::signal<void, bool> signal_done;
+
+ Manipulator(Designer &);
+
+ void set_selection(Selection *);
+ void start_move();
+ void start_rotate();
+ void start_elevate();
+ void duplicate();
+ void flatten();
+ void even_slope(bool =false);
+ void cancel();
+ void button_press(int, int, float, float, unsigned);
+ void pointer_motion(int, int, float, float);
+ void render();
+private:
void selection_changed();
void update_wrap();
void update_neighbors();
class Measure
{
-public:
- sigc::signal<void> signal_done;
- sigc::signal<void> signal_changed;
-
- Measure(Designer &);
- float get_parallel_distance() const { return par_dist; }
- float get_perpendicular_distance() const { return perp_dist; }
- float get_angle_difference() const { return adiff; }
- void start();
- void button_press(int, int, float, float, unsigned);
- void pointer_motion(int, int, float, float);
- void render();
private:
enum State
{
float adiff;
State state;
+public:
+ sigc::signal<void> signal_done;
+ sigc::signal<void> signal_changed;
+
+ Measure(Designer &);
+ float get_parallel_distance() const { return par_dist; }
+ float get_perpendicular_distance() const { return perp_dist; }
+ float get_angle_difference() const { return adiff; }
+ void start();
+ void button_press(int, int, float, float, unsigned);
+ void pointer_motion(int, int, float, float);
+ void render();
+private:
void snap_to_tracks(Marklin::Point &, float &);
};
class Selection
{
+private:
+ std::set<Marklin::Track *> tracks;
+
public:
sigc::signal<void> signal_changed;
void toggle_track(Marklin::Track *);
void select_more();
void select_linked();
-private:
- std::set<Marklin::Track *> tracks;
};
#endif
TrainPanel *tpanel=new TrainPanel(*this, ui_res, *train);
root->add(*tpanel);
int y=main_panel->get_geometry().y;
- for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
+ for(list<TrainPanel *>::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
y-=(*i)->get_geometry().h;
tpanel->set_position(0, y-tpanel->get_geometry().h);
train_panels.push_back(tpanel);
glDisable(GL_LIGHTING);
glColor4f(1, 1, 1, 1);
- const Track3DSeq <racks=layout_3d.get_tracks();
- for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
+ const list<Track3D *> <racks=layout_3d.get_tracks();
+ for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
{
Track &track=(*i)->get_track();
if(track.get_turnout_id())
{
if(btn==1 && placing_block)
{
- set_block_color(*placing_block, Color(1, 1, 1));
+ set_block_color(*placing_block, GL::Color(1, 1, 1));
placing_train->place(placing_block, placing_entry);
placing_train=0;
if(&block!=placing_block)
{
if(placing_block)
- set_block_color(*placing_block, Color(1, 1, 1));
+ set_block_color(*placing_block, GL::Color(1, 1, 1));
placing_block=█
placing_entry=0;
- set_block_color(*placing_block, Color(0.5, 1, 0.7));
+ set_block_color(*placing_block, GL::Color(0.5, 1, 0.7));
}
}
else if(track && track->get_track().get_turnout_id())
void Engineer::view_all()
{
- const Track3DSeq &tracks=layout_3d.get_tracks();
+ const list<Track3D *> &tracks=layout_3d.get_tracks();
cam_rot=0;
float best_height=-1;
float max_x=0;
float min_y=0;
float max_y=0;
- for(Track3DSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- Point minp,maxp;
+ Point minp, maxp;
(*i)->get_bounds(angle, minp, maxp);
min_x=min(min_x, minp.x);
max_x=max(max_x, maxp.x);
cam_pos.z=max(best_height*1.05/0.82843, 0.15);
}
-void Engineer::set_block_color(const Block &block, const Color &color)
+void Engineer::set_block_color(const Block &block, const GL::Color &color)
{
const set<Track *> &tracks=block.get_tracks();
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
- layout_3d.get_track(*i)->set_color(color);
+ layout_3d.get_track(**i).set_color(color);
}
void Engineer::sensor_event(bool state, Sensor *sensor)
{
Block &block=trfc_mgr->get_block_by_track((*i)->get_track());
if(state)
- (*i)->set_color(Color(1, 0.5, 0.3));
+ (*i)->set_color(GL::Color(1, 0.5, 0.3));
else if(block.get_train())
- set_block_color(block, Color(1, 1, 0.3));
+ set_block_color(block, GL::Color(1, 1, 0.3));
else
- (*i)->set_color(Color(1, 1, 1));
+ (*i)->set_color(GL::Color(1, 1, 1));
}
}
}
if(train)
- set_block_color(block, Color(1, 1, 0.3));
+ set_block_color(block, GL::Color(1, 1, 0.3));
else
- set_block_color(block, Color(1, 1, 1));
+ set_block_color(block, GL::Color(1, 1, 1));
}
void Engineer::project_3d()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
- //glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
- glFrustum(-0.069036, 0.041421, -0.041421, 0.041421, 0.1, 10);
+ float offset=200.0/screen_w*0.055228;
+ glFrustum(-0.055228-offset, 0.055228-offset, -0.041421, 0.041421, 0.1, 10);
glMatrixMode(GL_MODELVIEW);
}
Track3D *Engineer::pick_track(int x, int y)
{
- float xx=((float)(x-(int)screen_w*5/8)/screen_h)*0.82843;
- //float xx=((float)(x-(int)screen_w/2)/screen_h)*0.82843;
- float yy=((float)y/screen_h-0.5)*0.82843;
- float size=(float)4/screen_h*0.82843;
+ float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2-100)/screen_h)*0.82843;
+ float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
+ float size=4.0/screen_h*0.82843;
project_3d();
glLoadIdentity();
class Engineer: public Msp::Application
{
-public:
- Engineer(int argc, char **argv);
- ~Engineer();
-
- Marklin::Control &get_control() { return control; }
- Marklin::Train *add_train(unsigned);
- int main();
- void quit() { exit(0); }
private:
- typedef std::list<TrainPanel *> TrainPanelSeq;
-
Msp::Graphics::Display *dpy;
Msp::Graphics::Window *wnd;
Msp::Graphics::GLContext *glc;
float cam_rot;
Msp::GLtk::Resources ui_res;
MainPanel *main_panel;
- TrainPanelSeq train_panels;
+ std::list<TrainPanel *> train_panels;
bool no_lighting;
Marklin::TrafficManager *trfc_mgr;
Marklin::Train *placing_train;
TrainProperties *train_prop;
bool train_prop_stale;
+public:
+ Engineer(int argc, char **argv);
+ ~Engineer();
+
+ Marklin::Control &get_control() { return control; }
+ Marklin::Train *add_train(unsigned);
+ int main();
+ void quit() { exit(0); }
+private:
void tick();
void key_press(unsigned, unsigned, wchar_t);
void button_press(int, int, unsigned, unsigned);
void button_release(int, int, unsigned, unsigned);
void pointer_motion(int, int);
void view_all();
- void set_block_color(const Marklin::Block &, const Marklin::Color &);
+ void set_block_color(const Marklin::Block &, const Msp::GL::Color &);
void sensor_event(bool, Marklin::Sensor *);
void block_reserved(const Marklin::Block &, const Marklin::Train *);
void project_3d();
class MainPanel: public Msp::GLtk::Panel
{
-public:
- MainPanel(Engineer &, Msp::GLtk::Resources &);
- void set_status_text(const std::string &);
private:
Engineer &engineer;
Msp::GLtk::Indicator *ind_on;
Msp::GLtk::Indicator *ind_off;
Msp::GLtk::Label *lbl_status;
+public:
+ MainPanel(Engineer &, Msp::GLtk::Resources &);
+
+ void set_status_text(const std::string &);
+private:
void power_on();
void power_off();
void new_loc();
class TrainPanel: public Msp::GLtk::Panel
{
-public:
- TrainPanel(Engineer &, const Msp::GLtk::Resources &, Marklin::Train &);
private:
Engineer &engineer;
Marklin::Train &train;
Marklin::Locomotive *loco;
Msp::GLtk::Label *lbl_speed;
+public:
+ TrainPanel(Engineer &, const Msp::GLtk::Resources &, Marklin::Train &);
+private:
void speed_slider_changed(double);
void loco_speed_changed(unsigned);
};
class TrainProperties: public Msp::GLtk::Panel
{
-public:
- sigc::signal<void> signal_ok;
-
- TrainProperties(Engineer &, Msp::GLtk::Resources &, Marklin::Train *);
private:
Engineer &engineer;
Marklin::Train *train;
Msp::GLtk::Entry *ent_addr;
Msp::GLtk::Entry *ent_name;
+public:
+ sigc::signal<void> signal_ok;
+
+ TrainProperties(Engineer &, Msp::GLtk::Resources &, Marklin::Train *);
+private:
void ok_clicked();
};
public:
Block(TrafficManager &, Track &);
+
unsigned get_sensor_id() const { return sensor_id; }
const std::set<Track *> &get_tracks() const { return tracks; }
const std::vector<Endpoint> &get_endpoints() const { return endpoints; }
static unsigned next_id;
};
-typedef std::list<Block *> BlockSeq;
} // namespace Marklin
void track(unsigned);
};
+
private:
std::map<unsigned, TrackType *> tracks;
--- /dev/null
+#include "command.h"
+
+using namespace std;
+
+namespace Marklin {
+
+Command::Command(const string &c):
+ cmd(c),
+ sent(false)
+{ }
+
+void Command::set_sent(bool s)
+{
+ sent=s;
+}
+
+} // namespace Marklin
class Command
{
+private:
+ std::string cmd;
+ bool sent;
+
public:
sigc::signal<void, Error, const std::string &> signal_done;
- Command(const std::string &c): cmd(c), sent(false) { }
- void set_sent(bool s) { sent=s; }
+ Command(const std::string &);
+
+ void set_sent(bool);
const std::string &get_command() const { return cmd; }
bool get_sent() const { return sent; }
-private:
- std::string cmd;
- bool sent;
};
} // namespace Marklin
debug(false)
{ }
+Control::~Control()
+{
+ for(map<unsigned, Sensor *>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
+ delete i->second;
+ for(map<unsigned, Turnout *>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
+ delete i->second;
+ for(map<unsigned, Locomotive *>::iterator i=locomotives.begin(); i!=locomotives.end(); ++i)
+ delete i->second;
+ close(serial_fd);
+}
+
void Control::set_power(bool p)
{
power=p;
command(string(1, CMD_POWER_OFF));
}
+void Control::set_debug(bool d)
+{
+ debug=d;
+}
+
void Control::open(const string &dev)
{
serial_fd=::open(dev.c_str(), O_RDWR);
poll_sensors=false;
}
- if(queue.size() && queue.front().get_sent())
+ if(!queue.empty() && queue.front().get_sent())
{
pollfd pfd={serial_fd, POLLIN, 0};
if(poll(&pfd, 1, 0)>0)
{
- string resp=read_reply((Cmd)(unsigned char)queue.front().get_command()[0]);
+ string resp=read_reply(static_cast<Cmd>(static_cast<unsigned char>(queue.front().get_command()[0])));
if(debug)
{
printf("read: ");
for(unsigned i=0; i<resp.size(); ++i)
- printf("%02X ", (unsigned char)resp[i]);
+ printf("%02X ", static_cast<unsigned char>(resp[i]));
printf("(%d bytes)\n", resp.size());
}
- queue.front().signal_done.emit((Error)resp[0], resp.substr(1));
+ queue.front().signal_done.emit(static_cast<Error>(resp[0]), resp.substr(1));
queue.erase(queue.begin());
}
else
return;
}
- if(queue.size())
+ if(!queue.empty())
{
string cmd=queue.front().get_command();
{
printf("write: ");
for(unsigned i=0; i<cmd.size(); ++i)
- printf("%02X ",(unsigned char)cmd[i]);
- printf("(%d bytes)\n",cmd.size());
+ printf("%02X ", static_cast<unsigned char>(cmd[i]));
+ printf("(%d bytes)\n", cmd.size());
}
write(serial_fd, cmd.data(), cmd.size());
return timer.add(dt);
}
-Control::~Control()
-{
- for(map<unsigned, Sensor *>::iterator i=sensors.begin(); i!=sensors.end(); ++i)
- delete i->second;
- for(map<unsigned, Turnout *>::iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
- delete i->second;
- for(map<unsigned, Locomotive *>::iterator i=locomotives.begin(); i!=locomotives.end(); ++i)
- delete i->second;
- close(serial_fd);
-}
-
-/*** private ***/
-
void Control::read_all(int fd, char *buf, int size)
{
int pos=0;
unsigned count=resp[0];
for(unsigned i=0; i<count; ++i)
{
- unsigned addr=(unsigned char)resp[i*2+1]+((resp[i*2+2]&7)<<8);
+ unsigned addr=static_cast<unsigned char>(resp[i*2+1])+((resp[i*2+2]&7)<<8);
bool status=!(resp[i*2+2]&0x80);
cout<<"Turnout "<<addr<<", status "<<status<<'\n';
signal_turnout_event.emit(addr, status);
{
for(unsigned i=0; resp[i]; i+=3)
{
- unsigned module=(unsigned char)resp[i];
+ unsigned module=static_cast<unsigned char>(resp[i]);
cout<<"S88 module "<<module<<", status ";
for(unsigned j=0; j<16; ++j)
sigc::signal<void, unsigned, bool> signal_sensor_event;
Control();
+ ~Control();
+
void set_power(bool);
bool get_power() const { return power; }
- void set_debug(bool d) { debug=d; }
- //const TurnoutMap &get_turnouts() const { return turnouts; }
+ void set_debug(bool);
const std::map<unsigned, Sensor *> &get_sensors() const { return sensors; }
unsigned get_queue_length() const { return queue.size(); }
void open(const std::string &);
Command &command(const std::string &);
+
void add_turnout(Turnout &);
Turnout &get_turnout(unsigned) const;
void add_locomotive(Locomotive &);
Locomotive &get_locomotive(unsigned) const;
void add_sensor(Sensor &);
Sensor &get_sensor(unsigned) const;
+
void tick();
Msp::Time::Timer::Slot &set_timer(const Msp::Time::TimeDelta &);
- ~Control();
private:
void read_all(int, char *, int);
std::string read_reply(Cmd);
struct Point
{
- float x,y,z;
+ float x, y, z;
Point(): x(0), y(0), z(0) { }
Point(float x_, float y_, float z_): x(x_), y(y_), z(z_) { }
catalogue(c)
{ }
+Layout::~Layout()
+{
+ for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ delete *i;
+}
+
void Layout::add_track(Track &t)
{
if(tracks.insert(&t).second)
filename=fn;
- if(base.size())
+ if(!base.empty())
out<<"base \""<<base<<"\";\n";
for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
return 0;
}
-Layout::~Layout()
-{
- for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
- delete *i;
-}
-
-/*******************
-** Layout::Loader
-*/
Layout::Loader::Loader(Layout &l):
layout(l)
void track(unsigned);
};
+private:
+ Catalogue &catalogue;
+ std::string filename;
+ std::string base;
+ std::set<Track *> tracks;
+ //RouteSeq routes;
+
+public:
sigc::signal<void, Track &> signal_track_added;
sigc::signal<void, Track &> signal_track_removed;
Layout(Catalogue &);
+ ~Layout();
+
const std::set<Track *> &get_tracks() const { return tracks; }
void add_track(Track &);
void remove_track(Track &);
void check_links();
void load(const std::string &);
int save(const std::string &);
- ~Layout();
-private:
- Catalogue &catalogue;
- std::string filename;
- std::string base;
- std::set<Track *> tracks;
- //RouteSeq routes;
};
} // namespace Marklin
if((funcs>>i)&2)
cmd[4]|=(1<<i);
}
- control.command(string(cmd,5));
+ control.command(string(cmd, 5));
}
void Locomotive::status_reply(Error err, const string &reply)
{
if(err==ERR_NO_ERROR)
{
- if((unsigned char)reply[0]<=1)
+ if(static_cast<unsigned char>(reply[0])<=1)
speed=0;
else
- speed=(unsigned char)reply[0]*2/19+1;
+ speed=static_cast<unsigned char>(reply[0])*2/19+1;
reverse=(reply[1]&0x20)?false:true;
funcs=(reply[1]&0xF)<<1;
if(reply[1]&0x10)
class Locomotive
{
+private:
+ Control &control;
+ unsigned addr;
+ unsigned speed;
+ bool reverse;
+ unsigned funcs;
+
public:
sigc::signal<void, unsigned> signal_speed_changed;
Locomotive(Control &, unsigned);
+
void set_speed(unsigned);
void set_reverse(bool);
void set_function(unsigned, bool);
bool get_function(unsigned f) const { return (funcs>>f)&1; }
void refresh_status();
private:
- Control &control;
- unsigned addr;
- unsigned speed;
- bool reverse;
- unsigned funcs;
-
void send_command(bool);
void status_reply(Error, const std::string &);
bool reverse_timeout();
class Route
{
public:
- typedef std::map<unsigned, unsigned> TurnoutMap;
-
Route();
const std::map<unsigned, Turnout *> &get_turnouts() const { return turnouts; }
void add_track(Track *);
class Sensor
{
+private:
+ Control &control;
+ unsigned addr;
+ bool state;
+ Msp::Time::TimeStamp off_timeout;
+
public:
sigc::signal<void, bool> signal_state_changed;
Sensor(Control &, unsigned);
+
unsigned get_address() const { return addr; }
bool get_state() const { return state; }
void tick();
private:
- Control &control;
- unsigned addr;
- bool state;
- Msp::Time::TimeStamp off_timeout;
-
void sensor_event(unsigned, bool);
};
bool dead_end;
TrackPart();
+
void collect_endpoints(std::vector<Endpoint> &);
};
}
}
- for(BlockSeq::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ for(list<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
{
- for(BlockSeq::iterator j=i; j!=blocks.end(); ++j)
+ for(list<Block *>::iterator j=i; j!=blocks.end(); ++j)
if(j!=i)
(*i)->check_link(**j);
(*i)->print_debug();
TrafficManager::~TrafficManager()
{
- for(BlockSeq::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ for(list<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
delete *i;
- for(TrainSeq::iterator i=trains.begin(); i!=trains.end(); ++i)
+ for(list<Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
delete *i;
}
Block &TrafficManager::get_block_by_track(const Track &t) const
{
- for(BlockSeq::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
{
const set<Track *> &tracks=(*i)->get_tracks();
if(tracks.count(const_cast<Track *>(&t)))
class TrafficManager
{
+private:
+ Control &control;
+ Layout &layout;
+ std::list<Block *> blocks;
+ std::list<Train *> trains;
+
public:
sigc::signal<void, const Block &, const Train *> signal_block_reserved;
Control &get_control() const { return control; }
Block &get_block_by_track(const Track &) const;
- const TrainSeq &get_trains() const { return trains; }
+ const std::list<Train *> &get_trains() const { return trains; }
void add_train(Train *);
void tick();
private:
- Control &control;
- Layout &layout;
- std::list<Block *> blocks;
- std::list<Train *> trains;
-
void turnout_route_changed(unsigned, Turnout *);
};
++i;
for(list<BlockRef>::iterator j=cur_blocks.begin(); j!=i; ++j)
j->block->reserve(0);
- cout<<" "<<distance(cur_blocks.begin(),i )<<" blocks freed, ";
+ cout<<" "<<distance(cur_blocks.begin(), i)<<" blocks freed, ";
cur_blocks.erase(cur_blocks.begin(), i);
cout<<cur_blocks.size()<<" cur_blocks\n";
}
bool result=false;
unsigned size=rsv_blocks.size();
- while(size<2)
+ while(size<3)
{
int exit=last->block->traverse(last->entry);
if(exit>=0)
sigc::signal<void, const std::string &> signal_name_changed;
Train(TrafficManager &, Locomotive &);
+
void set_name(const std::string &);
void set_speed(unsigned);
const std::string &get_name() const { return name; }
void sensor_event(bool, Sensor *);
bool reserve_more();
};
-typedef std::list<Train *> TrainSeq;
} // namespace Marklin
cmd[0]=CMD_TURNOUT_STATUS;
cmd[1]=addr&0xFF;
cmd[2]=(addr>>8)&0xFF;
- control.command(string(cmd,3)).signal_done.connect(sigc::mem_fun(this, &Turnout::status_reply));
+ control.command(string(cmd, 3)).signal_done.connect(sigc::mem_fun(this, &Turnout::status_reply));
}
void Turnout::set_route(unsigned r)
cmd[2]|=0x40;
if(route==0)
cmd[2]|=0x80;
- control.command(string(cmd,3));
+ control.command(string(cmd, 3));
}
void Turnout::status_reply(Error err, const string &reply)
class Turnout
{
+private:
+ Control &control;
+ unsigned addr;
+ unsigned route;
+
public:
sigc::signal<void, unsigned> signal_route_changed;
Turnout(Control &, unsigned);
+
void set_route(unsigned);
unsigned get_address() const { return addr; }
unsigned get_route() const { return route; }
private:
- Control &control;
- unsigned addr;
- unsigned route;
-
void command(bool);
void status_reply(Error, const std::string &);
bool switch_timeout();
void track(unsigned);
};
- typedef map<unsigned, unsigned> TrackMap;
-
- TrackMap inventory;
- TrackMap layout;
+ map<unsigned, unsigned> inventory;
+ map<unsigned, unsigned> layout;
void load_inventory(const string &);
void load_layout(const string &);
void ShoppingList::print(ostream &out)
{
out<<"// Need to get:\n";
- for(TrackMap::iterator i=layout.begin(); i!=layout.end(); ++i)
+ for(map<unsigned, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
{
- TrackMap::iterator j=inventory.find(i->first);
+ map<unsigned, unsigned>::iterator j=inventory.find(i->first);
if(j!=inventory.end())
{
if(j->second<i->second)
}
out<<"// Pre-existing:\n";
- for(TrackMap::iterator i=layout.begin(); i!=layout.end(); ++i)
+ for(map<unsigned, unsigned>::iterator i=layout.begin(); i!=layout.end(); ++i)
{
- TrackMap::iterator j=inventory.find(i->first);
+ map<unsigned, unsigned>::iterator j=inventory.find(i->first);
if(j!=inventory.end())
out<<"track "<<i->first<<' '<<min(i->second,j->second)<<";\n";
}
out<<"// Unused:\n";
- for(TrackMap::iterator i=inventory.begin(); i!=inventory.end(); ++i)
+ for(map<unsigned, unsigned>::iterator i=inventory.begin(); i!=inventory.end(); ++i)
{
- TrackMap::iterator j=layout.find(i->first);
+ map<unsigned, unsigned>::iterator j=layout.find(i->first);
if(j!=layout.end())
{
if(j->second<i->second)