libresman

view examples/imgthumbs/src/thumbs.c @ 12:84f55eab27cb

ok now it sortof works, probably something is failing in the done callback mechanism
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 08 Feb 2014 04:21:08 +0200
parents bebc065a941f
children a42888d26839
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 #ifndef GL_COMPRESSED_RGB
13 #define GL_COMPRESSED_RGB 0x84ed
14 #endif
16 struct resman *texman;
18 static int load_res_texture(const char *fname, int id, void *cls);
19 static int done_res_texture(int id, void *cls);
20 static void free_res_texture(int id, void *cls);
23 struct thumbnail *create_thumbs(const char *dirpath)
24 {
25 DIR *dir;
26 struct dirent *dent;
27 struct thumbnail *list = 0;
29 /*unsigned int intfmt = GL_COMPRESSED_RGB;
30 if(!strstr((char*)glGetString(GL_EXTENSIONS), "GL_ARB_texture_compression")) {
31 printf("warning, no texture compression available.\n");
32 intfmt = GL_RGB;
33 }*/
35 if(!texman) {
36 texman = resman_create();
37 resman_set_load_func(texman, load_res_texture, 0);
38 resman_set_done_func(texman, done_res_texture, 0);
39 resman_set_destroy_func(texman, free_res_texture, 0);
40 }
42 if(!(dir = opendir(dirpath))) {
43 fprintf(stderr, "failed to open directory: %s: %s\n", dirpath, strerror(errno));
44 return 0;
45 }
47 while((dent = readdir(dir))) {
48 /*int xsz, ysz;
49 unsigned char *pixels;*/
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;/*(float)xsz / (float)ysz;*/
70 resman_lookup(texman, node->fname, node);
72 /*if(!(pixels = img_load_pixels(node->fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
73 free(node->fname);
74 free(node);
75 continue;
76 }
78 printf("loaded image: %s\n", node->fname);
80 glGenTextures(1, &node->tex);
81 glBindTexture(GL_TEXTURE_2D, node->tex);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
84 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
85 img_free_pixels(pixels);
86 */
88 node->next = list;
89 list = node;
90 }
91 closedir(dir);
93 return list;
94 }
96 void free_thumbs(struct thumbnail *thumbs)
97 {
98 if(!thumbs) return;
100 while(thumbs) {
101 struct thumbnail *tmp = thumbs;
102 thumbs = thumbs->next;
104 free(tmp->fname);
105 free(tmp);
106 }
107 }
110 void update_thumbs(void)
111 {
112 resman_poll(texman);
113 }
115 void draw_thumbs(struct thumbnail *thumbs, float thumb_sz, float start_y)
116 {
117 int vp[4];
118 float gap = thumb_sz / 4.0;
119 float x = gap;
120 float y = gap + start_y;
121 float view_aspect;
123 glGetIntegerv(GL_VIEWPORT, vp);
124 view_aspect = (float)(vp[2] - vp[0]) / (vp[3] - vp[1]);
126 glMatrixMode(GL_PROJECTION);
127 glPushMatrix();
128 glLoadIdentity();
129 glOrtho(0, 1, 1.0 / view_aspect, 0, -1, 1);
131 glMatrixMode(GL_MODELVIEW);
133 while(thumbs) {
134 glPushMatrix();
135 glTranslatef(x, y, 0);
137 glScalef(thumb_sz, thumb_sz, 1);
139 glBegin(GL_QUADS);
140 glColor3f(0.25, 0.25, 0.25);
141 glTexCoord2f(0, 0); glVertex2f(0, 0);
142 glTexCoord2f(1, 0); glVertex2f(1, 0);
143 glTexCoord2f(1, 1); glVertex2f(1, 1);
144 glTexCoord2f(0, 1); glVertex2f(0, 1);
145 glEnd();
147 if(thumbs->tex) {
148 if(thumbs->aspect >= 1.0) {
149 glTranslatef(0, 0.5 - 0.5 / thumbs->aspect, 0);
150 glScalef(1, 1.0 / thumbs->aspect, 1);
151 } else {
152 glTranslatef(0.5 - thumbs->aspect / 2.0, 0, 0);
153 glScalef(thumbs->aspect, 1, 1);
154 }
156 if(glIsTexture(thumbs->tex)) {
157 glEnable(GL_TEXTURE_2D);
158 glBindTexture(GL_TEXTURE_2D, thumbs->tex);
160 glBegin(GL_QUADS);
161 glColor3f(1, 1, 1);
162 glTexCoord2f(0, 0); glVertex2f(0, 0);
163 glTexCoord2f(1, 0); glVertex2f(1, 0);
164 glTexCoord2f(1, 1); glVertex2f(1, 1);
165 glTexCoord2f(0, 1); glVertex2f(0, 1);
166 glEnd();
167 } else {
168 fprintf(stderr, "invalid texture: %u\n", thumbs->tex);
169 }
171 glPopMatrix();
172 glDisable(GL_TEXTURE_2D);
173 }
175 thumbs->layout_pos[0] = x;
176 thumbs->layout_pos[1] = y;
177 thumbs->layout_size[0] = thumb_sz;
178 thumbs->layout_size[1] = thumb_sz;
180 x += thumb_sz + gap;
181 if(x >= 1.0 - thumb_sz) {
182 x = gap;
183 y += thumb_sz + gap;
184 }
186 thumbs = thumbs->next;
187 }
189 glMatrixMode(GL_PROJECTION);
190 glPopMatrix();
191 glMatrixMode(GL_MODELVIEW);
192 }
194 static int load_res_texture(const char *fname, int id, void *cls)
195 {
196 struct thumbnail *rdata = resman_get_res_data(texman, id);
198 assert(rdata);
199 if(!rdata->img) {
200 if(!(rdata->img = img_create())) {
201 return -1;
202 }
203 }
205 if(img_load(rdata->img, fname) == -1) {
206 img_free(rdata->img);
207 return -1;
208 }
209 rdata->aspect = (float)rdata->img->width / (float)rdata->img->height;
211 /* set the resource's data to the loaded image, so that we can use
212 * it in the done callback */
213 resman_set_res_data(texman, id, rdata);
214 return 0;
215 }
217 static int done_res_texture(int id, void *cls)
218 {
219 struct thumbnail *rdata;
221 rdata = resman_get_res_data(texman, id);
223 if(resman_get_res_result(texman, id) != 0 || !rdata) {
224 fprintf(stderr, "failed to load resource %d (%s)\n", id, resman_get_res_name(texman, id));
225 } else {
226 printf("done loading resource %d (%s)\n", id, resman_get_res_name(texman, id));
227 }
229 if(!rdata->tex) {
230 glGenTextures(1, &rdata->tex);
231 }
232 glBindTexture(GL_TEXTURE_2D, rdata->tex);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
235 glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(rdata->img),
236 rdata->img->width, rdata->img->height, 0, img_glfmt(rdata->img),
237 img_gltype(rdata->img), rdata->img->pixels);
238 return 0;
239 }
241 static void free_res_texture(int id, void *cls)
242 {
243 struct thumbnail *rdata = resman_get_res_data(texman, id);
245 if(rdata) {
246 if(rdata->tex) {
247 glDeleteTextures(1, &rdata->tex);
248 }
249 if(rdata->img) {
250 img_free(rdata->img);
251 }
252 }
253 }