rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@1
|
2 #include <stdlib.h>
|
nuclear@1
|
3 #include <string.h>
|
nuclear@1
|
4 #include <errno.h>
|
nuclear@11
|
5 #include <assert.h>
|
nuclear@1
|
6 #include <dirent.h>
|
nuclear@1
|
7 #include <imago2.h>
|
nuclear@1
|
8 #include "opengl.h"
|
nuclear@1
|
9 #include "thumbs.h"
|
nuclear@11
|
10 #include "resman.h"
|
nuclear@1
|
11
|
nuclear@16
|
12 #define DBG_SYNC
|
nuclear@16
|
13
|
nuclear@6
|
14 #ifndef GL_COMPRESSED_RGB
|
nuclear@6
|
15 #define GL_COMPRESSED_RGB 0x84ed
|
nuclear@6
|
16 #endif
|
nuclear@6
|
17
|
nuclear@11
|
18 struct resman *texman;
|
nuclear@14
|
19 struct thumbnail *dbg;
|
nuclear@11
|
20
|
nuclear@11
|
21 static int load_res_texture(const char *fname, int id, void *cls);
|
nuclear@11
|
22 static int done_res_texture(int id, void *cls);
|
nuclear@11
|
23 static void free_res_texture(int id, void *cls);
|
nuclear@11
|
24
|
nuclear@11
|
25
|
nuclear@1
|
26 struct thumbnail *create_thumbs(const char *dirpath)
|
nuclear@1
|
27 {
|
nuclear@1
|
28 DIR *dir;
|
nuclear@1
|
29 struct dirent *dent;
|
nuclear@13
|
30 /* allocate dummy head node */
|
nuclear@13
|
31 struct thumbnail *list = calloc(1, sizeof *list);
|
nuclear@14
|
32 dbg = list;
|
nuclear@1
|
33
|
nuclear@11
|
34 if(!texman) {
|
nuclear@11
|
35 texman = resman_create();
|
nuclear@11
|
36 resman_set_load_func(texman, load_res_texture, 0);
|
nuclear@11
|
37 resman_set_done_func(texman, done_res_texture, 0);
|
nuclear@11
|
38 resman_set_destroy_func(texman, free_res_texture, 0);
|
nuclear@2
|
39 }
|
nuclear@2
|
40
|
nuclear@1
|
41 if(!(dir = opendir(dirpath))) {
|
nuclear@1
|
42 fprintf(stderr, "failed to open directory: %s: %s\n", dirpath, strerror(errno));
|
nuclear@1
|
43 return 0;
|
nuclear@1
|
44 }
|
nuclear@1
|
45
|
nuclear@1
|
46 while((dent = readdir(dir))) {
|
nuclear@16
|
47 #ifdef DBG_SYNC
|
nuclear@16
|
48 int xsz, ysz;
|
nuclear@16
|
49 unsigned char *pixels;
|
nuclear@16
|
50 #endif
|
nuclear@1
|
51 struct thumbnail *node;
|
nuclear@1
|
52
|
nuclear@1
|
53 if(!(node = malloc(sizeof *node))) {
|
nuclear@1
|
54 perror("failed to allocate thumbnail list node");
|
nuclear@1
|
55 continue;
|
nuclear@1
|
56 }
|
nuclear@11
|
57 memset(node, 0, sizeof *node);
|
nuclear@1
|
58
|
nuclear@1
|
59 if(!(node->fname = malloc(strlen(dirpath) + strlen(dent->d_name) + 2))) {
|
nuclear@1
|
60 free(node);
|
nuclear@1
|
61 continue;
|
nuclear@1
|
62 }
|
nuclear@1
|
63 strcpy(node->fname, dirpath);
|
nuclear@1
|
64 if(dirpath[strlen(dirpath) - 1] != '/') {
|
nuclear@1
|
65 strcat(node->fname, "/");
|
nuclear@1
|
66 }
|
nuclear@1
|
67 strcat(node->fname, dent->d_name);
|
nuclear@1
|
68
|
nuclear@16
|
69 node->aspect = 1.0;
|
nuclear@11
|
70
|
nuclear@16
|
71 #ifndef DBG_SYNC
|
nuclear@12
|
72 resman_lookup(texman, node->fname, node);
|
nuclear@16
|
73 #else
|
nuclear@16
|
74 if(!(pixels = img_load_pixels(node->fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
|
nuclear@1
|
75 free(node->fname);
|
nuclear@1
|
76 free(node);
|
nuclear@1
|
77 continue;
|
nuclear@1
|
78 }
|
nuclear@1
|
79
|
nuclear@1
|
80 printf("loaded image: %s\n", node->fname);
|
nuclear@1
|
81
|
nuclear@1
|
82 glGenTextures(1, &node->tex);
|
nuclear@1
|
83 glBindTexture(GL_TEXTURE_2D, node->tex);
|
nuclear@1
|
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@1
|
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@16
|
86 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
nuclear@1
|
87 img_free_pixels(pixels);
|
nuclear@16
|
88
|
nuclear@16
|
89 node->aspect = (float)xsz / (float)ysz;
|
nuclear@16
|
90 #endif
|
nuclear@1
|
91
|
nuclear@13
|
92 node->next = list->next;
|
nuclear@13
|
93 node->prev = list;
|
nuclear@14
|
94 list->next = node;
|
nuclear@1
|
95 }
|
nuclear@1
|
96 closedir(dir);
|
nuclear@1
|
97
|
nuclear@1
|
98 return list;
|
nuclear@1
|
99 }
|
nuclear@1
|
100
|
nuclear@1
|
101 void free_thumbs(struct thumbnail *thumbs)
|
nuclear@1
|
102 {
|
nuclear@1
|
103 if(!thumbs) return;
|
nuclear@1
|
104
|
nuclear@1
|
105 while(thumbs) {
|
nuclear@1
|
106 struct thumbnail *tmp = thumbs;
|
nuclear@1
|
107 thumbs = thumbs->next;
|
nuclear@1
|
108
|
nuclear@1
|
109 free(tmp->fname);
|
nuclear@1
|
110 free(tmp);
|
nuclear@1
|
111 }
|
nuclear@1
|
112 }
|
nuclear@1
|
113
|
nuclear@1
|
114
|
nuclear@11
|
115 void update_thumbs(void)
|
nuclear@11
|
116 {
|
nuclear@11
|
117 resman_poll(texman);
|
nuclear@11
|
118 }
|
nuclear@11
|
119
|
nuclear@1
|
120 void draw_thumbs(struct thumbnail *thumbs, float thumb_sz, float start_y)
|
nuclear@1
|
121 {
|
nuclear@1
|
122 int vp[4];
|
nuclear@1
|
123 float gap = thumb_sz / 4.0;
|
nuclear@1
|
124 float x = gap;
|
nuclear@1
|
125 float y = gap + start_y;
|
nuclear@1
|
126 float view_aspect;
|
nuclear@1
|
127
|
nuclear@1
|
128 glGetIntegerv(GL_VIEWPORT, vp);
|
nuclear@14
|
129 view_aspect = (float)(vp[2] - vp[0]) / (float)(vp[3] - vp[1]);
|
nuclear@1
|
130
|
nuclear@1
|
131 glMatrixMode(GL_PROJECTION);
|
nuclear@1
|
132 glPushMatrix();
|
nuclear@1
|
133 glLoadIdentity();
|
nuclear@1
|
134 glOrtho(0, 1, 1.0 / view_aspect, 0, -1, 1);
|
nuclear@1
|
135
|
nuclear@1
|
136 glMatrixMode(GL_MODELVIEW);
|
nuclear@1
|
137
|
nuclear@13
|
138 thumbs = thumbs->next; /* skip dummy node */
|
nuclear@1
|
139 while(thumbs) {
|
nuclear@14
|
140 printf("drawing thumb: %s\n", thumbs->fname);
|
nuclear@1
|
141 glPushMatrix();
|
nuclear@1
|
142 glTranslatef(x, y, 0);
|
nuclear@1
|
143
|
nuclear@1
|
144 glScalef(thumb_sz, thumb_sz, 1);
|
nuclear@1
|
145
|
nuclear@1
|
146 glBegin(GL_QUADS);
|
nuclear@1
|
147 glColor3f(0.25, 0.25, 0.25);
|
nuclear@1
|
148 glTexCoord2f(0, 0); glVertex2f(0, 0);
|
nuclear@1
|
149 glTexCoord2f(1, 0); glVertex2f(1, 0);
|
nuclear@1
|
150 glTexCoord2f(1, 1); glVertex2f(1, 1);
|
nuclear@1
|
151 glTexCoord2f(0, 1); glVertex2f(0, 1);
|
nuclear@1
|
152 glEnd();
|
nuclear@1
|
153
|
nuclear@12
|
154 if(thumbs->tex) {
|
nuclear@12
|
155 if(thumbs->aspect >= 1.0) {
|
nuclear@12
|
156 glTranslatef(0, 0.5 - 0.5 / thumbs->aspect, 0);
|
nuclear@12
|
157 glScalef(1, 1.0 / thumbs->aspect, 1);
|
nuclear@12
|
158 } else {
|
nuclear@12
|
159 glTranslatef(0.5 - thumbs->aspect / 2.0, 0, 0);
|
nuclear@12
|
160 glScalef(thumbs->aspect, 1, 1);
|
nuclear@12
|
161 }
|
nuclear@12
|
162
|
nuclear@12
|
163 if(glIsTexture(thumbs->tex)) {
|
nuclear@12
|
164 glEnable(GL_TEXTURE_2D);
|
nuclear@12
|
165 glBindTexture(GL_TEXTURE_2D, thumbs->tex);
|
nuclear@12
|
166
|
nuclear@12
|
167 glBegin(GL_QUADS);
|
nuclear@12
|
168 glColor3f(1, 1, 1);
|
nuclear@12
|
169 glTexCoord2f(0, 0); glVertex2f(0, 0);
|
nuclear@12
|
170 glTexCoord2f(1, 0); glVertex2f(1, 0);
|
nuclear@12
|
171 glTexCoord2f(1, 1); glVertex2f(1, 1);
|
nuclear@12
|
172 glTexCoord2f(0, 1); glVertex2f(0, 1);
|
nuclear@12
|
173 glEnd();
|
nuclear@12
|
174 } else {
|
nuclear@12
|
175 fprintf(stderr, "invalid texture: %u\n", thumbs->tex);
|
nuclear@12
|
176 }
|
nuclear@12
|
177
|
nuclear@12
|
178 glPopMatrix();
|
nuclear@12
|
179 glDisable(GL_TEXTURE_2D);
|
nuclear@1
|
180 }
|
nuclear@1
|
181
|
nuclear@1
|
182 thumbs->layout_pos[0] = x;
|
nuclear@1
|
183 thumbs->layout_pos[1] = y;
|
nuclear@1
|
184 thumbs->layout_size[0] = thumb_sz;
|
nuclear@1
|
185 thumbs->layout_size[1] = thumb_sz;
|
nuclear@1
|
186
|
nuclear@1
|
187 x += thumb_sz + gap;
|
nuclear@1
|
188 if(x >= 1.0 - thumb_sz) {
|
nuclear@1
|
189 x = gap;
|
nuclear@1
|
190 y += thumb_sz + gap;
|
nuclear@1
|
191 }
|
nuclear@1
|
192
|
nuclear@1
|
193 thumbs = thumbs->next;
|
nuclear@1
|
194 }
|
nuclear@1
|
195
|
nuclear@1
|
196 glMatrixMode(GL_PROJECTION);
|
nuclear@1
|
197 glPopMatrix();
|
nuclear@1
|
198 glMatrixMode(GL_MODELVIEW);
|
nuclear@1
|
199 }
|
nuclear@11
|
200
|
nuclear@11
|
201 static int load_res_texture(const char *fname, int id, void *cls)
|
nuclear@11
|
202 {
|
nuclear@12
|
203 struct thumbnail *rdata = resman_get_res_data(texman, id);
|
nuclear@11
|
204
|
nuclear@11
|
205 assert(rdata);
|
nuclear@11
|
206 if(!rdata->img) {
|
nuclear@11
|
207 if(!(rdata->img = img_create())) {
|
nuclear@11
|
208 return -1;
|
nuclear@11
|
209 }
|
nuclear@11
|
210 }
|
nuclear@11
|
211
|
nuclear@12
|
212 if(img_load(rdata->img, fname) == -1) {
|
nuclear@11
|
213 img_free(rdata->img);
|
nuclear@13
|
214 rdata->img = 0;
|
nuclear@11
|
215 return -1;
|
nuclear@11
|
216 }
|
nuclear@11
|
217 rdata->aspect = (float)rdata->img->width / (float)rdata->img->height;
|
nuclear@11
|
218
|
nuclear@11
|
219 /* set the resource's data to the loaded image, so that we can use
|
nuclear@11
|
220 * it in the done callback */
|
nuclear@12
|
221 resman_set_res_data(texman, id, rdata);
|
nuclear@11
|
222 return 0;
|
nuclear@11
|
223 }
|
nuclear@11
|
224
|
nuclear@11
|
225 static int done_res_texture(int id, void *cls)
|
nuclear@11
|
226 {
|
nuclear@13
|
227 struct thumbnail *rdata = resman_get_res_data(texman, id);
|
nuclear@13
|
228 int load_result = resman_get_res_result(texman, id);
|
nuclear@11
|
229
|
nuclear@13
|
230 if(load_result == -1) {
|
nuclear@13
|
231 /* returning -1 will remove this resource, the free_res_texture
|
nuclear@13
|
232 * destroy handler will be called, which will remove the node
|
nuclear@13
|
233 * from the list
|
nuclear@13
|
234 */
|
nuclear@13
|
235 return -1;
|
nuclear@13
|
236 }
|
nuclear@11
|
237
|
nuclear@12
|
238 if(resman_get_res_result(texman, id) != 0 || !rdata) {
|
nuclear@12
|
239 fprintf(stderr, "failed to load resource %d (%s)\n", id, resman_get_res_name(texman, id));
|
nuclear@11
|
240 } else {
|
nuclear@12
|
241 printf("done loading resource %d (%s)\n", id, resman_get_res_name(texman, id));
|
nuclear@11
|
242 }
|
nuclear@11
|
243
|
nuclear@11
|
244 if(!rdata->tex) {
|
nuclear@11
|
245 glGenTextures(1, &rdata->tex);
|
nuclear@11
|
246 }
|
nuclear@11
|
247 glBindTexture(GL_TEXTURE_2D, rdata->tex);
|
nuclear@11
|
248 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@11
|
249 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@11
|
250 glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(rdata->img),
|
nuclear@11
|
251 rdata->img->width, rdata->img->height, 0, img_glfmt(rdata->img),
|
nuclear@11
|
252 img_gltype(rdata->img), rdata->img->pixels);
|
nuclear@11
|
253 return 0;
|
nuclear@11
|
254 }
|
nuclear@11
|
255
|
nuclear@11
|
256 static void free_res_texture(int id, void *cls)
|
nuclear@11
|
257 {
|
nuclear@13
|
258 struct thumbnail *thumb = resman_get_res_data(texman, id);
|
nuclear@11
|
259
|
nuclear@14
|
260 printf("deleting thumb %d: %s\n", id, thumb->fname);
|
nuclear@14
|
261
|
nuclear@13
|
262 if(thumb) {
|
nuclear@13
|
263 if(thumb->tex) {
|
nuclear@13
|
264 glDeleteTextures(1, &thumb->tex);
|
nuclear@11
|
265 }
|
nuclear@13
|
266 if(thumb->img) {
|
nuclear@13
|
267 img_free(thumb->img);
|
nuclear@11
|
268 }
|
nuclear@11
|
269 }
|
nuclear@13
|
270
|
nuclear@13
|
271 /* remove from the list */
|
nuclear@13
|
272 if(thumb->prev) {
|
nuclear@13
|
273 thumb->prev->next = thumb->next;
|
nuclear@13
|
274 }
|
nuclear@13
|
275 if(thumb->next) {
|
nuclear@13
|
276 thumb->next->prev = thumb->prev;
|
nuclear@13
|
277 }
|
nuclear@13
|
278 free(thumb);
|
nuclear@11
|
279 }
|