diff options
author | Benjamin Otte <otte@redhat.com> | 2011-10-16 11:29:34 -0700 |
---|---|---|
committer | Benjamin Otte <otte@redhat.com> | 2011-10-16 22:31:54 -0700 |
commit | 2c5e043e76edfb4efcb5581541414e93782ee0e9 (patch) | |
tree | e5ad784188e95b3a33241fd1be4cb9a903d70ec5 | |
parent | a7fd7c93a440feb61444eb0b9c4f94947c8ad711 (diff) |
resource: Add a data resource
This should be the only way data (for images, sounds, maps, your mom,
...) enters the game.
-rw-r--r-- | libgame/Makefile.am | 2 | ||||
-rw-r--r-- | libgame/game-data-resource.c | 127 | ||||
-rw-r--r-- | libgame/game-data-resource.h | 59 | ||||
-rw-r--r-- | libgame/libgame.h | 1 |
4 files changed, 189 insertions, 0 deletions
diff --git a/libgame/Makefile.am b/libgame/Makefile.am index b2ceb35..a1fa703 100644 --- a/libgame/Makefile.am +++ b/libgame/Makefile.am @@ -21,6 +21,7 @@ libgame_@LIBGAME_VERS@_la_SOURCES = \ game-board.c \ game-colored.c \ game-container.c \ + game-data-resource.c \ game-filter.c \ game-game.c \ game-geom-basics.c \ @@ -58,6 +59,7 @@ basic_headers = \ game-board.h \ game-colored.h \ game-container.h \ + game-data-resource.h \ game-filter.h \ game-game.h \ game-geom-basics.h \ diff --git a/libgame/game-data-resource.c b/libgame/game-data-resource.c new file mode 100644 index 0000000..86dfcf7 --- /dev/null +++ b/libgame/game-data-resource.c @@ -0,0 +1,127 @@ +/* + * 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-data-resource.h" + +enum { + PROP_0, + PROP_DATA, + PROP_SIZE +}; + +G_DEFINE_ABSTRACT_TYPE (GameDataResource, game_data_resource, GAME_TYPE_RESOURCE) + +static void +game_data_resource_get_property (GObject *object, guint param_id, GValue *value, + GParamSpec * pspec) +{ + GameDataResource *resource = GAME_DATA_RESOURCE (object); + + switch (param_id) { + case PROP_DATA: + g_value_set_pointer (value, resource->data); + break; + case PROP_SIZE: + g_value_set_ulong (value, resource->size); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + } +} + +static void +game_data_resource_set_property (GObject *object, guint param_id, const GValue *value, + GParamSpec *pspec) +{ + switch (param_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + } +} + +static void +game_data_resource_dispose (GObject *object) +{ + GameDataResource *resource = GAME_DATA_RESOURCE (object); + + game_data_resource_set_data (resource, NULL, 0); + + G_OBJECT_CLASS (game_data_resource_parent_class)->dispose (object); +} + +static void +game_data_resource_class_init (GameDataResourceClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->set_property = game_data_resource_set_property; + object_class->get_property = game_data_resource_get_property; + object_class->dispose = game_data_resource_dispose; + + g_object_class_install_property (object_class, PROP_DATA, + g_param_spec_object ("data", _("data"), _("data of this resource"), + GAME_TYPE_GAME, G_PARAM_READABLE)); + g_object_class_install_property (object_class, PROP_SIZE, + g_param_spec_ulong ("size", _("size"), + _("size of the given data"), + 0, G_MAXULONG, 0, G_PARAM_READABLE)); + +} + +static void +game_data_resource_init (GameDataResource *resource) +{ +} + +const guchar * +game_data_resource_get_data (GameDataResource *resource) +{ + g_return_val_if_fail (GAME_IS_DATA_RESOURCE (resource), NULL); + + return resource->data; +} + +gsize +game_data_resource_get_size (GameDataResource *resource) +{ + g_return_val_if_fail (GAME_IS_DATA_RESOURCE (resource), 0); + + return resource->size; +} + +void +game_data_resource_set_data (GameDataResource *resource, + guchar * data, + gsize size) +{ + g_return_if_fail (GAME_IS_DATA_RESOURCE (resource)); + g_return_if_fail (size == 0 || data != NULL); + g_return_if_fail (data == NULL || size != 0); + + g_free (resource->data); + resource->data = data; + resource->size = size; + + g_object_notify (G_OBJECT (resource), "data"); + g_object_notify (G_OBJECT (resource), "size"); +} + diff --git a/libgame/game-data-resource.h b/libgame/game-data-resource.h new file mode 100644 index 0000000..c5b68c5 --- /dev/null +++ b/libgame/game-data-resource.h @@ -0,0 +1,59 @@ +/* + * 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_DATA_RESOURCE_H__ +#define __GAME_DATA_RESOURCE_H__ + +#include <libgame/game-resource.h> + +G_BEGIN_DECLS + +#define GAME_TYPE_DATA_RESOURCE (game_data_resource_get_type ()) +#define GAME_DATA_RESOURCE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GAME_TYPE_DATA_RESOURCE, GameDataResource)) +#define GAME_DATA_RESOURCE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GAME_TYPE_DATA_RESOURCE, GameDataResourceClass)) +#define GAME_IS_DATA_RESOURCE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GAME_TYPE_DATA_RESOURCE)) +#define GAME_IS_DATA_RESOURCE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GAME_TYPE_DATA_RESOURCE)) +#define GAME_DATA_RESOURCE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GAME_TYPE_DATA_RESOURCE, GameDataResourceClass)) + +typedef struct _GameDataResource GameDataResource; +typedef struct _GameDataResourceClass GameDataResourceClass; + +struct _GameDataResource { + GameResource resource; + + /*< private >*/ + guchar * data; + gsize size; +}; + +struct _GameDataResourceClass { + GameResourceClass resource_class; +}; + +GType game_data_resource_get_type (void) G_GNUC_CONST; + +const guchar * game_data_resource_get_data (GameDataResource * resource); +gsize game_data_resource_get_size (GameDataResource * resource); +void game_data_resource_set_data (GameDataResource * resource, + guchar * data, + gsize length); + + +G_END_DECLS + +#endif /* __GAME_DATA_RESOURCE_H__ */ diff --git a/libgame/libgame.h b/libgame/libgame.h index 7fc65c5..3c7c3e2 100644 --- a/libgame/libgame.h +++ b/libgame/libgame.h @@ -28,6 +28,7 @@ #include <libgame/game-board.h> #include <libgame/game-colored.h> #include <libgame/game-container.h> +#include <libgame/game-data-resource.h> #include <libgame/game-filter.h> #include <libgame/game-game.h> #include <libgame/game-geom-basics.h> |