~ubuntu-branches/ubuntu/precise/pingus/precise

« back to all changes in this revision

Viewing changes to src/screen/screen_manager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-02-28 19:44:25 UTC
  • mfrom: (4.1.4 hardy)
  • Revision ID: james.westby@ubuntu.com-20080228194425-e8ilohlijv02kgcf
Tags: 0.7.2-2
* Fix FTBFS with gcc-4.3 by adding the missing include in
  src/input/evdev_device.cpp (Closes: #462238):
   + debian/patches/20_fix_FTBFS_with_gcc-4.3.
* Rename former patch so that the filename reflects the order in which
  the patches are applied:
   - debian/patches/data_dir.patch
   + debian/patches/10_fix_data_directory.
* Bump Standards-Version from 3.7.2 to 3.7.3, no changes needed.
* Add a dh_desktop call in the arch-dep part of debian/rules.
* Adjust the “missing-dep-for-interpreter guile” override since lintian
  now lists an alternative for that dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: screen_manager.cpp 3269 2007-10-01 04:24:14Z grumbel $
 
2
//
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 2000 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.
 
10
//
 
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.
 
15
//
 
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.
 
19
 
 
20
#include "SDL.h"
 
21
#include <iostream>
 
22
#include "../globals.hpp"
 
23
#include "math/size.hpp"
 
24
#include "pathname.hpp"
 
25
#include "display/cursor.hpp"
 
26
#include "display/display.hpp"
 
27
#include "screen_manager.hpp"
 
28
#include "../path_manager.hpp"
 
29
#include "screenshot.hpp"
 
30
#include "../display/drawing_context.hpp"
 
31
#include "../input/controller.hpp"
 
32
#include "../input/manager.hpp"
 
33
 
 
34
ScreenManager* ScreenManager::instance_ = 0;
 
35
 
 
36
ScreenManager::ScreenManager()
 
37
{
 
38
  display_gc = new DrawingContext();
 
39
 
 
40
  cached_action = CA_NONE;
 
41
}
 
42
 
 
43
ScreenManager::~ScreenManager ()
 
44
{
 
45
  delete display_gc;
 
46
}
 
47
 
 
48
void
 
49
ScreenManager::display()
 
50
{
 
51
  Input::Manager input_manager;
 
52
 
 
53
  Input::Controller* input_controller = 0;
 
54
 
 
55
  if (controller_file.empty())
 
56
    input_controller = input_manager.create_controller(Pathname("controller/default.scm", 
 
57
                                                                Pathname::DATA_PATH));
 
58
  else
 
59
    input_controller = input_manager.create_controller(Pathname(controller_file,
 
60
                                                                Pathname::SYSTEM_PATH));
 
61
 
 
62
  Cursor* cursor = 0;
 
63
  if (swcursor_enabled)
 
64
    {
 
65
      cursor = new Cursor("core/cursors/animcross");
 
66
      Display::add_flip_screen_hook(cursor);
 
67
      SDL_ShowCursor(SDL_DISABLE);
 
68
    }
 
69
 
 
70
  DeltaManager delta_manager;
 
71
 
 
72
  // Main loop for the menu
 
73
  while (!screens.empty())
 
74
    {
 
75
      float time_delta = delta_manager.getset();
 
76
 
 
77
      if (time_delta > 1.0)
 
78
        {
 
79
          if (maintainer_mode)
 
80
            std::cout << "ScreenManager: detected large delta (" << time_delta
 
81
                      << "), ignoring and doing frameskip" << std::endl;
 
82
          continue;
 
83
        }
 
84
 
 
85
      input_manager.update(time_delta);
 
86
 
 
87
      // Fill the delta with values
 
88
      GameDelta delta(time_delta, delta_manager.get_absolute(),  
 
89
                      input_controller->poll_events());
 
90
 
 
91
      last_screen = get_current_screen();
 
92
 
 
93
      // Most likly the screen will get changed in this update call
 
94
      get_current_screen()->update (delta);
 
95
 
 
96
      if (cursor)
 
97
        cursor->update(time_delta);
 
98
 
 
99
      // Last screen has poped, so we are going to end here
 
100
      if (screens.empty())
 
101
        continue;
 
102
 
 
103
      while (cached_action != CA_NONE)
 
104
        {
 
105
          switch (cached_action)
 
106
            {
 
107
            case CA_POP:
 
108
              real_pop_screen();
 
109
              break;
 
110
            case CA_POP_ALL:
 
111
              real_pop_all_screens();
 
112
              break;
 
113
            case CA_REPLACE:
 
114
              real_replace_screen(replace_screen_arg);
 
115
              break;
 
116
            case CA_CLEAR:
 
117
              real_clear();
 
118
              break;
 
119
            default:
 
120
              break;
 
121
            }
 
122
        }
 
123
 
 
124
      // FIXME: is there a more gentel way to do that instead of spreading the checks all around here?
 
125
      // Last screen has poped, so we are going to end here
 
126
      if (screens.empty())
 
127
        continue;
 
128
 
 
129
      // skip draw if the screen changed to avoid glitches
 
130
      if (last_screen == get_current_screen() || fast_mode)
 
131
        {
 
132
          if (get_current_screen()->draw(*display_gc))
 
133
            {
 
134
              display_gc->render(Display::get_screen(), Rect(Vector2i(0,0), Size(Display::get_width(),
 
135
                                                                                     Display::get_height())));
 
136
              Display::flip_display ();
 
137
              display_gc->clear();
 
138
            }
 
139
        }
 
140
      else
 
141
        {
 
142
          //std::cout << "ScreenManager: fading screens" << std::endl;
 
143
          fade_over(last_screen, get_current_screen());
 
144
        }
 
145
 
 
146
      // Stupid hack to make this thing take less CPU
 
147
      SDL_Delay(1);
 
148
    }
 
149
 
 
150
  Display::remove_flip_screen_hook(cursor);
 
151
  delete cursor;
 
152
  delete input_controller;
 
153
}
 
154
 
 
155
ScreenPtr&
 
156
ScreenManager::get_current_screen()
 
157
{
 
158
  assert(!screens.empty());
 
159
  return screens.back ();
 
160
}
 
161
 
 
162
ScreenManager*
 
163
ScreenManager::instance ()
 
164
{
 
165
  if (instance_)
 
166
    return instance_;
 
167
  else
 
168
    return instance_ = new ScreenManager ();
 
169
}
 
170
 
 
171
void
 
172
ScreenManager::push_screen (Screen* screen, bool delete_screen)
 
173
{
 
174
  if (!screens.empty())
 
175
    {
 
176
      screens.back ()->on_shutdown ();
 
177
    }
 
178
 
 
179
  screens.push_back (ScreenPtr(screen, delete_screen));
 
180
  screen->on_startup ();
 
181
}
 
182
 
 
183
void
 
184
ScreenManager::pop_screen ()
 
185
{
 
186
  assert (cached_action == CA_NONE || cached_action == CA_POP);
 
187
  cached_action = CA_POP;
 
188
}
 
189
 
 
190
void
 
191
ScreenManager::pop_all_screens()
 
192
{
 
193
  assert(cached_action == CA_NONE);
 
194
  cached_action = CA_POP_ALL;
 
195
}
 
196
 
 
197
void
 
198
ScreenManager::replace_screen (Screen* screen, bool delete_screen)
 
199
{
 
200
  assert (cached_action == CA_NONE);
 
201
  cached_action = CA_REPLACE;
 
202
  replace_screen_arg = ScreenPtr(screen, delete_screen);
 
203
}
 
204
 
 
205
void
 
206
ScreenManager::real_replace_screen (const ScreenPtr& ptr)
 
207
{
 
208
  cached_action = CA_NONE;
 
209
  screens.back ()->on_shutdown ();
 
210
  screens.back () = ptr;
 
211
  screens.back ()->on_startup ();
 
212
}
 
213
 
 
214
void
 
215
ScreenManager::real_pop_screen ()
 
216
{
 
217
  cached_action = CA_NONE;
 
218
  ScreenPtr back = screens.back ();
 
219
  screens.pop_back();
 
220
  back->on_shutdown();
 
221
 
 
222
  if (!screens.empty ())
 
223
    {
 
224
      screens.back()->on_startup ();
 
225
    }
 
226
}
 
227
 
 
228
void
 
229
ScreenManager::real_pop_all_screens()
 
230
{
 
231
  cached_action = CA_NONE;
 
232
  ScreenPtr back = screens.back();
 
233
  screens.pop_back();
 
234
  back->on_shutdown();
 
235
 
 
236
  screens.clear();
 
237
}
 
238
 
 
239
void
 
240
ScreenManager::clear()
 
241
{
 
242
  cached_action = CA_CLEAR;
 
243
}
 
244
 
 
245
void
 
246
ScreenManager::real_clear()
 
247
{
 
248
  cached_action = CA_NONE;
 
249
  screens.clear();
 
250
}
 
251
 
 
252
void
 
253
ScreenManager::fade_over (ScreenPtr& old_screen, ScreenPtr& new_screen)
 
254
{
 
255
  DeltaManager delta_manager;
 
256
  float passed_time = 0;
 
257
 
 
258
  float progress = 0.0f;
 
259
  while (progress <= 1.0f)
 
260
    {
 
261
      float time_delta = delta_manager.getset ();
 
262
      passed_time += time_delta;
 
263
 
 
264
      int border_x = int((Display::get_width()/2)  * (1.0f - progress));
 
265
      int border_y = int((Display::get_height()/2) * (1.0f - progress));
 
266
 
 
267
      old_screen->draw(*display_gc);
 
268
      display_gc->render(Display::get_screen(), Rect(Vector2i(0,0), Size(Display::get_width(),
 
269
                                                                         Display::get_height())));
 
270
      display_gc->clear();
 
271
      
 
272
      Display::push_cliprect(Rect(Vector2i(0 + border_x, 0 + border_y),
 
273
                                  Size(screen_width  - 2*border_x, screen_height - 2*border_y)));
 
274
 
 
275
      new_screen->draw(*display_gc);
 
276
      display_gc->render(Display::get_screen(), Rect(Vector2i(0,0), Size(Display::get_width(),
 
277
                                                                         Display::get_height())));
 
278
      display_gc->clear();
 
279
 
 
280
      //GameDelta delta (time_delta, CL_System::get_time(), events);
 
281
      // FIXME: Animation looks nifty but doesn't work all that good
 
282
      //new_screen->update (delta);
 
283
      //old_screen->update (delta);
 
284
      
 
285
      Display::pop_cliprect();
 
286
      Display::flip_display ();
 
287
      display_gc->clear();
 
288
      
 
289
      progress = passed_time/1.0f;
 
290
    }
 
291
}
 
292
 
 
293
void
 
294
ScreenManager::resize(const Size& size)
 
295
{
 
296
  display_gc->set_rect(Rect(Vector2i(0, 0), size));
 
297
 
 
298
  // FIXME: Calling this causes horrible flicker, any better way to resize the screen?
 
299
  Display::set_video_mode(size.width, size.height);
 
300
 
 
301
  get_current_screen()->resize(size);
 
302
}
 
303
 
 
304
Screen*
 
305
ScreenManager::get_screen()
 
306
{
 
307
  return get_current_screen().get();
 
308
}
 
309
 
 
310
void
 
311
ScreenManager::init()
 
312
{
 
313
  instance_ = 0;
 
314
}
 
315
 
 
316
void
 
317
ScreenManager::deinit()
 
318
{
 
319
  delete instance_;
 
320
  instance_ = 0;
 
321
}
 
322
 
 
323
/* EOF */