scenefile
diff src/mesh.c @ 0:8c6d64af9505
scenefile
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Fri, 13 Jan 2012 09:34:16 +0200 |
parents | |
children | 38489ad82bf4 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/mesh.c Fri Jan 13 09:34:16 2012 +0200 1.3 @@ -0,0 +1,125 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include "mesh.h" 1.9 + 1.10 +int mattr_init(struct mesh_attrib *ma) 1.11 +{ 1.12 + memset(ma, 0, sizeof *ma); 1.13 + return 0; 1.14 +} 1.15 + 1.16 +void mattr_destroy(struct mesh_attrib *ma) 1.17 +{ 1.18 + if(ma) { 1.19 + free(ma->name); 1.20 + free(ma->data); 1.21 + } 1.22 +} 1.23 + 1.24 +int mattr_set_name(struct mesh_attrib *ma, const char *name) 1.25 +{ 1.26 + char *tmp; 1.27 + 1.28 + if(!(tmp = malloc(strlen(name) + 1))) { 1.29 + return -1; 1.30 + } 1.31 + strcpy(tmp, name); 1.32 + 1.33 + free(ma->name); 1.34 + ma->name = tmp; 1.35 + return 0; 1.36 +} 1.37 + 1.38 +#define INITSZ (16 * ma->elem_size) 1.39 +int mattr_add_elem(struct mesh_attrib *ma, void *data) 1.40 +{ 1.41 + int nsz = (ma->count + 1) * ma->elem_size; 1.42 + 1.43 + if(nsz > ma->datasz) { 1.44 + void *tmp; 1.45 + 1.46 + nsz = ma->datasz ? ma->datasz * 2 : INITSZ; 1.47 + 1.48 + if(!(tmp = realloc(ma->data, nsz))) { 1.49 + return -1; 1.50 + } 1.51 + ma->data = tmp; 1.52 + ma->datasz = nsz; 1.53 + } 1.54 + 1.55 + memcpy((char*)ma->data + ma->elem_size * ma->count++, data, ma->elem_size); 1.56 + return 0; 1.57 +} 1.58 + 1.59 +/* -------- mesh -------- */ 1.60 + 1.61 +int mesh_init(struct mesh *m) 1.62 +{ 1.63 + memset(m, 0, sizeof *m); 1.64 + return 0; 1.65 +} 1.66 + 1.67 +void mesh_destroy(struct mesh *m) 1.68 +{ 1.69 + int i; 1.70 + 1.71 + free(m->name); 1.72 + 1.73 + for(i=0; i<m->num_attr; i++) { 1.74 + mattr_destroy(&m->attr[i]); 1.75 + } 1.76 + free(m->attr); 1.77 + 1.78 + for(i=0; i<m->num_attr; i++) { 1.79 + free(m->polyidx[i]); 1.80 + } 1.81 + free(m->polyidx); 1.82 +} 1.83 + 1.84 +int mesh_set_name(struct mesh *m, const char *name) 1.85 +{ 1.86 + char *tmp; 1.87 + 1.88 + if(!(tmp = malloc(strlen(name) + 1))) { 1.89 + return -1; 1.90 + } 1.91 + strcpy(tmp, name); 1.92 + 1.93 + free(m->name); 1.94 + m->name = tmp; 1.95 + return 0; 1.96 +} 1.97 + 1.98 +int mesh_add_attrib(struct mesh *m, struct mesh_attrib *attr) 1.99 +{ 1.100 + void *tmp; 1.101 + int idx = m->num_attr++; 1.102 + 1.103 + if(!(tmp = realloc(m->attr, m->num_attr * sizeof *attr))) { 1.104 + return -1; 1.105 + } 1.106 + m->attr = tmp; 1.107 + m->attr[idx] = *attr; 1.108 + 1.109 + if(!(tmp = realloc(m->polyidx, m->num_attr * sizeof *m->polyidx))) { 1.110 + m->num_attr--; 1.111 + return -1; 1.112 + } 1.113 + m->polyidx[idx] = 0; 1.114 + 1.115 + return 0; 1.116 +} 1.117 + 1.118 +int mesh_find_attrib(struct mesh *m, const char *name) 1.119 +{ 1.120 + int i; 1.121 + 1.122 + for(i=0; i<m->num_attr; i++) { 1.123 + if(strcmp(m->attr[i].name, name) == 0) { 1.124 + return i; 1.125 + } 1.126 + } 1.127 + return -1; 1.128 +}