OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
material.c
Go to the documentation of this file.
1 /*
2  * mape - C4 Landscape.txt editor
3  *
4  * Copyright (c) 2005-2009, Armin Burgmeier
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
31 #include "mape/material.h"
32 
33 /* Declare private API */
36 
39 
42 
46 };
47 
48 enum {
50 
51  /* read only */
53 };
54 
55 #define MAPE_MATERIAL_MAP_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), MAPE_TYPE_MATERIAL_MAP, MapeMaterialMapPrivate))
56 
57 static GQuark mape_material_map_error_quark;
58 
59 G_DEFINE_TYPE(MapeMaterialMap, mape_material_map, G_TYPE_OBJECT)
60 
61 /*
62  * MapeMaterial
63  */
64 
65 static MapeMaterial*
66 mape_material_new(MapeMaterialMap* map,
67  guint mat_index)
68 {
70  priv = MAPE_MATERIAL_MAP_PRIVATE(map);
71 
73  mat_index);
74 }
75 
76 static MapeMaterial*
77 mape_material_copy(const MapeMaterial* material)
78 {
79  return (MapeMaterial*)material;
80 }
81 
82 static void
83 mape_material_free(MapeMaterial* material)
84 {
85 }
86 
87 /*
88  * GObject overrides.
89  */
90 
91 static void
92 mape_material_map_init(MapeMaterialMap* material_map)
93 {
95  priv = MAPE_MATERIAL_MAP_PRIVATE(material_map);
96 
98 }
99 
100 static void
101 mape_material_map_finalize(GObject* object)
102 {
103  MapeMaterialMap* material_map;
105 
106  material_map = MAPE_MATERIAL_MAP(object);
107  priv = MAPE_MATERIAL_MAP_PRIVATE(material_map);
108 
110  G_OBJECT_CLASS(mape_material_map_parent_class)->finalize(object);
111 }
112 
113 static void
114 mape_material_map_set_property(GObject* object,
115  guint prop_id,
116  const GValue* value,
117  GParamSpec* pspec)
118 {
119  MapeMaterialMap* material_map;
121 
122  material_map = MAPE_MATERIAL_MAP(object);
123  priv = MAPE_MATERIAL_MAP_PRIVATE(material_map);
124 
125  switch(prop_id)
126  {
127  /* we have only readonly properties */
128  default:
129  G_OBJECT_WARN_INVALID_PROPERTY_ID(value, prop_id, pspec);
130  break;
131  }
132 }
133 
134 static void
135 mape_material_map_get_property(GObject* object,
136  guint prop_id,
137  GValue* value,
138  GParamSpec* pspec)
139 {
140  MapeMaterialMap* material_map;
142 
143  material_map = MAPE_MATERIAL_MAP(object);
144  priv = MAPE_MATERIAL_MAP_PRIVATE(material_map);
145 
146  switch(prop_id)
147  {
148  case PROP_N_MATERIALS:
149  g_value_set_uint(value, c4_material_map_handle_get_num(priv->handle));
150  break;
151  default:
152  G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
153  break;
154  }
155 }
156 
157 /*
158  * Gype registration.
159  */
160 
161 static void
162 mape_material_map_class_init(MapeMaterialMapClass *class)
163 {
164  GObjectClass* object_class;
165 
166  object_class = G_OBJECT_CLASS(class);
167  mape_material_map_parent_class =
168  G_OBJECT_CLASS(g_type_class_peek_parent(class));
169  g_type_class_add_private(class, sizeof(MapeMaterialMapPrivate));
170 
171  object_class->finalize = mape_material_map_finalize;
172  object_class->set_property = mape_material_map_set_property;
173  object_class->get_property = mape_material_map_get_property;
174 
175  mape_material_map_error_quark =
176  g_quark_from_static_string("MAPE_MATERIAL_MAP_ERROR");
177 
178  g_object_class_install_property(
179  object_class,
181  g_param_spec_uint(
182  "n-materials",
183  "Material count",
184  "The number of loaded materials",
185  0,
186  G_MAXUINT,
187  0,
188  G_PARAM_READABLE
189  )
190  );
191 }
192 
193 GType
195 {
196  static GType material_type = 0;
197 
198  if(material_type == 0)
199  {
200  material_type = g_boxed_type_register_static(
201  "MapeMaterial",
202  (GBoxedCopyFunc)mape_material_copy,
203  (GBoxedFreeFunc)mape_material_free);
204  }
205 
206  return material_type;
207 }
208 
209 /*
210  * Public API.
211  */
212 
224 {
225  return MAPE_MATERIAL_MAP(g_object_new(MAPE_TYPE_MATERIAL_MAP, NULL));
226 }
227 
241 gboolean
243  MapeGroup* from,
244  GError** error)
245 {
247  guint new_count;
248 
249  g_return_val_if_fail(MAPE_IS_MATERIAL_MAP(map), FALSE);
250  g_return_val_if_fail(MAPE_IS_GROUP(from), FALSE);
251  g_return_val_if_fail(mape_group_is_open(from), FALSE);
252  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
253 
254  priv = MAPE_MATERIAL_MAP_PRIVATE(map);
255 
256  new_count = c4_material_map_handle_load(
257  priv->handle, _mape_group_get_handle(from));
258 
259  if(new_count > 0)
260  g_object_notify(G_OBJECT(map), "n-materials");
261 
262  return TRUE;
263 }
264 
273 guint
275 {
276  g_return_val_if_fail(MAPE_IS_MATERIAL_MAP(map), 0);
278  MAPE_MATERIAL_MAP_PRIVATE(map)->handle);
279 }
280 
289 void
291  MapeTextureMap* texmap)
292 {
294  MAPE_MATERIAL_MAP_PRIVATE(matmap)->handle,
296  );
297 }
298 
309 const MapeMaterial*
311  guint index)
312 {
314 
315  g_return_val_if_fail(MAPE_IS_MATERIAL_MAP(map), NULL);
316 
317  priv = MAPE_MATERIAL_MAP_PRIVATE(map);
318  g_return_val_if_fail(index < c4_material_map_handle_get_num(priv->handle),
319  NULL);
320 
321  return mape_material_new(map, index);
322 }
323 
324 /*
325  * mape_material_map_get_material_by_name:
326  * @map: A #MapeMaterialMap.
327  * @name: The name of the material to retrieve.
328  *
329  * Returns the material in the map which has the given name, if any. If there
330  * is no such material the function returns %NULL.
331  *
332  * Returns: A new #MapeMaterial to be freed with mape_material_free() when
333  * no longer needed, or %NULL.
334  */
337  const gchar* name)
338 {
340  const gchar* cur_name;
341  guint i;
342 
343  g_return_val_if_fail(MAPE_IS_MATERIAL_MAP(map), NULL);
344  g_return_val_if_fail(name != NULL, NULL);
345 
346  priv = MAPE_MATERIAL_MAP_PRIVATE(map);
347 
348  for(i = 0; i < c4_material_map_handle_get_num(priv->handle); ++i)
349  {
350  cur_name = c4_material_handle_get_name(
352  );
353 
354  if(g_ascii_strcasecmp(cur_name, name) == 0)
355  return mape_material_new(map, i);
356  }
357 
358  return NULL;
359 }
360 
370 const gchar*
372 {
373  g_return_val_if_fail(material != NULL, NULL);
375 }
376 
387 const gchar*
389 {
390  g_return_val_if_fail(material != NULL, NULL);
392 }
393 
394 /* This function is for internal use only */
397 {
398  g_return_val_if_fail(MAPE_IS_MATERIAL_MAP(map), NULL);
399  return MAPE_MATERIAL_MAP_PRIVATE(map)->handle;
400 }
401 
402 /* vim:set et sw=2 ts=2: */
guint c4_material_map_handle_load(C4MaterialMapHandle *material_map, C4GroupHandle *group)
struct _C4MaterialMapHandle C4MaterialMapHandle
C4MaterialHandle * c4_material_map_handle_get_material(C4MaterialMapHandle *material_map, guint index)
guint mape_material_map_get_material_count(MapeMaterialMap *map)
Definition: material.c:274
void c4_material_map_handle_free(C4MaterialMapHandle *material_map)
gboolean mape_material_map_load(MapeMaterialMap *map, MapeGroup *from, GError **error)
Definition: material.c:242
GType mape_material_get_type(void)
Definition: material.c:194
const gchar * c4_material_handle_get_name(C4MaterialHandle *material)
C4GroupHandle * _mape_group_get_handle(MapeGroup *group)
Definition: group.c:682
void c4_material_map_crossmap_materials(C4MaterialMapHandle *material_map, C4TextureMapHandle *texture_map)
typedefG_BEGIN_DECLS struct _C4GroupHandle C4GroupHandle
Definition: group-handle.h:23
const gchar * mape_material_get_texture_overlay(const MapeMaterial *material)
Definition: material.c:388
#define MAPE_IS_MATERIAL_MAP(obj)
Definition: material.h:29
void mape_material_map_set_default_textures(MapeMaterialMap *matmap, MapeTextureMap *texmap)
Definition: material.c:290
MapeMaterial * mape_material_map_get_material_by_name(MapeMaterialMap *map, const gchar *name)
Definition: material.c:336
MapeMaterialMap * mape_material_map_new(void)
Definition: material.c:223
C4MaterialMapHandle * _mape_material_map_get_handle(MapeMaterialMap *map)
Definition: material.c:396
C4MaterialMapHandle * handle
Definition: material.c:45
guint c4_material_map_handle_get_num(C4MaterialMapHandle *material_map)
const gchar * c4_material_handle_get_texture_overlay(C4MaterialHandle *material)
C4TextureMapHandle * _mape_texture_map_get_handle(MapeTextureMap *map)
Definition: texture.c:591
const MapeMaterial * mape_material_map_get_material(MapeMaterialMap *map, guint index)
Definition: material.c:310
#define MAPE_MATERIAL_MAP(obj)
Definition: material.h:27
gboolean mape_group_is_open(MapeGroup *group)
Definition: group.c:220
#define MAPE_MATERIAL_MAP_PRIVATE(obj)
Definition: material.c:55
const gchar * mape_material_get_name(const MapeMaterial *material)
Definition: material.c:371
C4MaterialMapHandle * c4_material_map_handle_new(void)
typedefG_BEGIN_DECLS struct _C4MaterialHandle C4MaterialHandle
struct _MapeMaterial MapeMaterial
Definition: material.h:35
#define MAPE_IS_GROUP(obj)
Definition: group.h:26
#define MAPE_TYPE_MATERIAL_MAP
Definition: material.h:26
typedefG_BEGIN_DECLS struct _C4TextureMapHandle C4TextureMapHandle