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

« back to all changes in this revision

Viewing changes to src/components/playfield.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: playfield.cpp 3385 2007-10-30 04:39:26Z grumbel $
 
2
//
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 1999 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 <stdio.h>
 
21
#include <iostream>
 
22
#include "../globals.hpp"
 
23
#include "../pingu_holder.hpp"
 
24
#include "../display/drawing_context.hpp"
 
25
#include "../display/scene_context.hpp"
 
26
#include "../world.hpp"
 
27
#include "../server.hpp"
 
28
#include "../true_server.hpp"
 
29
#include "../pingu.hpp"
 
30
#include "../display/display.hpp"
 
31
#include "button_panel.hpp"
 
32
#include "playfield.hpp"
 
33
 
 
34
Playfield::Playfield (Client* client_, const Rect& rect_)
 
35
  : RectComponent(rect_),
 
36
    client(client_),
 
37
    buttons(client->get_button_panel()),
 
38
    current_pingu(0),
 
39
    // We keep the SceneContext has member variable so that we don't
 
40
    // have to reallocate it every frame, which is quite a costly operation
 
41
    scene_context(new SceneContext()),
 
42
    state(rect),
 
43
    cap(client->get_button_panel())
 
44
{
 
45
  world              = client->get_server()->get_world();
 
46
  mouse_scrolling    = false;
 
47
 
 
48
  state.set_limit(Rect(Vector2i(0, 0), Size(world->get_width(), world->get_height())));
 
49
 
 
50
  // FIXME: Temporary workaround till start-pos is integrated a bit more properly
 
51
  state.set_pos(world->get_start_pos(0));
 
52
}
 
53
 
 
54
Playfield::~Playfield()
 
55
{
 
56
  delete scene_context;
 
57
}
 
58
 
 
59
void
 
60
Playfield::draw(DrawingContext& gc)
 
61
{
 
62
  scene_context->clear();
 
63
  scene_context->set_cliprect(rect);
 
64
 
 
65
  //scene_context->light().fill_screen(Color(50, 50, 50));
 
66
 
 
67
  state.push(*scene_context);
 
68
 
 
69
  cap.set_pingu(current_pingu);
 
70
  cap.draw(*scene_context);
 
71
 
 
72
  world->draw(*scene_context);
 
73
 
 
74
  // Draw the scrolling band
 
75
  if (mouse_scrolling && !drag_drop_scrolling)
 
76
    {
 
77
      gc.draw_line(mouse_pos.x, mouse_pos.y,
 
78
                   scroll_center.x, scroll_center.y-15,
 
79
                   Color(0, 255, 0));
 
80
 
 
81
      gc.draw_line(mouse_pos.x, mouse_pos.y,
 
82
                   scroll_center.x, scroll_center.y,
 
83
                   Color(255, 0, 0));
 
84
 
 
85
      gc.draw_line(mouse_pos.x, mouse_pos.y,
 
86
                   scroll_center.x, scroll_center.y+15,
 
87
                   Color(0, 0, 255));
 
88
 
 
89
      gc.draw_line(mouse_pos.x, mouse_pos.y,
 
90
                   scroll_center.x + 15, scroll_center.y,
 
91
                   Color(0, 255, 255));
 
92
 
 
93
      gc.draw_line(mouse_pos.x, mouse_pos.y,
 
94
                   scroll_center.x - 15, scroll_center.y,
 
95
                   Color(255, 255, 0));
 
96
    }
 
97
 
 
98
  state.pop(*scene_context);
 
99
  gc.draw(new SceneContextDrawingRequest(scene_context, Vector3f(0,0,-10000)));
 
100
}
 
101
 
 
102
Pingu*
 
103
Playfield::current_pingu_find (const Vector2f& pos)
 
104
{
 
105
  double min_dist = 500.0;
 
106
  double dist;
 
107
  Pingu* c_pingu = 0;
 
108
 
 
109
  for (PinguIter pingu = world->get_pingus()->begin();
 
110
       pingu != world->get_pingus()->end();
 
111
       ++pingu)
 
112
    {
 
113
      if ((*pingu)->is_over(static_cast<int>(pos.x), static_cast<int>(pos.y)))
 
114
        {
 
115
          dist = (*pingu)->dist(static_cast<int>(pos.x), static_cast<int>(pos.y));
 
116
 
 
117
          if (dist < min_dist)
 
118
            {
 
119
              min_dist = dist;
 
120
              c_pingu = *pingu;
 
121
            }
 
122
        }
 
123
    }
 
124
  return c_pingu;
 
125
}
 
126
 
 
127
void
 
128
Playfield::update(float delta)
 
129
{
 
130
  // FIXME: This should be delta dependant
 
131
  if (!mouse_scrolling)
 
132
    {
 
133
      current_pingu = current_pingu_find(state.screen2world(mouse_pos));
 
134
      cap.set_pingu(current_pingu);
 
135
    }
 
136
  else
 
137
    {
 
138
      if (drag_drop_scrolling)
 
139
        {
 
140
          state.set_pos(old_state_pos + (scroll_center - mouse_pos));
 
141
        }
 
142
      else
 
143
        { 
 
144
          state.set_pos(Vector2f(state.get_pos().x - float(scroll_center.x - mouse_pos.x) * 0.2f,
 
145
                                 state.get_pos().y - float(scroll_center.y - mouse_pos.y) * 0.2f));
 
146
        }
 
147
    }
 
148
 
 
149
  if (auto_scrolling && (fullscreen_enabled || SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON))
 
150
    {
 
151
      // FIXME: May need to modify this function if it's not gradient enough.
 
152
      scroll_speed = static_cast<int>(800 * delta);
 
153
      //std::cout << "scroll_speed: " << scroll_speed << std::endl;
 
154
    
 
155
      if (mouse_pos.x < 10)
 
156
        {
 
157
          state.set_pos(state.get_pos() - Vector2i(scroll_speed, 0));
 
158
        }
 
159
      else if (mouse_pos.x > Display::get_width() - 10)
 
160
        {
 
161
          state.set_pos(state.get_pos() + Vector2i(scroll_speed, 0));
 
162
        }
 
163
 
 
164
      if (mouse_pos.y < 10)
 
165
        {
 
166
          state.set_pos(state.get_pos() - Vector2i(0, scroll_speed));
 
167
        }
 
168
      else if (mouse_pos.y > Display::get_height() - 10)
 
169
        {
 
170
          state.set_pos(state.get_pos() + Vector2i(0, scroll_speed));    
 
171
        }
 
172
    }
 
173
}
 
174
 
 
175
void
 
176
Playfield::on_primary_button_press(int x, int y)
 
177
{
 
178
  UNUSED_ARG(x);
 
179
  UNUSED_ARG(y);
 
180
 
 
181
  if (current_pingu)
 
182
    {
 
183
      server->send_pingu_action_event(current_pingu, buttons->get_action_name());
 
184
    }
 
185
}
 
186
 
 
187
void
 
188
Playfield::on_secondary_button_press(int x, int y)
 
189
{
 
190
  mouse_scrolling = true;
 
191
  scroll_center.x = x;
 
192
  scroll_center.y = y;
 
193
 
 
194
  old_state_pos = state.get_pos();
 
195
}
 
196
 
 
197
void
 
198
Playfield::on_secondary_button_release (int x, int y)
 
199
{
 
200
  UNUSED_ARG(x);
 
201
  UNUSED_ARG(y);
 
202
 
 
203
  mouse_scrolling = false;
 
204
}
 
205
 
 
206
void
 
207
Playfield::on_pointer_move (int x, int y)
 
208
{
 
209
  // FIXME: useless stuff, but currently the controller doesn't have a state
 
210
  mouse_pos.x = x;
 
211
  mouse_pos.y = y;
 
212
 
 
213
  if (maintainer_mode)
 
214
    {
 
215
      Uint8 *keystate = SDL_GetKeyState(NULL);
 
216
      if (keystate[SDLK_r])
 
217
        {
 
218
          CollisionMask mask("other/bash_radius_gfx");
 
219
          Vector2f p = state.screen2world(mouse_pos);
 
220
          server->get_world()->remove(mask, 
 
221
                                      int(p.x - mask.get_width()/2), 
 
222
                                      int(p.y - mask.get_height()/2));
 
223
        }
 
224
      else if (keystate[SDLK_g])
 
225
        {
 
226
          CollisionMask mask("other/bash_radius_gfx");
 
227
          Vector2f p = state.screen2world(mouse_pos);
 
228
          server->get_world()->put(mask, 
 
229
                                   int(p.x - mask.get_width()/2), 
 
230
                                   int(p.y - mask.get_height()/2),
 
231
                                   Groundtype::GP_GROUND);
 
232
        }
 
233
      else if (keystate[SDLK_b])
 
234
        {
 
235
          CollisionMask mask("other/bash_radius_gfx");
 
236
          Vector2f p = state.screen2world(mouse_pos);
 
237
          server->get_world()->put(mask, 
 
238
                                   int(p.x - mask.get_width()/2), 
 
239
                                   int(p.y - mask.get_height()/2),
 
240
                                   Groundtype::GP_BRIDGE);
 
241
        }
 
242
    }
 
243
}
 
244
 
 
245
void
 
246
Playfield::set_server(Server* s)
 
247
{
 
248
  server = s;
 
249
}
 
250
 
 
251
Vector2i
 
252
Playfield::get_pos() const
 
253
{
 
254
  return Vector2i(static_cast<int>(state.get_pos().x), 
 
255
                  static_cast<int>(state.get_pos().y));
 
256
}
 
257
 
 
258
void
 
259
Playfield::set_viewpoint(int x, int y)
 
260
{
 
261
  state.set_pos(Vector2f((float)x, (float)y));
 
262
}
 
263
 
 
264
void
 
265
Playfield::scroll (int x, int y)
 
266
{
 
267
  state.set_pos(state.get_pos() + Vector2f((float)x, (float)y));
 
268
}
 
269
 
 
270
 
 
271
/* EOF */