File: demo_session.cpp

package info (click to toggle)
pingus 0.7.6-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 31,512 kB
  • ctags: 6,206
  • sloc: cpp: 42,076; xml: 2,319; lisp: 521; ruby: 455; ansic: 365; objc: 248; sh: 247; makefile: 143; python: 15
file content (286 lines) | stat: -rw-r--r-- 8,001 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//  Pingus - A free Lemmings clone
//  Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "pingus/screens/demo_session.hpp"

#include <algorithm>
#include <iostream>
#include <functional>

#include "engine/gui/gui_manager.hpp"
#include "engine/gui/surface_button.hpp"
#include "engine/screen/screen_manager.hpp"
#include "pingus/components/pingus_counter.hpp"
#include "pingus/components/playfield.hpp"
#include "pingus/components/smallmap.hpp"
#include "pingus/components/button_panel.hpp"
#include "pingus/pingus_demo.hpp"
#include "pingus/server.hpp"
#include "pingus/world.hpp"
#include "util/log.hpp"

static bool false_func() { return false; }

class BButton : public GUI::SurfaceButton
{
private:
  Sprite highlight;
  std::function<void(void)> callback;
  std::function<bool(void)> highlight_func;
  
public:
  BButton(int x, int y, const std::string& name, 
          std::function<void (void)> callback_,
          std::function<bool(void)> highlight_func_ = &false_func) :
    SurfaceButton(x, y, name, name + "-pressed", name + "-hover"),
    highlight("core/demo/highlight"),
    callback(callback_),
    highlight_func(highlight_func_)
  {}

  virtual void draw (DrawingContext& gc) 
  {

    if (highlight_func())
    {
      gc.draw(button_pressed_surface, Vector2i(x_pos, y_pos));
      gc.draw(highlight, Vector2i(x_pos, y_pos));
    }
    else
    {
      SurfaceButton::draw(gc);
    }
  }

  void on_click() {
    callback();
  }
};

DemoSession::DemoSession(const Pathname& pathname_) :
  pathname(pathname_),
  server(),
  demo(),
  events(),
  pcounter(),
  playfield(),
  small_map(),
  fastforward_button(),
  pause_button(),
  restart_button(),
  pause(false),
  fast_forward(false)
{
  // Load Demo file
  demo = std::unique_ptr<PingusDemo>(new PingusDemo(pathname));

  events = demo->get_events();
  // Reverse the vector so that we can use pop_back()  
  std::reverse(events.begin(), events.end());

  // Create server
  PingusLevel plf(Pathname("levels/" + demo->get_levelname()  + ".pingus", Pathname::DATA_PATH));

  if (plf.get_checksum() != demo->get_checksum())
  {
    log_warn("checksum missmatch between demo (" 
             << demo->get_checksum() << ") and level (" 
             << plf.get_checksum() << ")");
  }

  server   = std::unique_ptr<Server>(new Server(plf, false));

  // Create GUI
  pcounter = new PingusCounter(server.get());
  gui_manager->add(pcounter);
  gui_manager->add(new ButtonPanel(server.get(), Vector2i(0, (size.height - 100)/2)));

  int world_width  = server->get_world()->get_width();
  int world_height = server->get_world()->get_height();

  playfield = new Playfield(server.get(), 0,
                            Rect(Vector2i(Math::max((size.width  - world_width)/2,  0),
                                          Math::max((size.height - world_height)/2, 0)), 
                                 Size(Math::min(size.width,  world_width),
                                      Math::min(size.height, world_height))));

  gui_manager->add(playfield);

  small_map    = new SmallMap(server.get(), playfield, Rect(Vector2i(5, size.height - 105), Size(175, 100)));
  gui_manager->add(small_map);

  gui_manager->add(fastforward_button= new BButton(32+50, 32, "core/demo/fastforward",
                                                   std::bind(&DemoSession::on_fast_forward_press, this),
                                                   std::bind(&DemoSession::is_fast_forward, this)));
  gui_manager->add(pause_button =new BButton(32,  32, "core/demo/pause",
                                             std::bind(&DemoSession::on_pause_press, this),
                                             std::bind(&DemoSession::is_pause, this)));
  gui_manager->add(restart_button = new BButton(size.width - 32 - 48, 32, "core/demo/reload",
                                                std::bind(&DemoSession::restart, this)));
}

DemoSession::~DemoSession()
{
}

void
DemoSession::draw_background(DrawingContext& gc)
{
  Rect rect = playfield->get_rect();
  
  if (rect != Rect(Vector2i(0,0), Size(gc.get_width(), gc.get_height())))
  { // Draw a black border around the playfield when the playfield is smaller then the screen
    Color border_color(0, 0, 0);
    // top
    gc.draw_fillrect(Rect(0, 0, gc.get_width(), rect.top),
                     border_color);
    // bottom
    gc.draw_fillrect(Rect(0, rect.bottom, gc.get_width(), gc.get_height()),
                     border_color);
    // left
    gc.draw_fillrect(Rect(0, rect.top, rect.left, rect.bottom),
                     border_color);
    // right
    gc.draw_fillrect(Rect(rect.right, rect.top, gc.get_width(), rect.bottom),
                     border_color);
  }  
}

/** Pass a delta to the screen */
void
DemoSession::update(float delta)
{
  GUIScreen::update(delta);

  if (server->is_finished())
  {
    ScreenManager::instance()->pop_screen();
  }
  else
  {
    // FIXME: Duplicate all timing code here?!

    if (!pause)
    {
      if (fast_forward)
      {
        for (int i = 0; i < 4; ++i)
        {
          server->update();
          update_demo();
        }
      }
      else
      {
        server->update();
        update_demo();
      }
    }
  }
}

void
DemoSession::update_demo()
{
  while(!events.empty() && events.back().time_stamp == server->get_time())
  {
    ServerEvent& event = events.back();

    if (0)
    {
      std::cout << "Sending: ";
      event.write(std::cout);
    }

    event.send(server.get());
    events.pop_back();
  }
  
  // Check for unexpected things (might happen if the demo file is broken)
  if (!events.empty() && events.back().time_stamp < server->get_time())
  {
    log_info("DemoPlayer Bug: We missed a timestamp: " << events.back().time_stamp);
  }
}

void
DemoSession::on_pause_press()
{
  if (0)
  {
    for(std::vector<ServerEvent>::iterator i = events.begin(); i != events.end(); ++i)
    {
      std::cout << "Event: ";
      i->write(std::cout);      
    }
  }

  pause = !pause;

}

void
DemoSession::on_fast_forward_press()
{
  if (0)
    log_info("Fast Forward Pressed: " << events.size() << " " << server->get_time());

  fast_forward = !fast_forward;
}

void
DemoSession::on_escape_press()
{
  log_info("Escape Pressed");
  server->send_finish_event();
  ScreenManager::instance()->pop_screen();
}

void
DemoSession::on_scroller_move(float x, float y)
{
  // FIXME: Rounding considered evil?
  playfield->scroll(static_cast<int>(-x),
                    static_cast<int>(-y));
}

void
DemoSession::restart()
{
  ScreenManager::instance()->replace_screen(std::make_shared<DemoSession>(pathname));
}

void
DemoSession::resize(const Size& size_)
{
  GUIScreen::resize(size_);

  int world_width  = server->get_world()->get_width();
  int world_height = server->get_world()->get_height();

  playfield->set_rect(Rect(Vector2i(Math::max((size.width  - world_width)/2,  0),
                                    Math::max((size.height - world_height)/2, 0)), 
                           Size(Math::min(size.width,  world_width),
                                Math::min(size.height, world_height))));

  fastforward_button->set_pos(32+50, 32);
  pause_button->set_pos(32,  32);
  restart_button->set_pos(size.width - 32 - 48, 32);

  small_map->set_rect(Rect(Vector2i(5, size.height - 105), Size(175, 100)));
}

/* EOF */