Adonthell  0.4
adonthell.cc
Go to the documentation of this file.
1 /*
2  $Id: adonthell.cc,v 1.11 2003/01/17 22:05:56 ksterker Exp $
3 
4  Copyright (C) 1999/2000/2001 Alexandre Courbot
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 /**
17  * @file adonthell.cc
18  *
19  * @author Alexandre Courbot
20  * @author Kai Sterker
21  * @brief Implements the adonthell class.
22  */
23 
24 #include "win_manager.h"
25 #include "gametime.h"
26 #include "gamedate.h"
27 #include "adonthell.h"
28 #include "audio.h"
29 
30 // Pointer to the active main loop
31 adonthell *data::engine = NULL;
32 
33 // constructor
35 {
36  letsexit = false;
37  update_map_ = false;
38  control_active_ = false;
39 
40  // load the script taking care of additional game commands
41  control.create_instance ("schedules.control", "control");
42 }
43 
44 // start and execute the game's main loop
45 void adonthell::main (win_base *wnd, const string name)
46 {
47  win_manager mgr;
48 
49  if (wnd != NULL)
50  {
51  mgr.add (wnd /*, name */);
52  mgr.set_focus (wnd);
53  }
54  else
55  {
56  mapview_start ();
57  set_control_active (true);
58  fade_in ();
59  }
60 
61  while (letsexit == false)
62  {
63  main_loop ();
64 
65  // blit the surface to the physical screen
66  screen::show ();
67 
68  // perform operations to keep the game's speed constant
70 
71  // update the internal clock
73  }
74 
75  // only leave one main loop at a time
76  letsexit = false;
77 }
78 
79 // the main loop
81 {
82  input::update ();
83 
84  // check whether music has finished playing
85  if (audio::is_background_finished ()) audio::run_schedule ();
86 
87  // on slower machines, we update several times before drawing,
88  // i.e. we are skipping frames to keep the game's speed constant
89  for (int i = 0; i < gametime::frames_to_skip (); i++)
90  {
91  // grab any user input and update the internal state of
92  // all windows of the current level
94  if (update_map ()) lmap.update ();
96  if (control_active ()) control.run ();
97  }
98 
99  if (!letsexit)
100  {
101  // first clear the screen to avoid artifacts
102  screen::clear ();
103 
104  // draw everything to our display surface
106  }
107 }
108 
109 // quit the main loop
111 {
112  letsexit = true;
113 }
114 
115 // fade the screen out
117 {
118  s_int16 i = 0;
119 
120  while (i < 60)
121  {
122  gametime::update ();
123  i += gametime::frames_to_skip () * 2;
124  if (i > 60) i = 60;
125 
126  main_loop ();
127 
128  screen::transition (i * 2);
129  screen::show ();
130  }
131 }
132 
133 // fade the screen in
135 {
136  s_int16 i = 60;
137 
138  while (i > 0)
139  {
140  gametime::update ();
141  i -= gametime::frames_to_skip () * 2;
142  if (i < 0) i = 0;
143 
144  main_loop ();
145 
146  screen::transition (i * 2);
147  screen::show ();
148  }
149 }
150 
151 // load the engine state
153 {
154  string name;
155 
156  // get the current time
157  gamedate::get_state (file);
158 
159  // Get the map filename
160  name << file;
161  // Load the map from the file
162  lmap.get (file);
163  // Load the map state (events)
164  if (!lmap.get_state (file))
165  return false;
166 
167  view.mapview::attach_map (&lmap);
168 
169  // Load the mapview state
170  view.mapview::get_state (file);
171  view.pack ();
172 
173  return true;
174 }
175 
176 // save the engine state
178 {
179  // save the current time
180  gamedate::put_state (file);
181 
182  // Save the map filename
183  string name = lmap.filename ();
184  name >> file;
185 
186  // Save the map itself
187  lmap.put (file);
188  // Save the map state (events)
189  lmap.put_state (file);
190  // Save the mapview state
191  view.mapview::put_state (file);
192 
193  return 0;
194 }
195 
197 {
198  set_update_map (true);
199 
200  view.mapview::resize (screen::length (), screen::height ());
201  view.mapview::attach_map (&lmap);
202 
203  view.set_visible (true);
204  view.pack ();
205 
206  win_manager::active->add (&view);
207 }
208 
210 {
211  set_update_map (false);
212  view.mapview::detach_map ();
213  win_manager::active->remove (&view);
214 }