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
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 }
static bool get_state(igzstream &in)
Load the state of the gamedate class from disk.
Definition: gamedate.cc:57
void mapview_start()
Definition: adonthell.cc:196
void main(win_base *wnd=NULL, const string name="")
Starts the main loop.
Definition: adonthell.cc:45
Class to write data from a Gzip compressed file.
Definition: fileops.h:223
void fade_in()
Fades in from a black screen.
Definition: adonthell.cc:134
Class to read data from a Gzip compressed file.
Definition: fileops.h:131
void fade_out()
Fades the screen to black.
Definition: adonthell.cc:116
void update()
Update the state of all top level windows.
Definition: win_manager.cc:164
static void update()
Update the input state.
Definition: input.cc:63
static win_manager * active
Pointer to the active, i.e.
Definition: win_manager.h:143
void set_visible(const bool b)
Set the visible parameter.
Definition: win_base.h:136
s_int8 put_state(ogzstream &file) const
Saves the landmap&#39;s state into an opened file.
Definition: landmap.cc:232
Declares the gametime class.
static u_int16 length()
Returns the length of the screen.
Definition: screen.h:63
s_int8 put(ogzstream &file) const
Put a map into an opened file.
Definition: landmap.cc:144
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:49
void main_quit()
Quit the main loop.
Definition: adonthell.cc:110
bool create_instance(string file, string classname, PyObject *args=NULL)
Creates an instance of a Python class.
Definition: py_object.cc:53
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:61
void remove(win_base *wnd)
Remove a window from the window manager.
Definition: win_manager.cc:119
static u_int8 frames_to_skip()
Returns the number of updates to perform before drawing the next frame.
Definition: gametime.h:113
static void clear()
Totally clears the screen with black.
Definition: screen.h:98
void run(PyObject *args=NULL)
Calls the run () method of this object.
Definition: py_object.h:121
void update()
Update the entire map (mapcharacters, mapobjects, etc...
Definition: landmap.cc:65
s_int8 get(igzstream &file)
Load a map from an opened file.
Definition: landmap.cc:78
adonthell()
Standard constructor.
Definition: adonthell.cc:34
s_int8 get_state(igzstream &file)
Restore the engine&#39;s state.
Definition: adonthell.cc:152
void set_focus(win_base *wnd)
Gives the input focus to wnd.
Definition: win_manager.cc:180
void set_control_active(bool c)
Set whether the control script should be executed or not.
Definition: adonthell.h:151
Declares the gamedate class.
#define s_int16
16 bits long signed integer
Definition: types.h:41
void draw()
Draws all windows.
Definition: win_manager.cc:146
string filename() const
Get the filename of the map, i.e the file from which it has been loaded (if any). ...
Definition: landmap.h:115
void main_loop()
The actual main loop.
Definition: adonthell.cc:80
void set_update_map(bool u)
Definition: adonthell.h:176
static void transition(u_int16 i)
Make a nice transition effect.
Definition: screen.cc:160
static void put_state(ogzstream &out)
Save the state of the gamedate class to disk.
Definition: gamedate.cc:66
static void show()
Ensure the framebuffer is copied to the physical screen.
Definition: screen.cc:110
void mapview_stop()
Definition: adonthell.cc:209
static void update()
Update the game date.
Definition: gamedate.cc:36
Declares the win_manager class.
void add(win_base *wnd)
Add a window to the window manager.
Definition: win_manager.cc:101
s_int8 put_state(ogzstream &file)
Save the engine&#39;s state.
Definition: adonthell.cc:177
s_int8 get_state(igzstream &file)
Restore the landmap&#39;s state from an opened file.
Definition: landmap.cc:207
Common properties for each win_base&#39;s object.
Definition: win_base.h:47
#define s_int8
8 bits long signed integer
Definition: types.h:38
void input_update()
Checks for user input.
Definition: win_manager.cc:157
bool update_map()
Definition: adonthell.h:168
This is the heart of the Adonthell engine.
Definition: adonthell.h:40
adonthell * engine
Engine used during the game.
Definition: adonthell.cc:31
static u_int16 height()
Returns the height of the screen.
Definition: screen.h:71
bool control_active()
Returns whether the control script is active or not.
Definition: adonthell.h:136