libresman

view examples/imgthumbs/src/thumbs.c @ 18:711698580eb0

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