libresman
diff examples/imgthumbs/src/thumbs.c @ 11:bebc065a941f
doesn't work yet
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 07 Feb 2014 07:50:02 +0200 |
parents | 410c19c735b2 |
children | 84f55eab27cb |
line diff
1.1 --- a/examples/imgthumbs/src/thumbs.c Wed Feb 05 02:01:49 2014 +0200 1.2 +++ b/examples/imgthumbs/src/thumbs.c Fri Feb 07 07:50:02 2014 +0200 1.3 @@ -2,25 +2,41 @@ 1.4 #include <stdlib.h> 1.5 #include <string.h> 1.6 #include <errno.h> 1.7 +#include <assert.h> 1.8 #include <dirent.h> 1.9 #include <imago2.h> 1.10 #include "opengl.h" 1.11 #include "thumbs.h" 1.12 +#include "resman.h" 1.13 1.14 #ifndef GL_COMPRESSED_RGB 1.15 #define GL_COMPRESSED_RGB 0x84ed 1.16 #endif 1.17 1.18 +struct resman *texman; 1.19 + 1.20 +static int load_res_texture(const char *fname, int id, void *cls); 1.21 +static int done_res_texture(int id, void *cls); 1.22 +static void free_res_texture(int id, void *cls); 1.23 + 1.24 + 1.25 struct thumbnail *create_thumbs(const char *dirpath) 1.26 { 1.27 DIR *dir; 1.28 struct dirent *dent; 1.29 struct thumbnail *list = 0; 1.30 1.31 - unsigned int intfmt = GL_COMPRESSED_RGB; 1.32 - if(!strstr(glGetString(GL_EXTENSIONS), "GL_ARB_texture_compression")) { 1.33 + /*unsigned int intfmt = GL_COMPRESSED_RGB; 1.34 + if(!strstr((char*)glGetString(GL_EXTENSIONS), "GL_ARB_texture_compression")) { 1.35 printf("warning, no texture compression available.\n"); 1.36 intfmt = GL_RGB; 1.37 + }*/ 1.38 + 1.39 + if(!texman) { 1.40 + texman = resman_create(); 1.41 + resman_set_load_func(texman, load_res_texture, 0); 1.42 + resman_set_done_func(texman, done_res_texture, 0); 1.43 + resman_set_destroy_func(texman, free_res_texture, 0); 1.44 } 1.45 1.46 if(!(dir = opendir(dirpath))) { 1.47 @@ -29,14 +45,15 @@ 1.48 } 1.49 1.50 while((dent = readdir(dir))) { 1.51 - int xsz, ysz; 1.52 - unsigned char *pixels; 1.53 + /*int xsz, ysz; 1.54 + unsigned char *pixels;*/ 1.55 struct thumbnail *node; 1.56 1.57 if(!(node = malloc(sizeof *node))) { 1.58 perror("failed to allocate thumbnail list node"); 1.59 continue; 1.60 } 1.61 + memset(node, 0, sizeof *node); 1.62 1.63 if(!(node->fname = malloc(strlen(dirpath) + strlen(dent->d_name) + 2))) { 1.64 free(node); 1.65 @@ -48,7 +65,11 @@ 1.66 } 1.67 strcat(node->fname, dent->d_name); 1.68 1.69 - if(!(pixels = img_load_pixels(node->fname, &xsz, &ysz, IMG_FMT_RGBA32))) { 1.70 + node->aspect = 1.0;/*(float)xsz / (float)ysz;*/ 1.71 + 1.72 + resman_lookup(texman, node->fname, 0); 1.73 + 1.74 + /*if(!(pixels = img_load_pixels(node->fname, &xsz, &ysz, IMG_FMT_RGBA32))) { 1.75 free(node->fname); 1.76 free(node); 1.77 continue; 1.78 @@ -62,8 +83,7 @@ 1.79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.80 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 1.81 img_free_pixels(pixels); 1.82 - 1.83 - node->aspect = (float)xsz / (float)ysz; 1.84 + */ 1.85 1.86 node->next = list; 1.87 list = node; 1.88 @@ -87,6 +107,11 @@ 1.89 } 1.90 1.91 1.92 +void update_thumbs(void) 1.93 +{ 1.94 + resman_poll(texman); 1.95 +} 1.96 + 1.97 void draw_thumbs(struct thumbnail *thumbs, float thumb_sz, float start_y) 1.98 { 1.99 int vp[4]; 1.100 @@ -106,6 +131,10 @@ 1.101 glMatrixMode(GL_MODELVIEW); 1.102 1.103 while(thumbs) { 1.104 + if(!thumbs->tex) { 1.105 + thumbs = thumbs->next; 1.106 + continue; 1.107 + } 1.108 1.109 glPushMatrix(); 1.110 glTranslatef(x, y, 0); 1.111 @@ -160,3 +189,67 @@ 1.112 glPopMatrix(); 1.113 glMatrixMode(GL_MODELVIEW); 1.114 } 1.115 + 1.116 +static int load_res_texture(const char *fname, int id, void *cls) 1.117 +{ 1.118 + struct resman *rman = cls; 1.119 + struct thumbnail *rdata = resman_get_res_data(rman, id); 1.120 + 1.121 + assert(rdata); 1.122 + if(!rdata->img) { 1.123 + if(!(rdata->img = img_create())) { 1.124 + return -1; 1.125 + } 1.126 + } 1.127 + 1.128 + if(!img_load(rdata->img, fname) == -1) { 1.129 + img_free(rdata->img); 1.130 + return -1; 1.131 + } 1.132 + rdata->aspect = (float)rdata->img->width / (float)rdata->img->height; 1.133 + 1.134 + /* set the resource's data to the loaded image, so that we can use 1.135 + * it in the done callback */ 1.136 + resman_set_res_data(rman, id, rdata); 1.137 + return 0; 1.138 +} 1.139 + 1.140 +static int done_res_texture(int id, void *cls) 1.141 +{ 1.142 + struct thumbnail *rdata; 1.143 + struct resman *rman = cls; 1.144 + 1.145 + rdata = resman_get_res_data(rman, id); 1.146 + 1.147 + if(resman_get_res_result(rman, id) != 0 || !rdata) { 1.148 + fprintf(stderr, "failed to load resource %d (%s)\n", id, resman_get_res_name(rman, id)); 1.149 + } else { 1.150 + printf("done loading resource %d (%s)\n", id, resman_get_res_name(rman, id)); 1.151 + } 1.152 + 1.153 + if(!rdata->tex) { 1.154 + glGenTextures(1, &rdata->tex); 1.155 + } 1.156 + glBindTexture(GL_TEXTURE_2D, rdata->tex); 1.157 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.158 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.159 + glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(rdata->img), 1.160 + rdata->img->width, rdata->img->height, 0, img_glfmt(rdata->img), 1.161 + img_gltype(rdata->img), rdata->img->pixels); 1.162 + return 0; 1.163 +} 1.164 + 1.165 +static void free_res_texture(int id, void *cls) 1.166 +{ 1.167 + struct resman *rman = cls; 1.168 + struct thumbnail *rdata = resman_get_res_data(rman, id); 1.169 + 1.170 + if(rdata) { 1.171 + if(rdata->tex) { 1.172 + glDeleteTextures(1, &rdata->tex); 1.173 + } 1.174 + if(rdata->img) { 1.175 + img_free(rdata->img); 1.176 + } 1.177 + } 1.178 +}