diff options
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dae/imp_dae_library.c')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dae/imp_dae_library.c | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dae/imp_dae_library.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dae/imp_dae_library.c new file mode 100644 index 0000000..dd10351 --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dae/imp_dae_library.c | |||
@@ -0,0 +1,167 @@ | |||
1 | /* $Id$ */ | ||
2 | |||
3 | /* | ||
4 | libg3d - 3D object loading library | ||
5 | |||
6 | Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de> | ||
7 | |||
8 | This library is free software; you can redistribute it and/or | ||
9 | modify it under the terms of the GNU Lesser General Public | ||
10 | License as published by the Free Software Foundation; either | ||
11 | version 2.1 of the License, or (at your option) any later version. | ||
12 | |||
13 | This library is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | Lesser General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU Lesser General Public | ||
19 | License along with this library; if not, write to the Free Software | ||
20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | #include <glib.h> | ||
24 | #include <libxml/tree.h> | ||
25 | |||
26 | #include "imp_dae_xml.h" | ||
27 | #include "imp_dae_library.h" | ||
28 | |||
29 | struct _DaeLibrary { | ||
30 | GHashTable *ids; | ||
31 | GSList *libs; | ||
32 | }; | ||
33 | |||
34 | typedef struct { | ||
35 | GHashTable *ids; | ||
36 | GSList *nodes; | ||
37 | } DaeLibraryNodes; | ||
38 | |||
39 | static gchar *dae_library_names[][2] = { | ||
40 | { "library_animations", "animation" }, | ||
41 | { "library_cameras", "camera" }, | ||
42 | { "library_controllers", "controller" }, | ||
43 | { "library_effects", "effect" }, | ||
44 | { "library_geometries", "geometry" }, | ||
45 | { "library_images", "image" }, | ||
46 | { "library_lights", "light" }, | ||
47 | { "library_materials", "material" }, | ||
48 | { "library_nodes", "node" }, | ||
49 | { "library_physics_scenes", "physics_scene" }, | ||
50 | { "library_visual_scenes", "visual_scene" }, | ||
51 | { NULL, NULL } | ||
52 | }; | ||
53 | |||
54 | static gboolean dae_add_library(DaeLibrary *lib, xmlNodePtr libnode, | ||
55 | guint32 entryid) | ||
56 | { | ||
57 | xmlNodePtr node; | ||
58 | DaeLibraryNodes *nodelib; | ||
59 | gchar *id; | ||
60 | |||
61 | nodelib = g_new0(DaeLibraryNodes, 1); | ||
62 | nodelib->ids = g_hash_table_new_full( | ||
63 | g_str_hash, g_str_equal, g_free, NULL); | ||
64 | g_hash_table_insert(lib->ids, g_strdup(dae_library_names[entryid][1]), | ||
65 | nodelib); | ||
66 | lib->libs = g_slist_append(lib->libs, nodelib); | ||
67 | |||
68 | node = libnode->children; | ||
69 | while(node != NULL) { | ||
70 | if((node->type == XML_ELEMENT_NODE) && | ||
71 | (xmlStrcmp(node->name, | ||
72 | (const xmlChar *)dae_library_names[entryid][1]) == 0)) { | ||
73 | /* found library entry */ | ||
74 | id = dae_xml_get_attr(node, "id"); | ||
75 | if(id != NULL) { | ||
76 | #if DEBUG > 2 | ||
77 | g_debug("\t%s id=\"%s\"", dae_library_names[entryid][1], id); | ||
78 | #endif | ||
79 | g_hash_table_insert(nodelib->ids, id, node); | ||
80 | nodelib->nodes = g_slist_append(nodelib->nodes, node); | ||
81 | } | ||
82 | } | ||
83 | node = node->next; | ||
84 | } | ||
85 | return TRUE; | ||
86 | } | ||
87 | |||
88 | gboolean dae_library_add(DaeLibrary *lib, const gchar *libname, | ||
89 | const gchar *id, xmlNodePtr node) | ||
90 | { | ||
91 | DaeLibraryNodes *nodelib; | ||
92 | |||
93 | g_return_val_if_fail(lib != NULL, FALSE); | ||
94 | |||
95 | nodelib = g_hash_table_lookup(lib->ids, libname); | ||
96 | if(nodelib == NULL) { | ||
97 | nodelib = g_new0(DaeLibraryNodes, 1); | ||
98 | nodelib->ids = g_hash_table_new_full( | ||
99 | g_str_hash, g_str_equal, g_free, NULL); | ||
100 | g_hash_table_insert(lib->ids, g_strdup(libname), nodelib); | ||
101 | lib->libs = g_slist_append(lib->libs, nodelib); | ||
102 | } | ||
103 | g_hash_table_insert(nodelib->ids, g_strdup(id), node); | ||
104 | nodelib->nodes = g_slist_append(nodelib->nodes, node); | ||
105 | return TRUE; | ||
106 | } | ||
107 | |||
108 | DaeLibrary *dae_library_load(xmlDocPtr xmldoc) | ||
109 | { | ||
110 | DaeLibrary *lib; | ||
111 | gint i = 0; | ||
112 | xmlNodePtr rootnode, node; | ||
113 | |||
114 | lib = g_new0(DaeLibrary, 1); | ||
115 | lib->ids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | ||
116 | |||
117 | rootnode = xmlDocGetRootElement(xmldoc); | ||
118 | g_return_val_if_fail(rootnode != NULL, NULL); | ||
119 | |||
120 | while(dae_library_names[i][0] != NULL) { | ||
121 | #if DEBUG > 2 | ||
122 | g_debug("loading library %s", dae_library_names[i][0]); | ||
123 | #endif | ||
124 | node = rootnode->children; | ||
125 | while(node != NULL) { | ||
126 | if((node->type == XML_ELEMENT_NODE) && | ||
127 | (xmlStrcmp(node->name, | ||
128 | (const xmlChar *)dae_library_names[i][0]) == 0)) { | ||
129 | dae_add_library(lib, node, i); | ||
130 | break; | ||
131 | } | ||
132 | node = node->next; | ||
133 | } | ||
134 | i ++; | ||
135 | } | ||
136 | return lib; | ||
137 | } | ||
138 | |||
139 | xmlNodePtr dae_library_lookup(DaeLibrary *library, const gchar *tagname, | ||
140 | const gchar *id) | ||
141 | { | ||
142 | DaeLibraryNodes *nodelib; | ||
143 | |||
144 | nodelib = g_hash_table_lookup(library->ids, tagname); | ||
145 | if(nodelib == NULL) { | ||
146 | g_warning("DAE: failed to lookup library for '%s'", tagname); | ||
147 | return NULL; | ||
148 | } | ||
149 | return (xmlNodePtr)g_hash_table_lookup(nodelib->ids, id); | ||
150 | } | ||
151 | |||
152 | void dae_library_cleanup(DaeLibrary *library) | ||
153 | { | ||
154 | DaeLibraryNodes *nodelib; | ||
155 | GSList *item; | ||
156 | |||
157 | for(item = library->libs; item != NULL; item = item->next) { | ||
158 | nodelib = (DaeLibraryNodes *)item->data; | ||
159 | g_hash_table_destroy(nodelib->ids); | ||
160 | g_slist_free(nodelib->nodes); | ||
161 | } | ||
162 | |||
163 | g_hash_table_destroy(library->ids); | ||
164 | g_slist_free(library->libs); | ||
165 | g_free(library); | ||
166 | } | ||
167 | |||