]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/engineer.cpp
Convert engineer to use mspgbase instead of SDL
[r2c2.git] / source / engineer / engineer.cpp
index d57deb7e0e431865ee1318cc71000845885718b7..d5f8495171daecd082a2e43d75670c0952335b49 100644 (file)
@@ -1,10 +1,13 @@
 #include <cmath>
 #include <limits>
-#include <SDL.h>
 #include <GL/gl.h>
 #include <msp/core/except.h>
 #include <msp/core/getopt.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/strings/formatter.h>
 #include <msp/strings/lexicalcast.h>
@@ -91,14 +94,13 @@ Engineer::~Engineer()
 
 Train *Engineer::add_train(unsigned addr)
 {
-       /*if(control.get_locomotive(addr))
-               return 0;*/
-
        if(addr==0)
        {
                train_prop=new TrainProperties(*this, ui_res, 0);
+               root->add(*train_prop);
                train_prop->signal_ok.connect(sigc::mem_fun(this, &Engineer::dismiss_train_prop));
                train_prop_stale=false;
+               train_prop->set_visible(true);
 
                return 0;
        }
@@ -108,18 +110,18 @@ Train *Engineer::add_train(unsigned addr)
                Train *train=new Train(*trfc_mgr, *loco);
                train->set_name(format("Train %d", trfc_mgr->get_trains().size()));
 
-               //train_prop=new TrainProperties(ui_res, *train);
-
                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)
                        y-=(*i)->get_geometry().h;
                tpanel->set_position(0, y-tpanel->get_geometry().h);
                train_panels.push_back(tpanel);
+               tpanel->set_visible(true);
 
                placing_train=train;
                placing_block=0;
-               status_text="Select train location";
+               main_panel->set_status_text("Select location");
 
                return train;
        }
@@ -127,24 +129,22 @@ Train *Engineer::add_train(unsigned addr)
 
 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);
 
-       SDL_EnableUNICODE(1);
+       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);
@@ -154,48 +154,38 @@ int Engineer::main()
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
        DataFile::load(ui_res, "engineer.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)
+       {
+               (*i)->set_min_filter(GL::NEAREST);
+               (*i)->set_mag_filter(GL::NEAREST);
+       }
+
        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);
 
-       Application::main();
+       wnd->show();
 
-       delete main_panel;
-       for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
-               delete *i;
+       Application::main();
 
-       SDL_Quit();
+       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, event.key.keysym.unicode);
-                       break;
-               case SDL_QUIT:
-                       exit(0);
-                       break;
-               }
-       }
+       dpy->tick();
 
        control.tick();
+       trfc_mgr->tick();
 
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 
@@ -238,41 +228,42 @@ void Engineer::tick()
 
        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::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::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();
        }
 
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       glOrtho(0, screen_w, 0, screen_h, 0, 1);
-       glMatrixMode(GL_MODELVIEW);
-       glLoadIdentity();
+       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();
-       if(train_prop)
-               train_prop->render();
+       root->render();
 
        if(train_prop_stale)
        {
@@ -280,14 +271,7 @@ void Engineer::tick()
                train_prop=0;
        }
 
-       const GL::Font &font=ui_res.get_default_font();
-       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();
+       glc->swap_buffers();
 }
 
 void Engineer::key_press(unsigned key, unsigned mod, wchar_t ch)
@@ -296,25 +280,8 @@ void Engineer::key_press(unsigned key, unsigned mod, wchar_t ch)
                train_prop->key_press(key, mod, ch);
 }
 
-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)
@@ -323,6 +290,7 @@ void Engineer::button_press(int x, int y, unsigned btn)
 
                        placing_train->place(placing_block, placing_entry);
                        placing_train=0;
+                       main_panel->set_status_text(string());
                }
                else if(btn==3)
                {
@@ -332,7 +300,7 @@ void Engineer::button_press(int x, int y, unsigned btn)
        }
        else
        {
-               Track3D *track=pick_track(x, y);
+               Track3D *track=pick_track(x, screen_h-y-1);
                if(track)
                {
                        if(unsigned tid=track->get_track().get_turnout_id())
@@ -352,66 +320,32 @@ void Engineer::button_press(int x, int y, unsigned btn)
        }
 }
 
-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-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, screen_h-y-1);
+               if(track && placing_train)
                {
-                       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)
+                                       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));
+                       }
                }
+               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 if(track && track->get_track().get_turnout_id())
-       {
-               ostringstream ss;
-               ss<<"Turnout "<<track->get_track().get_turnout_id();
-               status_text=ss.str();
-       }
-       else if(!placing_train)
-               status_text="";
 }
 
 void Engineer::view_all()
@@ -471,8 +405,11 @@ void Engineer::sensor_event(bool state, Sensor *sensor)
        for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
                if((*i)->get_track().get_sensor_id()==sensor->get_address())
                {
+                       Block &block=trfc_mgr->get_block_by_track((*i)->get_track());
                        if(state)
                                (*i)->set_color(Color(1, 0.5, 0.3));
+                       else if(block.get_train())
+                               set_block_color(block, Color(1, 1, 0.3));
                        else
                                (*i)->set_color(Color(1, 1, 1));
                }
@@ -483,7 +420,6 @@ void Engineer::block_reserved(const Block &block, const Train *train)
        if(unsigned sid=block.get_sensor_id())
        {
                Sensor &sensor=control.get_sensor(sid);
-               cout<<block.get_sensor_id()<<' '<<&sensor<<' '<<sensor.get_state()<<'\n';
                if(sensor.get_state())
                        return;
        }