1 /***************************************************************************
2 copyright : (C) 2003 by Michael Speck
3 email : http://lgames.sf.net
4 ***************************************************************************/
5
6 /***************************************************************************
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 ***************************************************************************/
14
15 #include "defs.h"
16 #include "data.h"
17
18 SDL_Surface *img_particles = 0;
19 SDL_Surface *img_ground = 0;
20 SDL_Surface *img_crater = 0;
21 SDL_Surface *img_small_crater = 0;
22 SDL_Surface *img_units = 0;
23 SDL_Surface *img_trees = 0;
24 SDL_Surface *img_shots = 0;
25 SDL_Surface *img_ammo = 0;
26 SDL_Surface *img_logo = 0;
27 SDL_Surface *img_icons = 0;
28 SDL_Surface *img_black = 0;
29 SDL_Surface *background = 0;
30 SDL_Surface *img_cursors = 0;
31 SDL_Surface *img_gun = 0;
32
33 SDL_Font *ft_main = 0;
34 SDL_Font *ft_chart = 0, *ft_chart_highlight = 0;
35 SDL_Font *ft_menu = 0, *ft_menu_highlight = 0;
36 SDL_Font *ft_help = 0;
37 SDL_Font *ft_copyright = 0;
38
39 SDL_Sound *wav_expl1 = 0; /* grenade explosion */
40 SDL_Sound *wav_expl2 = 0; /* (cluster)bomb explosion */
41 SDL_Sound *wav_expl3 = 0; /* tank explosion */
42 SDL_Sound *wav_cannon1 = 0; /* grenade autocannon */
43 SDL_Sound *wav_cannon2 = 0;
44 SDL_Sound *wav_click = 0;
45 SDL_Sound *wav_highlight = 0;
46
47 SDL_Cursor *cr_empty = 0;
48
49 int img_count = 0, wav_count = 0, ft_count = 0;
50 int ammo_w = 0, ammo_h = 0; /* size of an ammo tile */
51 int cursor_w = 0, cursor_x_offset = 0;
52 int gun_w = 0, gun_h = 0;
53
54 extern int audio_on;
55
56 extern SDL_Surface *screen; /* display */
57
58 #define SET_ALPHA( surf, alpha ) SDL_SetAlpha( surf, SDL_SRCALPHA | SDL_RLEACCEL, alpha )
59 #define SET_CKEY( surf, ckey ) SDL_SetColorKey( surf, SDL_SRCCOLORKEY, ckey )
60
data_free_cursor(SDL_Cursor ** cursor)61 static void data_free_cursor( SDL_Cursor **cursor )
62 {
63 if ( *cursor ) {
64 SDL_FreeCursor( *cursor );
65 *cursor = 0;
66 }
67 }
68
data_create_cursor(int width,int height,int hot_x,int hot_y,char * source)69 static SDL_Cursor* data_create_cursor( int width, int height, int hot_x, int hot_y, char *source )
70 {
71 unsigned char *mask = 0, *data = 0;
72 SDL_Cursor *cursor = 0;
73 int i, j, k;
74 unsigned char data_byte, mask_byte;
75 int pot;
76
77 /* meaning of char from source: b : black, w: white, ' ':transparent */
78
79 mask = malloc( width * height * sizeof ( char ) / 8 );
80 data = malloc( width * height * sizeof ( char ) / 8 );
81
82 k = 0;
83 for (j = 0; j < width * height; j += 8, k++) {
84 pot = 1;
85 data_byte = mask_byte = 0;
86 /* create byte */
87 for (i = 7; i >= 0; i--) {
88 switch ( source[j + i] ) {
89 case 'b': data_byte += pot;
90 case 'w': mask_byte += pot; break;
91 }
92 pot *= 2;
93 }
94 /* add to mask */
95 data[k] = data_byte;
96 mask[k] = mask_byte;
97
98 }
99 /* create and return cursor */
100 cursor = SDL_CreateCursor( data, mask, width, height, hot_x, hot_y );
101 free( mask );
102 free( data );
103 return cursor;
104 }
105
data_free_image(SDL_Surface ** surf)106 static void data_free_image( SDL_Surface **surf )
107 {
108 if ( *surf ) {
109 SDL_FreeSurface( *surf );
110 *surf = 0;
111 }
112 }
113
data_create_image(int w,int h)114 static SDL_Surface *data_create_image( int w, int h )
115 {
116 SDL_Surface *surf = 0;
117 printf( "creating %ix%i bitmap ... ", w, h );
118 surf = SDL_CreateRGBSurface(
119 SDL_SWSURFACE, w, h,
120 screen->format->BitsPerPixel,
121 screen->format->Rmask, screen->format->Bmask,
122 screen->format->Gmask, screen->format->Amask );
123 if ( surf == 0 ) {
124 printf( "%s\n", SDL_GetError() );
125 exit(1);
126 }
127 img_count++; /* statistics */
128 printf( "ok\n" );
129 return surf;
130 }
131
data_load_image(char * name)132 static SDL_Surface *data_load_image( char *name )
133 {
134 char path[128];
135 SDL_Surface *tmp = 0, *surf = 0;
136 snprintf( path, 128, "%s/gfx/%s", SRC_DIR, name ); path[127] = 0;
137 printf( "loading %s ... ", path );
138 tmp = SDL_LoadBMP( path );
139 if ( tmp == 0 || (surf = SDL_DisplayFormat(tmp)) == 0 ) {
140 printf( "%s\n", SDL_GetError() );
141 data_free_image( &tmp );
142 exit(1);
143 }
144 data_free_image( &tmp );
145 img_count++; /* statistics */
146 printf( "ok\n" );
147 return surf;
148 }
149
data_free_font(SDL_Font ** font)150 static void data_free_font( SDL_Font **font )
151 {
152 if ( *font ) {
153 data_free_image( &(*font)->Surface );
154 free( *font );
155 *font = 0;
156 }
157 }
158
data_load_font(char * name)159 static SDL_Font* data_load_font( char *name )
160 {
161 SDL_Font *font = calloc( 1, sizeof( SDL_Font ) );
162 if ( font == 0 ) { printf( "out of memory\n" ); exit(1); }
163 font->Surface = data_load_image( name );
164 InitFont2( font );
165 ft_count++; /* statistics */
166 return font;
167 }
168
169 #ifdef AUDIO_ENABLED
data_free_sound(SDL_Sound ** wav)170 static void data_free_sound( SDL_Sound **wav )
171 {
172 if ( *wav ) {
173 Mix_FreeChunk( *wav );
174 *wav = 0;
175 }
176 }
177
data_load_sound(char * name)178 static SDL_Sound *data_load_sound( char *name )
179 {
180 char path[128];
181 SDL_Sound *wav = 0;
182 snprintf( path, 128, "%s/sounds/%s", SRC_DIR, name ); path[127] = 0;
183 printf( "loading %s ... ", path );
184 wav = Mix_LoadWAV( path ); /* convets already */
185 if ( wav == 0 ) {
186 printf( "%s\n", SDL_GetError() );
187 exit(1);
188 }
189 wav_count++; /* statistics */
190 printf( "ok\n" );
191 return wav;
192 }
193 #endif
194
data_load()195 void data_load()
196 {
197 img_ground = data_load_image( "ground.bmp" );
198 img_particles = data_load_image( "particles.bmp" );
199 SET_CKEY( img_particles, 0x0 );
200 img_crater = data_load_image("crater.bmp" );
201 SET_CKEY( img_crater, 0x0 ); SET_ALPHA( img_crater, 196 );
202 img_small_crater = data_load_image( "small_crater.bmp" );
203 SET_CKEY( img_small_crater, 0x0 ); SET_ALPHA( img_small_crater, 196 );
204 img_units = data_load_image( "units.bmp" );
205 SET_CKEY( img_units, 0x0 );
206 img_trees = data_load_image( "trees.bmp" );
207 SET_CKEY( img_trees, 0x0 );
208 img_shots = data_load_image( "shots.bmp" );
209 SET_CKEY( img_shots, 0x0);
210 img_ammo = data_load_image( "ammo.bmp" );
211 SET_CKEY( img_ammo, 0x0 );
212 ammo_w = img_ammo->w; ammo_h = img_ammo->h / AMMO_BOMB;
213 img_logo = data_load_image( "logo.bmp" );
214 SDL_LockSurface( img_logo );
215 SET_CKEY( img_logo, *(Uint16*)(img_logo->pixels) );
216 SDL_UnlockSurface( img_logo );
217 img_icons = data_load_image( "icons.bmp" );
218 img_black = data_create_image( screen->w, screen->h );
219 SET_ALPHA( img_black, 128 );
220 background = data_create_image( screen->w, screen->h );
221 img_cursors = data_load_image( "cursors.bmp" );
222 SET_CKEY( img_cursors, 0x0 );
223 cursor_w = img_cursors->w / CURSOR_COUNT;
224 img_gun = data_load_image( "gun.bmp" );
225 SDL_LockSurface( img_gun );
226 SET_CKEY( img_gun, *(Uint16*)(img_gun->pixels) );
227 SDL_UnlockSurface( img_gun );
228 gun_w = img_gun->w / 9; gun_h = img_gun->h;
229
230 ft_main = data_load_font( "ft_red.bmp" );
231 ft_chart = data_load_font( "ft_red.bmp" );
232 ft_chart_highlight = data_load_font( "ft_yellow.bmp" );
233 ft_help = data_load_font( "ft_red14.bmp" );
234 ft_copyright = data_load_font( "ft_neon.bmp" );
235 /* use the same fonts for menu */
236 ft_menu = ft_chart; ft_menu_highlight = ft_chart_highlight;
237
238 #ifdef AUDIO_ENABLED
239 if ( audio_on != -1 ) {
240 wav_expl1 = data_load_sound( "expl1.wav" );
241 wav_expl2 = data_load_sound( "expl2.wav" );
242 wav_expl3 = data_load_sound( "expl3.wav" );
243 wav_cannon1 = data_load_sound( "autocannon.wav" );
244 wav_cannon2 = data_load_sound( "gunfire.wav" );
245 wav_click = data_load_sound( "click.wav" );
246 wav_highlight = data_load_sound( "highlight.wav" );
247 }
248 #endif
249
250 cr_empty = data_create_cursor( 16, 16, 8, 8,
251 " "
252 " "
253 " "
254 " "
255 " "
256 " "
257 " "
258 " "
259 " "
260 " "
261 " "
262 " "
263 " "
264 " "
265 " "
266 " " );
267 }
268
data_delete()269 void data_delete()
270 {
271 data_free_image( &img_particles );
272 data_free_image( &img_ground );
273 data_free_image( &img_crater );
274 data_free_image( &img_small_crater );
275 data_free_image( &img_units );
276 data_free_image( &img_trees );
277 data_free_image( &img_shots );
278 data_free_image( &img_ammo );
279 data_free_image( &img_logo );
280 data_free_image( &img_icons );
281 data_free_image( &img_black );
282 data_free_image( &background );
283 data_free_image( &img_cursors );
284 data_free_image( &img_gun );
285 printf( "%i images deleted\n", img_count );
286
287 data_free_font( &ft_main );
288 data_free_font( &ft_chart );
289 data_free_font( &ft_chart_highlight );
290 data_free_font( &ft_help );
291 data_free_font( &ft_copyright );
292 printf( "%i fonts deleted\n", ft_count );
293
294 #ifdef AUDIO_ENABLED
295 if ( audio_on != -1 ) {
296 data_free_sound( &wav_expl1 );
297 data_free_sound( &wav_expl2 );
298 data_free_sound( &wav_expl3 );
299 data_free_sound( &wav_cannon1 );
300 data_free_sound( &wav_click );
301 data_free_sound( &wav_highlight );
302 printf( "%i sounds deleted\n", wav_count );
303 }
304 #endif
305
306 data_free_cursor( &cr_empty );
307 }
308