+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
#include <cmath>
#include <limits>
-#include <SDL.h>
+#include <signal.h>
#include <GL/gl.h>
-#include <msp/core/error.h>
+#include <msp/core/except.h>
#include <msp/core/getopt.h>
+#include <msp/fs/stat.h>
+#include <msp/gbase/display.h>
+#include <msp/gbase/window.h>
+#include <msp/gl/immediate.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/projection.h>
+#include <msp/gl/transform.h>
+#include <msp/io/print.h>
+#include <msp/strings/formatter.h>
#include <msp/strings/lexicalcast.h>
#include <msp/strings/regex.h>
+#include <msp/time/units.h>
+#include "libmarklin/except.h"
+#include "libmarklin/tracktype.h"
#include "engineer.h"
#include "mainpanel.h"
#include "trainpanel.h"
+#include "trainproperties.h"
using namespace std;
using namespace Marklin;
using namespace Msp;
-#include <iostream>
-
Engineer::Engineer(int argc, char **argv):
screen_w(1280),
screen_h(960),
fullscreen(false),
layout(catalogue),
layout_3d(layout),
+ server(0),
+ placing_train(0),
+ placing_block(0),
+ placing_entry(0),
no_lighting(false),
- placing_train(0)
+ simulate(false)
{
string res;
- bool debug=false;
- string device="/dev/ttyS0";
- unsigned quality=4;
+ bool debug = false;
+ string device = "/dev/ttyS0";
+ unsigned quality = 4;
+ bool network = false;
GetOpt getopt;
getopt.add_option('r', "resolution", res, GetOpt::REQUIRED_ARG);
getopt.add_option('g', "debug", debug, GetOpt::NO_ARG);
getopt.add_option('d', "device", device, GetOpt::REQUIRED_ARG);
getopt.add_option('q', "quality", quality, GetOpt::REQUIRED_ARG);
+ getopt.add_option('s', "simulate", simulate, GetOpt::NO_ARG);
+ getopt.add_option('n', "network", network, GetOpt::NO_ARG);
getopt.add_option( "no-lighting", no_lighting, GetOpt::NO_ARG);
getopt(argc, argv);
{
if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res))
{
- screen_w=lexical_cast<unsigned>(m[1].str);
- screen_h=lexical_cast<unsigned>(m[2].str);
+ screen_w = lexical_cast<unsigned>(m[1].str);
+ screen_h = lexical_cast<unsigned>(m[2].str);
}
else
throw UsageError("Invalid resolution");
layout_3d.set_quality(quality);
- catalogue.load("tracks.dat");
+ DataFile::load(catalogue, "tracks.dat");
+ DataFile::load(catalogue, "locos.dat");
- const list<string> &args=getopt.get_args();
+ const vector<string> &args = getopt.get_args();
if(args.empty())
throw UsageError("No layout given");
- layout.load(args.front());
+ DataFile::load(layout, args.front());
- trfc_mgr=new TrafficManager(control, layout);
+ trfc_mgr = new TrafficManager(control, layout);
+ if(FS::exists("engineer.state"))
+ DataFile::load(*trfc_mgr, "engineer.state");
+ trfc_mgr->signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
+ trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
- view_all();
+ if(network)
+ {
+ server = new Server(*trfc_mgr);
+ server->use_event_dispatcher(event_disp);
+ }
- /*const TrackSeq &tracks=layout.get_tracks();
+ const map<unsigned, Sensor *> &sensors = control.get_sensors();
+ for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
+ i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_event), i->second));
- for(TrackSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
- {
- if(unsigned trnt_id=(*i)->get_turnout_id())
- {
- Turnout *trnt=new Turnout(control, trnt_id);
- trnt->signal_route_changed.connect(sigc::mem_fun(this, &Engineer::turnout_route_changed));
- }
- if(unsigned sens_id=(*i)->get_sensor_id())
- {
- Sensor *sens=new Sensor(control, sens_id);
- sens->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_state_changed), sens_id));
- }
- }*/
-}
+ view_all();
-Engineer::~Engineer()
-{
+ catch_signal(SIGINT);
+ catch_signal(SIGTERM);
+ catch_signal(SIGSEGV);
+ catch_signal(SIGILL);
+ catch_signal(SIGFPE);
+ catch_signal(SIGABRT);
}
-void Engineer::add_train(unsigned addr)
+Engineer::~Engineer()
{
- if(control.get_locomotive(addr))
- return;
+ const list<Train *> &trains = trfc_mgr->get_trains();
+ for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+ (*i)->set_speed(0);
- Locomotive *loco=new Locomotive(control, addr);
- Train *train=new Train(*trfc_mgr, *loco);
+ while(control.get_queue_length())
+ control.tick();
- TrainPanel *tpanel=new TrainPanel(*this, ui_res, *train);
- int y=main_panel->get_geometry().y;
- for(TrainPanelSeq::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);
+ if(!simulate)
+ trfc_mgr->save("engineer.state");
+ delete trfc_mgr;
+ delete server;
+}
- placing_train=train;
+void Engineer::place_train(Train &train)
+{
+ placing_train = &train;
+ placing_block = 0;
+ main_panel->set_status_text("Select location");
}
int Engineer::main()
{
- SDL_Init(SDL_INIT_VIDEO);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
- SDL_Surface *screen=SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL|(fullscreen?SDL_FULLSCREEN:0));
- if(!screen)
- {
- SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
- screen=SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL|(fullscreen?SDL_FULLSCREEN:0));
- }
- if(!screen)
- {
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
- screen=SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL|(fullscreen?SDL_FULLSCREEN:0));
- }
- if(!screen)
- throw Exception("Couldn't create window");
+ dpy = new Graphics::Display;
+
+ Graphics::WindowOptions wopt;
+ wopt.width = screen_w;
+ wopt.height = screen_h;
+ wopt.fullscreen = fullscreen;
+ wnd = new Graphics::Window(*dpy, wopt);
+
+ Graphics::GLOptions glopt;
+ //glopt.multisample = 4;
+ glc = new Graphics::GLContext(*wnd, glopt);
+
+ wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
+ wnd->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press));
+ wnd->signal_button_release.connect(sigc::mem_fun(this, &Engineer::button_release));
+ wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion));
glEnableClientState(GL_VERTEX_ARRAY);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- font=new GL::Font();
- if(screen_w>=1024)
+ DataFile::load(ui_res, "marklin.res");
+ root = new GLtk::Root(ui_res, *wnd);
+ root->set_visible(true);
+
+ list<GL::Texture *> texs = ui_res.get_list<GL::Texture>();
+ for(list<GL::Texture *>::iterator i=texs.begin(); i!=texs.end(); ++i)
{
- font_size=20;
- Parser::load(*font, "dejavu-20.font");
+ (*i)->set_min_filter(GL::NEAREST);
+ (*i)->set_mag_filter(GL::NEAREST);
}
- else
+
+ main_panel = new MainPanel(*this, ui_res);
+ root->add(*main_panel);
+ main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
+ main_panel->set_visible(true);
+
+ const list<Train *> &trains = trfc_mgr->get_trains();
+ int y = main_panel->get_geometry().y;
+ for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
{
- font_size=12;
- Parser::load(*font, "dejavu-12.font");
+ TrainPanel *tpanel = new TrainPanel(*this, ui_res, **i);
+ root->add(*tpanel);
+ tpanel->set_position(0, y-tpanel->get_geometry().h);
+ train_panels.push_back(tpanel);
+ tpanel->set_visible(true);
+ y -= tpanel->get_geometry().h;
}
- Parser::load(ui_res, "engineer.res");
- main_panel=new MainPanel(*this, ui_res);
- main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
+ const list<Block *> &blocks = trfc_mgr->get_blocks();
+ for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ reset_block_color(**i);
- Application::main();
+ wnd->show();
- delete font;
- delete main_panel;
+ Application::main();
- SDL_Quit();
+ delete root;
+ delete glc;
+ delete wnd;
+ delete dpy;
return exit_code;
}
void Engineer::tick()
{
- //cout<<"tick\n";
-
- SDL_Event event;
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_MOUSEBUTTONDOWN:
- button_press(event.button.x, screen_h-1-event.button.y, event.button.button);
- break;
- case SDL_MOUSEBUTTONUP:
- button_release(event.button.x, screen_h-1-event.button.y, event.button.button);
- break;
- case SDL_MOUSEMOTION:
- pointer_motion(event.motion.x, screen_h-1-event.motion.y);
- break;
- case SDL_KEYDOWN:
- key_press(event.key.keysym.sym, event.key.keysym.mod);
- break;
- case SDL_QUIT:
- exit(0);
- break;
- }
- }
+ dpy->tick();
control.tick();
+ trfc_mgr->tick();
+ event_disp.tick(Time::zero);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
float params[4];
- params[0]=0;
- params[1]=-0.2;
- params[2]=1;
- params[3]=0;
+ params[0] = 0;
+ params[1] = -0.2;
+ params[2] = 1;
+ params[3] = 0;
glLightfv(GL_LIGHT0, GL_POSITION, params);
}
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();
+ Track &track = (*i)->get_track();
if(track.get_turnout_id())
{
- Turnout *trnt=control.get_turnout(track.get_turnout_id());
- if(trnt)
- (*i)->render_route(trnt->get_route());
- else
- (*i)->render_route(-1);
+ Turnout &trnt = control.get_turnout(track.get_turnout_id());
+ (*i)->render_path(trnt.get_path());
}
else
- (*i)->render_route(-1);
+ (*i)->render_path(-1);
}
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, screen_w, 0, screen_h, 0, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
+ if(placing_train && placing_block)
+ {
+ GL::push_matrix();
+
+ const Marklin::Block::Endpoint &bep = placing_block->get_endpoints()[placing_entry];
+ float rot = bep.track->get_endpoint_direction(bep.track_ep);
+ Point pos = bep.track->get_endpoint_position(bep.track_ep);
+
+ GL::translate(pos.x, pos.y, pos.z+0.03);
+ GL::rotate(rot*180/M_PI+180, 0, 0, 1);
+ GL::Texture::unbind();
+
+ GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
+ imm.color(1.0f, 1.0f, 1.0f);
+ imm.begin(GL::TRIANGLE_FAN);
+ imm.vertex(0.08, 0);
+ imm.vertex(0.05, 0.03);
+ imm.vertex(0.05, 0.01);
+ imm.vertex(0, 0.01);
+ imm.vertex(0, -0.01);
+ imm.vertex(0.05, -0.01);
+ imm.vertex(0.05, -0.03);
+ imm.end();
+
+ GL::pop_matrix();
+ }
+
+ const list<Train *> &trains = trfc_mgr->get_trains();
+ for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+ {
+ if(!(*i)->is_placed())
+ continue;
+
+ GL::PushMatrix _push;
+
+ const Point &tp = (*i)->get_position();
+ GL::translate(tp.x, tp.y, tp.z+0.02);
+ GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
+ imm.color(0.8f, 0.8f, 1.0f);
+ imm.begin(GL::TRIANGLE_FAN);
+ imm.vertex(0, 0);
+ for(unsigned j=0; j<=12; ++j)
+ imm.vertex(0.02*cos(j*M_PI/6), 0.02*sin(j*M_PI/6));
+ imm.end();
+
+ GL::rotate(cam_rot*180/M_PI, 0, 0, 1);
+ GL::translate(0.03, -0.02, 0);
+ GL::scale_uniform(0.04);
+ ui_res.get_default_font().draw_string((*i)->get_name());
+ GL::Texture::unbind();
+ }
+
+ GL::matrix_mode(GL::PROJECTION);
+ GL::load_identity();
+ GL::ortho_bottomleft(screen_w, screen_h);
+ GL::matrix_mode(GL::MODELVIEW);
+ GL::load_identity();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_MULTISAMPLE);
- main_panel->render();
- for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
- (*i)->render();
+ root->render();
- glLoadIdentity();
- glTranslatef(340, 10, 0);
- glScalef(20, 20, 20);
- glColor4f(1, 1, 1, 1);
- font->draw_string(status_text);
-
- SDL_GL_SwapBuffers();
+ glc->swap_buffers();
}
-void Engineer::key_press(unsigned, unsigned)
+void Engineer::button_press(int x, int y, unsigned btn, unsigned)
{
-}
-
-void Engineer::button_press(int x, int y, unsigned btn)
-{
- if(main_panel->get_geometry().is_inside(x, y))
+ if(placing_train)
{
- main_panel->button_press(x, y, btn);
- return;
- }
- for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
- if((*i)->get_geometry().is_inside(x, y))
+ if(btn==1 && placing_block && !placing_block->get_train())
{
- (*i)->button_press(x, y, btn);
- return;
- }
+ set_block_color(*placing_block, GL::Color(1, 1, 1));
- Track3D *track=pick_track(x, y);
- if(track)
- {
- if(placing_train)
+ placing_train->place(*placing_block, placing_entry);
+ placing_train = 0;
+ main_panel->set_status_text(string());
+ }
+ else if(btn==3)
{
- Block *block=trfc_man->get_block_by_track(&track->get_track());
+ const vector<Block::Endpoint> &endpoints = placing_block->get_endpoints();
+ placing_entry = (placing_entry+1)%endpoints.size();
}
- else
+ }
+ else
+ {
+ Track3D *track = pick_track(x, screen_h-y-1);
+ if(track)
{
- Turnout *turnout=control.get_turnout(track->get_track().get_turnout_id());
- if(turnout)
- turnout->set_route(1-turnout->get_route());
+ if(unsigned tid=track->get_track().get_turnout_id())
+ {
+ Turnout &turnout = control.get_turnout(tid);
+ try
+ {
+ turnout.set_path((turnout.get_path()+1)%track->get_track().get_type().get_n_paths());
+ main_panel->set_status_text(format("Turnout %d switched", turnout.get_address()));
+ }
+ catch(const TurnoutBusy &e)
+ {
+ main_panel->set_status_text(e.what());
+ }
+ }
+ else if(simulate)
+ {
+ if(unsigned sid=track->get_track().get_sensor_id())
+ {
+ Sensor &sensor = control.get_sensor(sid);
+ control.signal_sensor_event.emit(sid, !sensor.get_state());
+ }
+ }
}
}
}
-void Engineer::button_release(int x, int y, unsigned btn)
+void Engineer::button_release(int, int, unsigned, unsigned)
{
- if(main_panel->get_geometry().is_inside(x, y))
- {
- main_panel->button_release(x, y, btn);
- return;
- }
- for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
- if((*i)->get_geometry().is_inside(x, y))
- {
- (*i)->button_release(x, y, btn);
- return;
- }
}
void Engineer::pointer_motion(int x, int y)
{
- if(main_panel->get_geometry().is_inside(x, y))
+ if(placing_train)
{
- main_panel->pointer_motion(x, y);
- return;
- }
- for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
- if((*i)->get_geometry().is_inside(x, y))
+ Track3D *track = pick_track(x, screen_h-y-1);
+ if(track && placing_train)
{
- (*i)->pointer_motion(x, y);
- return;
+ Block &block = trfc_mgr->get_block_by_track(track->get_track());
+ if(&block!=placing_block)
+ {
+ if(placing_block)
+ reset_block_color(*placing_block);
+ placing_block = █
+ placing_entry = 0;
+ set_block_color(*placing_block, GL::Color(0.5, 1, 0.7));
+ }
}
-
- Track3D *track=pick_track(x, y);
- if(track && track->get_track().get_turnout_id())
- {
- ostringstream ss;
- ss<<"Turnout "<<track->get_track().get_turnout_id();
- status_text=ss.str();
+ else if(track && track->get_track().get_turnout_id())
+ main_panel->set_status_text(format("Turnout %d", track->get_track().get_turnout_id()));
+ else if(!placing_train)
+ main_panel->set_status_text(string());
}
- else
- status_text="";
}
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 mid_x=0;
- float mid_y=0;
+ cam_rot = 0;
+ float best_height = -1;
+ float mid_x = 0;
+ float mid_y = 0;
for(float angle=0; angle<M_PI; angle+=0.01)
{
- float min_x=0;
- float max_x=0;
- float min_y=0;
- float max_y=0;
- for(Track3DSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ float min_x = 0;
+ float max_x = 0;
+ float min_y = 0;
+ float max_y = 0;
+ 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);
- min_y=min(min_y, minp.y);
- max_y=max(max_y, maxp.y);
+ min_x = min(min_x, minp.x);
+ max_x = max(max_x, maxp.x);
+ min_y = min(min_y, minp.y);
+ max_y = max(max_y, maxp.y);
}
- float width=max_x-min_x;
- float height=max_y-min_y;
- height=max(height, width);
+ float width = max_x-min_x;
+ float height = max_y-min_y;
+ height = max(height, width);
if(height<best_height || best_height<0)
{
- cam_rot=angle;
- best_height=height;
- mid_x=(min_x+max_x)/2;
- mid_y=(min_y+max_y)/2;
+ cam_rot = angle;
+ best_height = height;
+ mid_x = (min_x+max_x)/2;
+ mid_y = (min_y+max_y)/2;
}
}
- float c=cos(cam_rot);
- float s=sin(cam_rot);
- cam_pos.x=c*mid_x-s*mid_y;
- cam_pos.y=s*mid_x+c*mid_y;
- cam_pos.z=max(best_height*1.05/0.82843, 0.15);
+ float c = cos(cam_rot);
+ float s = sin(cam_rot);
+ cam_pos.x = c*mid_x-s*mid_y;
+ cam_pos.y = s*mid_x+c*mid_y;
+ cam_pos.z = max(best_height*1.05/0.82843, 0.15);
}
-void Engineer::turnout_route_changed(unsigned)
+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);
}
-void Engineer::sensor_state_changed(bool state, unsigned addr)
+void Engineer::reset_block_color(const Block &block)
{
- const Track3DSeq <racks=layout_3d.get_tracks();
- for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
- if((*i)->get_track().get_sensor_id()==addr)
+ if(unsigned sid=block.get_sensor_id())
+ {
+ Sensor &sensor = control.get_sensor(sid);
+ if(sensor.get_state())
{
- if(state)
- (*i)->set_color(Color(1, 0, 0));
- else
- (*i)->set_color(Color(1, 1, 1));
+ set_block_color(block, GL::Color(1, 0.5, 0.3));
+ return;
}
+ }
+
+ if(block.get_train())
+ set_block_color(block, GL::Color(1, 1, 0.3));
+ else
+ set_block_color(block, GL::Color(1, 1, 1));
+}
+
+void Engineer::sensor_event(bool, Sensor *sensor)
+{
+ const list<Block *> &blocks = trfc_mgr->get_blocks();
+ for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ if((*i)->get_sensor_id()==sensor->get_address())
+ reset_block_color(**i);
+}
+
+void Engineer::block_reserved(const Block &block, const Train *)
+{
+ reset_block_color(block);
}
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();
glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
return layout_3d.pick_track(xx, yy, size);
+}
- /*unsigned select_buf[1024];
- glSelectBuffer(1024, select_buf);
- glRenderMode(GL_SELECT);
-
- //XXX Hardcoded values
- float xn=((float)(x-800)/960)*0.082843;
- float yn=((float)(y-480)/960)*0.082843;
- float size=(float)4/960*0.082843;
-
- project_3d();
- glLoadIdentity();
-
- double clip[4];
- clip[0]=0.1;
- clip[1]=0;
- clip[2]=xn-size;
- clip[3]=0;
- glClipPlane(GL_CLIP_PLANE0, clip);
- glEnable(GL_CLIP_PLANE0);
-
- clip[0]=-0.1;
- clip[2]=-(xn+size);
- glClipPlane(GL_CLIP_PLANE1, clip);
- glEnable(GL_CLIP_PLANE1);
-
- clip[0]=0;
- clip[1]=0.1;
- clip[2]=yn-size;
- glClipPlane(GL_CLIP_PLANE2, clip);
- glEnable(GL_CLIP_PLANE2);
-
- clip[1]=-0.1;
- clip[2]=-(yn+size);
- glClipPlane(GL_CLIP_PLANE3, clip);
- glEnable(GL_CLIP_PLANE3);
-
- glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
- glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
-
- layout_3d.render();
+void Engineer::train_added(Train &train)
+{
+ TrainPanel *tpanel = new TrainPanel(*this, ui_res, train);
+ root->add(*tpanel);
+ int y = main_panel->get_geometry().y;
+ 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);
+ tpanel->set_visible(true);
- glDisable(GL_CLIP_PLANE0);
- glDisable(GL_CLIP_PLANE1);
- glDisable(GL_CLIP_PLANE2);
- glDisable(GL_CLIP_PLANE3);
+ place_train(train);
+}
- unsigned n_records=glRenderMode(GL_RENDER);
- if(n_records)
+void Engineer::sighandler(int sig)
+{
+ if(sig==SIGSEGV || sig==SIGILL || sig==SIGFPE || sig==SIGABRT)
{
- Track *track=0;
- unsigned i=0;
- unsigned track_depth=numeric_limits<unsigned>::max();
- for(unsigned j=0; j<n_records; ++j)
- {
- unsigned ns_size=select_buf[i++];
- unsigned min_depth=select_buf[i++];
- ++i; // Skip max_depth
- if(min_depth<track_depth)
- {
- track=(Track *)select_buf[i+ns_size-1];
- track_depth=min_depth;
- }
- i+=ns_size;
- }
-
- return track;
+ signal(sig, SIG_DFL);
+ IO::print(IO::cerr, "Fatal signal received, terminating\n");
+ const map<unsigned, Locomotive *> &locos = control.get_locomotives();
+ for(map<unsigned, Locomotive *>::const_iterator i=locos.begin(); i!=locos.end(); ++i)
+ i->second->set_speed(0);
+ control.flush();
+ raise(sig);
}
-
- return 0;*/
+ else if(sig==SIGTERM || sig==SIGINT)
+ exit(0);
}
Application::RegApp<Engineer> Engineer::reg;