OpenClonk
texture.h File Reference
#include <glib-object.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "mape/group.h"
Include dependency graph for texture.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _MapeTextureMapClass
 
struct  _MapeTextureMap
 

Macros

#define MAPE_TYPE_TEXTURE_MAP   (mape_texture_map_get_type())
 
#define MAPE_TEXTURE_MAP(obj)   (G_TYPE_CHECK_INSTANCE_CAST((obj), MAPE_TYPE_TEXTURE_MAP, MapeTextureMap))
 
#define MAPE_TEXTURE_MAP_CLASS(klass)   (G_TYPE_CHECK_CLASS_CAST((klass), MAPE_TYPE_TEXTURE_MAP, MapeTextureMapClass))
 
#define MAPE_IS_TEXTURE_MAP(obj)   (G_TYPE_CHECK_INSTANCE_TYPE((obj), MAPE_TYPE_TEXTURE_MAP))
 
#define MAPE_IS_TEXTURE_MAP_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE((klass), MAPE_TYPE_TEXTURE_MAP))
 
#define MAPE_TEXTURE_MAP_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj), MAPE_TYPE_TEXTURE_MAP, MapeTextureMapClass))
 

Typedefs

typedef struct _MapeTextureMap MapeTextureMap
 
typedef struct _MapeTextureMapClass MapeTextureMapClass
 
typedef enum _MapeTextureMapError MapeTextureMapError
 

Enumerations

enum  _MapeTextureMapError { MAPE_TEXTURE_MAP_ERROR_LOAD }
 

Functions

GType mape_texture_map_get_type (void) G_GNUC_CONST
 
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)
 
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)
 
guint32 mape_texture_map_get_average_texture_color (MapeTextureMap *texture_map, const gchar *name)
 

Class Documentation

◆ _MapeTextureMapClass

struct _MapeTextureMapClass

MapeTextureMapClass:

This structure does not contain any public fields.

Definition at line 52 of file texture.h.

Class Members
GObjectClass parent_class

◆ _MapeTextureMap

struct _MapeTextureMap

MapeTextureMap:

MapeTextureMap is an opaque data type. You should only access it via the public API functions.

Definition at line 63 of file texture.h.

Class Members
GObject parent

Macro Definition Documentation

◆ MAPE_IS_TEXTURE_MAP

#define MAPE_IS_TEXTURE_MAP (   obj)    (G_TYPE_CHECK_INSTANCE_TYPE((obj), MAPE_TYPE_TEXTURE_MAP))

Definition at line 29 of file texture.h.

◆ MAPE_IS_TEXTURE_MAP_CLASS

#define MAPE_IS_TEXTURE_MAP_CLASS (   klass)    (G_TYPE_CHECK_CLASS_TYPE((klass), MAPE_TYPE_TEXTURE_MAP))

Definition at line 30 of file texture.h.

◆ MAPE_TEXTURE_MAP

#define MAPE_TEXTURE_MAP (   obj)    (G_TYPE_CHECK_INSTANCE_CAST((obj), MAPE_TYPE_TEXTURE_MAP, MapeTextureMap))

Definition at line 27 of file texture.h.

◆ MAPE_TEXTURE_MAP_CLASS

#define MAPE_TEXTURE_MAP_CLASS (   klass)    (G_TYPE_CHECK_CLASS_CAST((klass), MAPE_TYPE_TEXTURE_MAP, MapeTextureMapClass))

Definition at line 28 of file texture.h.

◆ MAPE_TEXTURE_MAP_GET_CLASS

#define MAPE_TEXTURE_MAP_GET_CLASS (   obj)    (G_TYPE_INSTANCE_GET_CLASS((obj), MAPE_TYPE_TEXTURE_MAP, MapeTextureMapClass))

Definition at line 31 of file texture.h.

◆ MAPE_TYPE_TEXTURE_MAP

#define MAPE_TYPE_TEXTURE_MAP   (mape_texture_map_get_type())

Definition at line 26 of file texture.h.

Typedef Documentation

◆ MapeTextureMap

Definition at line 24 of file texture.h.

◆ MapeTextureMapClass

Definition at line 24 of file texture.h.

◆ MapeTextureMapError

MapeTextureMapError: @MAPE_TEXTURE_MAP_ERROR_LOAD: An error occured when loading the texture map.

These errors are from the MAPE_TEXTURE_MAP_ERROR error domain. They can occur when operating on texture maps.

Enumeration Type Documentation

◆ _MapeTextureMapError

MapeTextureMapError: @MAPE_TEXTURE_MAP_ERROR_LOAD: An error occured when loading the texture map.

These errors are from the MAPE_TEXTURE_MAP_ERROR error domain. They can occur when operating on texture maps.

Enumerator
MAPE_TEXTURE_MAP_ERROR_LOAD 

Definition at line 43 of file texture.h.

43  {
enum _MapeTextureMapError MapeTextureMapError
@ MAPE_TEXTURE_MAP_ERROR_LOAD
Definition: texture.h:44

Function Documentation

◆ mape_texture_map_get_average_texture_color()

guint32 mape_texture_map_get_average_texture_color ( MapeTextureMap texture_map,
const gchar *  name 
)

Definition at line 580 of file texture.c.

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 }
guint32 c4_texture_handle_get_average_texture_color(C4TextureMapHandle *texture_map, const char *name)
#define MAPE_TEXTURE_MAP_PRIVATE(obj)
Definition: texture.c:46
C4TextureMapHandle * handle
Definition: texture.c:31
#define MAPE_IS_TEXTURE_MAP(obj)
Definition: texture.h:29

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

Here is the call graph for this function:

◆ mape_texture_map_get_material_name_from_mapping()

const gchar* mape_texture_map_get_material_name_from_mapping ( MapeTextureMap texture_map,
guint  index 
)

mape_texture_map_get_material_name_from_mapping: @texture_map: A MapeTextureMap. @index: 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.

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)

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

Here is the call graph for this function:

◆ mape_texture_map_get_overload_materials()

gboolean mape_texture_map_get_overload_materials ( MapeTextureMap texture_map)

mape_texture_map_get_overload_materials: @texture_map: 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.

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 }

References MAPE_IS_TEXTURE_MAP, and MAPE_TEXTURE_MAP_PRIVATE.

◆ mape_texture_map_get_overload_textures()

gboolean mape_texture_map_get_overload_textures ( MapeTextureMap texture_map)

mape_texture_map_get_overload_textures: @texture_map: 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.

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 }

References MAPE_IS_TEXTURE_MAP, and MAPE_TEXTURE_MAP_PRIVATE.

◆ mape_texture_map_get_texture_count()

guint mape_texture_map_get_texture_count ( MapeTextureMap texture_map)

mape_texture_map_get_texture_count: @texture_map: 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.

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 }
GHashTable * texture_table
Definition: texture.c:32

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().

Here is the caller graph for this function:

◆ mape_texture_map_get_texture_name()

const gchar* mape_texture_map_get_texture_name ( MapeTextureMap texture_map,
guint  index 
)

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

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

Returns: The texture name of the texture corresponding to @index.

Definition at line 478 of file texture.c.

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)
guint mape_texture_map_get_texture_count(MapeTextureMap *texture_map)
Definition: texture.c:459

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mape_texture_map_get_texture_name_from_mapping()

const gchar* mape_texture_map_get_texture_name_from_mapping ( MapeTextureMap texture_map,
guint  index 
)

mape_texture_map_get_texture_name_from_mapping: @texture_map: A MapeTextureMap. @index: 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.

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)

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

Here is the call graph for this function:

◆ mape_texture_map_get_type()

GType mape_texture_map_get_type ( void  )

◆ mape_texture_map_load_map()

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

mape_texture_map_load_map: @texture_map: A MapeTextureMap. @group: A MapeGroup to load the material mappings from. @error: 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, @error 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.

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 }
#define MAPE_IS_GROUP(obj)
Definition: group.h:26
guint c4_texture_map_handle_load_map(C4TextureMapHandle *texture_map, C4GroupHandle *group, const char *entry_name, gboolean *overload_materials, gboolean *overload_textures)
gboolean overload_materials
Definition: texture.c:33
C4GroupHandle * _mape_group_get_handle(MapeGroup *group)
Definition: group.c:682
gboolean overload_textures
Definition: texture.c:34

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mape_texture_map_load_textures()

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

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

Loads all textures in @group. If this includes textures which are already contained in @texture_map, then these textures will not be loaded. If an error occurs, @error 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.

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 }
guchar * mape_group_load_entry(MapeGroup *group, const gchar *entry, gsize *size, GError **error)
Definition: group.c:557
gchar * mape_group_get_next_entry(MapeGroup *group)
Definition: group.c:496
void mape_group_rewind(MapeGroup *group)
Definition: group.c:462
gboolean c4_texture_map_handle_add_texture(C4TextureMapHandle *texture_map, const char *texture, guint32 avg_color)

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mape_texture_map_lookup_texture()

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

Definition at line 504 of file texture.c.

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 }

References MAPE_IS_TEXTURE_MAP, MAPE_TEXTURE_MAP_PRIVATE, and _MapeTextureMapPrivate::texture_table.

Referenced by mape_mat_tex_view_reload().

Here is the caller graph for this function:

◆ mape_texture_map_new()

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.

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

References MAPE_TEXTURE_MAP, and MAPE_TYPE_TEXTURE_MAP.