# HG changeset patch # User John Tsiombikas # Date 1326440056 -7200 # Node ID 8c6d64af9505d787a9dbf57b5fb05befefb20022 scenefile diff -r 000000000000 -r 8c6d64af9505 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Fri Jan 13 09:34:16 2012 +0200 @@ -0,0 +1,5 @@ +\.o$ +\.d$ +\.swp$ +^libscenefile\.so +^libscenefile.a$ diff -r 000000000000 -r 8c6d64af9505 Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri Jan 13 09:34:16 2012 +0200 @@ -0,0 +1,62 @@ +PREFIX = /usr/local + +src = $(wildcard src/*.c) +obj = $(src:.c=.o) +dep = $(obj:.o=.d) + +name = scenefile +lib_a = lib$(name).a + +dbg = -g +#opt = -O3 + +ifeq ($(shell uname -s), Darwin) + lib_so = $(name).dylib + shared = -dynamiclib +else + abi = 0 + rev = 0 + devlink = lib$(name).so + soname = $(devlink).$(abi) + lib_so = $(soname).$(rev) + shared = -shared -Wl,-soname=$(soname) + pic = -fPIC +endif + +CFLAGS = -pedantic -Wall $(dbg) $(opt) $(pic) + +.PHONY: all +all: $(lib_so) $(lib_a) + +$(lib_so): $(obj) + $(CC) -o $@ $(shared) $(obj) $(LDFLAGS) + +$(lib_a): $(obj) + $(AR) rcs $@ $(obj) + +-include $(dep) + +%.d: %.c + @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ + +.PHONY: clean +clean: + rm -f $(obj) $(lib_so) $(lib_a) + +.PHONY: install +install: $(lib_so) $(lib_a) + mkdir -p $(INSTDIR)$(PREFIX)/lib $(INSTDIR)$(PREFIX)/include + cp $(lib_a) $(INSTDIR)$(PREFIX)/lib/$(lib_a) + cp $(lib_so) $(INSTDIR)$(PREFIX)/lib/$(lib_so) + [ -n "$(soname)" ] && cd $(INSTDIR)$(PREFIX)/lib \ + && ln -s $(lib_so) $(soname) \ + && ln -s $(soname) $(devlink) \ + || true + +.PHONY: uninstall +uninstall: + rm -f $(INSTDIR)$(PREFIX)/lib/$(lib_a) $(INSTDIR)$(PREFIX)/lib/$(lib_so) + [ -n "$(soname)" ] \ + && rm -f $(INSTDIR)$(PREFIX)/lib/$(soname) \ + && rm -f $(INSTDIR)$(PREFIX)/lib/$(devlink) \ + || true diff -r 000000000000 -r 8c6d64af9505 src/mesh.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mesh.c Fri Jan 13 09:34:16 2012 +0200 @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include "mesh.h" + +int mattr_init(struct mesh_attrib *ma) +{ + memset(ma, 0, sizeof *ma); + return 0; +} + +void mattr_destroy(struct mesh_attrib *ma) +{ + if(ma) { + free(ma->name); + free(ma->data); + } +} + +int mattr_set_name(struct mesh_attrib *ma, const char *name) +{ + char *tmp; + + if(!(tmp = malloc(strlen(name) + 1))) { + return -1; + } + strcpy(tmp, name); + + free(ma->name); + ma->name = tmp; + return 0; +} + +#define INITSZ (16 * ma->elem_size) +int mattr_add_elem(struct mesh_attrib *ma, void *data) +{ + int nsz = (ma->count + 1) * ma->elem_size; + + if(nsz > ma->datasz) { + void *tmp; + + nsz = ma->datasz ? ma->datasz * 2 : INITSZ; + + if(!(tmp = realloc(ma->data, nsz))) { + return -1; + } + ma->data = tmp; + ma->datasz = nsz; + } + + memcpy((char*)ma->data + ma->elem_size * ma->count++, data, ma->elem_size); + return 0; +} + +/* -------- mesh -------- */ + +int mesh_init(struct mesh *m) +{ + memset(m, 0, sizeof *m); + return 0; +} + +void mesh_destroy(struct mesh *m) +{ + int i; + + free(m->name); + + for(i=0; inum_attr; i++) { + mattr_destroy(&m->attr[i]); + } + free(m->attr); + + for(i=0; inum_attr; i++) { + free(m->polyidx[i]); + } + free(m->polyidx); +} + +int mesh_set_name(struct mesh *m, const char *name) +{ + char *tmp; + + if(!(tmp = malloc(strlen(name) + 1))) { + return -1; + } + strcpy(tmp, name); + + free(m->name); + m->name = tmp; + return 0; +} + +int mesh_add_attrib(struct mesh *m, struct mesh_attrib *attr) +{ + void *tmp; + int idx = m->num_attr++; + + if(!(tmp = realloc(m->attr, m->num_attr * sizeof *attr))) { + return -1; + } + m->attr = tmp; + m->attr[idx] = *attr; + + if(!(tmp = realloc(m->polyidx, m->num_attr * sizeof *m->polyidx))) { + m->num_attr--; + return -1; + } + m->polyidx[idx] = 0; + + return 0; +} + +int mesh_find_attrib(struct mesh *m, const char *name) +{ + int i; + + for(i=0; inum_attr; i++) { + if(strcmp(m->attr[i].name, name) == 0) { + return i; + } + } + return -1; +} diff -r 000000000000 -r 8c6d64af9505 src/mesh.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mesh.h Fri Jan 13 09:34:16 2012 +0200 @@ -0,0 +1,44 @@ +#ifndef MESH_H_ +#define MESH_H_ + +struct mesh_attrib { + char *name; + int count, elem_size; + void *data; + + int datasz; +}; + +struct mesh { + char *name; + int poly_nverts; /* 3, 4, etc */ + + struct mesh_attrib *attr; + int num_attr; + + int **polyidx; + int num_poly; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +int mattr_init(struct mesh_attrib *ma); +void mattr_destroy(struct mesh_attrib *ma); + +int mattr_set_name(struct mesh_attrib *ma, const char *name); +int mattr_add_elem(struct mesh_attrib *ma, void *data); + +int mesh_init(struct mesh *m); +void mesh_destroy(struct mesh *m); + +int mesh_set_name(struct mesh *m, const char *name); +int mesh_add_attrib(struct mesh *m, struct mesh_attrib *attr); +int mesh_find_attrib(struct mesh *m, const char *name); + +#ifdef __cplusplus +} +#endif + +#endif /* MESH_H_ */