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