1 /**
2  * @file list_sprites.cc
3  * @brief Call the drawing methods of all sprites
4  * @date 2007-10-20
5  * @author Bruno Ethvignot
6  * @version $Revision: 24 $
7  */
8 /*
9  * copyright (c) 1991-2014 TLK Games all rights reserved
10  * $Id: list_sprites.cc 24 2014-09-28 15:30:04Z bruno.ethvignot@gmail.com $
11  *
12  * TecnoballZ is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * TecnoballZ is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25  * MA  02110-1301, USA.
26  */
27 #include "../include/list_sprites.h"
28 
29 /**
30  * Create the object
31  */
list_sprites()32 list_sprites::list_sprites ()
33 {
34   shapes = NULL;
35   shadows = NULL;
36   num_of_shapes = 0;
37   num_of_shadows = 0;
38   max_of_shapes = 0;
39   object_init ();
40 }
41 
42 /**
43  * Release the object
44  */
~list_sprites()45 list_sprites::~list_sprites ()
46 {
47   if (shapes != NULL)
48     {
49       delete[]shapes;
50       shapes = NULL;
51     }
52   if (shadows != NULL)
53     {
54       delete[]shadows;
55       shadows = NULL;
56     }
57   object_free ();
58 }
59 
60 /**
61  * Allocate memory for the list of shapes and shadow
62  * @params numof max numbers of shapes
63  */
64 void
init(Uint32 numof)65 list_sprites::init (Uint32 numof)
66 {
67   try
68   {
69     if (NULL == shapes)
70       {
71         max_of_shapes = numof;
72         shapes = new sprite_object *[max_of_shapes];
73       }
74     if (NULL == shadows)
75       {
76         shadows = new sprite_object *[max_of_shapes];
77       }
78   }
79   catch (std::bad_alloc &)
80   {
81     std::
82       cerr << "(!)list_sprites::init() "
83       "not enough memory to allocate " <<
84       max_of_shapes << " list elements!" << std::endl;
85     throw;
86   }
87   reset ();
88 }
89 
90 /**
91  * Clear the list
92  */
93 void
reset()94 list_sprites::reset ()
95 {
96   num_of_shapes = 0;
97   num_of_shadows = 0;
98   for (Sint32 i = 0; i < max_of_shapes; i++)
99     {
100       shapes[i] = NULL;
101       shadows[i] = NULL;
102     }
103 }
104 
105 
106 
107 /**
108  * Return the number of sprites remaining
109  * @return number of free sprites
110  */
111 Uint32
get_sprites_remaining()112 list_sprites::get_sprites_remaining ()
113 {
114   return max_of_shapes - num_of_shapes;
115 }
116 
117 /**
118  * Add a sprite to the display list to draw the sprites
119  * @param sprite pointer to a sprite object
120  */
121 void
add(sprite_object * sprite)122 list_sprites::add (sprite_object * sprite)
123 {
124   if (num_of_shapes >= max_of_shapes - 1)
125     {
126       std::cerr << "(!)list_sprites::add maximum number of sprites "
127         << num_of_shapes << "has been reached!";
128       throw std::runtime_error ("(!)list_sprites::add maximum "
129                                 "number of sprites has been reached!");
130     }
131   shapes[num_of_shapes] = sprite;
132   num_of_shapes++;
133   sprite->set_display_pos (num_of_shapes);
134   if (sprite->has_shadow ())
135     {
136       shadows[num_of_shadows] = sprite;
137       num_of_shadows++;
138     }
139 }
140 
141 /**
142  *  Draw all sprites into the main offscreen
143  */
144 void
draw()145 list_sprites::draw ()
146 {
147 
148   /* draw the shahows of the objects */
149   for (Sint32 i = 0; i < num_of_shadows; i++)
150     {
151       sprite_object *sprite = shadows[i];
152       sprite->draw_shadow ();
153     }
154 
155   /* draw the objects */
156   for (Sint32 i = 0; i < num_of_shapes; i++)
157     {
158       sprite_object *sprite = shapes[i];
159       sprite->draw ();
160     }
161 }
162 
163 /**
164  * Restore background under the sprites
165  */
166 void
clear()167 list_sprites::clear ()
168 {
169   for (Sint32 i = 0; i < num_of_shapes; i++)
170     {
171       sprite_object *sprite = shapes[i];
172       sprite->restore_background_under_sprite ();
173     }
174 
175   for (Sint32 i = 0; i < num_of_shadows; i++)
176     {
177       sprite_object *sprite = shadows[i];
178       sprite->restore_background_under_shadow ();
179     }
180 }
181