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

« back to all changes in this revision

Viewing changes to src/game_session.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Goulais
  • Date: 2004-08-09 10:26:00 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040809102600-lg2q9lfars0q1p42
Tags: 0.6.0-8
Applied patch from Andreas Jochens (Closes: #263992)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: game_session.cxx,v 1.39 2003/04/09 16:20:19 torangan Exp $
 
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 <iostream>
 
21
#include "gui/screen_manager.hxx"
 
22
#include "client.hxx"
 
23
#include "true_server.hxx"
 
24
#include "game_session.hxx"
 
25
#include "game_session_result.hxx"
 
26
#include "timer.hxx"
 
27
#include "pingus_resource.hxx"
 
28
#include "plf.hxx"
 
29
#include "pingu_holder.hxx"
 
30
#include "world.hxx"
 
31
#include "result_screen.hxx"
 
32
#include "savegame_manager.hxx"
 
33
#include "globals.hxx"
 
34
 
 
35
PingusGameSession::PingusGameSession (PLFHandle arg_plf, bool arg_show_result_screen)
 
36
  : plf(arg_plf),
 
37
    show_result_screen(arg_show_result_screen)
 
38
{
 
39
  Timer plf_timer("GameSession plf creation");
 
40
 
 
41
  plf_timer.stop();
 
42
 
 
43
  Timer server_timer("GameSession server creation");
 
44
  server = new TrueServer(*plf);
 
45
  server_timer.stop();
 
46
 
 
47
  Timer client_timer("GameSession client creation");
 
48
  client = new Client(server);
 
49
  client_timer.stop();
 
50
 
 
51
  number_of_redraws = 0;
 
52
  number_of_updates = 0;
 
53
 
 
54
  left_over_time = 0;
 
55
}
 
56
 
 
57
PingusGameSession::~PingusGameSession ()
 
58
{
 
59
  if (maintainer_mode)
 
60
    std::cout << "XXXXXXXX"
 
61
              << " Redraws: " << number_of_redraws
 
62
              << " Updates: " << number_of_updates 
 
63
              << " FrameSkip: " << number_of_updates - number_of_redraws
 
64
              << std::endl;
 
65
 
 
66
  delete client;
 
67
  delete server;
 
68
}
 
69
 
 
70
void
 
71
PingusGameSession::on_startup()
 
72
{
 
73
  client->on_startup();
 
74
}
 
75
 
 
76
void
 
77
PingusGameSession::on_shutdown()
 
78
{
 
79
  client->on_shutdown();
 
80
}
 
81
 
 
82
PingusGameSessionResult
 
83
PingusGameSession::get_result ()
 
84
{
 
85
  return PingusGameSessionResult ();
 
86
}
 
87
 
 
88
bool
 
89
PingusGameSession::draw(GraphicContext& gc)
 
90
{
 
91
  ++number_of_redraws;
 
92
  client->draw (gc);
 
93
  return true;
 
94
}
 
95
 
 
96
void
 
97
PingusGameSession::update (const GameDelta& delta)
 
98
{
 
99
  // FIXME: Timing code could need another rewrite...
 
100
  if (server->is_finished())
 
101
    {
 
102
      //ScreenManager::instance()->pop_screen();
 
103
      PinguHolder* pingu_holder = server->get_world()->get_pingus();
 
104
      Result result;
 
105
      
 
106
      result.plf    = server->get_plf();
 
107
 
 
108
      result.saved  = pingu_holder->get_number_of_exited();
 
109
      result.killed = pingu_holder->get_number_of_killed();
 
110
      result.total  = server->get_plf()->get_pingus();
 
111
 
 
112
      result.needed = server->get_plf()->get_number_to_save();
 
113
 
 
114
      result.max_time  = server->get_plf()->get_time();
 
115
      result.used_time = server->get_time();
 
116
 
 
117
      { // Write the savegame
 
118
        Savegame savegame;
 
119
        savegame.levelname    = result.plf->get_resname();
 
120
        savegame.time         = result.used_time;
 
121
        savegame.saved_pingus = result.saved;
 
122
 
 
123
        if (result.saved >= result.needed)
 
124
          savegame.status     = Savegame::FINISHED;
 
125
        else
 
126
          savegame.status     = Savegame::ACCESSIBLE;
 
127
 
 
128
        SavegameManager::instance()->store(savegame);
 
129
      }
 
130
 
 
131
      if (show_result_screen)
 
132
        ScreenManager::instance()->replace_screen(new ResultScreen(result));
 
133
      else
 
134
        ScreenManager::instance()->pop_screen();
 
135
      return;
 
136
    }
 
137
 
 
138
  int time_passed = int(delta.get_time() * 1000) + left_over_time;
 
139
  int update_time = game_speed;
 
140
 
 
141
  left_over_time = 0;
 
142
 
 
143
  {
 
144
    int i;
 
145
    for (i = 0; 
 
146
         ((i * update_time < time_passed)
 
147
          || i < min_frame_skip)
 
148
           && !(i > max_frame_skip);
 
149
         ++i)
 
150
      {
 
151
        // This updates the world and all objects
 
152
        server->update ();
 
153
        ++number_of_updates;
 
154
      }
 
155
      
 
156
    // Time that got not used for updates
 
157
    left_over_time = time_passed - (i * update_time);
 
158
  }
 
159
 
 
160
  if (left_over_time < 0)
 
161
    {
 
162
      // FIXME: This doesn't really belong here
 
163
      CL_System::sleep(-left_over_time);
 
164
    }
 
165
  
 
166
  // Client is independend of the update limit, well, not completly...
 
167
  client->update (delta);
 
168
 
 
169
#if 0
 
170
  // Move the game one loop further
 
171
  server->update ();
 
172
  client->update (delta);
 
173
#endif
 
174
}
 
175
 
 
176
void
 
177
PingusGameSession::on_pause_press ()
 
178
{
 
179
  client->on_pause_press (); 
 
180
}
 
181
 
 
182
void
 
183
PingusGameSession::on_fast_forward_press ()
 
184
{
 
185
  client->on_fast_forward_press (); 
 
186
}
 
187
 
 
188
void
 
189
PingusGameSession::on_armageddon_press () 
 
190
{
 
191
  client->on_armageddon_press (); 
 
192
}
 
193
 
 
194
void
 
195
PingusGameSession::on_escape_press () 
 
196
 
197
  client->on_escape_press (); 
 
198
}
 
199
 
 
200
/* EOF */