Debian Codegraph @

terps/scare/scresour.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o ...
24 */
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "scare.h"
31 #include "scprotos.h"
32 #include "scgamest.h"
33
34
35 /* Assorted definitions and constants. */
36 static const sc_char NUL = '\0';
37
38
39 /*
40 * res_has_sound()
41 * res_has_graphics()
42 *
43 * Return TRUE if the game uses sound or graphics.
44 */
45 sc_bool
46 res_has_sound (sc_gameref_t game)
47 {
48 const sc_prop_setref_t bundle = gs_get_bundle (game);
49 sc_vartype_t vt_key[2];
50 sc_bool has_sound;
51 assert (gs_is_game_valid (game));
52
53 vt_key[0].string = "Globals";
54 vt_key[1].string = "Sound";
55 has_sound = prop_get_boolean (bundle, "B<-ss", vt_key);
56 return has_sound;
57 }
58
59 sc_bool
60 res_has_graphics (sc_gameref_t game)
61 {
62 const sc_prop_setref_t bundle = gs_get_bundle (game);
63 sc_vartype_t vt_key[2];
64 sc_bool has_graphics;
65 assert (gs_is_game_valid (game));
66
67 vt_key[0].string = "Globals";
68 vt_key[1].string = "Graphics";
69 has_graphics = prop_get_boolean (bundle, "B<-ss", vt_key);
70 return has_graphics;
71 }
72
73
74 /*
75 * res_set_resource()
76 * res_clear_resource()
77 * res_compare_resource()
78 *
79 * Convenience functions to set, clear, and compare resource fields.
80 */
81 static void
82 res_set_resource (sc_resourceref_t resource, const sc_char *name,
83 sc_int offset, sc_int length)
84 {
85 resource->name = name;
86 resource->offset = offset;
87 resource->length = length;
88 }
89
90 void
91 res_clear_resource (sc_resourceref_t resource)
92 {
93 res_set_resource (resource, "", 0, 0);
94 }
95
96 sc_bool
97 res_compare_resource (sc_resourceref_t from, sc_resourceref_t with)
98 {
99 return strcmp (from->name, with->name) == 0
100 && from->offset == with->offset && from->length == with->length;
101 }
102
103
104 /*
105 * res_handle_resource()
106 *
107 * General helper for handling graphics and sound resources. Supplied with a
108 * partial key to the node containing resources, it identifies what resource
109 * is appropriate, and sets this as the requested resource in the game, for
110 * later use on sync'ing, using the handler appropriate for the game version.
111 *
112 * The partial format is something like "sis" (the bit to follow I<- or S<-
113 * in prop_get), and the partial key is guaranteed to contain at least
114 * strlen(partial_format) elements.
115 */
116 void
117 res_handle_resource (sc_gameref_t game,
118 const sc_char *partial_format,
119 const sc_vartype_t vt_partial[])
120 {
121 const sc_prop_setref_t bundle = gs_get_bundle (game);
122 sc_vartype_t vt_key[2], *vt_full;
123 sc_int partial_length, resource_start_offset;
124 sc_bool embedded;
125 sc_char *format;
126 assert (gs_is_game_valid (game));
127 assert (partial_format && vt_partial);
128
129 /*
130 * Check for resources. If this game doesn't use any, exit now to avoid the
131 * overhead of pointless lookups and allocations.
132 */
133 if (!(res_has_sound (game) || res_has_graphics (game)))
134 return;
135
136 /*
137 * Get the global offset for all resources. For version 3.9 games this
138 * should be zero. For version 4.0 games, it's the start of resource data
139 * in the TAF file where resources are embedded.
140 */
141 vt_key[0].string = "ResourceOffset";
142 resource_start_offset = prop_get_integer (bundle, "I<-s", vt_key);
143
144 /*
145 * Get the flag that indicated embedded resources. For version 3.9 games
146 * this should be false. If not set, offset and length are forced to zero
147 * for interface functions.
148 */
149 vt_key[0].string = "Globals";
150 vt_key[1].string = "Embedded";
151 embedded = prop_get_boolean (bundle, "B<-ss", vt_key);
152
153 /*
154 * Allocate a format for use with properties calls, five characters longer
155 * than the partial passed in. Build a key one element larger than the
156 * partial supplied, and copy over all supplied elements.
157 */
158 partial_length = strlen (partial_format);
159 format = sc_malloc (partial_length + 5);
160
161 vt_full = sc_malloc ((partial_length + 1) * sizeof (vt_partial[0]));
162 memcpy (vt_full, vt_partial, partial_length * sizeof (vt_partial[0]));
163
164 /* Search for sound resources, and offer if found. */
165 if (res_has_sound (game))
166 {
167 const sc_char *soundfile;
168 sc_int soundoffset, soundlen;
169
170 /* Get soundfile property from the node supplied. */
171 vt_full[partial_length].string = "SoundFile";
172 strcpy (format, "S<-");
173 strcat (format, partial_format);
174 strcat (format, "s");
175 soundfile = prop_get_string (bundle, format, vt_full);
176
177 /* If a sound is defined, handle it. */
178 if (!sc_strempty (soundfile))
179 {
180 if (embedded)
181 {
182 /* Retrieve offset and length. */
183 vt_full[partial_length].string = "SoundOffset";
184 strcpy (format, "I<-");
185 strcat (format, partial_format);
186 strcat (format, "s");
187 soundoffset = prop_get_integer (bundle, format, vt_full)
188 + resource_start_offset;
189
190 vt_full[partial_length].string = "SoundLen";
191 strcpy (format, "I<-");
192 strcat (format, partial_format);
193 strcat (format, "s");
194 soundlen = prop_get_integer (bundle, format, vt_full);
195 }
196 else
197 {
198 /* Coerce offset and length to zero. */
199 soundoffset = 0;
200 soundlen = 0;
201 }
202
203 /*
204 * If the sound is the special "##", latch stop, otherwise note
205 * details to play on sync.
206 */
207 if (!strcmp (soundfile, "##"))
208 {
209 game->stop_sound = TRUE;
210 res_clear_resource (&game->requested_sound);
211 }
212 else
213 {
214 res_set_resource (&game->requested_sound,
215 soundfile, soundoffset, soundlen);
216 }
217 }
218 }
219
220 /* Now do the same thing for graphics resources. */
221 if (res_has_graphics (game))
222 {
223 const sc_char *graphicfile;
224 sc_int graphicoffset, graphiclen;
225
226 /* Get graphicfile property from the node supplied. */
227 vt_full[partial_length].string = "GraphicFile";
228 strcpy (format, "S<-");
229 strcat (format, partial_format);
230 strcat (format, "s");
231 graphicfile = prop_get_string (bundle, format, vt_full);
232
233 /* If a graphic is defined, handle it. */
234 if (!sc_strempty (graphicfile))
235 {
236 if (embedded)
237 {
238 /* Retrieve offset and length. */
239 vt_full[partial_length].string = "GraphicOffset";
240 strcpy (format, "I<-");
241 strcat (format, partial_format);
242 strcat (format, "s");
243 graphicoffset = prop_get_integer (bundle, format, vt_full)
244 + resource_start_offset;
245
246 vt_full[partial_length].string = "GraphicLen";
247 strcpy (format, "I<-");
248 strcat (format, partial_format);
249 strcat (format, "s");
250 graphiclen = prop_get_integer (bundle, format, vt_full);
251 }
252 else
253 {
254 /* Coerce offset and length to zero. */
255 graphicoffset = 0;
256 graphiclen = 0;
257 }
258
259 /* Graphics resource retrieved, note to show on sync. */
260 res_set_resource (&game->requested_graphic,
261 graphicfile, graphicoffset, graphiclen);
262 }
263 }
264
265 /* Free allocated memory. */
266 sc_free (format);
267 sc_free (vt_full);
268 }
269
270
271 /*
272 * res_sync_resources()
273 *
274 * Bring resources into line with the game; called on undo, restart,
275 * restore, and so on.
276 */
277 void
278 res_sync_resources (sc_gameref_t game)
279 {
280 assert (gs_is_game_valid (game));
281
282 /* Deal with any latched sound stop first. */
283 if (game->stop_sound)
284 {
285 if (game->sound_active)
286 {
287 if_update_sound ("", 0, 0, FALSE);
288 game->sound_active = FALSE;
289
290 res_clear_resource (&game->playing_sound);
291 }
292 game->stop_sound = FALSE;
293 }
294
295 /* Look for a change of sound, and pass to interface on change. */
296 if (!res_compare_resource (&game->playing_sound,
297 &game->requested_sound))
298 {
299 const sc_char *name;
300 sc_char *clean_name;
301 sc_bool is_looping;
302
303 /* If the sound name ends '##', this is a looping sound. */
304 name = game->requested_sound.name;
305 is_looping = !strcmp (name + strlen (name) - 2, "##");
306
307 clean_name = sc_malloc (strlen (name) + 1);
308 strcpy (clean_name, name);
309 if (is_looping)
310 clean_name[strlen (clean_name) - 2] = NUL;
311
312 if_update_sound (clean_name,
313 game->requested_sound.offset,
314 game->requested_sound.length, is_looping);
315 game->playing_sound = game->requested_sound;
316 game->sound_active = TRUE;
317
318 sc_free (clean_name);
319 }
320
321 /* Look for a change of graphic, and pass to interface on change. */
322 if (!res_compare_resource (&game->displayed_graphic,
323 &game->requested_graphic))
324 {
325 if_update_graphic (game->requested_graphic.name,
326 game->requested_graphic.offset,
327 game->requested_graphic.length);
328 game->displayed_graphic = game->requested_graphic;
329 }
330 }
331
332
333 /*
334 * res_cancel_resources()
335 *
336 * Turn off sound and graphics, and reset the game's tracking of resources in
337 * use to match. Called on game restart or restore.
338 */
339 void
340 res_cancel_resources (sc_gameref_t game)
341 {
342 assert (gs_is_game_valid (game));
343
344 /* Request that everything stops and clears. */
345 game->stop_sound = FALSE;
346 res_clear_resource (&game->requested_sound);
347 res_clear_resource (&game->requested_graphic);
348
349 /* Synchronize to have the above take effect. */
350 res_sync_resources (game);
351 }