diff options
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_def.c')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_def.c | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_def.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_def.c new file mode 100644 index 0000000..bf310b1 --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_def.c | |||
@@ -0,0 +1,127 @@ | |||
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 | #include <glib.h> | ||
23 | |||
24 | #include "imp_acf_def.h" | ||
25 | |||
26 | #define ACF_DEBUG_TYPES 2 | ||
27 | |||
28 | AcfFile *acf_def_read(G3DStream *stream, const AcfDef *def, | ||
29 | gboolean bigendian) | ||
30 | { | ||
31 | AcfFile *acf; | ||
32 | AcfValue *value; | ||
33 | gint32 i, j; | ||
34 | |||
35 | acf = g_new0(AcfFile, 1); | ||
36 | acf->db = g_hash_table_new(g_str_hash, g_str_equal); | ||
37 | |||
38 | for(i = 0; def[i].type != XEOF; i ++) { | ||
39 | value = g_new0(AcfValue, 1); | ||
40 | value->name = g_strdup(def[i].description); | ||
41 | value->type = def[i].type; | ||
42 | value->num = def[i].num; | ||
43 | switch(value->type) { | ||
44 | case XCHR: | ||
45 | value->xchr = g_new0(gchar, value->num + 1); | ||
46 | g3d_stream_read(stream, value->xchr, value->num); | ||
47 | #if DEBUG > ACF_DEBUG_TYPES | ||
48 | g_debug("ACF: XCHR: %s = %s", value->name, value->xchr); | ||
49 | #endif | ||
50 | break; | ||
51 | case XINT: | ||
52 | value->xint = g_new0(gint32, value->num); | ||
53 | for(j = 0; j < value->num; j ++) | ||
54 | value->xint[j] = bigendian ? | ||
55 | g3d_stream_read_int32_be(stream) : | ||
56 | g3d_stream_read_int32_le(stream); | ||
57 | #if DEBUG > ACF_DEBUG_TYPES | ||
58 | g_debug("ACF: XINT: %s(1/%d) = %i", value->name, value->num, | ||
59 | value->xint[0]); | ||
60 | #endif | ||
61 | break; | ||
62 | case XFLT: | ||
63 | value->xflt = g_new0(G3DFloat, value->num); | ||
64 | for(j = 0; j < value->num; j ++) | ||
65 | value->xflt[j] = bigendian ? | ||
66 | g3d_stream_read_float_be(stream) : | ||
67 | g3d_stream_read_float_le(stream); | ||
68 | #if DEBUG > ACF_DEBUG_TYPES | ||
69 | g_debug("ACF: XFLT: %s(1/%d) = %f", value->name, value->num, | ||
70 | value->xflt[0]); | ||
71 | #endif | ||
72 | break; | ||
73 | case XEOF: | ||
74 | /* should never happen, just make compiler happy */ | ||
75 | break; | ||
76 | } | ||
77 | g_hash_table_insert(acf->db, def[i].description, value); | ||
78 | } | ||
79 | |||
80 | return acf; | ||
81 | } | ||
82 | |||
83 | static gboolean acf_def_remove_value_cb(gpointer key, gpointer hashvalue, | ||
84 | gpointer data) | ||
85 | { | ||
86 | AcfValue *value = hashvalue; | ||
87 | |||
88 | if(value->xchr) | ||
89 | g_free(value->xchr); | ||
90 | if(value->xint) | ||
91 | g_free(value->xint); | ||
92 | if(value->xflt) | ||
93 | g_free(value->xflt); | ||
94 | g_free(value->name); | ||
95 | g_free(value); | ||
96 | return TRUE; | ||
97 | } | ||
98 | |||
99 | void acf_def_free(AcfFile *acf) | ||
100 | { | ||
101 | g_hash_table_foreach_remove(acf->db, acf_def_remove_value_cb, NULL); | ||
102 | g_free(acf); | ||
103 | } | ||
104 | |||
105 | AcfValue *acf_def_lookup(AcfFile *acf, const gchar *name) | ||
106 | { | ||
107 | return g_hash_table_lookup(acf->db, name); | ||
108 | } | ||
109 | |||
110 | void acf_def_dump(AcfValue *value) | ||
111 | { | ||
112 | gint32 i; | ||
113 | |||
114 | if(value->type == XCHR) { | ||
115 | g_debug("ACF: %s: %s", value->name, value->xchr); | ||
116 | return; | ||
117 | } | ||
118 | |||
119 | g_debug("ACF: %s: dumping %d %s items", value->name, value->num, | ||
120 | (value->type == XINT) ? "XINT" : "XFLT"); | ||
121 | for(i = 0; i < value->num; i ++) { | ||
122 | if(value->type == XINT) | ||
123 | g_debug("\tXINT: %s[%i] = %i", value->name, i, value->xint[i]); | ||
124 | else if(value->type == XFLT) | ||
125 | g_debug("\tXFLT: %s[%i] = %f", value->name, i, value->xflt[i]); | ||
126 | } | ||
127 | } | ||