summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Otte <otte@redhat.com>2011-10-16 10:58:19 -0700
committerBenjamin Otte <otte@redhat.com>2011-10-16 13:35:16 -0700
commita7fd7c93a440feb61444eb0b9c4f94947c8ad711 (patch)
treefa3599b89831228852932eff6914ce83884e5a99
parent47504e67af03341185194304d1f2b86a7bd7bedb (diff)
resource: Add resources
Resources are (immutable in the game) objects that potentially can spawn instances for in-game happenings. Some resources just exist for other resources to use. They are also able to be serialized and deserialized to GVariants, so that it's easy to create them from files.
-rw-r--r--libgame/Makefile.am2
-rw-r--r--libgame/game-resource.c109
-rw-r--r--libgame/game-resource.h60
-rw-r--r--libgame/libgame.h1
4 files changed, 172 insertions, 0 deletions
diff --git a/libgame/Makefile.am b/libgame/Makefile.am
index dff8369..b2ceb35 100644
--- a/libgame/Makefile.am
+++ b/libgame/Makefile.am
@@ -42,6 +42,7 @@ libgame_@LIBGAME_VERS@_la_SOURCES = \
game-private.c \
game-recorder.c \
game-replay.c \
+ game-resource.c \
game-score-bind.c \
game-solo.c \
game-sprite.c \
@@ -75,6 +76,7 @@ basic_headers = \
game-portable.h \
game-recorder.h \
game-replay.h \
+ game-resource.h \
game-score-bind.h \
game-solo.h \
game-sprite.h \
diff --git a/libgame/game-resource.c b/libgame/game-resource.c
new file mode 100644
index 0000000..5a49408
--- /dev/null
+++ b/libgame/game-resource.c
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "game-private.h"
+#include "game-resource.h"
+
+G_DEFINE_ABSTRACT_TYPE (GameResource, game_resource, GAME_TYPE_OBJECT)
+
+static gboolean
+game_resource_do_load (GameResource *dest, GVariantIter *iter)
+{
+ return TRUE;
+}
+
+static void
+game_resource_do_save (GameResource *dest, GVariantBuilder *builder)
+{
+}
+
+static void
+game_resource_class_init (GameResourceClass *klass)
+{
+ klass->load = game_resource_do_load;
+ klass->save = game_resource_do_save;
+}
+
+static void
+game_resource_init (GameResource *resource)
+{
+}
+
+GameResource *
+game_resource_load (GameGame *game,
+ GVariant *variant)
+{
+ GameResource *resource;
+ GameResourceClass *klass;
+ GVariantIter iter;
+ const char *type_name;
+ GType type;
+
+ g_return_val_if_fail (GAME_IS_GAME (game), NULL);
+ g_return_val_if_fail (variant != NULL, NULL);
+
+ g_variant_iter_init (&iter, variant);
+
+ g_variant_iter_next (&iter, "&s", &type_name);
+ type = g_type_from_name (type_name);
+ g_assert (g_type_is_a (type, GAME_TYPE_RESOURCE));
+
+ resource = GAME_RESOURCE (game_game_add_object (game, type, NULL));
+
+ klass = GAME_RESOURCE_GET_CLASS (resource);
+ if (!klass->load (resource, &iter))
+ {
+ g_assert_not_reached ();
+ }
+
+ return resource;
+}
+
+GVariant *
+game_resource_save (GameResource *resource)
+{
+ GameResourceClass *klass;
+ GVariantBuilder builder;
+ GVariant *variant;
+
+ g_return_val_if_fail (GAME_IS_RESOURCE (resource), NULL);
+
+ klass = GAME_RESOURCE_GET_CLASS (resource);
+
+ g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
+ g_variant_builder_add (&builder, "s", G_OBJECT_TYPE_NAME (resource));
+
+ klass->save (resource, &builder);
+
+ variant = g_variant_builder_end (&builder);
+
+ return variant;
+}
+
+GameObject *
+game_resource_spawn (GameResource *resource)
+{
+ GameResourceClass *klass;
+
+ g_return_val_if_fail (GAME_IS_RESOURCE (resource), NULL);
+
+ klass = GAME_RESOURCE_GET_CLASS (resource);
+
+ return klass->spawn (resource);
+}
+
diff --git a/libgame/game-resource.h b/libgame/game-resource.h
new file mode 100644
index 0000000..0fe7aa7
--- /dev/null
+++ b/libgame/game-resource.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GAME_RESOURCE_H__
+#define __GAME_RESOURCE_H__
+
+#include <libgame/game-object.h>
+
+G_BEGIN_DECLS
+
+#define GAME_TYPE_RESOURCE (game_resource_get_type ())
+#define GAME_RESOURCE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GAME_TYPE_RESOURCE, GameResource))
+#define GAME_RESOURCE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GAME_TYPE_RESOURCE, GameResourceClass))
+#define GAME_IS_RESOURCE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GAME_TYPE_RESOURCE))
+#define GAME_IS_RESOURCE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GAME_TYPE_RESOURCE))
+#define GAME_RESOURCE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GAME_TYPE_RESOURCE, GameResourceClass))
+
+typedef struct _GameResource GameResource;
+typedef struct _GameResourceClass GameResourceClass;
+
+struct _GameResource {
+ GameObject object;
+};
+
+struct _GameResourceClass {
+ GameObjectClass object_class;
+
+ void (* save) (GameResource * resource,
+ GVariantBuilder *builder);
+ gboolean (* load) (GameResource * resource,
+ GVariantIter * iter);
+ GameObject * (* spawn) (GameResource * resource);
+};
+
+GType game_resource_get_type (void) G_GNUC_CONST;
+
+GameResource * game_resource_load (GameGame * game,
+ GVariant * variant);
+GVariant * game_resource_save (GameResource * resource);
+GameObject * game_resource_spawn (GameResource * object);
+
+
+G_END_DECLS
+
+#endif /* __GAME_RESOURCE_H__ */
diff --git a/libgame/libgame.h b/libgame/libgame.h
index 83457dd..7fc65c5 100644
--- a/libgame/libgame.h
+++ b/libgame/libgame.h
@@ -45,6 +45,7 @@
#include <libgame/game-portable.h>
#include <libgame/game-recorder.h>
#include <libgame/game-replay.h>
+#include <libgame/game-resource.h>
#include <libgame/game-score-bind.h>
#include <libgame/game-solo.h>
#include <libgame/game-sprite.h>