Adonthell  0.4
adonthell.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 1999/2000/2001 Alexandre Courbot
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 /**
21  * @file adonthell.cc
22  *
23  * @author Alexandre Courbot
24  * @author Kai Sterker
25  * @brief Implements the adonthell class.
26  */
27 
28 #include "win_manager.h"
29 #include "gametime.h"
30 #include "gamedate.h"
31 #include "adonthell.h"
32 #include "audio.h"
33 
34 // Pointer to the active main loop
36 
37 // constructor
39 {
40  letsexit = false;
41  update_map_ = false;
42  control_active_ = false;
43 
44  // load the script taking care of additional game commands
45  control.create_instance ("schedules.control", "control");
46 }
47 
48 // start and execute the game's main loop
49 void adonthell::main (win_base *wnd, const string name)
50 {
51  win_manager mgr;
52 
53  if (wnd != NULL)
54  {
55  mgr.add (wnd /*, name */);
56  mgr.set_focus (wnd);
57  }
58  else
59  {
60  mapview_start ();
61  set_control_active (true);
62  fade_in ();
63  }
64 
65  while (letsexit == false)
66  {
67  main_loop ();
68 
69  // blit the surface to the physical screen
70  screen::show ();
71 
72  // perform operations to keep the game's speed constant
74 
75  // update the internal clock
77  }
78 
79  // only leave one main loop at a time
80  letsexit = false;
81 }
82 
83 // the main loop
85 {
86  input::update ();
87 
88  // check whether music has finished playing
89  if (audio::is_background_finished ()) audio::run_schedule ();
90 
91  // on slower machines, we update several times before drawing,
92  // i.e. we are skipping frames to keep the game's speed constant
93  for (int i = 0; i < gametime::frames_to_skip (); i++)
94  {
95  // grab any user input and update the internal state of
96  // all windows of the current level
98  if (update_map ()) lmap.update ();
100  if (control_active ()) control.run ();
101  }
102 
103  if (!letsexit)
104  {
105  // first clear the screen to avoid artifacts
106  screen::clear ();
107 
108  // draw everything to our display surface
110  }
111 }
112 
113 // quit the main loop
115 {
116  letsexit = true;
117 }
118 
119 // fade the screen out
121 {
122  s_int16 i = 0;
123 
124  while (i < 60)
125  {
126  gametime::update ();
127  i += gametime::frames_to_skip () * 2;
128  if (i > 60) i = 60;
129 
130  main_loop ();
131 
132  screen::transition (i * 2);
133  screen::show ();
134  }
135 }
136 
137 // fade the screen in
139 {
140  s_int16 i = 60;
141 
142  while (i > 0)
143  {
144  gametime::update ();
145  i -= gametime::frames_to_skip () * 2;
146  if (i < 0) i = 0;
147 
148  main_loop ();
149 
150  if (letsexit)
151  {
152  // if we're fading in while a window is closing
153  // ensure that everything is rendered properly
154  screen::clear ();
156  }
157 
158  screen::transition (i * 2);
159  screen::show ();
160  }
161 }
162 
163 // load the engine state
165 {
166  string name;
167 
168  // get the current time
169  gamedate::get_state (file);
170 
171  // Get the map filename
172  name << file;
173  // Load the map from the file
174  lmap.get (file);
175  // Load the map state (events)
176  if (!lmap.get_state (file))
177  return false;
178 
179  view.mapview::attach_map (&lmap);
180 
181  // Load the mapview state
182  view.mapview::get_state (file);
183  view.pack ();
184 
185  return true;
186 }
187 
188 // save the engine state
190 {
191  // save the current time
192  gamedate::put_state (file);
193 
194  // Save the map filename
195  string name = lmap.filename ();
196  name >> file;
197 
198  // Save the map itself
199  lmap.put (file);
200  // Save the map state (events)
201  lmap.put_state (file);
202  // Save the mapview state
203  view.mapview::put_state (file);
204 
205  return 0;
206 }
207 
209 {
210  set_update_map (true);
211 
212  view.mapview::resize (screen::length (), screen::height ());
213  view.mapview::attach_map (&lmap);
214 
215  view.set_visible (true);
216  view.pack ();
217 
218  win_manager::active->add (&view);
219 }
220 
222 {
223  set_update_map (false);
224  view.mapview::detach_map ();
225  win_manager::active->remove (&view);
226 }
static bool get_state(igzstream &in)
Load the state of the gamedate class from disk.
Definition: gamedate.cc:61
void mapview_start()
Definition: adonthell.cc:208
void main(win_base *wnd=NULL, const string name="")
Starts the main loop.
Definition: adonthell.cc:49
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
void fade_in()
Fades in from a black screen.
Definition: adonthell.cc:138
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
void fade_out()
Fades the screen to black.
Definition: adonthell.cc:120
void update()
Update the state of all top level windows.
Definition: win_manager.cc:173
static void update()
Update the input state.
Definition: input.cc:64
static win_manager * active
Pointer to the active, i.e.
Definition: win_manager.h:147
void set_visible(const bool b)
Set the visible parameter.
Definition: win_base.h:140
static void show()
Ensure the framebuffer is copied to the physical screen.
Definition: screen.h:161
s_int8 put_state(ogzstream &file) const
Saves the landmap&#39;s state into an opened file.
Definition: landmap.cc:236
Declares the gametime class.
static u_int16 length()
Returns the length of the screen.
Definition: screen.h:84
s_int8 put(ogzstream &file) const
Put a map into an opened file.
Definition: landmap.cc:148
static void update()
Call this after each run of the main loop to sync the game&#39;s speed to the machine it is running on...
Definition: gametime.cc:53
void main_quit()
Quit the main loop.
Definition: adonthell.cc:114
bool create_instance(string file, string classname, PyObject *args=NULL)
Creates an instance of a Python class.
Definition: py_object.cc:57
Declares the adonthell class.
The window manager takes care of basic GUI functions, such as input focus, window state updates and d...
Definition: win_manager.h:65
void remove(win_base *wnd)
Remove a window from the window manager.
Definition: win_manager.cc:128
static u_int8 frames_to_skip()
Returns the number of updates to perform before drawing the next frame.
Definition: gametime.h:117
static void clear()
Totally clears the screen with black.
Definition: screen.h:143
void run(PyObject *args=NULL)
Calls the run () method of this object.
Definition: py_object.h:125
void update()
Update the entire map (mapcharacters, mapobjects, etc...
Definition: landmap.cc:69
s_int8 get(igzstream &file)
Load a map from an opened file.
Definition: landmap.cc:82
adonthell()
Standard constructor.
Definition: adonthell.cc:38
s_int8 get_state(igzstream &file)
Restore the engine&#39;s state.
Definition: adonthell.cc:164
void set_focus(win_base *wnd)
Gives the input focus to wnd.
Definition: win_manager.cc:189
void set_control_active(bool c)
Set whether the control script should be executed or not.
Definition: adonthell.h:155
Declares the gamedate class.
#define s_int16
16 bits long signed integer
Definition: types.h:47
void draw()
Draws all windows.
Definition: win_manager.cc:155
string filename() const
Get the filename of the map, i.e the file from which it has been loaded (if any). ...
Definition: landmap.h:119
void main_loop()
The actual main loop.
Definition: adonthell.cc:84
void set_update_map(bool u)
Definition: adonthell.h:180
static void transition(u_int16 i)
Make a nice transition effect.
Definition: screen.cc:347
static void put_state(ogzstream &out)
Save the state of the gamedate class to disk.
Definition: gamedate.cc:70
void mapview_stop()
Definition: adonthell.cc:221
static void update()
Update the game date.
Definition: gamedate.cc:40
Declares the win_manager class.
void add(win_base *wnd)
Add a window to the window manager.
Definition: win_manager.cc:110
s_int8 put_state(ogzstream &file)
Save the engine&#39;s state.
Definition: adonthell.cc:189
s_int8 get_state(igzstream &file)
Restore the landmap&#39;s state from an opened file.
Definition: landmap.cc:211
Common properties for each win_base&#39;s object.
Definition: win_base.h:51
#define s_int8
8 bits long signed integer
Definition: types.h:44
void input_update()
Checks for user input.
Definition: win_manager.cc:166
bool update_map()
Definition: adonthell.h:172
This is the heart of the Adonthell engine.
Definition: adonthell.h:44
adonthell * engine
Engine used during the game.
Definition: adonthell.cc:35
static u_int16 height()
Returns the height of the screen.
Definition: screen.h:92
bool control_active()
Returns whether the control script is active or not.
Definition: adonthell.h:140