updated readme
[andemo] / src / assman.c
1 #include "opengl.h"
2 #include "assman.h"
3 #include "sdr.h"
4 #include "imago2.h"
5 #include "assfile.h"
6 #include "rbtree.h"
7 #include "util.h"
8
9 static struct rbtree *rb;
10
11 static size_t io_read(void *buf, size_t bytes, void *uptr)
12 {
13         return ass_fread(buf, 1, bytes, uptr);
14 }
15
16 static long io_seek(long offs, int whence, void *uptr)
17 {
18         return ass_fseek(uptr, offs, whence);
19 }
20
21 unsigned int get_tex2d(const char *fname)
22 {
23         unsigned int id;
24         struct img_pixmap pixmap;
25         struct img_io io;
26
27         if((id = lookup_asset(fname))) {
28                 return id;
29         }
30
31         if(!(io.uptr = ass_fopen(fname, "rb"))) {
32                 fprintf(stderr, "failed to open image: %s\n", fname);
33                 return 0;
34         }
35         io.read = io_read;
36         io.write = 0;
37         io.seek = io_seek;
38
39         img_init(&pixmap);
40         if(img_read(&pixmap, &io) == -1) {
41                 fprintf(stderr, "failed to read image file: %s\n", fname);
42                 ass_fclose(io.uptr);
43                 return 0;
44         }
45         ass_fclose(io.uptr);
46
47         if(!(id = img_gltexture(&pixmap))) {
48                 fprintf(stderr, "failed to create OpenGL texture from: %s\n", fname);
49         }
50         img_destroy(&pixmap);
51
52         if(id) add_asset(fname, id);
53         return id;
54 }
55
56 unsigned int get_texcube(const char *fname)
57 {
58         return 0;       /* TODO */
59 }
60
61 unsigned int get_vsdr(const char *fname)
62 {
63         unsigned int sdr;
64         long sz;
65         char *buf;
66         struct assfile *fp;
67
68         if((sdr = lookup_asset(fname))) {
69                 return sdr;
70         }
71
72         if(!(fp = ass_fopen(fname, "rb"))) {
73                 fprintf(stderr, "failed to load vertex shader: %s\n", fname);
74                 return 0;
75         }
76         ass_fseek(fp, 0, SEEK_END);
77         sz = ass_ftell(fp);
78         ass_fseek(fp, 0, SEEK_SET);
79
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);
83                 free(buf);
84                 ass_fclose(fp);
85                 return 0;
86         }
87         buf[sz] = 0;
88         ass_fclose(fp);
89
90         printf("vertex shader %s ", fname);
91         fflush(stdout);
92         sdr = create_vertex_shader(buf);
93         free(buf);
94
95         if(sdr) add_asset(fname, sdr);
96         return sdr;
97 }
98
99 unsigned int get_psdr(const char *fname)
100 {
101         unsigned int sdr;
102         long sz;
103         char *buf;
104         struct assfile *fp;
105
106         if((sdr = lookup_asset(fname))) {
107                 return sdr;
108         }
109
110         if(!(fp = ass_fopen(fname, "rb"))) {
111                 fprintf(stderr, "failed to load vertex shader: %s\n", fname);
112                 return 0;
113         }
114         ass_fseek(fp, 0, SEEK_END);
115         sz = ass_ftell(fp);
116         ass_fseek(fp, 0, SEEK_SET);
117
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);
121                 free(buf);
122                 ass_fclose(fp);
123                 return 0;
124         }
125         buf[sz] = 0;
126         ass_fclose(fp);
127
128         printf("pixel shader %s ", fname);
129         fflush(stdout);
130         sdr = create_pixel_shader(buf);
131         free(buf);
132
133         if(sdr) add_asset(fname, sdr);
134         return sdr;
135 }
136
137 unsigned int get_sdrprog(const char *vfname, const char *pfname)
138 {
139         unsigned int vsdr, psdr;
140
141         if(!(vsdr = get_vsdr(vfname)) || !(psdr = get_psdr(pfname))) {
142                 return 0;
143         }
144         /* TODO: when you don't feel lazy, manage this one too */
145         return create_program_link(vsdr, psdr, 0);
146 }
147
148 static int init_once(void)
149 {
150         if(!rb && !(rb = rb_create(RB_KEY_STRING))) {
151                 return -1;
152         }
153         return 0;
154 }
155
156 int add_asset(const char *name, unsigned int id)
157 {
158         if(init_once() == -1) return -1;
159
160         if(rb_insert(rb, (char*)name, (void*)(uintptr_t)id) == -1) {
161                 return -1;
162         }
163         return 0;
164 }
165
166 unsigned int lookup_asset(const char *name)
167 {
168         struct rbnode *n;
169
170         if(init_once() == -1) return -1;
171
172         if(!(n = rb_find(rb, (char*)name))) {
173                 return 0;
174         }
175         return (unsigned int)(uintptr_t)n->data;
176 }