+#include <algorithm>
#include <cmath>
+#include <cstdlib>
#include <limits>
-#include <SDL.h>
-#include <GL/gl.h>
-#include <msp/core/error.h>
-#include <msp/core/getopt.h>
-#include <msp/strings/lexicalcast.h>
-#include <msp/strings/regex.h>
+#include <signal.h>
+#include <msp/fs/stat.h>
+#include <msp/fs/utils.h>
+#include <msp/graphics/display.h>
+#include <msp/graphics/window.h>
+#include <msp/gl/blend.h>
+#include <msp/gl/framebuffer.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/misc.h>
+#include <msp/gl/tests.h>
+#include <msp/gltk/floatingarrangement.h>
+#include <msp/io/print.h>
+#include <msp/strings/format.h>
+#include <msp/time/units.h>
+#include <msp/time/utils.h>
+#include "libr2c2/driver.h"
+#include "libr2c2/trackcircuit.h"
+#include "libr2c2/tracktype.h"
+#include "3d/allocation.h"
+#include "3d/path.h"
+#include "3d/track.h"
+#include "3d/trackcircuit.h"
+#include "3d/vehicle.h"
+#include "dynamicdialog.h"
#include "engineer.h"
-#include "mainpanel.h"
-#include "trainpanel.h"
+#include "mainwindow.h"
+#include "newtraindialog.h"
+#include "traindialog.h"
using namespace std;
-using namespace Marklin;
+using namespace R2C2;
using namespace Msp;
-#include <iostream>
-
Engineer::Engineer(int argc, char **argv):
- screen_w(1280),
- screen_h(960),
- fullscreen(false),
- layout(catalogue),
+ options(argc, argv),
+ window(options.screen_w, options.screen_h, options.fullscreen),
+ keyboard(window),
+ mouse(window),
+ ui_res("data/r2c2.res"),
+ import_active(false),
+ layout(catalogue, (options.driver.empty() ? 0 : Driver::create(options.driver))),
layout_3d(layout),
- no_lighting(false),
- placing_train(0)
+ server(0),
+ main_view(layout_3d, window.get_width(), window.get_height()),
+ emergency_blink_state(0)
{
- string res;
- bool debug=false;
- string device="/dev/ttyS0";
- unsigned quality=4;
-
- GetOpt getopt;
- getopt.add_option('r', "resolution", res, GetOpt::REQUIRED_ARG);
- getopt.add_option('f', "fullscreen", fullscreen, GetOpt::NO_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( "no-lighting", no_lighting, GetOpt::NO_ARG);
- getopt(argc, argv);
-
- if(!res.empty())
+ // Setup GUI
+ window.set_title("Railroad Engineer");
+ window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
+
+ root = new GLtk::Root(ui_res, &window, &keyboard, &mouse);
+ GLtk::Layout *root_layout = new GLtk::Layout;
+ root->set_layout(root_layout);
+ root_layout->set_margin(GLtk::Sides());
+ root_arrangement = new GLtk::FloatingArrangement(*root_layout);
+ mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Engineer::button_press), false));
+ mouse.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Engineer::axis_motion), false));
+ root->set_visible(true);
+
+ main_wnd = new MainWindow(*this);
+ root->add(*main_wnd);
+ main_wnd->autosize();
+ main_wnd->set_position(0, window.get_height()-main_wnd->get_geometry().h);
+
+ overlay = new Overlay3D(ui_res.get_default_font());
+
+ // Setup railroad control
+ catalogue.add_source("data/Märklin/H0");
+ DataFile::load(layout, options.layout_fn);
+
+ if(layout.has_driver())
{
- 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);
- }
- else
- throw UsageError("Invalid resolution");
+ Driver &driver = layout.get_driver();
+ driver.signal_locomotive_detected.connect(sigc::mem_fun(this, &Engineer::locomotive_detected));
+ driver.signal_halt.connect(sigc::mem_fun(this, &Engineer::halt_event));
+ }
+ layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
+ layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::emergency));
+ const set<Block *> &blocks = layout.get_all<Block>();
+ for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ if(TrackCircuit *tc = (*i)->get_sensor())
+ new TrackCircuit3D(layout_3d, *tc);
+
+ const set<Track *> &tracks = layout.get_all<Track>();
+ for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ if((*i)->get_type().is_turnout())
+ new Path3D(layout_3d.get_3d(**i));
+
+ if(FS::exists(options.state_fn))
+ DataFile::load(layout, options.state_fn);
+
+ if(options.network)
+ {
+ server = new Server(layout);
+ server->use_event_dispatcher(event_disp);
}
- if(device!="none")
- control.open(device);
+ // Setup 3D view
+ GL::Pipeline &pipeline = main_view.get_pipeline();
- control.set_debug(debug);
+ GL::Pipeline::Pass *pass = &pipeline.add_pass("unlit");
+ pass->set_depth_test(&GL::DepthTest::lequal());
+ pipeline.add_renderable_for_pass(layout_3d.get_path_scene(), "unlit");
- layout_3d.set_quality(quality);
+ pass = &pipeline.add_pass("overlay");
+ pass->set_blend(&GL::Blend::alpha());
+ pipeline.add_renderable_for_pass(*overlay, "overlay");
- catalogue.load("tracks.dat");
+ view_all();
- const list<string> &args=getopt.get_args();
- if(args.empty())
- throw UsageError("No layout given");
- layout.load(args.front());
+ // Catch various signals so we can stop the trains in case we get terminated
+ catch_signal(SIGINT);
+ catch_signal(SIGTERM);
+ catch_signal(SIGSEGV);
+ catch_signal(SIGILL);
+ catch_signal(SIGFPE);
+ catch_signal(SIGABRT);
+}
- trfc_mgr=new TrafficManager(control, layout);
+Engineer::~Engineer()
+{
+ if(!options.simulate)
+ {
+ layout.save_dynamic(options.state_fn+".tmp");
+ FS::rename(options.state_fn+".tmp", options.state_fn);
+ }
- view_all();
+ layout.get_driver().halt(true);
+ layout.get_driver().flush();
- /*const TrackSeq &tracks=layout.get_tracks();
+ delete overlay;
+ delete root_arrangement;
+ delete root;
- 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));
- }
- }*/
+ delete server;
}
-Engineer::~Engineer()
+void Engineer::set_status(const string &text)
{
+ main_wnd->set_status_text(text);
+ status_timeout = Time::now()+10*Time::sec;
}
-void Engineer::add_train(unsigned addr)
+void Engineer::add_dynamic_dialog(DynamicDialog &tv)
{
- if(control.get_locomotive(addr))
- return;
+ dyn_dialogs.push_back(&tv);
+}
- Locomotive *loco=new Locomotive(control, addr);
- Train *train=new Train(*trfc_mgr, *loco);
+void Engineer::remove_dynamic_dialog(DynamicDialog &tv)
+{
+ dyn_dialogs.erase(remove(dyn_dialogs.begin(), dyn_dialogs.end(), &tv), dyn_dialogs.end());
+}
- 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);
+int Engineer::main()
+{
+ window.show();
- placing_train=train;
+ return Application::main();
}
-int Engineer::main()
+void Engineer::tick()
{
- 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");
-
- glEnableClientState(GL_VERTEX_ARRAY);
- glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
- glEnable(GL_COLOR_MATERIAL);
- glDepthFunc(GL_LEQUAL);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-
- font=new GL::Font();
- if(screen_w>=1024)
+ window.get_display().tick();
+
+ for(list<Train *>::iterator i=new_trains.begin(); i!=new_trains.end(); ++i)
+ process_new_train(**i);
+ new_trains.clear();
+
+ layout.tick(options.sim_speed);
+ layout_3d.tick();
+ event_disp.tick(Time::zero);
+
+ for(list<DynamicDialog *>::iterator i=dyn_dialogs.begin(); i!=dyn_dialogs.end(); ++i)
+ (*i)->update();
+
+ Time::TimeStamp t = Time::now();
+ if(status_timeout && t>status_timeout)
{
- font_size=20;
- Parser::load(*font, "dejavu-20.font");
+ main_wnd->set_status_text(string());
+ status_timeout = Time::TimeStamp();
}
- else
+ if(!emergencies.empty() && t>emergency_blink_timeout)
{
- font_size=12;
- Parser::load(*font, "dejavu-12.font");
+ emergency_blink_state = (emergency_blink_state+1)%2;
+ GL::Color color(1.0f/(1+emergency_blink_state), 0.0f, 0.0f);
+ for(list<TrackChain3D *>::iterator i=emergencies.begin(); i!=emergencies.end(); ++i)
+ (*i)->set_color(color);
+ emergency_blink_timeout = t+0.5*Time::sec;
}
- 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);
-
- Application::main();
+ GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
- delete font;
- delete main_panel;
+ main_view.render();
- SDL_Quit();
+ root->render();
- return exit_code;
+ window.swap_buffers();
}
-void Engineer::tick()
+void Engineer::button_press(unsigned btn)
{
- //cout<<"tick\n";
-
- SDL_Event event;
- while(SDL_PollEvent(&event))
+ if(btn==1)
{
- switch(event.type)
+ Object *obj = pick_object(pointer);
+ if(Track *track = dynamic_cast<Track *>(obj))
+ {
+ if(track->get_type().is_turnout())
+ {
+ Block &block = track->get_block();
+ if(block.get_train() && block.get_train()->is_block_critical(block))
+ set_status("Turnout is busy");
+ else
+ {
+ unsigned paths = track->get_type().get_paths();
+ unsigned i = track->get_active_path()+1;
+ while(!(paths&(1<<i)))
+ {
+ if(!(paths>>i))
+ i = 0;
+ else
+ ++i;
+ }
+ track->set_active_path(i);
+ set_status(format("Turnout %d", track->get_turnout_address()));
+ }
+ }
+ if(unsigned saddr = track->get_sensor_address())
+ {
+ if(options.simulate)
+ layout.get_driver().set_sensor(saddr, !layout.get_driver().get_sensor(saddr));
+ set_status(format("Sensor %d", saddr));
+ }
+ }
+ else if(Vehicle *veh = dynamic_cast<Vehicle *>(obj))
{
- 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;
+ TrainDialog *dlg = new TrainDialog(*this, *veh->get_train());
+ root->add(*dlg);
+ dlg->autosize();
}
}
+}
- control.tick();
-
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
-
- project_3d();
- glLoadIdentity();
- glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
- glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
+void Engineer::axis_motion(unsigned axis, float value, float)
+{
+ if(axis==0)
+ pointer.x = value;
+ if(axis==1)
+ pointer.y = value;
+}
- if(!no_lighting)
+void Engineer::view_all()
+{
+ const Layout3D::ObjectMap &objects = layout_3d.get_all();
+
+ float view_aspect = float(window.get_width())/window.get_height();
+ float view_height = tan(main_view.get_camera().get_field_of_view()/2.0f)*2.0f;
+ float best_score = 0;
+ GL::Vector3 pos;
+ GL::Vector3 up;
+ for(Angle angle; angle<Angle::half_turn(); angle+=Angle::from_radians(0.01))
{
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- float params[4];
- params[0]=0;
- params[1]=-0.2;
- params[2]=1;
- params[3]=0;
- glLightfv(GL_LIGHT0, GL_POSITION, params);
- }
+ Transform trans = Transform::rotation(-angle, Vector(0, 0, 1));
+ BoundingBox bbox;
+ for(Layout3D::ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
+ bbox = bbox|trans.transform(i->second->get_object().get_bounding_box());
- //glEnable(GL_DEPTH_TEST);
- glEnable(GL_MULTISAMPLE);
+ const Vector &minp = bbox.get_minimum_point();
+ const Vector &maxp = bbox.get_maximum_point();
- layout_3d.render();
+ float width = maxp.x-minp.x;
+ float height = maxp.y-minp.y;
+ float aspect = width/height;
+ float score = min(aspect/view_aspect, view_aspect/aspect);
- 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)
- {
- Track &track=(*i)->get_track();
- if(track.get_turnout_id())
+ if(score>best_score)
{
- Turnout *trnt=control.get_turnout(track.get_turnout_id());
- if(trnt)
- (*i)->render_route(trnt->get_route());
- else
- (*i)->render_route(-1);
- }
- else
- (*i)->render_route(-1);
- }
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, screen_w, 0, screen_h, 0, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- glDisable(GL_DEPTH_TEST);
- glDisable(GL_LIGHTING);
- glDisable(GL_MULTISAMPLE);
+ best_score = score;
- main_panel->render();
- for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
- (*i)->render();
+ float size = max(width/view_aspect, height);
+ float c = cos(angle);
+ float s = sin(angle);
+ float x = (minp.x+maxp.x)/2;
+ float y = (minp.y+maxp.y)/2;
+ float z = max(size*1.05/view_height, 0.15);
- glLoadIdentity();
- glTranslatef(340, 10, 0);
- glScalef(20, 20, 20);
- glColor4f(1, 1, 1, 1);
- font->draw_string(status_text);
+ pos = GL::Vector3(c*x-s*y, s*x+c*y, z);
+ up = GL::Vector3(-s, c, 0);
+ }
+ }
- SDL_GL_SwapBuffers();
+ GL::Camera &camera = main_view.get_camera();
+ camera.set_position(pos);
+ camera.set_up_direction(up);
+ camera.set_look_direction(GL::Vector3(0, 0, -1));
+ camera.set_aspect(float(window.get_width())/window.get_height());
+ camera.set_depth_clip(pos.z*0.5, pos.z*1.5);
}
-void Engineer::key_press(unsigned, unsigned)
+Object *Engineer::pick_object(const Vector &p)
{
+ const GL::Vector3 &start = main_view.get_camera().get_position();
+ GL::Vector4 ray = main_view.get_camera().unproject(GL::Vector4(p.x, p.y, 0, 0));
+
+ // XXX Do this better; make this function a template?
+ if(Vehicle *veh = layout.pick<Vehicle>(Ray(start, ray.slice<3>(0))))
+ return veh;
+ return layout.pick<Track>(Ray(start, ray.slice<3>(0)));
}
-void Engineer::button_press(int x, int y, unsigned btn)
+void Engineer::emergency(Block *block, const string &msg)
{
- if(main_panel->get_geometry().is_inside(x, y))
+ set_status(msg);
+ if(block)
{
- main_panel->button_press(x, y, btn);
- return;
+ TrackChain3D *tch3d = new TrackChain3D(layout_3d, *block);
+ tch3d->set_color(GL::Color(1.0f, 0.0f, 0.0f));
+ tch3d->set_layer(2);
+ emergencies.push_back(tch3d);
}
- for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
- if((*i)->get_geometry().is_inside(x, y))
- {
- (*i)->button_press(x, y, btn);
- return;
- }
+}
- Track3D *track=pick_track(x, y);
- if(track)
+void Engineer::halt_event(bool h)
+{
+ if(!h)
{
- if(placing_train)
- {
- Block *block=trfc_man->get_block_by_track(&track->get_track());
- }
- else
- {
- Turnout *turnout=control.get_turnout(track->get_track().get_turnout_id());
- if(turnout)
- turnout->set_route(1-turnout->get_route());
- }
+ for(list<TrackChain3D *>::iterator i=emergencies.begin(); i!=emergencies.end(); ++i)
+ delete *i;
+ emergencies.clear();
}
}
-void Engineer::button_release(int x, int y, unsigned btn)
+void Engineer::locomotive_detected(const Driver::DetectedLocomotive &loco)
{
- if(main_panel->get_geometry().is_inside(x, y))
+ if(!import_active)
{
- main_panel->button_release(x, y, btn);
- return;
+ NewTrainDialog *dlg = new NewTrainDialog(*this);
+ dlg->prefill(loco);
+ dlg->signal_response.connect(sigc::mem_fun(this, &Engineer::import_finished));
+ root->add(*dlg);
+ import_active = true;
}
- 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)
+void Engineer::import_finished(int)
{
- if(main_panel->get_geometry().is_inside(x, y))
- {
- 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))
- {
- (*i)->pointer_motion(x, y);
- return;
- }
+ import_active = false;
+}
- 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
- status_text="";
+void Engineer::process_new_train(Train &train)
+{
+ Vehicle3D &loco3d = layout_3d.get_3d(train.get_vehicle(0));
+ overlay->set_label(loco3d, train.get_name());
+ train.signal_name_changed.connect(sigc::bind<0>(sigc::mem_fun(overlay, &Overlay3D::set_label), sigc::ref(loco3d)));
}
-void Engineer::view_all()
+void Engineer::train_added(Train &train)
{
- const Track3DSeq &tracks=layout_3d.get_tracks();
+ new_trains.push_back(&train);
- 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)
+ GL::Color best_color;
+ float best_d_sq = 0;
+ for(unsigned i=0; i<10; ++i)
{
- 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)
+ GL::Color color;
+ unsigned h = rand()%3;
+ color.r = (h==0 ? 0.0 : rand()*1.0/RAND_MAX);
+ color.g = (h==1 ? 0.0 : rand()*1.0/RAND_MAX);
+ color.b = (h==2 ? 0.0 : rand()*1.0/RAND_MAX);
+ color = color*(1/max(max(color.r, color.g), color.b));
+ float min_d_sq = 3;
+ for(map<Train *, GL::Color>::const_iterator j=train_colors.begin(); j!=train_colors.end(); ++j)
{
- 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);
+ float dr = color.r-j->second.r;
+ float dg = color.g-j->second.g;
+ float db = color.b-j->second.b;
+ float d_sq = dr*dr+dg*dg+db*db;
+ if(d_sq<min_d_sq)
+ min_d_sq = d_sq;
}
-
- float width=max_x-min_x;
- float height=max_y-min_y;
- height=max(height, width);
-
- if(height<best_height || best_height<0)
+ if(min_d_sq>best_d_sq)
{
- cam_rot=angle;
- best_height=height;
- mid_x=(min_x+max_x)/2;
- mid_y=(min_y+max_y)/2;
+ best_color = color;
+ best_d_sq = min_d_sq;
}
}
+ train_colors[&train] = best_color;
- 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::sensor_state_changed(bool state, unsigned addr)
-{
- 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(state)
- (*i)->set_color(Color(1, 0, 0));
- else
- (*i)->set_color(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);
- glMatrixMode(GL_MODELVIEW);
+ Allocation3D *alloc = new Allocation3D(layout_3d, train);
+ alloc->set_color(best_color);
}
-Track3D *Engineer::pick_track(int x, int y)
+void Engineer::sighandler(int sig)
{
- 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;
-
- project_3d();
- glLoadIdentity();
- glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
- 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();
-
- glDisable(GL_CLIP_PLANE0);
- glDisable(GL_CLIP_PLANE1);
- glDisable(GL_CLIP_PLANE2);
- glDisable(GL_CLIP_PLANE3);
-
- unsigned n_records=glRenderMode(GL_RENDER);
- if(n_records)
+ 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");
+ layout.get_driver().halt(true);
+ layout.get_driver().flush();
+ raise(sig);
}
-
- return 0;*/
+ else if(sig==SIGTERM || sig==SIGINT)
+ exit(0);
}
-
-Application::RegApp<Engineer> Engineer::reg;