1 /**
2 * @file objects_list.h
3 * @brief Template of management of objects list
4 * @date 2012-09-08
5 * @copyright 1991-2014 TLK Games
6 * @author Bruno Ethvignot
7 * @version $Revision: 24 $
8 */
9 /*
10 * copyright (c) 1991-2014 TLK Games all rights reserved
11 * $Id: objects_list.h 24 2014-09-28 15:30:04Z bruno.ethvignot@gmail.com $
12 *
13 * TecnoballZ is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * TecnoballZ is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 * MA 02110-1301, USA.
27 */
28 #ifndef __OBJECTS_LIST__
29 #define __OBJECTS_LIST__
30
31 template < class X, class T > class objects_list;
32 #include "../include/list_sprites.h"
33 #include "../include/handler_display.h"
34 #include "../include/tecnoballz.h"
35
36 template < class X, class T > class objects_list:public virtual tecnoballz
37 {
38 protected:
39 static T *singleton;
40 X ** sprites_list;
41 Uint32 max_of_sprites;
42 Uint32 num_of_sprites_allocated;
43 Uint32 num_of_sprites;
44 bool sprites_have_shades;
45 Uint32 sprite_type_id;
46 bool is_draw_pixel_by_pixel;
47
48 public:
49 objects_list ();
50 virtual ~objects_list ();
51 static T * get_instance ();
52 void littleInit ();
53 void release_sprites_list ();
54 void alloc_sprites_list ();
55 void create_sprites_list (Uint32 maxof, bool have_shades = true);
56 void create_sprites_list ();
57 void disable_sprites ();
58 void enable_sprites ();
59 X **get_sprites_list ();
60 X *get_first_sprite ();
61 Uint32 get_max_of_sprites ();
62 void set_max_of_sprites (Uint32 maxof);
63 };
64
65 template < class X, class T > T* objects_list < X, T >::singleton = NULL;
66
67
68
69 /**
70 * Create the list of objects
71 */
objects_list()72 template < class X, class T > objects_list < X, T >::objects_list ()
73 {
74 }
75
76 /**
77 * Release the list objects
78 */
~objects_list()79 template < class X, class T > objects_list < X, T >::~objects_list ()
80 {
81 singleton = NULL;
82 }
83
get_instance()84 template < class X, class T > T* objects_list < X, T >::get_instance ()
85 {
86 if (NULL == singleton)
87 {
88 singleton = new T ();
89 }
90 return singleton;
91 }
92
93 /**
94 * Clear some members
95 */
littleInit()96 template < class X, class T > void objects_list < X, T >::littleInit ()
97 {
98 object_init ();
99 max_of_sprites = 0;
100 num_of_sprites_allocated = 0;
101 num_of_sprites = 0;
102 sprites_list = NULL;
103 sprites_have_shades = false;
104 sprite_type_id = 0;
105 is_draw_pixel_by_pixel = false;
106 }
107
108 /**
109 * Release the list of sprite object
110 */
release_sprites_list()111 template < class X, class T > void objects_list < X, T >::release_sprites_list ()
112 {
113 if (NULL == sprites_list)
114 {
115 return;
116 }
117 for (Uint32 i = 0; i < num_of_sprites_allocated; i++)
118 {
119 X *sprite = sprites_list[i];
120 if (NULL != sprite)
121 {
122 delete sprite;
123 }
124 sprites_list[i] = (X *) NULL;
125 }
126 delete[]sprites_list;
127 sprites_list = NULL;
128 max_of_sprites = 0;
129 num_of_sprites_allocated = 0;
130 object_free ();
131 }
132
133 /**
134 * Return list of the sprites objects
135 * @return pointer to the list of sprites objects
136 */
get_sprites_list()137 template < class X, class T > X ** objects_list < X, T >::get_sprites_list ()
138 {
139 return sprites_list;
140 }
141
142 /**
143 * Return first sprite of the list
144 * @return pointer to the first sprite object of the list
145 */
get_first_sprite()146 template < class X, class T > X * objects_list < X, T >::get_first_sprite ()
147 {
148 if (NULL == sprites_list)
149 {
150 return NULL;
151 }
152 return sprites_list[0];
153 }
154
155 /**
156 * Return the maxium number of sprites objects
157 * @return the maxium number of sprites objects
158 */
get_max_of_sprites()159 template < class X, class T > Uint32 objects_list < X, T >::get_max_of_sprites ()
160 {
161 return max_of_sprites;
162 }
163
164 /**
165 * Initialize the maximum number of sprites
166 * @param maxof maximum number of sprites
167 */
set_max_of_sprites(Uint32 maxof)168 template < class X, class T > void objects_list < X, T >::set_max_of_sprites (Uint32 maxof)
169 {
170 max_of_sprites = maxof;
171 }
172
173 /**
174 * Allocate memory for the list of sprites
175 */
alloc_sprites_list()176 template < class X, class T > void objects_list < X, T >::alloc_sprites_list ()
177 {
178 if (0 == max_of_sprites)
179 {
180 std::cerr << "(!)objects_list::alloc_sprites_list() " <<
181 "Our array should always have at least one element in it!" <<
182 std::endl;
183 throw ("(!)objects_list::alloc_sprites_list() failed! "
184 "At least one element is required");
185 }
186
187 release_sprites_list ();
188 try
189 {
190 sprites_list = new X*[max_of_sprites];
191 }
192 catch (std::bad_alloc &)
193 {
194 std::cerr << "(!)objects_list::alloc_sprites_list() " <<
195 "not enough memory to allocate " <<
196 max_of_sprites << " elements!" << std::endl;
197 throw;
198 }
199 for (Uint32 i = 0; i < max_of_sprites; i++)
200 {
201 sprites_list[i] = NULL;
202 }
203 num_of_sprites_allocated = max_of_sprites;
204 }
205
206 /**
207 * Initialize the list of sprites objects
208 * @param maxof maximum number of sprites
209 * @param have_shadow true if the sprite has shadow, true by default
210 */
create_sprites_list(Uint32 maxof,bool have_shades)211 template < class X, class T > void objects_list < X, T >::create_sprites_list (Uint32 maxof, bool have_shades)
212 {
213 max_of_sprites = maxof;
214 sprites_have_shades = have_shades;
215 create_sprites_list ();
216 }
217
218 /**
219 * Initialize the list of sprites objects
220 */
create_sprites_list()221 template < class X, class T > void objects_list < X, T >::create_sprites_list ()
222 {
223 alloc_sprites_list ();
224
225 X *sprite_template = new X ();
226 sprite_template->set_object_pos (0);
227 sprites_list[0] = sprite_template;
228
229 /* reserves only once the memory required for the
230 * graphic data of the sprite */
231 sprite_template->create_sprite (sprite_type_id, sprites_bitmap,
232 sprites_have_shades, is_draw_pixel_by_pixel);
233 sprite_template->set_draw_method (sprite_object::DRAW_WITH_TABLES);
234 sprites->add (sprite_template);
235 for (Uint32 i = 1; i < max_of_sprites; i++)
236 {
237 X *sprite = new X ();
238 sprite->set_object_pos (i);
239 sprite_template->duplicate_to (sprite);
240 sprites_list[i] = sprite;
241 sprites->add (sprite);
242 }
243 }
244
245 /**
246 * Enable all sprites objects
247 */
enable_sprites()248 template < class X, class T > void objects_list < X, T >::enable_sprites ()
249 {
250 for (Uint32 i = 0; i < max_of_sprites; i++)
251 {
252 X *sprite = sprites_list[i];
253 sprite->enable ();
254 }
255 }
256
257 /**
258 * Disable all sprites objects
259 */
disable_sprites()260 template < class X, class T > void objects_list < X, T >::disable_sprites ()
261 {
262 for (Uint32 i = 0; i < max_of_sprites; i++)
263 {
264 X *sprite = sprites_list[i];
265 sprite->disable ();
266 }
267 }
268
269 #endif
270