9 static struct rbtree *rb;
11 static size_t io_read(void *buf, size_t bytes, void *uptr)
13 return ass_fread(buf, 1, bytes, uptr);
16 static long io_seek(long offs, int whence, void *uptr)
18 return ass_fseek(uptr, offs, whence);
21 unsigned int get_tex2d(const char *fname)
24 struct img_pixmap pixmap;
27 if((id = lookup_asset(fname))) {
31 if(!(io.uptr = ass_fopen(fname, "rb"))) {
32 fprintf(stderr, "failed to open image: %s\n", fname);
40 if(img_read(&pixmap, &io) == -1) {
41 fprintf(stderr, "failed to read image file: %s\n", fname);
47 if(!(id = img_gltexture(&pixmap))) {
48 fprintf(stderr, "failed to create OpenGL texture from: %s\n", fname);
52 if(id) add_asset(fname, id);
56 unsigned int get_texcube(const char *fname)
61 unsigned int get_vsdr(const char *fname)
68 if((sdr = lookup_asset(fname))) {
72 if(!(fp = ass_fopen(fname, "rb"))) {
73 fprintf(stderr, "failed to load vertex shader: %s\n", fname);
76 ass_fseek(fp, 0, SEEK_END);
78 ass_fseek(fp, 0, SEEK_SET);
80 buf = malloc_nf(sz + 1);
81 if(ass_fread(buf, 1, sz, fp) < sz) {
82 fprintf(stderr, "failed to read vertex shader: %s\n", fname);
90 printf("vertex shader %s ", fname);
92 sdr = create_vertex_shader(buf);
95 if(sdr) add_asset(fname, sdr);
99 unsigned int get_psdr(const char *fname)
106 if((sdr = lookup_asset(fname))) {
110 if(!(fp = ass_fopen(fname, "rb"))) {
111 fprintf(stderr, "failed to load vertex shader: %s\n", fname);
114 ass_fseek(fp, 0, SEEK_END);
116 ass_fseek(fp, 0, SEEK_SET);
118 buf = malloc_nf(sz + 1);
119 if(ass_fread(buf, 1, sz, fp) < sz) {
120 fprintf(stderr, "failed to read vertex shader: %s\n", fname);
128 printf("pixel shader %s ", fname);
130 sdr = create_pixel_shader(buf);
133 if(sdr) add_asset(fname, sdr);
137 unsigned int get_sdrprog(const char *vfname, const char *pfname)
139 unsigned int vsdr, psdr;
141 if(!(vsdr = get_vsdr(vfname)) || !(psdr = get_psdr(pfname))) {
144 /* TODO: when you don't feel lazy, manage this one too */
145 return create_program_link(vsdr, psdr, 0);
148 static int init_once(void)
150 if(!rb && !(rb = rb_create(RB_KEY_STRING))) {
156 int add_asset(const char *name, unsigned int id)
158 if(init_once() == -1) return -1;
160 if(rb_insert(rb, (char*)name, (void*)(uintptr_t)id) == -1) {
166 unsigned int lookup_asset(const char *name)
170 if(init_once() == -1) return -1;
172 if(!(n = rb_find(rb, (char*)name))) {
175 return (unsigned int)(uintptr_t)n->data;