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

« back to all changes in this revision

Viewing changes to src/SmallMap.cc

  • 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: SmallMap.cc,v 1.36 2002/01/15 10:48:49 grumbel 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 <vector>
21
 
#include <algorithm>
22
 
#include "boost/smart_ptr.hpp"
23
 
#include "ExitData.hh"
24
 
#include "EntranceData.hh"
25
 
 
26
 
#include "globals.hh"
27
 
#include "blitter.hh"
28
 
#include "PinguHolder.hh"
29
 
#include "Display.hh"
30
 
#include "Playfield.hh"
31
 
#include "World.hh"
32
 
#include "PingusResource.hh"
33
 
#include "SmallMap.hh"
34
 
#include "PLF.hh"
35
 
#include "ColMap.hh"
36
 
 
37
 
 
38
 
SmallMap::SmallMap()
39
 
{
40
 
  width = 175;
41
 
  height = 75;
42
 
  scroll_mode = false;
43
 
}
44
 
 
45
 
SmallMap::~SmallMap()
46
 
{
47
 
}
48
 
  
49
 
void
50
 
SmallMap::init()
51
 
{
52
 
  CL_Canvas*  canvas;
53
 
  unsigned char* buffer;
54
 
  unsigned char* cbuffer;
55
 
  unsigned char  current_pixel;
56
 
  int tx, ty;
57
 
 
58
 
  entrance_sur = PingusResource::load_surface("misc/smallmap_entrance", "core");
59
 
  exit_sur     = PingusResource::load_surface("misc/smallmap_exit", "core");
60
 
 
61
 
  world = client->get_server()->get_world();
62
 
 
63
 
  colmap = world->get_colmap(); 
64
 
  buffer = colmap->get_data();
65
 
  plf = world->get_plf();
66
 
 
67
 
  canvas = new CL_Canvas(width, height);
68
 
 
69
 
  canvas->lock();
70
 
  
71
 
  cbuffer = static_cast<unsigned char*>(canvas->get_data());
72
 
 
73
 
  for(int y = 0; y < height; y++)
74
 
    {
75
 
      for (int x = 0; x < width; x++)
76
 
        {
77
 
          tx = x * colmap->get_width() / width;
78
 
          ty = y * colmap->get_height() / height;
79
 
          
80
 
          current_pixel = buffer[tx + (ty * colmap->get_width())];
81
 
          
82
 
          if (current_pixel == ColMap::NOTHING)
83
 
            {
84
 
              cbuffer[4 * ((y * width) + x) + 0] = 150;
85
 
              cbuffer[4 * ((y * width) + x) + 1] = 0;
86
 
              cbuffer[4 * ((y * width) + x) + 2] = 0;
87
 
              cbuffer[4 * ((y * width) + x) + 3] = 0;
88
 
            }
89
 
          else if (current_pixel & ColMap::BRIDGE)
90
 
            {
91
 
              cbuffer[4 * ((y * width) + x) + 0] = 255;
92
 
              cbuffer[4 * ((y * width) + x) + 1] = 100;
93
 
              cbuffer[4 * ((y * width) + x) + 2] = 255;
94
 
              cbuffer[4 * ((y * width) + x) + 3] =   0;
95
 
            }
96
 
          else if (current_pixel & ColMap::LAVA)
97
 
            {
98
 
              cbuffer[4 * ((y * width) + x) + 0] = 255;
99
 
              cbuffer[4 * ((y * width) + x) + 1] = 100;
100
 
              cbuffer[4 * ((y * width) + x) + 2] = 100;
101
 
              cbuffer[4 * ((y * width) + x) + 3] = 255;
102
 
            }
103
 
          else if (current_pixel & ColMap::SOLID)
104
 
            {
105
 
              cbuffer[4 * ((y * width) + x) + 0] = 255;
106
 
              cbuffer[4 * ((y * width) + x) + 1] = 100;
107
 
              cbuffer[4 * ((y * width) + x) + 2] = 100;
108
 
              cbuffer[4 * ((y * width) + x) + 3] = 100;
109
 
            }
110
 
          else
111
 
            {
112
 
              cbuffer[4 * ((y * width) + x) + 0] = 255;
113
 
              cbuffer[4 * ((y * width) + x) + 1] = 200;
114
 
              cbuffer[4 * ((y * width) + x) + 2] = 200;
115
 
              cbuffer[4 * ((y * width) + x) + 3] = 200;
116
 
            }
117
 
        }
118
 
    }
119
 
 
120
 
  /*
121
 
  std::vector<ExitData>     exit_d     = plf->get_exit();
122
 
  for(std::vector<ExitData>::iterator i = exit_d.begin(); i != exit_d.end(); i++)
123
 
    {
124
 
      // FIXME: Replace this with put_target() when it is bug free
125
 
      Blitter::put_surface(canvas, exit_sur, 
126
 
                           i->pos.x * width / colmap->get_width() - (exit_sur.get_width()/2), 
127
 
                           i->pos.y * height / colmap->get_height());
128
 
    }
129
 
 
130
 
  std::vector<EntranceData>     entrance_d     = plf->get_entrance();
131
 
  for(std::vector<EntranceData>::iterator i = entrance_d.begin(); i != entrance_d.end(); i++)
132
 
    {
133
 
      Blitter::put_surface(canvas, entrance_sur,
134
 
                           i->pos.x * width / colmap->get_width() - (entrance_sur.get_width()/2),
135
 
                           i->pos.y * height / colmap->get_height() - (entrance_sur.get_height()));
136
 
    }*/
137
 
 
138
 
  canvas->unlock();
139
 
  
140
 
  sur = CL_Surface(canvas, true);
141
 
  
142
 
  x_pos = 0;
143
 
  y_pos = CL_Display::get_height() - sur.get_height();
144
 
 
145
 
  rwidth = CL_Display::get_width() * width / client->get_server()->get_world()->get_colmap()->get_width();
146
 
  rheight = CL_Display::get_height() * height / client->get_server()->get_world()->get_colmap()->get_height();
147
 
}
148
 
 
149
 
void
150
 
SmallMap::set_client(Client* c)
151
 
{
152
 
  client = c;
153
 
  init();
154
 
}
155
 
 
156
 
void
157
 
SmallMap::draw()
158
 
{
159
 
  boost::shared_ptr<Playfield> playfield = client->get_playfield();
160
 
 
161
 
  int x_of = playfield->get_x_offset();
162
 
  int y_of = playfield->get_y_offset();
163
 
 
164
 
  sur.put_screen(0, CL_Display::get_height() - sur.get_height()); 
165
 
  
166
 
  x_of = -x_of * width / client->get_server()->get_world()->get_colmap()->get_width();
167
 
  y_of = -y_of * height / client->get_server()->get_world()->get_colmap()->get_height();
168
 
 
169
 
  Display::draw_rect(x_of, 
170
 
                     y_of + CL_Display::get_height() - sur.get_height(),
171
 
                     x_of + std::min(rwidth, int(sur.get_width())),
172
 
                     y_of + std::min(rheight, int(sur.get_height())) + CL_Display::get_height() - sur.get_height(),
173
 
                     0.0, 1.0, 0.0, 1.0);
174
 
  
175
 
  // FIXME: This should use put_target(), but put_target(), does not
176
 
  // seem to work?!
177
 
  /*  vector<exit_data>     exit_d     = plf->get_exit();
178
 
  for(std::vector<exit_data>::iterator i = exit_d.begin(); i != exit_d.end(); i++)
179
 
    {
180
 
      exit_sur->put_screen(i->x_pos * width / colmap->get_width() +  x_pos - 3, 
181
 
                           i->y_pos * height / colmap->get_height() + y_pos - 3);
182
 
    }
183
 
 
184
 
  vector<entrance_data>     entrance_d     = plf->get_entrance();
185
 
  for(std::vector<entrance_data>::iterator i = entrance_d.begin(); i != entrance_d.end(); i++)
186
 
    {
187
 
      entrance_sur->put_screen(i->x_pos * width / colmap->get_width() + x_pos - 3,
188
 
                               i->y_pos * height / colmap->get_height() + y_pos);
189
 
    }
190
 
  */
191
 
  draw_pingus();
192
 
}
193
 
 
194
 
void
195
 
SmallMap::draw_pingus()
196
 
{
197
 
  int x;
198
 
  int y;
199
 
  PinguHolder* pingus = client->get_server()->get_world()->get_pingu_p();
200
 
 
201
 
  for(std::list<boost::shared_ptr<Pingu> >::iterator i = pingus->begin();
202
 
      i != pingus->end(); i++)
203
 
    {
204
 
      //FIXME: Replace this with put pixel
205
 
      x = x_pos + ((*i)->get_x() * width / client->get_server()->get_world()->get_colmap()->get_width());
206
 
      y = y_pos + ((*i)->get_y() * height / client->get_server()->get_world()->get_colmap()->get_height());
207
 
 
208
 
      CL_Display::draw_line(x, y, x, y-1, 1.0, 1.0, 0.0, 1.0);
209
 
    }
210
 
}
211
 
 
212
 
void
213
 
SmallMap::update(float delta)
214
 
{
215
 
  int cx, cy;
216
 
  ColMap* colmap = client->get_server()->get_world()->get_colmap();
217
 
  
218
 
  if (scroll_mode)
219
 
    {
220
 
      cx = (int(CL_Mouse::get_x()) - x_pos) * int(colmap->get_width()) / width;
221
 
      cy = (int(CL_Mouse::get_y()) - y_pos) * int(colmap->get_height()) / height ;
222
 
 
223
 
      std::cout << "Viewpoint: " << cx << " " << cy << std::endl;
224
 
 
225
 
      client->get_playfield()->set_viewpoint(cx, cy);
226
 
    }
227
 
}
228
 
 
229
 
bool
230
 
SmallMap::mouse_over()
231
 
{
232
 
  //std::cout << "x_pos: " << CL_Mouse::get_x() << " y_pos: " << CL_Mouse::get_y() << std::endl;
233
 
 
234
 
  if (CL_Mouse::get_x() > x_pos && CL_Mouse::get_x() < x_pos + (int)width
235
 
      && CL_Mouse::get_y() > y_pos && CL_Mouse::get_y() < y_pos + (int)height)
236
 
    {
237
 
      return true;
238
 
    }
239
 
  else
240
 
    {
241
 
      return false;
242
 
    }
243
 
}
244
 
 
245
 
bool
246
 
SmallMap::on_button_press(const CL_Key& key)
247
 
{
248
 
  switch(key.id)
249
 
    {
250
 
    case CL_MOUSE_LEFTBUTTON:
251
 
      if (mouse_over())
252
 
        {
253
 
          scroll_mode = true;
254
 
          return true;
255
 
        }
256
 
      break;
257
 
    }
258
 
  return false;
259
 
}
260
 
 
261
 
bool
262
 
SmallMap::on_button_release(const CL_Key& key)
263
 
{
264
 
  switch(key.id)
265
 
    {
266
 
    case CL_MOUSE_LEFTBUTTON:
267
 
      scroll_mode = false;
268
 
      break;
269
 
    }
270
 
  return false;
271
 
}
272
 
 
273
 
/* EOF */
274