1 /**
2 * @file energy_gauge.c
3 * @brief Handle displaying of the energy gauge the top score panel
4 * for the player's spaceship and the current guardian
5 * @created 2007-01-06
6 * @date 2012-08-26
7 * @author Jean-Michel Martin de Santero
8 * @author Bruno Ethvignot
9 */
10 /*
11 * copyright (c) 1998-2015 TLK Games all rights reserved
12 * $Id: energy_gauge.c,v 1.20 2012/08/26 17:09:14 gurumeditation Exp $
13 *
14 * Powermanga is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Powermanga is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 * MA 02110-1301, USA.
28 */
29 #include "config.h"
30 #include "powermanga.h"
31 #include "tools.h"
32 #include "images.h"
33 #include "config_file.h"
34 #include "display.h"
35 #include "electrical_shock.h"
36 #include "enemies.h"
37 #include "energy_gauge.h"
38 #include "guardians.h"
39 #include "log_recorder.h"
40 #include "shots.h"
41 #include "gfx_wrapper.h"
42 #include "images.h"
43 #include "spaceship.h"
44
45 bool energy_gauge_spaceship_is_update = TRUE;
46 bool energy_gauge_guard_is_update = TRUE;
47 static image gauge_red;
48 static image gauge_green;
49 static image gauge_blue;
50 static const Sint32 GAUGE_SPACESHIP_WIDTH = 100;
51
52 static void draw_energy_gauge (Uint32 sizeof_bar, Sint32 energy,
53 Uint32 coordx, Uint32 energy_max);
54
55 /**
56 * Load sprites images of the gauge
57 * @return TRUE if it completed successfully or FALSE otherwise
58 */
59 bool
energy_gauge_once_init(void)60 energy_gauge_once_init (void)
61 {
62
63 if (!image_load_single
64 ("graphics/sprites/energy_gauge_red.spr", &gauge_red))
65 {
66 return FALSE;
67 }
68 if (!image_load_single
69 ("graphics/sprites/energy_gauge_green.spr", &gauge_green))
70 {
71 return FALSE;
72 }
73 if (!image_load_single
74 ("graphics/sprites/energy_gauge_blue.spr", &gauge_blue))
75 {
76 return FALSE;
77 }
78 return TRUE;
79 }
80
81 /**
82 * Convert from data image to PNG file
83 * @return TRUE if successful
84 */
85 #ifdef PNG_EXPORT_ENABLE
86 bool
energy_gauge_extract(void)87 energy_gauge_extract (void)
88 {
89 const char *model = EXPORT_DIR "/gauges/energy_gauge-%01d.png";
90 char *filename = memory_allocation (strlen (model) + 1);
91 if (filename == NULL)
92 {
93 LOG_ERR ("not enough memory to allocate %i bytes\n",
94 (Uint32) (strlen (model) + 1));
95 return FALSE;
96 }
97 if (!create_dir (EXPORT_DIR "/gauges"))
98 {
99 free_memory (filename);
100 return FALSE;
101 }
102 free_memory (filename);
103 return TRUE;
104 }
105 #endif
106
107 /**
108 * Release memory used for the images sprites
109 */
110 void
energy_gauge_free(void)111 energy_gauge_free (void)
112 {
113 LOG_DBG ("deallocates the memory used by the sprites images");
114 images_free (&gauge_blue, 1, 1, 1);
115 images_free (&gauge_green, 1, 1, 1);
116 images_free (&gauge_red, 1, 1, 1);
117 }
118
119 /**
120 * Draw the entire gauge in blue
121 */
122 void
energy_gauge_init(void)123 energy_gauge_init (void)
124 {
125 Sint32 i;
126 for (i = 0; i < 100; i++)
127 {
128 draw_image_in_score (&gauge_blue, 210 + i, 3);
129 }
130 }
131
132 /**
133 * Display player's ernergy level bar
134 */
135 void
energy_gauge_spaceship_update(void)136 energy_gauge_spaceship_update (void)
137 {
138 spaceship_struct *ship = spaceship_get ();
139 if (!energy_gauge_spaceship_is_update)
140 {
141 return;
142 }
143 draw_energy_gauge (GAUGE_SPACESHIP_WIDTH * pixel_size,
144 ship->spr.energy_level, 210 * pixel_size,
145 (ship->type * 20 + 20) * pixel_size);
146 }
147
148 /**
149 * Draw guardian's energy gauge
150 */
151 void
energy_gauge_guardian_update(void)152 energy_gauge_guardian_update (void)
153 {
154 Uint32 energy_level;
155 enemy *guard;
156 if (!energy_gauge_guard_is_update)
157 {
158 return;
159 }
160 guard = guardian->foe[0];
161 if (guard != NULL && guard->displacement == DISPLACEMENT_GUARDIAN)
162 {
163 energy_level =
164 (guard->spr.energy_level * 45) / guard->spr.max_energy_level;
165 draw_energy_gauge (45, energy_level, 10, 45);
166 }
167 else
168 {
169 draw_energy_gauge (45, 0, 10, 45);
170 }
171 }
172
173 /**
174 * Draw a energy gauge into the top score panel
175 * @param sizeof_bar maximum width of energy barline
176 * @param energy current energy level
177 * @param coordx x coordinate of the ernergy level bar
178 * @param energy_max maximum energy level
179 */
180 static void
draw_energy_gauge(Uint32 sizeof_bar,Sint32 energy,Uint32 coordx,Uint32 energy_max)181 draw_energy_gauge (Uint32 sizeof_bar, Sint32 energy, Uint32 coordx,
182 Uint32 energy_max)
183 {
184 Sint32 width;
185 Sint32 coordy = 3 * pixel_size;
186 if (energy > 0)
187 {
188 draw_image_in_score_repeat (&gauge_green, coordx, coordy, energy);
189 draw_image_in_score_repeat (&gauge_red, coordx + energy, coordy,
190 energy_max - energy);
191 coordx = coordx + energy_max;
192 width = sizeof_bar - energy_max;
193 }
194 else
195 {
196 width = sizeof_bar;
197 }
198 draw_image_in_score_repeat (&gauge_blue, coordx, coordy, width);
199 }
200