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