X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fengineer%2Fengineer.cpp;h=87a4d6d3afa6c4a75f424ce804e4c3b49491e3e4;hb=bc8ac89bbe774bb133b758416182aa18e5e0a5a5;hp=d57deb7e0e431865ee1318cc71000845885718b7;hpb=52cbe8d99669f843f8f75c51128e2748584dd03a;p=r2c2.git diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index d57deb7..87a4d6d 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -1,14 +1,33 @@ +/* $Id$ + +This file is part of the MSP Märklin suite +Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + #include #include -#include -#include +#include #include #include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include #include +#include #include #include #include +#include +#include "libmarklin/except.h" +#include "libmarklin/tracktype.h" #include "engineer.h" #include "mainpanel.h" #include "trainpanel.h" @@ -18,333 +37,244 @@ using namespace std; using namespace Marklin; using namespace Msp; -#include - Engineer::Engineer(int argc, char **argv): - screen_w(1280), - screen_h(960), - fullscreen(false), layout(catalogue), layout_3d(layout), - no_lighting(false), + server(0), placing_train(0), placing_block(0), placing_entry(0), - simulate(false), - train_prop(0), - train_prop_stale(false) + simulate(false) { + // Parse options + unsigned screen_w = 1280; + unsigned screen_h = 960; + bool fullscreen = false; string res; - bool debug=false; - string device="/dev/ttyS0"; - unsigned quality=4; + bool debug = false; + string device = "/dev/ttyS0"; + bool network = false; 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('s', "simulate", simulate, GetOpt::NO_ARG); - getopt.add_option( "no-lighting", no_lighting, GetOpt::NO_ARG); + getopt.add_option('n', "network", network, GetOpt::NO_ARG); getopt(argc, argv); + const vector &args = getopt.get_args(); + if(args.empty()) + throw UsageError("No layout given"); + if(!res.empty()) { if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res)) { - screen_w=lexical_cast(m[1].str); - screen_h=lexical_cast(m[2].str); + screen_w = lexical_cast(m[1].str); + screen_h = lexical_cast(m[2].str); } else throw UsageError("Invalid resolution"); } - if(device!="none") - control.open(device); + // Setup GUI + window = new Graphics::SimpleGLWindow(screen_w, screen_h, fullscreen); + window->set_title("Railroad Engineer"); + window->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0)); - control.set_debug(debug); + DataFile::load(ui_res, "marklin.res"); + root = new GLtk::Root(ui_res, *window); + root->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press)); + root->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion)); + root->set_visible(true); - layout_3d.set_quality(quality); + main_panel = new MainPanel(*this, ui_res); + root->add(*main_panel); + main_panel->set_position(0, window->get_height()-main_panel->get_geometry().h); + main_panel->set_visible(true); - catalogue.load("tracks.dat"); + // Setup railroad control + DataFile::load(catalogue, "tracks.dat"); + DataFile::load(catalogue, "locos.dat"); + DataFile::load(layout, args.front()); - const vector &args=getopt.get_args(); - if(args.empty()) - throw UsageError("No layout given"); - layout.load(args.front()); + if(device!="none") + control.open(device); + + control.set_debug(debug); - trfc_mgr=new TrafficManager(control, layout); + trfc_mgr = new TrafficManager(control, layout); + layout_3d.set_traffic_manager(*trfc_mgr); + 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)); + if(FS::exists("engineer.state")) + DataFile::load(*trfc_mgr, "engineer.state"); + + if(network) + { + server = new Server(*trfc_mgr); + server->use_event_dispatcher(event_disp); + } - const map &sensors=control.get_sensors(); + const map &sensors = control.get_sensors(); for(map::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)); - view_all(); -} - -Engineer::~Engineer() -{ - delete trfc_mgr; -} - -Train *Engineer::add_train(unsigned addr) -{ - /*if(control.get_locomotive(addr)) - return 0;*/ + // Setup 3D view + DataFile::load(arrow_mesh, "arrow.mesh"); - if(addr==0) - { - train_prop=new TrainProperties(*this, ui_res, 0); - train_prop->signal_ok.connect(sigc::mem_fun(this, &Engineer::dismiss_train_prop)); - train_prop_stale=false; + overlay = new Overlay3D(*window, camera, ui_res.get_default_font()); - return 0; - } - else - { - Locomotive *loco=new Locomotive(control, addr); - Train *train=new Train(*trfc_mgr, *loco); - train->set_name(format("Train %d", trfc_mgr->get_trains().size())); + pipeline = new GL::Pipeline(window->get_width(), window->get_height(), false); + pipeline->set_camera(&camera); + pipeline->add_renderable(layout_3d.get_scene()); - //train_prop=new TrainProperties(ui_res, *train); + light.set_position(0, -0.259, 0.966, 0); + lighting.attach(0, light); - 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); + GL::PipelinePass &pass = pipeline->add_pass(0); + pass.depth_test = &GL::DepthTest::lequal(); + pass.lighting = &lighting; - placing_train=train; - placing_block=0; - status_text="Select train location"; + view_all(); - return train; - } + // 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); } -int Engineer::main() +Engineer::~Engineer() { - 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"); + const list &trains = trfc_mgr->get_trains(); + for(list::const_iterator i=trains.begin(); i!=trains.end(); ++i) + (*i)->set_speed(0); - SDL_EnableUNICODE(1); + while(control.get_queue_length()) + control.tick(); - 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); + if(!simulate) + trfc_mgr->save("engineer.state"); - DataFile::load(ui_res, "engineer.res"); - main_panel=new MainPanel(*this, ui_res); - main_panel->set_position(0, screen_h-main_panel->get_geometry().h); + delete pipeline; + delete overlay; + delete root; + delete window; - Application::main(); + delete trfc_mgr; + delete server; +} - delete main_panel; - for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) - delete *i; +void Engineer::place_train(Train &train) +{ + placing_train = &train; + placing_block = 0; + main_panel->set_status_text("Select location"); +} - SDL_Quit(); +int Engineer::main() +{ + window->show(); - return exit_code; + return Application::main(); } 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, event.key.keysym.unicode); - break; - case SDL_QUIT: - exit(0); - break; - } - } + window->get_display().tick(); control.tick(); + trfc_mgr->tick(); + event_disp.tick(Time::zero); - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + for(list::iterator i=new_trains.begin(); i!=new_trains.end(); ++i) + overlay->set_label(layout_3d.get_train(**i), (*i)->get_name()); + new_trains.clear(); - project_3d(); - glLoadIdentity(); - glRotatef(-cam_rot*180/M_PI, 0, 0, 1); - glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z); + GL::clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT); - if(!no_lighting) + pipeline->render_all(); { - 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); - } - - //glEnable(GL_DEPTH_TEST); - glEnable(GL_MULTISAMPLE); - - layout_3d.render(); - - 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()) - { - Turnout &trnt=control.get_turnout(track.get_turnout_id()); - (*i)->render_route(trnt.get_route()); - } - else - (*i)->render_route(-1); + GL::Bind blend(GL::Blend::alpha()); + overlay->render(0); } if(placing_train && placing_block) { - 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::push_matrix(); - GL::translate(pos.x, pos.y, pos.z+0.03); - GL::rotate(rot*180/M_PI+180, 0, 0, 1); - GL::Texture::unbind(); - glColor4f(1, 1, 1, 1); - glBegin(GL_TRIANGLE_FAN); - glVertex2f(0.08, 0); - glVertex2f(0.05, 0.03); - glVertex2f(0.05, 0.01); - glVertex2f(0, 0.01); - glVertex2f(0, -0.01); - glVertex2f(0.05, -0.01); - glVertex2f(0.05, -0.03); - glEnd(); - GL::pop_matrix(); - } - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0, screen_w, 0, screen_h, 0, 1); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); + GL::PushMatrix push_mat; - glDisable(GL_DEPTH_TEST); - glDisable(GL_LIGHTING); - glDisable(GL_MULTISAMPLE); + 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); - main_panel->render(); - for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) - (*i)->render(); - if(train_prop) - train_prop->render(); + GL::translate(pos.x, pos.y, pos.z+0.03); + GL::rotate(rot*180/M_PI+180, 0, 0, 1); - if(train_prop_stale) - { - delete train_prop; - train_prop=0; + arrow_mesh.draw(); } - const GL::Font &font=ui_res.get_default_font(); + const GLtk::Geometry &rgeom = root->get_geometry(); + GL::matrix_mode(GL::PROJECTION); + GL::load_identity(); + GL::ortho_bottomleft(rgeom.w, rgeom.h); + GL::matrix_mode(GL::MODELVIEW); GL::load_identity(); - GL::translate(340, 10, 0); - GL::scale_uniform(font.get_default_size()); - glColor4f(1, 1, 1, 1); - font.draw_string(status_text); - SDL_GL_SwapBuffers(); -} + { + GL::Bind blend(GL::Blend::alpha()); + root->render(); + GL::Texture::unbind(); + } -void Engineer::key_press(unsigned key, unsigned mod, wchar_t ch) -{ - if(train_prop) - train_prop->key_press(key, mod, ch); + window->swap_buffers(); } -void Engineer::button_press(int x, int y, unsigned btn) +void Engineer::button_press(int x, int y, unsigned btn, unsigned) { - if(main_panel->get_geometry().is_inside(x, y)) - { - main_panel->button_press(x, y-main_panel->get_geometry().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_press(x, y, btn); - return; - } - if(train_prop && train_prop->get_geometry().is_inside(x, y)) - { - train_prop->button_press(x, y, btn); - return; - } - if(placing_train) { - if(btn==1 && placing_block) + if(btn==1 && placing_block && !placing_block->get_train()) { - set_block_color(*placing_block, Color(1, 1, 1)); + reset_block_color(*placing_block); - placing_train->place(placing_block, placing_entry); - placing_train=0; + placing_train->place(*placing_block, placing_entry); + placing_train = 0; + main_panel->set_status_text(string()); } else if(btn==3) { - const vector &endpoints=placing_block->get_endpoints(); - placing_entry=(placing_entry+1)%endpoints.size(); + const vector &endpoints = placing_block->get_endpoints(); + placing_entry = (placing_entry+1)%endpoints.size(); } } else { - Track3D *track=pick_track(x, y); + Track3D *track = pick_track(x, window->get_height()-y-1); if(track) { if(unsigned tid=track->get_track().get_turnout_id()) { - Turnout &turnout=control.get_turnout(tid); - turnout.set_route(1-turnout.get_route()); + 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); + Sensor &sensor = control.get_sensor(sid); control.signal_sensor_event.emit(sid, !sensor.get_state()); } } @@ -352,175 +282,154 @@ void Engineer::button_press(int x, int y, unsigned btn) } } -void Engineer::button_release(int x, int y, unsigned btn) -{ - if(main_panel->get_geometry().is_inside(x, y)) - { - main_panel->button_release(x, y-main_panel->get_geometry().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; - } - if(train_prop && train_prop->get_geometry().is_inside(x, y)) - { - train_prop->button_release(x, y, btn); - return; - } -} - void Engineer::pointer_motion(int x, int y) { - 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; - } - if(train_prop && train_prop->get_geometry().is_inside(x, y)) - { - train_prop->pointer_motion(x, y); - return; - } - - Track3D *track=pick_track(x, y); - if(track && placing_train) + if(placing_train) { - Block *block=trfc_mgr->get_block_by_track(&track->get_track()); - if(block!=placing_block) + Track3D *track = pick_track(x, window->get_height()-y-1); + if(track) { - if(placing_block) - set_block_color(*placing_block, Color(1, 1, 1)); - placing_block=block; - placing_entry=0; - set_block_color(*placing_block, Color(0.5, 1, 0.7)); + 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)); + } } } - else if(track && track->get_track().get_turnout_id()) - { - ostringstream ss; - ss<<"Turnout "<get_track().get_turnout_id(); - status_text=ss.str(); - } - else if(!placing_train) - status_text=""; } void Engineer::view_all() { - const Track3DSeq &tracks=layout_3d.get_tracks(); + const list &tracks = layout_3d.get_tracks(); - cam_rot=0; - float best_height=-1; - float mid_x=0; - float mid_y=0; + float view_aspect = float(window->get_width()-200)/window->get_height(); + float view_height = tan(camera.get_field_of_view()/2)*2; + float best_score = 0; + GL::Vector3 pos; + GL::Vector3 up; for(float angle=0; angle::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; + float aspect = width/height; + float score = min(aspect/view_aspect, view_aspect/aspect); - if(heightbest_score) { - cam_rot=angle; - best_height=height; - mid_x=(min_x+max_x)/2; - mid_y=(min_y+max_y)/2; + best_score = score; + + float size = max(width/view_aspect, height); + float c = cos(angle); + float s = sin(angle); + float x = (min_x+max_x)/2-size*105/window->get_height(); + float y = (min_y+max_y)/2; + float z = max(size*1.05/view_height, 0.15); + + pos = GL::Vector3(c*x-s*y, s*x+c*y, z); + up = GL::Vector3(-s, c, 0); } } - 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); + camera.set_position(pos); + camera.set_up_direction(up); + camera.set_look_direction(GL::Vector3(0, 0, -1)); } -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 &tracks=block.get_tracks(); - for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - layout_3d.get_track(*i)->set_color(color); + (void)block; + (void)color; } -void Engineer::sensor_event(bool state, Sensor *sensor) -{ - const list <racks=layout_3d.get_tracks(); - for(list::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i) - if((*i)->get_track().get_sensor_id()==sensor->get_address()) - { - if(state) - (*i)->set_color(Color(1, 0.5, 0.3)); - else - (*i)->set_color(Color(1, 1, 1)); - } -} - -void Engineer::block_reserved(const Block &block, const Train *train) +void Engineer::reset_block_color(const Block &block) { if(unsigned sid=block.get_sensor_id()) { - Sensor &sensor=control.get_sensor(sid); - cout< &blocks = trfc_mgr->get_blocks(); + for(list::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); } 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 view_height = tan(camera.get_field_of_view()/2)*2; + float xx = ((float(x)-window->get_width()/2)/window->get_height())*view_height; + float yy = (float(y)/window->get_height()-0.5)*view_height; + float size = 4.0/window->get_height()*view_height; - project_3d(); - glLoadIdentity(); - glRotatef(-cam_rot*180/M_PI, 0, 0, 1); - glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z); + camera.apply(); return layout_3d.pick_track(xx, yy, size); } -void Engineer::dismiss_train_prop() +void Engineer::train_added(Train &train) { - train_prop_stale=true; + TrainPanel *tpanel = new TrainPanel(*this, ui_res, train); + root->add(*tpanel); + int y = main_panel->get_geometry().y; + for(list::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); + + new_trains.push_back(&train); +} + +void Engineer::sighandler(int sig) +{ + if(sig==SIGSEGV || sig==SIGILL || sig==SIGFPE || sig==SIGABRT) + { + signal(sig, SIG_DFL); + IO::print(IO::cerr, "Fatal signal received, terminating\n"); + const map &locos = control.get_locomotives(); + for(map::const_iterator i=locos.begin(); i!=locos.end(); ++i) + i->second->set_speed(0); + control.flush(); + raise(sig); + } + else if(sig==SIGTERM || sig==SIGINT) + exit(0); } Application::RegApp Engineer::reg;