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

« back to all changes in this revision

Viewing changes to src/sprite.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: sprite.cxx,v 1.13 2002/12/28 16:31:37 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 <assert.h>
 
21
#include <iostream>
 
22
#include "vector.hxx"
 
23
#include "pingus_resource.hxx"
 
24
#include "sprite.hxx"
 
25
#include "globals.hxx"
 
26
 
 
27
int round (float f) 
 
28
{
 
29
  if (f >= 0.0f)
 
30
    return int(f + 0.5f);
 
31
  else
 
32
    return int(f - 0.5f);
 
33
}
 
34
 
 
35
Sprite::Sprite ()
 
36
{
 
37
}
 
38
 
 
39
Sprite::Sprite (const Sprite& sprite) :
 
40
                frame (sprite.frame),
 
41
                frames_per_second (sprite.frames_per_second),
 
42
                sur (sprite.sur),
 
43
                direction (sprite.direction),
 
44
                looptype (sprite.looptype),
 
45
                is_finished (sprite.is_finished),
 
46
                x_align (sprite.x_align),
 
47
                y_align (sprite.y_align)
 
48
{
 
49
}
 
50
 
 
51
Sprite&
 
52
Sprite::operator= (const Sprite& sprite)
 
53
{
 
54
  if (this == &sprite)
 
55
    return *this;
 
56
    
 
57
  frame             = sprite.frame;
 
58
  frames_per_second = sprite.frames_per_second;
 
59
  sur               = sprite.sur;
 
60
  direction         = sprite.direction;
 
61
  looptype          = sprite.looptype;
 
62
  is_finished       = sprite.is_finished;
 
63
  x_align           = sprite.x_align;
 
64
  y_align           = sprite.y_align;
 
65
  
 
66
  return *this;
 
67
}
 
68
 
 
69
Sprite::Sprite (std::string arg_sur_name,
 
70
                std::string arg_datafile,
 
71
                float arg_frames_per_second,
 
72
                Sprite::Direction dir,
 
73
                LoopType arg_loop_type)
 
74
  : frame (0.0f),
 
75
    frames_per_second (arg_frames_per_second),
 
76
    sur(PingusResource::load_surface (arg_sur_name, arg_datafile)),
 
77
    direction (dir),
 
78
    looptype (arg_loop_type),
 
79
    is_finished (false),
 
80
    x_align (0), y_align (0)
 
81
{
 
82
}
 
83
 
 
84
Sprite::Sprite (const CL_Surface& arg_sur,
 
85
                float arg_frames_per_second,
 
86
                Sprite::Direction dir,
 
87
                LoopType arg_loop_type)
 
88
      : frame (0.0f),
 
89
    frames_per_second (arg_frames_per_second),
 
90
    direction (dir),
 
91
    looptype (arg_loop_type),
 
92
    is_finished (false),
 
93
    x_align (0), y_align (0)
 
94
{
 
95
  sur = arg_sur;
 
96
}
 
97
 
 
98
Sprite::Sprite (const ResDescriptor& desc,
 
99
                float arg_frames_per_second,
 
100
                Sprite::Direction dir,
 
101
                LoopType arg_loop_type)
 
102
  : frame (0.0f),
 
103
    frames_per_second (arg_frames_per_second),
 
104
    direction (dir),
 
105
    looptype (arg_loop_type),
 
106
    is_finished (false),
 
107
    x_align (0), y_align (0)
 
108
{
 
109
  sur = PingusResource::load_surface (desc);
 
110
}
 
111
 
 
112
void 
 
113
Sprite::put_screen (int x, int y)
 
114
{
 
115
  if (!sur)
 
116
    return;
 
117
  // FIXME: HACK
 
118
  update (0.0f);
 
119
  //std::cout << "Frame: " << round(frame) << " " << frame << " " << max_frames () << std::endl;
 
120
 
 
121
  switch (direction)
 
122
    {
 
123
    case Sprite::NONE:
 
124
      sur.put_screen (x + x_align, y + y_align, round(frame));
 
125
      break;
 
126
    case Sprite::LEFT:
 
127
      sur.put_screen (x + x_align, y + y_align, round(frame));
 
128
      break;
 
129
    case Sprite::RIGHT:
 
130
      sur.put_screen (x + x_align, y + y_align, round(frame) + max_frames ());
 
131
      break;
 
132
    default:
 
133
      std::cout << "Direction: " << direction << std::endl;
 
134
      assert(0);
 
135
    }
 
136
}
 
137
 
 
138
 
 
139
void 
 
140
Sprite::put_screen (const Vector& pos)
 
141
{
 
142
  put_screen (int(pos.x), int(pos.y));
 
143
}
 
144
 
 
145
 
 
146
void 
 
147
Sprite::set_align (int arg_x, int arg_y)
 
148
{
 
149
  x_align = arg_x;
 
150
  y_align = arg_y;
 
151
}
 
152
 
 
153
void
 
154
Sprite::set_align_center ()
 
155
{
 
156
  x_align = -int(sur.get_width ())/2;
 
157
  y_align = -int(sur.get_height ())/2;
 
158
}
 
159
 
 
160
void 
 
161
Sprite::set_align_center_bottom ()
 
162
{
 
163
  x_align = -int(sur.get_width ())/2;
 
164
  y_align = -int(sur.get_height ()); 
 
165
}
 
166
 
 
167
 
 
168
void 
 
169
Sprite::next_frame ()
 
170
{
 
171
  ++frame;
 
172
 
 
173
  if (round(frame) >= int(sur.get_num_frames ()))
 
174
    frame = 0;
 
175
}
 
176
 
 
177
 
 
178
 
 
179
void 
 
180
Sprite::previous_frame ()
 
181
{
 
182
  --frame;
 
183
 
 
184
  if (round(frame) < 0)
 
185
    frame = sur.get_num_frames () - 1;  
 
186
}
 
187
 
 
188
 
 
189
int 
 
190
Sprite::get_frame ()
 
191
{
 
192
  return int(frame);
 
193
}
 
194
 
 
195
float 
 
196
Sprite::get_progress ()
 
197
{
 
198
  return float(frame)/max_frames ();
 
199
}
 
200
 
 
201
int 
 
202
Sprite::max_frames ()
 
203
{
 
204
  switch (direction)
 
205
    {
 
206
    case NONE:
 
207
      return sur.get_num_frames ();
 
208
    case LEFT:
 
209
    case RIGHT:
 
210
      return sur.get_num_frames ()/2;
 
211
    default:
 
212
      assert (0);
 
213
          return 0;
 
214
    }
 
215
}
 
216
 
 
217
void
 
218
Sprite::update ()
 
219
{
 
220
  // FIXME: game_speed is incorrect, but should work
 
221
  update(game_speed / 1000.0f);
 
222
}
 
223
 
 
224
void
 
225
Sprite::update (float delta)
 
226
{
 
227
  if (!sur)
 
228
    return;
 
229
 
 
230
  // The sprite contains no frames, so we have nothing to update
 
231
  if (max_frames () <= 1)
 
232
    return;
 
233
 
 
234
  switch (looptype)
 
235
    {
 
236
    case ENDLESS:
 
237
      frame += frames_per_second * delta;
 
238
 
 
239
      if (round(frame) < 0) {
 
240
        std::cout << "frame below zero: " << frame << std::endl;
 
241
        frame  = (max_frames ()-1.0f);
 
242
      }
 
243
 
 
244
      if (round(frame) >= max_frames ()) {
 
245
        is_finished = true;
 
246
        frame = 0;
 
247
      }
 
248
      break;
 
249
      
 
250
    case ONCE:
 
251
      frame += frames_per_second * delta;
 
252
      if (round(frame) >= max_frames ()) 
 
253
        {
 
254
          is_finished = true;
 
255
          frame = max_frames () - 1;
 
256
        }
 
257
      
 
258
      break;
 
259
    }
 
260
}
 
261
 
 
262
bool 
 
263
Sprite::finished ()
 
264
{
 
265
  switch (looptype)
 
266
    {
 
267
    case ENDLESS:
 
268
      return is_finished;
 
269
 
 
270
    case ONCE:
 
271
      return is_finished;
 
272
 
 
273
    default:
 
274
      assert (!"Wrong looptype!");
 
275
      return false;
 
276
    }
 
277
}
 
278
 
 
279
void 
 
280
Sprite::reset ()
 
281
{
 
282
  frame = 0.0f;
 
283
  is_finished = false;
 
284
}
 
285
 
 
286
CL_Surface&
 
287
Sprite::get_surface ()
 
288
{
 
289
  return sur;
 
290
}
 
291
 
 
292
void
 
293
Sprite::set_frame (int n)
 
294
{
 
295
  frame = n;
 
296
}
 
297
 
 
298
/* EOF */