summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Otte <otte@redhat.com>2011-10-16 17:36:38 -0700
committerBenjamin Otte <otte@redhat.com>2011-10-16 22:32:55 -0700
commit329d4dd598133f7b133be108417ddba6db13825a (patch)
tree72df7f6488cdd98dba4bb31bdfc67def9aa03d28
parent5cfd57ca9e56b60aa821529062629c285d9d00b8 (diff)
game: Add a resource file to what is loaded by default
Load the gvariant printed file $gamename.resources when the game starts. This file contains all the resources used by the game. These resources are available by name using game_game_get_resource() (and can be automatically used using game_game_spawn_object() and other helper functions).
-rw-r--r--libgame/game-game.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/libgame/game-game.c b/libgame/game-game.c
index 74be247..e51b263 100644
--- a/libgame/game-game.c
+++ b/libgame/game-game.c
@@ -1101,6 +1101,59 @@ game_game_get_object (GameGame *game, const char *name)
return g_hash_table_lookup (game->named_objects, name);
}
+static void
+game_game_add_resource (GameGame * game,
+ const char * name,
+ GameResource *resource)
+{
+ g_object_ref (resource);
+ g_object_set (resource, "name", name, NULL);
+}
+
+static void
+game_game_load_default_resources (GameGame *game)
+{
+ GameDataResource *data_resource;
+ char *s;
+ const guchar *data;
+ GVariant *variant, *v;
+ GVariantIter iter;
+ GError *error = NULL;
+ GameResource *resource;
+
+ s = g_strconcat (game->name, ".resources", NULL);
+ data_resource = game_game_load_resource (game, s);
+ g_free (s);
+
+ data = game_data_resource_get_data (data_resource);
+ if (data == NULL)
+ return;
+
+ variant = g_variant_parse (G_VARIANT_TYPE ("a{sv}"),
+ (char *) data,
+ (char *) data + game_data_resource_get_size (data_resource),
+ NULL,
+ &error);
+ if (variant == NULL)
+ {
+ g_warning ("Could not load game resources: %s", error->message);
+ g_error_free (error);
+ return;
+ }
+
+ g_variant_iter_init (&iter, variant);
+
+ while (g_variant_iter_loop (&iter, "{sv}", &s, &v))
+ {
+ resource = game_resource_load (game, v);
+ game_game_add_resource (game, s, resource);
+ g_object_unref (resource);
+ }
+
+ g_variant_unref (variant);
+ g_object_unref (data_resource);
+}
+
GameResource *
game_game_get_resource (GameGame *game, const char *name)
{
@@ -1109,6 +1162,8 @@ game_game_get_resource (GameGame *game, const char *name)
g_return_val_if_fail (GAME_IS_GAME (game), NULL);
g_return_val_if_fail (name != NULL, NULL);
+ game_game_load_default_resources (game);
+
p = g_hash_table_lookup (game->named_objects, name);
if (GAME_IS_RESOURCE (p))
return p;