scenefile
changeset 0:8c6d64af9505
scenefile
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Fri, 13 Jan 2012 09:34:16 +0200 |
parents | |
children | 38489ad82bf4 |
files | .hgignore Makefile src/mesh.c src/mesh.h |
diffstat | 4 files changed, 236 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Fri Jan 13 09:34:16 2012 +0200 1.3 @@ -0,0 +1,5 @@ 1.4 +\.o$ 1.5 +\.d$ 1.6 +\.swp$ 1.7 +^libscenefile\.so 1.8 +^libscenefile.a$
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/Makefile Fri Jan 13 09:34:16 2012 +0200 2.3 @@ -0,0 +1,62 @@ 2.4 +PREFIX = /usr/local 2.5 + 2.6 +src = $(wildcard src/*.c) 2.7 +obj = $(src:.c=.o) 2.8 +dep = $(obj:.o=.d) 2.9 + 2.10 +name = scenefile 2.11 +lib_a = lib$(name).a 2.12 + 2.13 +dbg = -g 2.14 +#opt = -O3 2.15 + 2.16 +ifeq ($(shell uname -s), Darwin) 2.17 + lib_so = $(name).dylib 2.18 + shared = -dynamiclib 2.19 +else 2.20 + abi = 0 2.21 + rev = 0 2.22 + devlink = lib$(name).so 2.23 + soname = $(devlink).$(abi) 2.24 + lib_so = $(soname).$(rev) 2.25 + shared = -shared -Wl,-soname=$(soname) 2.26 + pic = -fPIC 2.27 +endif 2.28 + 2.29 +CFLAGS = -pedantic -Wall $(dbg) $(opt) $(pic) 2.30 + 2.31 +.PHONY: all 2.32 +all: $(lib_so) $(lib_a) 2.33 + 2.34 +$(lib_so): $(obj) 2.35 + $(CC) -o $@ $(shared) $(obj) $(LDFLAGS) 2.36 + 2.37 +$(lib_a): $(obj) 2.38 + $(AR) rcs $@ $(obj) 2.39 + 2.40 +-include $(dep) 2.41 + 2.42 +%.d: %.c 2.43 + @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ 2.44 + 2.45 +.PHONY: clean 2.46 +clean: 2.47 + rm -f $(obj) $(lib_so) $(lib_a) 2.48 + 2.49 +.PHONY: install 2.50 +install: $(lib_so) $(lib_a) 2.51 + mkdir -p $(INSTDIR)$(PREFIX)/lib $(INSTDIR)$(PREFIX)/include 2.52 + cp $(lib_a) $(INSTDIR)$(PREFIX)/lib/$(lib_a) 2.53 + cp $(lib_so) $(INSTDIR)$(PREFIX)/lib/$(lib_so) 2.54 + [ -n "$(soname)" ] && cd $(INSTDIR)$(PREFIX)/lib \ 2.55 + && ln -s $(lib_so) $(soname) \ 2.56 + && ln -s $(soname) $(devlink) \ 2.57 + || true 2.58 + 2.59 +.PHONY: uninstall 2.60 +uninstall: 2.61 + rm -f $(INSTDIR)$(PREFIX)/lib/$(lib_a) $(INSTDIR)$(PREFIX)/lib/$(lib_so) 2.62 + [ -n "$(soname)" ] \ 2.63 + && rm -f $(INSTDIR)$(PREFIX)/lib/$(soname) \ 2.64 + && rm -f $(INSTDIR)$(PREFIX)/lib/$(devlink) \ 2.65 + || true
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/mesh.c Fri Jan 13 09:34:16 2012 +0200 3.3 @@ -0,0 +1,125 @@ 3.4 +#include <stdio.h> 3.5 +#include <stdlib.h> 3.6 +#include <string.h> 3.7 +#include <errno.h> 3.8 +#include "mesh.h" 3.9 + 3.10 +int mattr_init(struct mesh_attrib *ma) 3.11 +{ 3.12 + memset(ma, 0, sizeof *ma); 3.13 + return 0; 3.14 +} 3.15 + 3.16 +void mattr_destroy(struct mesh_attrib *ma) 3.17 +{ 3.18 + if(ma) { 3.19 + free(ma->name); 3.20 + free(ma->data); 3.21 + } 3.22 +} 3.23 + 3.24 +int mattr_set_name(struct mesh_attrib *ma, const char *name) 3.25 +{ 3.26 + char *tmp; 3.27 + 3.28 + if(!(tmp = malloc(strlen(name) + 1))) { 3.29 + return -1; 3.30 + } 3.31 + strcpy(tmp, name); 3.32 + 3.33 + free(ma->name); 3.34 + ma->name = tmp; 3.35 + return 0; 3.36 +} 3.37 + 3.38 +#define INITSZ (16 * ma->elem_size) 3.39 +int mattr_add_elem(struct mesh_attrib *ma, void *data) 3.40 +{ 3.41 + int nsz = (ma->count + 1) * ma->elem_size; 3.42 + 3.43 + if(nsz > ma->datasz) { 3.44 + void *tmp; 3.45 + 3.46 + nsz = ma->datasz ? ma->datasz * 2 : INITSZ; 3.47 + 3.48 + if(!(tmp = realloc(ma->data, nsz))) { 3.49 + return -1; 3.50 + } 3.51 + ma->data = tmp; 3.52 + ma->datasz = nsz; 3.53 + } 3.54 + 3.55 + memcpy((char*)ma->data + ma->elem_size * ma->count++, data, ma->elem_size); 3.56 + return 0; 3.57 +} 3.58 + 3.59 +/* -------- mesh -------- */ 3.60 + 3.61 +int mesh_init(struct mesh *m) 3.62 +{ 3.63 + memset(m, 0, sizeof *m); 3.64 + return 0; 3.65 +} 3.66 + 3.67 +void mesh_destroy(struct mesh *m) 3.68 +{ 3.69 + int i; 3.70 + 3.71 + free(m->name); 3.72 + 3.73 + for(i=0; i<m->num_attr; i++) { 3.74 + mattr_destroy(&m->attr[i]); 3.75 + } 3.76 + free(m->attr); 3.77 + 3.78 + for(i=0; i<m->num_attr; i++) { 3.79 + free(m->polyidx[i]); 3.80 + } 3.81 + free(m->polyidx); 3.82 +} 3.83 + 3.84 +int mesh_set_name(struct mesh *m, const char *name) 3.85 +{ 3.86 + char *tmp; 3.87 + 3.88 + if(!(tmp = malloc(strlen(name) + 1))) { 3.89 + return -1; 3.90 + } 3.91 + strcpy(tmp, name); 3.92 + 3.93 + free(m->name); 3.94 + m->name = tmp; 3.95 + return 0; 3.96 +} 3.97 + 3.98 +int mesh_add_attrib(struct mesh *m, struct mesh_attrib *attr) 3.99 +{ 3.100 + void *tmp; 3.101 + int idx = m->num_attr++; 3.102 + 3.103 + if(!(tmp = realloc(m->attr, m->num_attr * sizeof *attr))) { 3.104 + return -1; 3.105 + } 3.106 + m->attr = tmp; 3.107 + m->attr[idx] = *attr; 3.108 + 3.109 + if(!(tmp = realloc(m->polyidx, m->num_attr * sizeof *m->polyidx))) { 3.110 + m->num_attr--; 3.111 + return -1; 3.112 + } 3.113 + m->polyidx[idx] = 0; 3.114 + 3.115 + return 0; 3.116 +} 3.117 + 3.118 +int mesh_find_attrib(struct mesh *m, const char *name) 3.119 +{ 3.120 + int i; 3.121 + 3.122 + for(i=0; i<m->num_attr; i++) { 3.123 + if(strcmp(m->attr[i].name, name) == 0) { 3.124 + return i; 3.125 + } 3.126 + } 3.127 + return -1; 3.128 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/mesh.h Fri Jan 13 09:34:16 2012 +0200 4.3 @@ -0,0 +1,44 @@ 4.4 +#ifndef MESH_H_ 4.5 +#define MESH_H_ 4.6 + 4.7 +struct mesh_attrib { 4.8 + char *name; 4.9 + int count, elem_size; 4.10 + void *data; 4.11 + 4.12 + int datasz; 4.13 +}; 4.14 + 4.15 +struct mesh { 4.16 + char *name; 4.17 + int poly_nverts; /* 3, 4, etc */ 4.18 + 4.19 + struct mesh_attrib *attr; 4.20 + int num_attr; 4.21 + 4.22 + int **polyidx; 4.23 + int num_poly; 4.24 +}; 4.25 + 4.26 +#ifdef __cplusplus 4.27 +extern "C" { 4.28 +#endif 4.29 + 4.30 +int mattr_init(struct mesh_attrib *ma); 4.31 +void mattr_destroy(struct mesh_attrib *ma); 4.32 + 4.33 +int mattr_set_name(struct mesh_attrib *ma, const char *name); 4.34 +int mattr_add_elem(struct mesh_attrib *ma, void *data); 4.35 + 4.36 +int mesh_init(struct mesh *m); 4.37 +void mesh_destroy(struct mesh *m); 4.38 + 4.39 +int mesh_set_name(struct mesh *m, const char *name); 4.40 +int mesh_add_attrib(struct mesh *m, struct mesh_attrib *attr); 4.41 +int mesh_find_attrib(struct mesh *m, const char *name); 4.42 + 4.43 +#ifdef __cplusplus 4.44 +} 4.45 +#endif 4.46 + 4.47 +#endif /* MESH_H_ */