]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/engineer.cpp
Miscellaneous compilation fixes
[r2c2.git] / source / engineer / engineer.cpp
index 8aea6f68ace07ad70360cd27bdb3fd874ccd913a..c396b5ea2491c04c7d1f4489823ce6ef5747fd95 100644 (file)
@@ -1,29 +1,20 @@
-/* $Id$
-
-This file is part of R²C²
-Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
 #include <algorithm>
 #include <cmath>
 #include <cstdlib>
 #include <limits>
 #include <signal.h>
-#include <msp/core/except.h>
 #include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
-#include <msp/gbase/display.h>
-#include <msp/gbase/window.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/projection.h>
 #include <msp/gl/tests.h>
-#include <msp/gl/transform.h>
 #include <msp/io/print.h>
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
 #include <msp/time/units.h>
 #include <msp/time/utils.h>
 #include "libr2c2/driver.h"
@@ -41,11 +32,12 @@ using namespace std;
 using namespace R2C2;
 using namespace Msp;
 
-Application::RegApp<Engineer> Engineer::reg;
-
 Engineer::Engineer(int argc, char **argv):
        options(argc, argv),
        window(options.screen_w, options.screen_h, options.fullscreen),
+       keyboard(window),
+       mouse(window),
+       ui_res("r2c2.res"),
        layout(catalogue, (options.driver.empty() ? 0 : Driver::create(options.driver))),
        layout_3d(layout),
        server(0),
@@ -54,18 +46,15 @@ Engineer::Engineer(int argc, char **argv):
        picking_track(0),
        picking_entry(0),
        picking_path(0),
-       pointer_x(0),
-       pointer_y(0),
        pointer_moved(false)
 {
        // Setup GUI
        window.set_title("Railroad Engineer");
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
 
-       DataFile::load(ui_res, "r2c2.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));
+       mouse.signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press));
+       mouse.signal_axis_motion.connect(sigc::mem_fun(this, &Engineer::axis_motion));
        root->set_visible(true);
 
        main_panel = new MainPanel(*this);
@@ -82,9 +71,10 @@ Engineer::Engineer(int argc, char **argv):
        DataFile::load(layout, options.layout_fn);
 
        layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
-       layout.signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
+       layout.signal_block_reserved.connect(sigc::hide<1>(sigc::mem_fun(this, &Engineer::reset_block_color)));
+       layout.signal_block_state_changed.connect(sigc::hide<1>(sigc::mem_fun(this, &Engineer::reset_block_color)));
        layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::set_status));
-       layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Engineer::sensor_event));
+
        if(FS::exists(options.state_fn))
                DataFile::load(layout, options.state_fn);
 
@@ -100,19 +90,22 @@ Engineer::Engineer(int argc, char **argv):
        pipeline.set_camera(&camera);
        pipeline.add_renderable_for_pass(layout_3d.get_scene(), 0);
        pipeline.add_renderable_for_pass(layout_3d.get_path_scene(), "unlit");
-       pipeline.add_renderable_for_pass(*overlay, "unlit");
+       pipeline.add_renderable_for_pass(*overlay, "overlay");
 
        light.set_position(GL::Vector4(0, -0.259, 0.966, 0));
        light.set_diffuse(GL::Color(0.9));
        lighting.set_ambient(GL::Color(0.4));
        lighting.attach(0, light);
 
-       GL::PipelinePass *pass = &pipeline.add_pass(0);
-       pass->depth_test = &GL::DepthTest::lequal();
-       pass->lighting = &lighting;
+       GL::Pipeline::Pass *pass = &pipeline.add_pass(0);
+       pass->set_depth_test(&GL::DepthTest::lequal());
+       pass->set_lighting(&lighting);
 
        pass = &pipeline.add_pass("unlit");
-       pass->depth_test = &GL::DepthTest::lequal();
+       pass->set_depth_test(&GL::DepthTest::lequal());
+
+       pass = &pipeline.add_pass("overlay");
+       pass->set_blend(&GL::Blend::alpha());
 
        view_all();
 
@@ -202,7 +195,7 @@ void Engineer::tick()
 
        GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
 
-       pipeline.render_all();
+       pipeline.render();
 
        if(pointer_moved)
        {
@@ -210,7 +203,7 @@ void Engineer::tick()
 
                if(picking)
                {
-                       Track *track = pick_track(pointer_x, window.get_height()-pointer_y-1);
+                       Track *track = pick_track(pointer);
                        if(track && track!=picking_track)
                        {
                                picking_track = track;
@@ -231,34 +224,23 @@ void Engineer::tick()
 
        if(picking && picking_track && picking_entry>=0)
        {
-               GL::PushMatrix push_mat;
+               GL::MatrixStack::Push push_mat(GL::MatrixStack::modelview());
 
                float rot = picking_track->get_endpoint_direction(picking_entry);
                Vector pos = picking_track->get_endpoint_position(picking_entry);
 
-               GL::translate(pos.x, pos.y, pos.z+0.03);
-               GL::rotate(rot*180/M_PI+180, 0, 0, 1);
+               GL::MatrixStack::modelview() *= GL::Matrix::translation(pos.x, pos.y, pos.z+0.03);
+               GL::MatrixStack::modelview() *= GL::Matrix::rotation(rot+M_PI, 0, 0, 1);
 
                arrow_mesh.draw();
        }
 
-       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::Bind blend(GL::Blend::alpha());
-               root->render();
-               GL::Texture::unbind();
-       }
+       root->render();
 
        window.swap_buffers();
 }
 
-void Engineer::button_press(int x, int y, unsigned btn, unsigned)
+void Engineer::button_press(unsigned btn)
 {
        if(picking)
        {
@@ -277,7 +259,7 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned)
        }
        else
        {
-               Track *track = pick_track(x, window.get_height()-y-1);
+               Track *track = pick_track(pointer);
                if(track)
                {
                        if(track->get_turnout_id())
@@ -310,10 +292,12 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned)
        }
 }
 
-void Engineer::pointer_motion(int x, int y)
+void Engineer::axis_motion(unsigned axis, float value, float)
 {
-       pointer_x = x;
-       pointer_y = y;
+       if(axis==0)
+               pointer.x = value;
+       if(axis==1)
+               pointer.y = value;
        pointer_moved = true;
 }
 
@@ -379,9 +363,7 @@ void Engineer::set_block_color(const Block &block, const GL::Color &color)
 
 void Engineer::reset_block_color(const Block &block)
 {
-       bool active = false;
-       if(unsigned sid=block.get_sensor_id())
-               active = layout.get_driver().get_sensor(sid);
+       bool active = block.get_state()>Block::INACTIVE;
 
        if(block.get_train())
        {
@@ -401,25 +383,10 @@ void Engineer::reset_block_color(const Block &block)
                set_block_color(block, GL::Color(1));
 }
 
-void Engineer::sensor_event(unsigned addr, bool)
-{
-       const set<Block *> &blocks = layout.get_blocks();
-       for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
-               if((*i)->get_sensor_id()==addr)
-                       reset_block_color(**i);
-}
-
-void Engineer::block_reserved(const Block &block, const Train *)
-{
-       reset_block_color(block);
-}
-
-Track *Engineer::pick_track(int x, int y)
+Track *Engineer::pick_track(const Vector &p)
 {
        const GL::Vector3 &start = camera.get_position();
-       float xx = x*2.0/window.get_width()-1.0;
-       float yy = y*2.0/window.get_height()-1.0;
-       GL::Vector4 ray = camera.unproject(GL::Vector4(xx, yy, 0, 0));
+       GL::Vector4 ray = camera.unproject(GL::Vector4(p.x, p.y, 0, 0));
 
        return layout.pick_track(Vector(start.x, start.y, start.z), Vector(ray.x, ray.y, ray.z));
 }