GObectify MapeTextureMap; add functions to get texture mapping entry

scancodes-fix
Armin Burgmeier 2012-02-18 21:10:07 +01:00
parent 81e04899dc
commit 644f97efd3
12 changed files with 823 additions and 273 deletions

View File

@ -557,6 +557,8 @@ set(MAPE_SOURCES
src/mape/cpp-handles/group-handle.cpp
src/mape/cpp-handles/material-handle.h
src/mape/cpp-handles/material-handle.cpp
src/mape/cpp-handles/texture-handle.h
src/mape/cpp-handles/texture-handle.cpp
src/mape/configfile.c
src/mape/configfile.h
src/mape/diskview.c
@ -589,7 +591,7 @@ set(MAPE_SOURCES
src/mape/random.h
src/mape/statusbar.c
src/mape/statusbar.h
src/mape/texture.cpp
src/mape/texture.c
src/mape/texture.h
src/mape/window.c
src/mape/window.h

View File

@ -0,0 +1,75 @@
/*
* mape - C4 Landscape.txt editor
*
* Copyright (c) 2005-2009 Armin Burgmeier
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
#include "C4Include.h"
#include "C4Texture.h"
#include "mape/cpp-handles/texture-handle.h"
#define TEXTURE_MAP_TO_HANDLE(texture_map) (reinterpret_cast<C4TextureMapHandle*>(texture_map))
#define HANDLE_TO_TEXTURE_MAP(handle) (reinterpret_cast<C4TextureMap*>(handle))
#define GROUP_TO_HANDLE(group) (reinterpret_cast<C4GroupHandle*>(group))
#define HANDLE_TO_GROUP(handle) (reinterpret_cast<C4Group*>(handle))
extern "C" {
C4TextureMapHandle* c4_texture_map_handle_new(void)
{
return TEXTURE_MAP_TO_HANDLE(new C4TextureMap);
}
void c4_texture_map_handle_free(C4TextureMapHandle* texture_map)
{
delete HANDLE_TO_TEXTURE_MAP(texture_map);
}
guint c4_texture_map_handle_load_map(C4TextureMapHandle* texture_map, C4GroupHandle* group, const char* entry_name, gboolean* overload_materials, gboolean* overload_textures)
{
bool fOverloadMaterials;
bool fOverloadTextures;
guint32 retval = HANDLE_TO_TEXTURE_MAP(texture_map)->LoadMap(*HANDLE_TO_GROUP(group), entry_name, &fOverloadMaterials, &fOverloadTextures);
if(overload_materials) *overload_materials = fOverloadMaterials;
if(overload_textures) *overload_textures = fOverloadTextures;
return retval;
}
gboolean c4_texture_map_handle_add_texture(C4TextureMapHandle* texture_map, const char* texture)
{
return HANDLE_TO_TEXTURE_MAP(texture_map)->AddTexture(texture, NULL);
}
const char* c4_texture_map_handle_get_texture(C4TextureMapHandle* texture_map, guint index)
{
return HANDLE_TO_TEXTURE_MAP(texture_map)->GetTexture(index);
}
const char* mape_texture_handle_get_entry_material_name(C4TextureMapHandle* texture_map, guint index)
{
const C4TexMapEntry* entry = HANDLE_TO_TEXTURE_MAP(texture_map)->GetEntry(index);
if(!entry) return NULL;
return entry->GetMaterialName();
}
const char* mape_texture_handle_get_entry_texture_name(C4TextureMapHandle* texture_map, guint index)
{
const C4TexMapEntry* entry = HANDLE_TO_TEXTURE_MAP(texture_map)->GetEntry(index);
if(!entry) return NULL;
return entry->GetTextureName();
}
} /* extern "C" */

View File

@ -0,0 +1,41 @@
/*
* mape - C4 Landscape.txt editor
*
* Copyright (c) 2005-2009 Armin Burgmeier
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
#ifndef INC_MAPE_C4_TEXTURE_HANDLE_H
#define INC_MAPE_C4_TEXTURE_HANDLE_H
#include <glib.h>
#include "mape/cpp-handles/group-handle.h"
G_BEGIN_DECLS
typedef struct _C4TextureMapHandle C4TextureMapHandle;
C4TextureMapHandle* c4_texture_map_handle_new(void);
void c4_texture_map_handle_free(C4TextureMapHandle* texture_map);
guint c4_texture_map_handle_load_map(C4TextureMapHandle* texture_map, C4GroupHandle* group, const char* entry_name, gboolean* overload_materials, gboolean* overload_textures);
gboolean c4_texture_map_handle_add_texture(C4TextureMapHandle* texture_map, const char* texture);
const char* c4_texture_map_handle_get_texture(C4TextureMapHandle* texture_map, guint index);
const char* mape_texture_handle_get_entry_material_name(C4TextureMapHandle* texture_map, guint index);
const char* mape_texture_handle_get_entry_texture_name(C4TextureMapHandle* texture_map, guint index);
G_END_DECLS
#endif /* INC_MAPE_C4_TEXTURE_HANDLE_H */

View File

@ -132,6 +132,10 @@ static gboolean mape_disk_view_load_materials(MapeDiskView* disk_view,
MapeGroup* parent_group;
MapeGroup* overloaded_group;
MapeTextureMap* texture_map;
gboolean overload_materials;
gboolean overload_textures;
/* Open Material.c4g group */
gtk_tree_model_get(
disk_view->tree_store,
@ -141,8 +145,6 @@ static gboolean mape_disk_view_load_materials(MapeDiskView* disk_view,
-1
);
/* TODO: Examine TexMap, check for infinite overloaded Mats/Texs */
if(group == NULL)
{
has_parent = gtk_tree_model_iter_parent(
@ -185,51 +187,67 @@ static gboolean mape_disk_view_load_materials(MapeDiskView* disk_view,
);
}
/* Look for overloaded Material.c4g */
has_parent = gtk_tree_model_iter_parent(
disk_view->tree_store,
&parent_iter,
material_iter
);
for(;;)
texture_map = mape_texture_map_new();
if(mape_texture_map_load_map(texture_map, group, error) == FALSE)
{
overloaded_group = NULL;
g_object_unref(texture_map);
return FALSE;
}
overload_materials = mape_texture_map_get_overload_materials(texture_map);
overload_textures = mape_texture_map_get_overload_textures(texture_map);
if(overload_materials || overload_textures)
{
/* Look for overloaded Material.c4g */
has_parent = gtk_tree_model_iter_parent(
disk_view->tree_store,
&new_parent,
&parent_iter
);
if(has_parent == FALSE)
break;
parent_iter = new_parent;
gtk_tree_model_get(
disk_view->tree_store,
&parent_iter,
MAPE_DISK_VIEW_COLUMN_GROUP,
&overloaded_group,
-1
material_iter
);
if(mape_group_has_entry(overloaded_group, "Material.c4g") ==
TRUE)
for(;;)
{
/* TODO: Check if the group is already open!
(mape_disk_view_find_iter). */
overloaded_group = mape_group_open_child(
overloaded_group,
"Material.c4g",
error
overloaded_group = NULL;
has_parent = gtk_tree_model_iter_parent(
disk_view->tree_store,
&new_parent,
&parent_iter
);
if(has_parent == FALSE)
break;
if(overloaded_group == NULL)
return FALSE;
parent_iter = new_parent;
gtk_tree_model_get(
disk_view->tree_store,
&parent_iter,
MAPE_DISK_VIEW_COLUMN_GROUP,
&overloaded_group,
-1
);
if(mape_group_has_entry(overloaded_group, "Material.c4g") ==
TRUE)
{
/* TODO: Check if the group is already open!
(mape_disk_view_find_iter). */
overloaded_group = mape_group_open_child(
overloaded_group,
"Material.c4g",
error
);
break;
if(overloaded_group == NULL)
{
g_object_unref(texture_map);
return FALSE;
}
break;
}
}
}
@ -243,11 +261,16 @@ static gboolean mape_disk_view_load_materials(MapeDiskView* disk_view,
result = mape_mat_tex_view_reload(
disk_view->mat_tex,
texture_map,
group,
overload_materials,
overload_textures,
overloaded_group,
error
);
g_object_unref(texture_map);
#if 0
if(overloaded_group != NULL)
mape_group_destroy(overloaded_group);

View File

@ -20,7 +20,7 @@
/*typedef struct MapeGroup_ MapeGroup;*/
/*typedef struct MapeMaterialMap_ MapeMaterialMap;*/
typedef struct MapeTextureMap_ MapeTextureMap;
/*typedef struct MapeTextureMap_ MapeTextureMap;*/
typedef struct MapeFileIcon_ MapeFileIcon;
typedef struct MapeFileIconSet_ MapeFileIconSet;
typedef struct MapeConfigFileEntry_ MapeConfigFileEntry;

View File

@ -27,11 +27,13 @@
#include <gdk/gdk.h>
#include "mape/cpp-handles/material-handle.h"
#include "mape/cpp-handles/texture-handle.h"
#include "mape/mapgen.h"
extern "C" C4MaterialHandle* _mape_material_map_get_handle(MapeMaterialMap*);
extern "C" C4MaterialMapHandle* _mape_material_map_get_handle(MapeMaterialMap*);
extern "C" C4TextureMapHandle* _mape_texture_map_get_handle(MapeTextureMap*);
#define CPPTEXMAP(map) ((C4TextureMap*)map->handle)
#define CPPTEXMAP(map) (reinterpret_cast<C4TextureMap*>(_mape_texture_map_get_handle(map)))
#define CPPMATMAP(map) (reinterpret_cast<C4MaterialMap*>(_mape_material_map_get_handle(map)))
extern "C"

View File

@ -39,11 +39,6 @@ _mape_group_get_handle(MapeGroup* group);
C4MaterialMapHandle*
_mape_material_map_get_handle(MapeMaterialMap* map);
/*struct _MapeMaterial {
MapeMaterialMap* map;
unsigned int mat_index;
};*/
typedef struct _MapeMaterialMapPrivate MapeMaterialMapPrivate;
struct _MapeMaterialMapPrivate {
C4MaterialMapHandle* handle;
@ -218,7 +213,7 @@ mape_material_get_type(void)
* mape_material_map_new:
*
* Creates a new #MapeMaterialMap. The map is initially empty. Use
* mape_material_map_load() to load materials from one or more Material.c4g
* mape_material_map_load() to load materials from one or more Material.ocg
* group files.
*
* Return Value: A new #MapeMaterialMap. Free with g_object_unref().
@ -237,9 +232,8 @@ mape_material_map_new(void)
*
* Loads all the material files (*.c4m) from the group @from. If this includes
* materials with the same name as materials already contained in @map, then
* the materials in @map will be replaced by the new ones. If an error occurs
* while loading the material map the function returns %FALSE and @error is
* set.
* the new materials will not be loaded. If an error occurs while loading the
* material map the function returns %FALSE and @error is set.
*
* Returns: %TRUE on success, %FALSE on failure.
*/

View File

@ -84,7 +84,7 @@ void mape_mat_tex_view_destroy(MapeMatTexView* view)
if(view->mat_map != NULL)
g_object_unref(view->mat_map);
if(view->tex_map != NULL)
mape_texture_map_destroy(view->tex_map);
g_object_unref(view->tex_map);
mape_icon_view_destroy(view->view_mat);
mape_icon_view_destroy(view->view_tex);
@ -93,12 +93,14 @@ void mape_mat_tex_view_destroy(MapeMatTexView* view)
}
gboolean mape_mat_tex_view_reload(MapeMatTexView* view,
MapeTextureMap* new_tex_map,
MapeGroup* base_group,
gboolean overload_materials,
gboolean overload_textures,
MapeGroup* overload_from,
GError** error)
{
MapeMaterialMap* new_mat_map;
MapeTextureMap* new_tex_map;
MapeFileIcon* icon;
const MapeMaterial* mat;
unsigned int i;
@ -110,7 +112,7 @@ gboolean mape_mat_tex_view_reload(MapeMatTexView* view,
return FALSE;
}
if(overload_from)
if(overload_materials && overload_from != NULL)
{
if(!mape_material_map_load(new_mat_map, overload_from, error))
{
@ -119,16 +121,25 @@ gboolean mape_mat_tex_view_reload(MapeMatTexView* view,
}
}
new_tex_map = mape_texture_map_new(base_group, overload_from, error);
if(new_tex_map == NULL)
if(!mape_texture_map_load_textures(new_tex_map, base_group, error))
{
g_object_unref(new_mat_map);
return FALSE;
}
if(overload_textures && overload_from != NULL)
{
if(!mape_texture_map_load_textures(new_tex_map, overload_from, error))
{
g_object_unref(new_mat_map);
return FALSE;
}
}
if(view->mat_map != NULL) g_object_unref(view->mat_map);
if(view->tex_map != NULL) mape_texture_map_destroy(view->tex_map);
if(view->tex_map != NULL) g_object_unref(view->tex_map);
view->mat_map = new_mat_map; view->tex_map = new_tex_map;
g_object_ref(new_tex_map);
mape_icon_view_clear(view->view_mat);
for(i = 0; i < mape_material_map_get_material_count(new_mat_map); ++ i)

View File

@ -23,6 +23,7 @@
#include "mape/forward.h"
#include "mape/group.h"
#include "mape/material.h"
#include "mape/texture.h"
struct MapeMatTexView_ {
GtkWidget* notebook;
@ -41,7 +42,10 @@ MapeMatTexView* mape_mat_tex_view_new(MapeFileIconSet* icon_set,
void mape_mat_tex_view_destroy(MapeMatTexView* view);
gboolean mape_mat_tex_view_reload(MapeMatTexView* view,
MapeTextureMap* new_tex_map,
MapeGroup* base_group,
gboolean overload_materials,
gboolean overload_textures,
MapeGroup* overload_from,
GError** error);

531
src/mape/texture.c 100644
View File

@ -0,0 +1,531 @@
/*
* mape - C4 Landscape.txt editor
*
* Copyright (c) 2005-2009 Armin Burgmeier
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
#include <string.h>
#include "mape/cpp-handles/group-handle.h"
#include "mape/cpp-handles/texture-handle.h"
#include "mape/texture.h"
/* Declare private API */
C4GroupHandle*
_mape_group_get_handle(MapeGroup* group);
C4TextureMapHandle*
_mape_texture_map_get_handle(MapeTextureMap* map);
typedef struct _MapeTextureMapPrivate MapeTextureMapPrivate;
struct _MapeTextureMapPrivate {
C4TextureMapHandle* handle;
GHashTable* texture_table;
gboolean overload_materials;
gboolean overload_textures;
};
enum {
PROP_0,
/* read only */
PROP_N_TEXTURES,
PROP_OVERLOAD_MATERIALS,
PROP_OVERLOAD_TEXTURES
};
#define MAPE_TEXTURE_MAP_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), MAPE_TYPE_TEXTURE_MAP, MapeTextureMapPrivate))
static GQuark mape_texture_map_error_quark;
G_DEFINE_TYPE(MapeTextureMap, mape_texture_map, G_TYPE_OBJECT)
/*
* GObject overrides.
*/
static void
mape_texture_map_init(MapeTextureMap* texture_map)
{
MapeTextureMapPrivate* priv;
priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
priv->handle = c4_texture_map_handle_new();
priv->texture_table = NULL;
priv->overload_materials = FALSE;
priv->overload_textures = FALSE;
}
static void
mape_texture_map_finalize(GObject* object)
{
MapeTextureMap* texture_map;
MapeTextureMapPrivate* priv;
texture_map = MAPE_TEXTURE_MAP(object);
priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
if(priv->texture_table != NULL)
g_hash_table_destroy(priv->texture_table);
c4_texture_map_handle_free(priv->handle);
G_OBJECT_CLASS(mape_texture_map_parent_class)->finalize(object);
}
static void
mape_texture_map_set_property(GObject* object,
guint prop_id,
const GValue* value,
GParamSpec* pspec)
{
switch(prop_id)
{
/* we have only readonly properties */
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(value, prop_id, pspec);
break;
}
}
static void
mape_texture_map_get_property(GObject* object,
guint prop_id,
GValue* value,
GParamSpec* pspec)
{
MapeTextureMap* texture_map;
MapeTextureMapPrivate* priv;
texture_map = MAPE_TEXTURE_MAP(object);
priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
switch(prop_id)
{
case PROP_N_TEXTURES:
if(priv->texture_table != NULL)
g_value_set_uint(value, g_hash_table_size(priv->texture_table));
else
g_value_set_uint(value, 0u);
break;
case PROP_OVERLOAD_MATERIALS:
g_value_set_boolean(value, priv->overload_materials);
break;
case PROP_OVERLOAD_TEXTURES:
g_value_set_boolean(value, priv->overload_textures);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/*
* Gype registration.
*/
static void
mape_texture_map_class_init(MapeTextureMapClass *class)
{
GObjectClass* object_class;
object_class = G_OBJECT_CLASS(class);
mape_texture_map_parent_class =
G_OBJECT_CLASS(g_type_class_peek_parent(class));
g_type_class_add_private(class, sizeof(MapeTextureMapPrivate));
object_class->finalize = mape_texture_map_finalize;
object_class->set_property = mape_texture_map_set_property;
object_class->get_property = mape_texture_map_get_property;
mape_texture_map_error_quark =
g_quark_from_static_string("MAPE_TEXTURE_MAP_ERROR");
g_object_class_install_property(
object_class,
PROP_N_TEXTURES,
g_param_spec_uint(
"n-textures",
"Texture count",
"The number of loaded textures",
0,
G_MAXUINT,
0,
G_PARAM_READABLE
)
);
g_object_class_install_property(
object_class,
PROP_OVERLOAD_MATERIALS,
g_param_spec_boolean(
"overload-materials",
"Overload Materials",
"Whether to overload globally loaded materials",
FALSE,
G_PARAM_READABLE
)
);
g_object_class_install_property(
object_class,
PROP_OVERLOAD_MATERIALS,
g_param_spec_boolean(
"overload-textures",
"Overload Textures",
"Whether to overload globally loaded textures",
FALSE,
G_PARAM_READABLE
)
);
}
/*
* Public API.
*/
/**
* 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().
**/
MapeTextureMap*
mape_texture_map_new(void)
{
return MAPE_TEXTURE_MAP(g_object_new(MAPE_TYPE_TEXTURE_MAP, NULL));
}
/**
* 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.
*/
gboolean
mape_texture_map_load_map(MapeTextureMap* texture_map,
MapeGroup* group,
GError** error)
{
MapeTextureMapPrivate* priv;
g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), FALSE);
g_return_val_if_fail(MAPE_IS_GROUP(group), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
c4_texture_map_handle_load_map(
priv->handle,
_mape_group_get_handle(group),
"TexMap.txt",
&priv->overload_materials,
&priv->overload_textures
);
g_object_notify(G_OBJECT(texture_map), "overload-materials");
g_object_notify(G_OBJECT(texture_map), "overload-textures");
return TRUE;
}
/**
* 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.
*/
gboolean
mape_texture_map_load_textures(MapeTextureMap* texture_map,
MapeGroup* group,
GError** error)
{
/* We don't use C4TextureMap::LoadTextures here because that would try
* to load them as C4Surface. Instead, we have our own routine which loads
* them into GdkPixbufs. */
static const char* SUFFIXES[] = {
".png", ".jpg", ".jpeg", ".bmp",
".PNG", ".JPG", ".JPEG", ".BMP",
NULL
};
MapeTextureMapPrivate* priv;
gchar* name;
const char* const* ext;
gsize len;
gchar* casefold_name;
guchar* data;
gsize datalen;
GdkPixbufLoader* loader;
GdkPixbuf* pixbuf;
g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), FALSE);
g_return_val_if_fail(MAPE_IS_GROUP(group), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
if(priv->texture_table == NULL)
{
priv->texture_table = g_hash_table_new_full(
g_str_hash,
g_str_equal,
g_free,
g_object_unref
);
}
mape_group_rewind(group);
while( (name = mape_group_get_next_entry(group)) != NULL)
{
for(ext = SUFFIXES; *ext != NULL; ++ext)
if(g_str_has_suffix(name, *ext))
break;
if(*ext != NULL)
{
/* Make texname out of filename */
len = strlen(name)-strlen(*ext);
name[len] = '\0';
/* Use this for hashtable lookup */
casefold_name = g_utf8_casefold(name, len);
/* If we have this texture loaded already then don't load it again --
* this is how overloading works: We first load the primary group and
* then the overloaded one. */
if(g_hash_table_lookup(priv->texture_table, casefold_name) == NULL)
{
data = mape_group_load_entry(group, &datalen, error);
if(data == NULL)
{
g_free(name);
return FALSE;
}
loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_set_size(loader, 24, 24);
if(!gdk_pixbuf_loader_write(loader, data, datalen, error))
{
g_free(name);
gdk_pixbuf_loader_close(loader, NULL);
g_object_unref(loader);
return FALSE;
}
if(!gdk_pixbuf_loader_close(loader, error))
{
g_free(name);
g_object_unref(loader);
return FALSE;
}
pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
g_object_ref(pixbuf);
g_object_unref(loader);
/* Add texture to texmap (without actual Surface),
* just so that the map generator knows the presence
* of the texture. */
c4_texture_map_handle_add_texture(priv->handle, name);
g_hash_table_insert(priv->texture_table, casefold_name, pixbuf);
}
else
{
g_free(casefold_name);
}
}
g_free(name);
}
return TRUE;
}
/**
* 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.
*/
gboolean
mape_texture_map_get_overload_materials(MapeTextureMap* texture_map)
{
g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), FALSE);
return MAPE_TEXTURE_MAP_PRIVATE(texture_map)->overload_materials;
}
/**
* 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.
*/
gboolean
mape_texture_map_get_overload_textures(MapeTextureMap* texture_map)
{
g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), FALSE);
return MAPE_TEXTURE_MAP_PRIVATE(texture_map)->overload_materials;
}
/**
* 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.
*/
guint
mape_texture_map_get_texture_count(MapeTextureMap* texture_map)
{
MapeTextureMapPrivate* priv;
g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), FALSE);
priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
return g_hash_table_size(priv->texture_table);
}
/**
* 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.
*/
const gchar*
mape_texture_map_get_texture_name(MapeTextureMap* texture_map,
guint index)
{
MapeTextureMapPrivate* priv;
g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), NULL);
g_return_val_if_fail(
index < mape_texture_map_get_texture_count(texture_map), NULL
);
priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
return c4_texture_map_handle_get_texture(priv->handle, index);
}
/**
* mape_texture_map_lookup_texture:
* @texture_map: A #MapeTextureMap.
* @name: The name of the texture to lookup.
*
* Looks up the graphics for the texture with name @name. If there is no such
* texture available in @texture_map 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)
{
MapeTextureMapPrivate* priv;
gchar* casefold_name;
GdkPixbuf* pixbuf;
g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), NULL);
g_return_val_if_fail(name != NULL, NULL);
priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
casefold_name = g_utf8_casefold(name, -1);
pixbuf = GDK_PIXBUF(g_hash_table_lookup(priv->texture_table, casefold_name));
g_free(casefold_name);
return pixbuf;
}
/**
* 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.
*/
const gchar*
mape_texture_map_get_material_name_from_mapping(MapeTextureMap* texture_map,
guint index)
{
MapeTextureMapPrivate* priv;
g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), NULL);
priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
return mape_texture_handle_get_entry_material_name(priv->handle, 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.
*/
const gchar*
mape_texture_map_get_texture_name_from_mapping(MapeTextureMap* texture_map,
guint index)
{
MapeTextureMapPrivate* priv;
g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(texture_map), NULL);
priv = MAPE_TEXTURE_MAP_PRIVATE(texture_map);
return mape_texture_handle_get_entry_texture_name(priv->handle, index);
}
/* This function is for internal use only */
C4TextureMapHandle*
_mape_texture_map_get_handle(MapeTextureMap* map)
{
g_return_val_if_fail(MAPE_IS_TEXTURE_MAP(map), NULL);
return MAPE_TEXTURE_MAP_PRIVATE(map)->handle;
}
/* vim:set et sw=2 ts=2: */

View File

@ -1,191 +0,0 @@
/*
* mape - C4 Landscape.txt editor
*
* Copyright (c) 2005-2009 Armin Burgmeier
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
#define MAPE_COMPILING_CPP
#include <C4Include.h>
#include <C4Texture.h>
#include <exception>
#include "mape/cpp-handles/group-handle.h"
#include "mape/group.h"
#include "mape/texture.h"
#define CPPTEXMAP(map) ( (C4TextureMap*)map->handle)
extern "C" C4GroupHandle* _mape_group_get_handle(MapeGroup*);
#define CPPGROUP(group) (reinterpret_cast<C4Group*>(_mape_group_get_handle(group)))
extern "C" {
static const char* SUFFIXES[] = {
".png", ".jpg", ".jpeg", ".bmp", NULL
};
/* Load all textures from a group. If there is a texture in the group that
* has the same name as an already loaded one, then replace the loaded
* one (overloading). */
static gboolean mape_texture_map_load_group(MapeTextureMap* map,
MapeGroup* group,
GError** error)
{
gchar* name;
const char* const* ext;
gsize len;
gchar* casefold_name;
guchar* data;
gsize datalen;
GdkPixbufLoader* loader;
GdkPixbuf* pixbuf;
mape_group_rewind(group);
while( (name = mape_group_get_next_entry(group)) != NULL)
{
for(ext = SUFFIXES; *ext != NULL; ++ext)
if(g_str_has_suffix(name, *ext))
break;
if(*ext != NULL)
{
data = mape_group_load_entry(group, &datalen, error);
if(data == NULL)
{
g_free(name);
return FALSE;
}
loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_set_size(loader, 24, 24);
if(!gdk_pixbuf_loader_write(loader, data, datalen, error))
{
g_free(name);
gdk_pixbuf_loader_close(loader, NULL);
g_object_unref(loader);
return FALSE;
}
if(!gdk_pixbuf_loader_close(loader, error))
{
g_free(name);
g_object_unref(loader);
return FALSE;
}
pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
g_object_ref(pixbuf);
g_object_unref(loader);
/* Make texname out of filename */
len = strlen(name)-strlen(*ext);
name[len] = '\0';
/* Add texture to texmap (without actual Surface),
* just so that the map generator knows the presence
* of the texture. */
CPPTEXMAP(map)->AddTexture(name, NULL);
/* Use this for hashtable lookup */
casefold_name = g_utf8_casefold(name, len);
g_hash_table_insert(map->textures, casefold_name, pixbuf);
}
g_free(name);
}
return TRUE;
}
MapeTextureMap* mape_texture_map_new(MapeGroup* base,
MapeGroup* overload_from,
GError** error)
{
MapeTextureMap* map;
map = (MapeTextureMap*)g_slice_new(MapeTextureMap);
map->handle = NULL;
map->textures = g_hash_table_new_full(
g_str_hash,
g_str_equal,
g_free,
g_object_unref
);
try
{
// TODO: Allow indefinite overloading... does Clonk do this?
// TODO: Do we need to LoadMap?
map->handle = new C4TextureMap;
// Load TexMap.txt
CPPTEXMAP(map)->LoadMap(overload_from ? *CPPGROUP(overload_from) : *CPPGROUP(base), "TexMap.txt", NULL, NULL);
// Load textures
if(!mape_texture_map_load_group(map, base, error))
{
mape_texture_map_destroy(map);
return NULL;
}
if(overload_from != NULL &&
!mape_texture_map_load_group(map, overload_from, error))
{
mape_texture_map_destroy(map);
return NULL;
}
}
catch(const std::exception& e)
{
g_set_error(
error,
g_quark_from_static_string("MAPE_TEXTURE_ERROR"),
MAPE_TEXTURE_ERROR_FAILED,
"%s",
e.what()
);
mape_texture_map_destroy(map);
return NULL;
}
return map;
}
void mape_texture_map_destroy(MapeTextureMap* map)
{
g_hash_table_unref(map->textures);
delete CPPTEXMAP(map);
g_slice_free(MapeTextureMap, map);
}
guint mape_texture_map_get_texture_count(MapeTextureMap* map)
{
return g_hash_table_size(map->textures);
}
const char* mape_texture_map_get_texture_name(MapeTextureMap* map,
unsigned int index)
{
// Bah, this is O(n). Better remove this function, and introduce
// mape_texture_map_foreach_texture().
return CPPTEXMAP(map)->GetTexture(index);
}
GdkPixbuf* mape_texture_map_lookup_texture(MapeTextureMap* map,
const gchar* name)
{
return GDK_PIXBUF(g_hash_table_lookup(map->textures, name));
}
} // extern "C"

View File

@ -15,43 +15,101 @@
* See clonk_trademark_license.txt for full license.
*/
#ifndef INC_MAPE_TEXTURE_H
#define INC_MAPE_TEXTURE_H
#ifndef INC_MAPE_TEXTURE_MAP_H
#define INC_MAPE_TEXTURE_MAP_H
#include <glib.h>
#include <glib-object.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "mape/forward.h"
#include "mape/group.h"
/* Simple C-based interface to C4TextureMap */
G_BEGIN_DECLS
#ifdef MAPE_COMPILING_CPP
extern "C" {
#endif
#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))
typedef enum MapeTextureError_ {
MAPE_TEXTURE_ERROR_FAILED
} MapeTextureError;
typedef struct _MapeTextureMap MapeTextureMap;
typedef struct _MapeTextureMapClass MapeTextureMapClass;
struct MapeTextureMap_ {
void* handle;
GHashTable* textures;
/**
* 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.
*/
typedef enum _MapeTextureMapError {
MAPE_TEXTURE_MAP_ERROR_LOAD
} MapeTextureMapError;
/**
* MapeTextureMapClass:
*
* This structure does not contain any public fields.
*/
struct _MapeTextureMapClass {
/*< private >*/
GObjectClass parent_class;
};
MapeTextureMap* mape_texture_map_new(MapeGroup* base,
MapeGroup* overload_from,
GError** error);
void mape_texture_map_destroy(MapeTextureMap* map);
/**
* MapeTextureMap:
*
* #MapeTextureMap is an opaque data type. You should only access it via the
* public API functions.
*/
struct _MapeTextureMap {
/*< private >*/
GObject parent;
};
guint mape_texture_map_get_texture_count(MapeTextureMap* map);
const gchar* mape_texture_map_get_texture_name(MapeTextureMap* map,
GType
mape_texture_map_get_type(void) G_GNUC_CONST;
MapeTextureMap*
mape_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);
GdkPixbuf* mape_texture_map_lookup_texture(MapeTextureMap* map,
const gchar* name);
#ifdef MAPE_COMPILING_CPP
} /* extern "C" */
#endif
G_END_DECLS
#endif /* INC_MAPE_TEXTURE_H */
#endif /* INC_MAPE_TEXTURE_MAP_H */
/* vim:set et sw=2 ts=2: */