Rebuild autotool system
[construo.git] / controller.cxx
blob57711bf75c12acc21070ec5fb4552b6392dea7dc
1 // $Id: controller.cxx,v 1.12 2003/07/27 18:46:11 grumbel Exp $
2 //
3 // Construo - A wire-frame construction game
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "worldview_component.hxx"
21 #include "construo_error.hxx"
22 #include "controller.hxx"
24 Controller* Controller::instance_ = 0;
26 Controller::Controller ()
28 instance_ = this;
29 running = false;
30 slow_down = false;
31 action_cam = false;
32 hide_dots = false;
33 world = new World ();
36 Controller::Controller (const std::string& filename)
38 instance_ = this;
39 running = false;
40 slow_down = false;
41 action_cam = false;
42 hide_dots = false;
43 world = new World (filename);
46 Controller::~Controller ()
48 instance_ = 0;
51 void
52 Controller::load_world (const std::string& filename)
54 if (world)
55 undo_world_stack.push_back(world);
57 std::cout << "Loading World..." << std::endl;
58 world = new World (filename);
60 WorldViewComponent::instance()->on_world_change();
62 running = false;
63 std::cout << "Loading World... DONE" << std::endl;
66 void
67 Controller::save_world (const std::string& filename)
69 std::cout << "Saving World..." << std::endl;
70 world->write_lisp (filename);
71 std::cout << "Saving World... DONE" << std::endl;
75 std::string
76 Controller::get_slot_filename(int n)
78 return "/user/" + std::string("quicksave") + char('0' + n) + ".construo";
81 void
82 Controller::save_to_slot (int n)
84 try {
85 save_world (get_slot_filename (n));
86 } catch (ConstruoError& err) {
87 std::cout << "Controller: Error: " << err.msg << std::endl;
91 void
92 Controller::load_from_slot (int n)
94 try {
95 load_world (get_slot_filename (n));
96 } catch (ConstruoError& err) {
97 std::cout << "Controller: Error: " << err.msg << std::endl;
101 void
102 Controller::update ()
104 float delta = delta_manager.getset ();
106 if (running)
108 float min_skip;
110 if (slow_down)
112 delta /= 50.0f/20.0f;
113 min_skip = 0.0007f;
115 else
117 delta /= 5.0f/20.0f;
118 min_skip = 0.02; // 0.02
121 float i = 0.0f;
122 while (i < delta)
124 world->update (min_skip);
125 i += min_skip;
130 void
131 Controller::start_simulation ()
133 if (!running)
134 undo_world_stack.push_back(world->duplicate());
136 if (undo_world_stack.size() > 100)
138 // FIXME: shrink stack here
139 //delete *undo_world_stack.front();
140 //std::cout << "Stak
143 running = !running;
146 void
147 Controller::push_undo()
149 undo_world_stack.push_back(world->duplicate());
152 void
153 Controller::clear_world ()
155 std::cout << "Controller: Clear" << std::endl;
156 undo_world_stack.push_back(world);
157 world = new World ();
158 running = false;
161 void
162 Controller::undo ()
164 #ifdef DEBUG
165 std::cout << "Controller::undo (): undostack: " << undo_world_stack.size()
166 << " redostack: " << redo_world_stack.size() << std::endl;
167 #endif
169 if (!undo_world_stack.empty())
171 //delete world; // fixme: memory hole
172 redo_world_stack.push_back (world);
173 world = undo_world_stack.back();
174 undo_world_stack.pop_back();
175 running = false;
177 else
179 std::cout << "Undo stack empty" << std::endl;
183 void
184 Controller::redo ()
186 if (!redo_world_stack.empty())
188 undo_world_stack.push_back (world);
189 world = redo_world_stack.back();
190 redo_world_stack.pop_back();
191 running = false;
193 else
195 std::cout << "Redo stack empty" << std::endl;
199 void
200 Controller::set_action_cam(bool a)
202 action_cam = a;
205 bool
206 Controller::get_action_cam()
208 return action_cam;
211 void
212 Controller::set_hide_dots (bool d)
214 hide_dots = d;
217 bool
218 Controller::get_hide_dots ()
220 return hide_dots;
223 /* EOF */