Adonthell  0.4
mapobject.cc
Go to the documentation of this file.
1 /*
2  $Id: mapobject.cc,v 1.6 2002/06/28 12:15:20 gnurou Exp $
3 
4  Copyright (C) 1999/2000/2001 Alexandre Courbot
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 
17 /**
18  * @file mapobject.cc
19  *
20  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
21  * @brief Defines the mapobject class.
22  */
23 
24 
25 #include "mapobject.h"
26 
27 using namespace std;
28 
29 
30 // Public methods.
31 
32 
34 {
35  clear ();
36 }
37 
39 {
40  clear ();
41 }
42 
44 {
45  vector <animation *>::iterator i;
46 
47  for (i = anim.begin (); i != anim.end (); i++)
48  delete (*i);
49  anim.clear ();
51 }
52 
54 {
55  vector <animation *>::iterator i;
56 
57  for (i = anim.begin (); i != anim.end (); i++)
58  (*i)->update ();
59 
60  return true;
61 }
62 
63 void mapobject::draw (s_int16 x, s_int16 y, const drawing_area * da_opt, surface * target) const
64 {
65  vector <animation *>::iterator i;
66 
67  for (i = anim.begin (); i != anim.end (); i++)
68  (*i)->draw (x, y, da_opt, target);
69 }
70 
72  const drawing_area * da_opt, surface * target) const
73 {
74  draw (x - base_x () * MAPSQUARE_SIZE, y - base_y () * MAPSQUARE_SIZE,
75  da_opt, target);
76 }
77 
79 {
80  u_int16 i;
81  u_int16 nbr_of_parts;
82 
83  if (!fileops::get_version (file, 1, 1, ""))
84  return -1;
85 
86  // Clear everything.
87  clear ();
88 
89  // Read all the animations.
90  nbr_of_parts << file;
91  for (i = 0; i < nbr_of_parts; i++)
92  {
93  anim.push_back (new animation);
94  anim.back ()->get (file);
95  anim.back ()->play ();
96  }
97 
99 
100  return 0;
101 }
102 
103 s_int8 mapobject::load (string fname)
104 {
105  igzstream file;
106  s_int8 retvalue = -1;
107 
108  string fdef = MAPOBJECTS_DIR;
109 
110  fdef += fname;
111 
112  file.open (fdef);
113  if (!file.is_open ())
114  return -1;
115  retvalue = get (file);
116  file.close ();
117  return retvalue;
118 }
119 
121 {
122  u_int16 i;
123 
124  fileops::put_version (file, 1);
125 
126  // Write all the animations.
127  nbr_of_animations () >> file;
128  for (i = 0; i < nbr_of_animations (); i++)
129  {
130  anim[i]->put (file);
131  }
132 
134 
135  return 0;
136 }
137 
138 s_int8 mapobject::save (string fname) const
139 {
140  ogzstream file;
141  s_int8 retvalue = -1;
142 
143  string fdef = MAPOBJECTS_DIR;
144 
145  fdef += fname;
146 
147  file.open (fdef);
148  if (!file.is_open ())
149  return -1;
150  retvalue = put (file);
151  file.close ();
152  return retvalue;
153 }
154 
156 {
157  vector <animation *>::iterator i;
158  if (pos > nbr_of_animations ())
159  return -2;
160  i = anim.begin ();
161  while (pos--)
162  i++;
163  anim.insert (i, an);
164  an->play ();
165  return 0;
166 }
167 
169 {
170  vector <animation *>::iterator i;
171 
172  if (pos > nbr_of_animations () - 1)
173  return -2;
174  i = anim.begin ();
175  while (pos--)
176  i++;
177  anim.erase (i);
178  return 0;
179 }
180 
182 {
183  // Clear everything.
184  clear ();
185 
186  // Copy the area.
188 
189  // Copy all animations.
190  vector <animation *>::iterator it;
191  for (it = ((mapobject&) src).anim.begin (); it != ((mapobject&) src).anim.end (); it++)
192  {
193  animation * an = new animation;
194  *an = *(*it);
196  }
197 
198  return *this;
199 }
Class to write data from a Gzip compressed file.
Definition: fileops.h:223
void close()
Close the file that was opened.
Definition: fileops.cc:59
Declares the mapobject class.
Class to read data from a Gzip compressed file.
Definition: fileops.h:131
void draw_from_base(s_int16 x, s_int16 y, const drawing_area *da_opt=NULL, surface *target=NULL) const
Similar to draw (), but assume the x and y parameters are where the base square should appear...
Definition: mapobject.cc:71
#define u_int16
16 bits long unsigned integer
Definition: types.h:32
s_int8 load(string fname)
Loads a mapobject from it's filename.
Definition: mapobject.cc:103
Class where drawables can actually be drawn to.
Definition: surface.h:51
~mapobject()
Destructor.
Definition: mapobject.cc:38
u_int16 base_y() const
Returns the Y offset of the base square of this object.
s_int8 insert_animation(animation *an, u_int16 pos)
Inserts an animation at a given position of the animations array.
Definition: mapobject.cc:155
s_int8 put(ogzstream &file) const
Saves an area into an opened file.
void draw(s_int16 x, s_int16 y, const drawing_area *da_opt=NULL, surface *target=NULL) const
Draw the object on the screen.
Definition: mapobject.cc:63
bool open(const string &fname)
Opens a file for write access.
Definition: fileops.cc:262
bool open(const string &fname)
Opens a file for read access.
Definition: fileops.cc:77
u_int16 nbr_of_animations() const
Returns the number of animations of this mapobject.
Definition: mapobject.h:177
static bool get_version(igzstream &file, u_int16 min, u_int16 max, string name)
Definition: fileops.cc:365
bool is_open()
Returns whether the file is opened or not.
Definition: fileops.h:99
Implements "drawing zones" for drawing operations.
Definition: drawing_area.h:50
s_int8 delete_animation(u_int16 pos)
Removes an animation at a given position.
Definition: mapobject.cc:168
void clear()
Totally clears the area.
u_int16 base_x() const
Returns the X offset of the base square of this object.
#define s_int16
16 bits long signed integer
Definition: types.h:41
s_int8 put(ogzstream &file) const
Saves an mapobject into an opened file, in game format, with alpha and mask values.
Definition: mapobject.cc:120
void play()
Starts the playback of the animation.
Definition: animation.h:344
const u_int16 MAPSQUARE_SIZE
Size of a mapsquare (in pixels).
mapobject & operator=(const mapobject &mo)
Mapobject copy (similar to copy ()).
Definition: mapobject.cc:181
Area of mapsquare_walkables, for use with mapcharacter and mapobject classes.
mapobject()
Default constructor.
Definition: mapobject.cc:33
bool update()
Updates the mapobject's state.
Definition: mapobject.cc:53
static void put_version(ogzstream &file, u_int16 version)
Sets the version number of a file.
Definition: fileops.cc:357
Class that handles animated elements, their update and their playback.
Definition: animation.h:308
s_int8 get(igzstream &file)
Loads an area from an opened file.
s_int8 get(igzstream &file)
Loads a mapobject from an opened file.
Definition: mapobject.cc:78
#define s_int8
8 bits long signed integer
Definition: types.h:38
Objects that can be placed on a landmap.
Definition: mapobject.h:46
s_int8 save(string fname) const
Saves an mapobject into an file, in game format, with alpha and mask values.
Definition: mapobject.cc:138
#define MAPOBJECTS_DIR
Where the mapobjects resides in the data tree.
Definition: mapobject.h:32
void clear()
Resets the mapobject to its post-constructor state.
Definition: mapobject.cc:43