1 /**
2  * @file controller_ejectors.cc
3  * @brief Ejectors corners controller
4  * @date 2007-10-21
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: controller_ejectors.cc 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 #include "../include/controller_ejectors.h"
29 
30 /**
31  * Create the ejectors controller
32  */
controller_ejectors()33 controller_ejectors::controller_ejectors ()
34 {
35   littleInit ();
36   max_of_sprites = MAX_OF_EJECTORS;
37   sprites_have_shades = true;
38 
39 }
40 
41 /**
42  * Release the ejectors controller
43  */
~controller_ejectors()44 controller_ejectors::~controller_ejectors ()
45 {
46   release_sprites_list ();
47 }
48 
49 /**
50  * Create the ejectors sprites
51  */
52 void
create_ejectors_sprites()53 controller_ejectors::create_ejectors_sprites ()
54 {
55 
56   alloc_sprites_list ();
57 
58   sprites_list[TOP_LEFT_EJECTOR] = new sprite_object ();
59   sprites_list[TOP_RIGHT_EJECTOR] = new sprite_object ();
60   sprites_list[BOTTOM_LEFT_EJECTOR] = new sprite_object ();
61   sprites_list[BOTTOM_RIGHT_EJECTOR] = new sprite_object ();
62 
63   sprites_list[TOP_LEFT_EJECTOR]->create_sprite (sprite_object::EJECTOR_1,
64                                                  sprites_bitmap, true);
65   sprites_list[TOP_LEFT_EJECTOR]->set_coordinates (COORD_EJECTOR_1 *
66                                                    resolution,
67                                                    (COORD_EJECTOR_1 *
68                                                     resolution) -
69                                                    sprites_list
70                                                    [TOP_LEFT_EJECTOR]->
71                                                    get_sprite_height () / 2);
72 
73   sprites_list[TOP_RIGHT_EJECTOR]->create_sprite (sprite_object::EJECTOR_4,
74                                                   sprites_bitmap, true);
75   sprites_list[TOP_RIGHT_EJECTOR]->set_coordinates (COORD_EJECTOR_2 *
76                                                     resolution,
77                                                     (COORD_EJECTOR_1 *
78                                                      resolution) -
79                                                     sprites_list
80                                                     [TOP_RIGHT_EJECTOR]->
81                                                     get_sprite_height () / 2);
82 
83   sprites_list[BOTTOM_LEFT_EJECTOR]->create_sprite (sprite_object::EJECTOR_2,
84                                                     sprites_bitmap, true);
85   sprites_list[BOTTOM_LEFT_EJECTOR]->set_coordinates (COORD_EJECTOR_1 *
86                                                       resolution,
87                                                       (COORD_EJECTOR_2 *
88                                                        resolution) -
89                                                       sprites_list
90                                                       [BOTTOM_LEFT_EJECTOR]->
91                                                       get_sprite_height () /
92                                                       2);
93 
94   sprites_list[BOTTOM_RIGHT_EJECTOR]->create_sprite (sprite_object::EJECTOR_3,
95                                                      sprites_bitmap, true);
96   sprites_list[BOTTOM_RIGHT_EJECTOR]->set_coordinates (COORD_EJECTOR_2 *
97                                                        resolution,
98                                                        (COORD_EJECTOR_2 *
99                                                         resolution) -
100                                                        sprites_list
101                                                        [BOTTOM_RIGHT_EJECTOR]->
102                                                        get_sprite_height () /
103                                                        2);
104 
105   /* has_background = false: ejectors are managed like sprites */
106   if (!has_background)
107     {
108       sprites->add (sprites_list[TOP_LEFT_EJECTOR]);
109       sprites->add (sprites_list[TOP_RIGHT_EJECTOR]);
110       sprites->add (sprites_list[BOTTOM_LEFT_EJECTOR]);
111       sprites->add (sprites_list[BOTTOM_RIGHT_EJECTOR]);
112       sprites_list[TOP_LEFT_EJECTOR]->enable ();
113       sprites_list[TOP_RIGHT_EJECTOR]->enable ();
114       sprites_list[BOTTOM_LEFT_EJECTOR]->enable ();
115       sprites_list[BOTTOM_RIGHT_EJECTOR]->enable ();
116     }
117 }
118 
119 /**
120  * Draw ejectors shadows
121  */
122 void
draw_shadow()123 controller_ejectors::draw_shadow ()
124 {
125   if (!has_background)
126     {
127       return;
128     }
129   sprites_list[TOP_LEFT_EJECTOR]->draw_shadow_to_brackground ();
130   sprites_list[TOP_RIGHT_EJECTOR]->draw_shadow_to_brackground ();
131   sprites_list[BOTTOM_LEFT_EJECTOR]->draw_shadow_to_brackground ();
132   sprites_list[BOTTOM_RIGHT_EJECTOR]->draw_shadow_to_brackground ();
133 }
134 
135 /**
136  * Draw ejectors shadows
137  */
138 void
draw()139 controller_ejectors::draw ()
140 {
141   if (!has_background)
142     {
143       return;
144     }
145   sprites_list[TOP_LEFT_EJECTOR]->draw_to_brackground ();
146   sprites_list[TOP_RIGHT_EJECTOR]->draw_to_brackground ();
147   sprites_list[BOTTOM_LEFT_EJECTOR]->draw_to_brackground ();
148   sprites_list[BOTTOM_RIGHT_EJECTOR]->draw_to_brackground ();
149 }
150 
151 /**
152  * Return a ejector sprite
153  * @param id identifier of the ejector
154  * @return a pointer to the ejector sprite
155  */
156 sprite_object *
get_ejector(Uint32 id)157 controller_ejectors::get_ejector (Uint32 id)
158 {
159   switch (id)
160     {
161     case TOP_LEFT_EJECTOR:
162       return sprites_list[TOP_LEFT_EJECTOR];
163     case TOP_RIGHT_EJECTOR:
164       return sprites_list[TOP_RIGHT_EJECTOR];
165     case BOTTOM_LEFT_EJECTOR:
166       return sprites_list[BOTTOM_LEFT_EJECTOR];
167     case BOTTOM_RIGHT_EJECTOR:
168       return sprites_list[BOTTOM_RIGHT_EJECTOR];
169     }
170   return NULL;
171 }
172 
173 /**
174  * Initialize the table of the positions of the balls on the ejectors.
175  * @param table Pointer to the structure of the ball ejectors
176  */
177 bool controller_ejectors::is_pos_ball_initialized = false;
178 void
initialize_ball_positions(ball_ejector_coords * table)179 controller_ejectors::initialize_ball_positions (ball_ejector_coords * table)
180 {
181   /* is the position of the balls already initialized? */
182   if (is_pos_ball_initialized)
183     {
184       return;
185     }
186   is_pos_ball_initialized = true;
187 
188   for (Uint32 i = 0; i < max_of_sprites; i++)
189     {
190       table->x_coord =
191         (table->x_coord * resolution) + sprites_list[i]->get_x_coord ();
192       table->y_coord =
193         (table->y_coord * resolution) + sprites_list[i]->get_y_coord ();
194       table++;
195     }
196 }
197