Fix error message when SDL_ttf is not present.
[attac-man.git] / tds.c
blobd2747dd72c2f3a58e2dcc7e6e193b04b7d8821de
1 /*
2 Pacman Arena
3 Copyright (C) 2003 Nuno Subtil
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #ifdef _WIN32
21 #include <windows.h>
22 #endif
24 #include <GL/gl.h>
25 #include <stdlib.h>
26 #include <string.h>
28 /* lib3ds */
29 #include <lib3ds/file.h>
30 #include <lib3ds/mesh.h>
31 #include <lib3ds/node.h>
32 #include <lib3ds/material.h>
33 #include <lib3ds/vector.h>
35 #include "object.h"
36 #include "linked-lists.h"
37 #include "tds.h"
39 static const char cvsid[] =
40 "$Id: tds.c,v 1.9 2003/07/19 01:10:18 nsubtil Exp $";
42 static struct tdsfile *tdsfiles = NULL;
45 carrega um ficheiro 3ds
47 struct tdsfile *tds_load_file(char *name)
49 Lib3dsFile *h;
50 struct tdsfile *new;
52 h = lib3ds_file_load(name);
53 if(h == NULL)
55 printf("%s: tds_load_file: lib3ds_file_load() failed\n", name);
56 return NULL;
59 new = (struct tdsfile *)malloc(sizeof(struct tdsfile));
60 new->name = strdup(name);
61 new->handler = h;
62 new->next = NULL;
64 LLIST_ADD(struct tdsfile, tdsfiles, new);
65 return new;
69 procura um ficheiro 3ds carregado
71 struct tdsfile *tds_find(char *name)
73 struct tdsfile *cur;
75 cur = tdsfiles;
76 while(cur)
78 if(strcmp(cur->name, name) == 0)
79 return cur;
81 cur = cur->next;
84 return NULL;
87 struct tdsfile *tds_get(char *name)
89 struct tdsfile *ret;
91 ret = tds_find(name);
92 if(ret == NULL)
93 ret = tds_load_file(name);
95 return ret;
99 carrega o mesh de um 3ds
101 struct object *tds_load(char *name)
103 struct tdsfile *file;
104 struct object *ret;
105 Lib3dsNode *p;
107 file = tds_get(name);
108 if(file == NULL)
109 return NULL;
111 lib3ds_file_eval(file->handler, 0.0);
112 ret = malloc(sizeof(struct object));
113 ret->num_fc = 0;
114 ret->fc_list = NULL;
115 ret->dlist_color = -1;
116 ret->dlist_nocolor = -1;
118 p = file->handler->nodes;
119 while(p)
121 tds_add_node_recursive(ret, p, file);
122 p = p->next;
125 return ret;
128 void tds_add_node_recursive(struct object *obj, Lib3dsNode *node, struct tdsfile *file)
130 Lib3dsNode *p;
131 Lib3dsMesh *mesh;
132 Lib3dsVector *normal_list;
133 int c;
134 int flag;
136 p = node->childs;
137 while(p)
139 tds_add_node_recursive(obj, p, file);
140 p = p->next;
143 if(node->type != LIB3DS_OBJECT_NODE)
144 return;
146 /* XXX - que raio é isto ? */
147 if(strcmp(node->name, "$$$DUMMY") == 0)
148 return;
150 if(strstr(node->name, "ctag"))
151 flag = 1;
152 else
153 flag = 0;
155 mesh = lib3ds_file_mesh_by_name(file->handler, node->name);
156 if(mesh == NULL)
158 printf("tds_add_node_recursive: in [%s]: mesh is null!\n", node->name);
159 return;
162 normal_list = (Lib3dsVector *)malloc(sizeof(Lib3dsVector) * mesh->faces * 3);
163 lib3ds_mesh_calculate_normals(mesh, normal_list);
165 for(c = 0; c < mesh->faces; c++)
167 Lib3dsFace *face;
168 struct face_tri *new;
170 new = malloc(sizeof(struct face_tri));
172 face = &mesh->faceL[c];
174 if(face->material[0])
176 Lib3dsMaterial *material;
178 material = lib3ds_file_material_by_name(file->handler, face->material);
179 memcpy(new->color, material->diffuse, sizeof(GLfloat) * 4);
180 } else {
181 new->color[R] = 1.0;
182 new->color[G] = 1.0;
183 new->color[B] = 1.0;
184 new->color[A] = 1.0;
187 new->a = mesh->pointL[face->points[0]].pos;
188 new->b = mesh->pointL[face->points[1]].pos;
189 new->c = mesh->pointL[face->points[2]].pos;
191 memcpy(new->na, normal_list[3 * c], sizeof(GLfloat) * 3);
192 memcpy(new->nb, normal_list[3 * c + 1], sizeof(GLfloat) * 3);
193 memcpy(new->nc, normal_list[3 * c + 2], sizeof(GLfloat) * 3);
195 new->color_tag = flag;
197 object_add_face(obj, new);
200 free(normal_list);