Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

controller.cxx

Go to the documentation of this file.
00001 //  $Id: controller.cxx,v 1.9 2003/01/11 19:07:48 grumbel Exp $
00002 //
00003 //  Construo - A wire-frame construction game
00004 //  Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include "construo_error.hxx"
00021 #include "controller.hxx"
00022 
00023 Controller* Controller::instance_ = 0;
00024 
00025 Controller::Controller ()
00026 {
00027   instance_  = this;
00028   running    = false;
00029   slow_down  = false;
00030   action_cam = false;
00031   hide_dots  = false;
00032   world      = new World ();
00033 }
00034 
00035 Controller::Controller (const std::string& filename)
00036 {
00037   instance_  = this;
00038   running    = false;
00039   slow_down  = false;
00040   action_cam = false;
00041   hide_dots  = false;
00042   world     = new World (filename);
00043 }
00044 
00045 Controller::~Controller ()
00046 {
00047   instance_ = 0;
00048 }
00049 
00050 void
00051 Controller::load_world (const std::string& filename)
00052 {
00053   if (world)
00054     undo_world_stack.push_back(world);
00055 
00056   std::cout << "Loading World..." << std::endl;
00057   world = new World (filename);
00058   running = false;
00059   std::cout << "Loading World... DONE" << std::endl; 
00060 }
00061 
00062 void
00063 Controller::save_world (const std::string& filename)
00064 {
00065   std::cout << "Saving World..." << std::endl;
00066   world->write_lisp (filename);
00067   std::cout << "Saving World... DONE" << std::endl;
00068 }
00069 
00070 
00071 std::string
00072 Controller::get_slot_filename(int n)
00073 {
00074   return "/user/" + std::string("quicksave") + char('0' + n) + ".construo";
00075 }
00076 
00077 void
00078 Controller::save_to_slot (int n)
00079 {
00080   try {
00081     save_world (get_slot_filename (n));
00082   } catch (ConstruoError& err) {
00083     std::cout << "Controller: Error: " << err.msg << std::endl;
00084   }
00085 }
00086 
00087 void
00088 Controller::load_from_slot (int n)
00089 {
00090   try {
00091     load_world (get_slot_filename (n));
00092   } catch (ConstruoError& err) {
00093     std::cout << "Controller: Error: " << err.msg << std::endl;
00094   }
00095 }
00096 
00097 void
00098 Controller::update ()
00099 {
00100   float delta = delta_manager.getset ();
00101 
00102   if (running)
00103     {
00104       float min_skip;
00105 
00106       if (slow_down)
00107         {
00108           delta /= 50.0f/20.0f;
00109           min_skip = 0.0007f;
00110         }
00111       else
00112         {
00113           delta /= 5.0f/20.0f;
00114           min_skip = 0.02;//; 0.007f;
00115         }
00116       
00117       float i = 0.0f;
00118       while (i < delta)
00119         {
00120           world->update (min_skip);
00121           i += min_skip;
00122         }
00123     }
00124 }
00125 
00126 void
00127 Controller::start_simulation ()
00128 {
00129   if (!running)
00130     undo_world_stack.push_back(world->duplicate());
00131 
00132   if (undo_world_stack.size() > 100)
00133     {
00134       // FIXME: shrink stack here
00135       //delete *undo_world_stack.front();
00136       //std::cout << "Stak
00137     }
00138 
00139   running = !running;
00140 }
00141 
00142 void
00143 Controller::push_undo()
00144 {
00145   undo_world_stack.push_back(world->duplicate());
00146 }
00147 
00148 void
00149 Controller::clear_world ()
00150 {
00151   std::cout << "Controller: Clear" << std::endl;
00152   undo_world_stack.push_back(world);
00153   world = new World ();
00154   running = false;                
00155 }
00156 
00157 void
00158 Controller::undo ()
00159 {
00160   std::cout << "Controller::undo (): undostack: " << undo_world_stack.size() 
00161             << " redostack: " << redo_world_stack.size() << std::endl;
00162 
00163   if (!undo_world_stack.empty())
00164     {
00165       //delete world; // fixme: memory hole
00166       redo_world_stack.push_back (world);
00167       world = undo_world_stack.back();
00168       undo_world_stack.pop_back();
00169       running = false;
00170     }
00171   else
00172     {
00173       std::cout << "Undo stack empty" << std::endl;
00174     }
00175 }
00176 
00177 void
00178 Controller::redo ()
00179 {
00180   if (!redo_world_stack.empty())
00181     {
00182       undo_world_stack.push_back (world);
00183       world = redo_world_stack.back();
00184       redo_world_stack.pop_back();
00185       running = false;
00186     }
00187   else
00188     {
00189       std::cout << "Redo stack empty" << std::endl;
00190     }
00191 }
00192 
00193 void
00194 Controller::set_action_cam(bool a)
00195 {
00196   action_cam = a;
00197 }
00198 
00199 bool
00200 Controller::get_action_cam()
00201 {
00202   return action_cam;
00203 }
00204 
00205 void
00206 Controller::set_hide_dots (bool d)
00207 {
00208   hide_dots = d;
00209 }
00210 
00211 bool
00212 Controller::get_hide_dots ()
00213 {
00214   return hide_dots;
00215 }
00216 
00217 /* EOF */

Generated on Thu Jul 24 10:24:29 2003 for Construo by doxygen1.3-rc3

Rabisu Mirror Service We provide mirrors to support Open source communities. Our mirror server is located in Istanbul/Turkey region.

Please do not hesitate to contact mirror@rabisu.com for new open source mirror submissions.