| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 36 |
static const sc_char NUL = '\0'; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 131 |
|
| 132 |
|
| 133 |
if (!(res_has_sound (game) || res_has_graphics (game))) |
| 134 |
return; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
vt_key[0].string = "ResourceOffset"; |
| 142 |
resource_start_offset = prop_get_integer (bundle, "I<-s", vt_key); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 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 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 165 |
if (res_has_sound (game)) |
| 166 |
{ |
| 167 |
const sc_char *soundfile; |
| 168 |
sc_int soundoffset, soundlen; |
| 169 |
|
| 170 |
|
| 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 |
|
| 178 |
if (!sc_strempty (soundfile)) |
| 179 |
{ |
| 180 |
if (embedded) |
| 181 |
{ |
| 182 |
|
| 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 |
|
| 199 |
soundoffset = 0; |
| 200 |
soundlen = 0; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 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 |
|
| 221 |
if (res_has_graphics (game)) |
| 222 |
{ |
| 223 |
const sc_char *graphicfile; |
| 224 |
sc_int graphicoffset, graphiclen; |
| 225 |
|
| 226 |
|
| 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 |
|
| 234 |
if (!sc_strempty (graphicfile)) |
| 235 |
{ |
| 236 |
if (embedded) |
| 237 |
{ |
| 238 |
|
| 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 |
|
| 255 |
graphicoffset = 0; |
| 256 |
graphiclen = 0; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
res_set_resource (&game->requested_graphic, |
| 261 |
graphicfile, graphicoffset, graphiclen); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
sc_free (format); |
| 267 |
sc_free (vt_full); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
void |
| 278 |
res_sync_resources (sc_gameref_t game) |
| 279 |
{ |
| 280 |
assert (gs_is_game_valid (game)); |
| 281 |
|
| 282 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
void |
| 340 |
res_cancel_resources (sc_gameref_t game) |
| 341 |
{ |
| 342 |
assert (gs_is_game_valid (game)); |
| 343 |
|
| 344 |
|
| 345 |
game->stop_sound = FALSE; |
| 346 |
res_clear_resource (&game->requested_sound); |
| 347 |
res_clear_resource (&game->requested_graphic); |
| 348 |
|
| 349 |
|
| 350 |
res_sync_resources (game); |
| 351 |
} |