OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
texture.c File Reference
#include <string.h>
#include "mape/cpp-handles/group-handle.h"
#include "mape/cpp-handles/texture-handle.h"
#include "mape/texture.h"
Include dependency graph for texture.c:

Go to the source code of this file.

Classes

struct  _MapeTextureMapPrivate
 

Macros

#define MAPE_TEXTURE_MAP_PRIVATE(obj)   (G_TYPE_INSTANCE_GET_PRIVATE((obj), MAPE_TYPE_TEXTURE_MAP, MapeTextureMapPrivate))
 

Typedefs

typedef struct
_MapeTextureMapPrivate 
MapeTextureMapPrivate
 

Enumerations

enum  { PROP_0, PROP_N_TEXTURES, PROP_OVERLOAD_MATERIALS, PROP_OVERLOAD_TEXTURES }
 

Functions

C4GroupHandle_mape_group_get_handle (MapeGroup *group)
 
C4TextureMapHandle_mape_texture_map_get_handle (MapeTextureMap *map)
 
MapeTextureMapmape_texture_map_new (void)
 
gboolean mape_texture_map_load_map (MapeTextureMap *texture_map, MapeGroup *group, GError **error)
 
gboolean mape_texture_map_load_textures (MapeTextureMap *texture_map, MapeGroup *group, GError **error)
 
gboolean mape_texture_map_get_overload_materials (MapeTextureMap *texture_map)
 
gboolean mape_texture_map_get_overload_textures (MapeTextureMap *texture_map)
 
guint mape_texture_map_get_texture_count (MapeTextureMap *texture_map)
 
const gchar * mape_texture_map_get_texture_name (MapeTextureMap *texture_map, guint index)
 
. If there is no such

texture available in then the function returns FALSE.

Returns: A #GdkPixbuf corresponding to the texture with the given name. This is owned by the texture map and should not be unrefed or freed.

GdkPixbuf * mape_texture_map_lookup_texture (MapeTextureMap *texture_map, const gchar *name)
 
const gchar * mape_texture_map_get_material_name_from_mapping (MapeTextureMap *texture_map, guint index)
 
const gchar * mape_texture_map_get_texture_name_from_mapping (MapeTextureMap *texture_map, guint index)
 
as a

32-bit integer.

guint32 mape_texture_map_get_average_texture_color (MapeTextureMap *texture_map, const gchar *name)
 

Class Documentation

struct _MapeTextureMapPrivate

Definition at line 30 of file texture.c.

Class Members
C4TextureMapHandle * handle
gboolean overload_materials
gboolean overload_textures
GHashTable * texture_table

Macro Definition Documentation

Typedef Documentation

Definition at line 29 of file texture.c.

Enumeration Type Documentation

anonymous enum
Enumerator
PROP_0 
PROP_N_TEXTURES 
PROP_OVERLOAD_MATERIALS 
PROP_OVERLOAD_TEXTURES 

Definition at line 37 of file texture.c.

37  {
38  PROP_0,
39 
40  /* read only */
44 };
Definition: texture.c:38

Function Documentation

C4GroupHandle* _mape_group_get_handle ( MapeGroup group)

SECTION:mape-group : MapeGroup : C4Group interface mape/group.h : Unstable

MapeGroup is a simple GObject-based interface to C4Group. It currntly only supports a subset of the C4Group operations, it does not support writing groups for example. It is just enough for what Mape requires.

Definition at line 682 of file group.c.

References MAPE_GROUP_PRIVATE, and MAPE_IS_GROUP.

Referenced by mape_texture_map_load_map().

683 {
684  g_return_val_if_fail(MAPE_IS_GROUP(group), NULL);
685  return MAPE_GROUP_PRIVATE(group)->handle;
686 }
#define MAPE_GROUP_PRIVATE(obj)
Definition: group.c:63
#define MAPE_IS_GROUP(obj)
Definition: group.h:26

Here is the caller graph for this function:

C4TextureMapHandle* _mape_texture_map_get_handle ( MapeTextureMap map)

Definition at line 591 of file texture.c.

References MAPE_IS_TEXTURE_MAP, and MAPE_TEXTURE_MAP_PRIVATE.

Referenced by mape_mapgen_render(), and mape_material_map_set_default_textures().

592 {
593  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(map), NULL);
594  return MAPE_TEXTURE_MAP_PRIVATE(map)->handle;
595 }
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29

Here is the caller graph for this function:

guint32 mape_texture_map_get_average_texture_color ( MapeTextureMap texture_map,
const gchar *  name 
)

Definition at line 580 of file texture.c.

References c4_texture_handle_get_average_texture_color(), _MapeTextureMapPrivate::handle, MAPE_IS_TEXTURE_MAP, and MAPE_TEXTURE_MAP_PRIVATE.

582 {
583  MapeTextureMapPrivate* priv;
584  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), 0);
585  priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
587 }
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29
C4TextureMapHandle * handle
Definition: texture.c:31
guint32 c4_texture_handle_get_average_texture_color(C4TextureMapHandle *texture_map, const char *name)

Here is the call graph for this function:

const gchar* mape_texture_map_get_material_name_from_mapping ( MapeTextureMap texture_map,
guint  index 
)

mape_texture_map_get_material_name_from_mapping: : A MapeTextureMap. : A material-texture combination index.

Returns the name of the material which corresponds to the given index in the material-texture mapping. The function returns NULL if the corresponding entry is empty. The mapping must be loaded with the function mape_texture_map_load_map() before this function can be used.

Returns: The name of the material which corresponds to the given index, or NULL.

Definition at line 536 of file texture.c.

References c4_texture_handle_get_entry_material_name(), _MapeTextureMapPrivate::handle, MAPE_IS_TEXTURE_MAP, and MAPE_TEXTURE_MAP_PRIVATE.

538 {
539  MapeTextureMapPrivate* priv;
540  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), NULL);
541  priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
543 }
const char * c4_texture_handle_get_entry_material_name(C4TextureMapHandle *texture_map, guint index)
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29
C4TextureMapHandle * handle
Definition: texture.c:31

Here is the call graph for this function:

gboolean mape_texture_map_get_overload_materials ( MapeTextureMap texture_map)

mape_texture_map_get_overload_materials: : A MapeTextureMap.

Returns whether the OverloadMaterials flag is set in the mapping. This flag is initialized by calling mape_texture_map_load_map().

Returns: Whether the OverloadMaterials flag is set in the mapping.

Definition at line 427 of file texture.c.

References MAPE_IS_TEXTURE_MAP, and MAPE_TEXTURE_MAP_PRIVATE.

428 {
429  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), FALSE);
430  return MAPE_TEXTURE_MAP_PRIVATE(texture_map)->overload_materials;
431 }
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29
gboolean mape_texture_map_get_overload_textures ( MapeTextureMap texture_map)

mape_texture_map_get_overload_textures: : A MapeTextureMap.

Returns whether the OverloadTextures flag is set in the mapping. This flag is initialized by calling mape_texture_map_load_map().

Returns: Whether the OverloadTextures flag is set in the mapping.

Definition at line 443 of file texture.c.

References MAPE_IS_TEXTURE_MAP, and MAPE_TEXTURE_MAP_PRIVATE.

444 {
445  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), FALSE);
446  return MAPE_TEXTURE_MAP_PRIVATE(texture_map)->overload_materials;
447 }
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29
guint mape_texture_map_get_texture_count ( MapeTextureMap texture_map)

mape_texture_map_get_texture_count: : A MapeTextureMap.

Returns the number of textures loaded. This will only be non-zero after mape_texture_map_load_textures() was called.

Returns: The number of textures loaded.

Definition at line 459 of file texture.c.

References MAPE_IS_TEXTURE_MAP, MAPE_TEXTURE_MAP_PRIVATE, and _MapeTextureMapPrivate::texture_table.

Referenced by mape_mat_tex_view_reload(), and mape_texture_map_get_texture_name().

460 {
461  MapeTextureMapPrivate* priv;
462  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), FALSE);
463  priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
464  return g_hash_table_size(priv->texture_table);
465 }
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29
GHashTable * texture_table
Definition: texture.c:32

Here is the caller graph for this function:

const gchar* mape_texture_map_get_texture_name ( MapeTextureMap texture_map,
guint  index 
)

mape_texture_map_get_texture_name: : A MapeTextureMap. : The index of the texture whose name to retrieve.

Returns the name of the <– –>th material in . Allowed values for range from 0 to mape_texture_map_get_texture_count() - 1.

Returns: The texture name of the texture corresponding to .

Definition at line 478 of file texture.c.

References c4_texture_map_handle_get_texture(), _MapeTextureMapPrivate::handle, MAPE_IS_TEXTURE_MAP, mape_texture_map_get_texture_count(), and MAPE_TEXTURE_MAP_PRIVATE.

Referenced by mape_mat_tex_view_reload().

480 {
481  MapeTextureMapPrivate* priv;
482 
483  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), NULL);
484  g_return_val_if_fail(
485  index < mape_texture_map_get_texture_count(texture_map), NULL
486  );
487 
488  priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
489  return c4_texture_map_handle_get_texture(priv->handle, index);
490 }
const char * c4_texture_map_handle_get_texture(C4TextureMapHandle *texture_map, guint index)
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
guint mape_texture_map_get_texture_count(MapeTextureMap *texture_map)
Definition: texture.c:459
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29
C4TextureMapHandle * handle
Definition: texture.c:31

Here is the call graph for this function:

Here is the caller graph for this function:

const gchar* mape_texture_map_get_texture_name_from_mapping ( MapeTextureMap texture_map,
guint  index 
)

mape_texture_map_get_texture_name_from_mapping: : A MapeTextureMap. : A material-texture combination index.

Returns the name of the texture which corresponds to the given index in the material-texture mapping. The function returns NULL if the corresponding entry is empty. The mapping must be loaded with the function mape_texture_map_load_map() before this function can be used.

Returns: The name of the texture which corresponds to the given index, or NULL.

Definition at line 559 of file texture.c.

References c4_texture_handle_get_entry_texture_name(), _MapeTextureMapPrivate::handle, MAPE_IS_TEXTURE_MAP, and MAPE_TEXTURE_MAP_PRIVATE.

561 {
562  MapeTextureMapPrivate* priv;
563  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), NULL);
564  priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
566 }
const char * c4_texture_handle_get_entry_texture_name(C4TextureMapHandle *texture_map, guint index)
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29
C4TextureMapHandle * handle
Definition: texture.c:31

Here is the call graph for this function:

gboolean mape_texture_map_load_map ( MapeTextureMap texture_map,
MapeGroup group,
GError **  error 
)

mape_texture_map_load_map: : A MapeTextureMap. : A MapeGroup to load the material mappings from. : Location to store error information, if any.

Loads the mappings of material-texture combinations to palette indices from the group's TexMap.txt. This should only be loaded once per group. If an error occurs, is set and the function returns FALSE.

Returns: TRUE if the mapping could be loaded or FALSE otherwise.

Definition at line 265 of file texture.c.

References _mape_group_get_handle(), c4_texture_map_handle_load_map(), _MapeTextureMapPrivate::handle, MAPE_IS_GROUP, MAPE_IS_TEXTURE_MAP, MAPE_TEXTURE_MAP_PRIVATE, _MapeTextureMapPrivate::overload_materials, and _MapeTextureMapPrivate::overload_textures.

Referenced by mape_mat_tex_view_reload().

268 {
269  MapeTextureMapPrivate* priv;
270 
271  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), FALSE);
272  g_return_val_if_fail(MAPE_IS_GROUP(group), FALSE);
273  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
274 
275  priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
276 
278  priv->handle,
279  _mape_group_get_handle(group),
280  "TexMap.txt",
281  &priv->overload_materials,
282  &priv->overload_textures
283  );
284 
285  g_object_notify(G_OBJECT(texture_map), "overload-materials");
286  g_object_notify(G_OBJECT(texture_map), "overload-textures");
287 
288  return TRUE;
289 }
gboolean overload_materials
Definition: texture.c:33
guint c4_texture_map_handle_load_map(C4TextureMapHandle *texture_map, C4GroupHandle *group, const char *entry_name, gboolean *overload_materials, gboolean *overload_textures)
gboolean overload_textures
Definition: texture.c:34
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
C4GroupHandle * _mape_group_get_handle(MapeGroup *group)
Definition: group.c:682
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29
C4TextureMapHandle * handle
Definition: texture.c:31
#define MAPE_IS_GROUP(obj)
Definition: group.h:26

Here is the call graph for this function:

Here is the caller graph for this function:

gboolean mape_texture_map_load_textures ( MapeTextureMap texture_map,
MapeGroup group,
GError **  error 
)

mape_texture_map_load_textures: : A MapeTextureMap. : The group to load textures from. : Location to store error information, if any.

Loads all textures in . If this includes textures which are already contained in , then these textures will not be loaded. If an error occurs, is set and the function returns FALSE.

Returns: TRUE if the textures could be loaded or FALSE otherwise.

Definition at line 304 of file texture.c.

References c4_texture_map_handle_add_texture(), _MapeTextureMapPrivate::handle, mape_group_get_next_entry(), mape_group_load_entry(), mape_group_rewind(), MAPE_IS_GROUP, MAPE_IS_TEXTURE_MAP, MAPE_TEXTURE_MAP_PRIVATE, and _MapeTextureMapPrivate::texture_table.

Referenced by mape_mat_tex_view_reload().

307 {
308  /* We don't use C4TextureMap::LoadTextures here because that would try
309  * to load them as C4Surface. Instead, we have our own routine which loads
310  * them into GdkPixbufs. */
311  static const char* SUFFIXES[] = {
312  ".png", ".jpg", ".jpeg", ".bmp",
313  ".PNG", ".JPG", ".JPEG", ".BMP",
314  NULL
315  };
316 
317  MapeTextureMapPrivate* priv;
318 
319  gchar* name;
320  const char* const* ext;
321  gsize len;
322  gchar* casefold_name;
323 
324  guchar* data;
325  gsize datalen;
326  GdkPixbufLoader* loader;
327  GdkPixbuf* pixbuf;
328 
329  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), FALSE);
330  g_return_val_if_fail(MAPE_IS_GROUP(group), FALSE);
331  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
332 
333  priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
334 
335  if(priv->texture_table == NULL)
336  {
337  priv->texture_table = g_hash_table_new_full(
338  g_str_hash,
339  g_str_equal,
340  g_free,
341  g_object_unref
342  );
343  }
344 
345  mape_group_rewind(group);
346  while( (name = mape_group_get_next_entry(group)) != NULL)
347  {
348  for(ext = SUFFIXES; *ext != NULL; ++ext)
349  if(g_str_has_suffix(name, *ext))
350  break;
351 
352  if(*ext != NULL)
353  {
354  /* Make texname out of filename */
355  len = strlen(name)-strlen(*ext);
356  /* Use this for hashtable lookup */
357  casefold_name = g_utf8_casefold(name, len);
358 
359  /* If we have this texture loaded already then don't load it again --
360  * this is how overloading works: We first load the primary group and
361  * then the overloaded one. */
362  if(g_hash_table_lookup(priv->texture_table, casefold_name) == NULL)
363  {
364  data = mape_group_load_entry(group, name, &datalen, error);
365  if(data == NULL)
366  {
367  g_free(name);
368  return FALSE;
369  }
370 
371  loader = gdk_pixbuf_loader_new();
372  gdk_pixbuf_loader_set_size(loader, 24, 24);
373  if(!gdk_pixbuf_loader_write(loader, data, datalen, error))
374  {
375  g_free(name);
376  g_free(data);
377  gdk_pixbuf_loader_close(loader, NULL);
378  g_object_unref(loader);
379  return FALSE;
380  }
381 
382  g_free(data);
383  if(!gdk_pixbuf_loader_close(loader, error))
384  {
385  g_free(name);
386  g_object_unref(loader);
387  return FALSE;
388  }
389 
390  pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
391  g_object_ref(pixbuf);
392  g_object_unref(loader);
393 
394  /* Add texture to texmap (without actual Surface, only texture color),
395  * just so that the map generator knows the presence
396  * of the texture. */
397  name[len] = '\0';
399  priv->handle,
400  name,
401  mape_texture_map_get_average_color(pixbuf)
402  );
403  g_hash_table_insert(priv->texture_table, casefold_name, pixbuf);
404  }
405  else
406  {
407  g_free(casefold_name);
408  }
409  }
410 
411  g_free(name);
412  }
413 
414  return TRUE;
415 }
gboolean c4_texture_map_handle_add_texture(C4TextureMapHandle *texture_map, const char *texture, guint32 avg_color)
gchar * mape_group_get_next_entry(MapeGroup *group)
Definition: group.c:496
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
void mape_group_rewind(MapeGroup *group)
Definition: group.c:462
guchar * mape_group_load_entry(MapeGroup *group, const gchar *entry, gsize *size, GError **error)
Definition: group.c:557
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29
C4TextureMapHandle * handle
Definition: texture.c:31
GHashTable * texture_table
Definition: texture.c:32
#define MAPE_IS_GROUP(obj)
Definition: group.h:26

Here is the call graph for this function:

Here is the caller graph for this function:

GdkPixbuf* mape_texture_map_lookup_texture ( MapeTextureMap texture_map,
const gchar *  name 
)

Definition at line 504 of file texture.c.

References MAPE_IS_TEXTURE_MAP, MAPE_TEXTURE_MAP_PRIVATE, and _MapeTextureMapPrivate::texture_table.

Referenced by mape_mat_tex_view_reload().

506 {
507  MapeTextureMapPrivate* priv;
508  gchar* casefold_name;
509  GdkPixbuf* pixbuf;
510 
511  g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), NULL);
512  g_return_val_if_fail(name != NULL, NULL);
513 
514  priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
515  casefold_name = g_utf8_casefold(name, -1);
516  pixbuf = GDK_PIXBUF(g_hash_table_lookup(priv->texture_table, casefold_name));
517  g_free(casefold_name);
518 
519  return pixbuf;
520 }
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29
GHashTable * texture_table
Definition: texture.c:32

Here is the caller graph for this function:

MapeTextureMap* mape_texture_map_new ( void  )

mape_texture_map_new:

Creates a new MapeTextureMap. The map is initially empty. Use mape_texture_map_load_map() to load mappings and mape_texture_map_load_textures() from a Material.ocg group file.

Return Value: A new MapeTextureMap. Free with g_object_unref().

Definition at line 247 of file texture.c.

References MAPE_TEXTURE_MAP, and MAPE_TYPE_TEXTURE_MAP.

248 {
249  return MAPE_TEXTURE_MAP(g_object_new(MAPE_TYPE_TEXTURE_MAP, NULL));
250 }
#define MAPE_TYPE_TEXTURE_MAP
Definition: texture.h:26
#define MAPE_TEXTURE_MAP(obj)
Definition: texture.h:27