libresman
diff examples/imgthumbs/src/thumbs.c @ 13:a42888d26839
bit more progress...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 08 Feb 2014 07:41:39 +0200 |
parents | 84f55eab27cb |
children | 2fcd1fbb0d18 0a789208498d |
line diff
1.1 --- a/examples/imgthumbs/src/thumbs.c Sat Feb 08 04:21:08 2014 +0200 1.2 +++ b/examples/imgthumbs/src/thumbs.c Sat Feb 08 07:41:39 2014 +0200 1.3 @@ -24,7 +24,8 @@ 1.4 { 1.5 DIR *dir; 1.6 struct dirent *dent; 1.7 - struct thumbnail *list = 0; 1.8 + /* allocate dummy head node */ 1.9 + struct thumbnail *list = calloc(1, sizeof *list); 1.10 1.11 /*unsigned int intfmt = GL_COMPRESSED_RGB; 1.12 if(!strstr((char*)glGetString(GL_EXTENSIONS), "GL_ARB_texture_compression")) { 1.13 @@ -85,8 +86,8 @@ 1.14 img_free_pixels(pixels); 1.15 */ 1.16 1.17 - node->next = list; 1.18 - list = node; 1.19 + node->next = list->next; 1.20 + node->prev = list; 1.21 } 1.22 closedir(dir); 1.23 1.24 @@ -130,6 +131,7 @@ 1.25 1.26 glMatrixMode(GL_MODELVIEW); 1.27 1.28 + thumbs = thumbs->next; /* skip dummy node */ 1.29 while(thumbs) { 1.30 glPushMatrix(); 1.31 glTranslatef(x, y, 0); 1.32 @@ -204,6 +206,7 @@ 1.33 1.34 if(img_load(rdata->img, fname) == -1) { 1.35 img_free(rdata->img); 1.36 + rdata->img = 0; 1.37 return -1; 1.38 } 1.39 rdata->aspect = (float)rdata->img->width / (float)rdata->img->height; 1.40 @@ -216,9 +219,16 @@ 1.41 1.42 static int done_res_texture(int id, void *cls) 1.43 { 1.44 - struct thumbnail *rdata; 1.45 + struct thumbnail *rdata = resman_get_res_data(texman, id); 1.46 + int load_result = resman_get_res_result(texman, id); 1.47 1.48 - rdata = resman_get_res_data(texman, id); 1.49 + if(load_result == -1) { 1.50 + /* returning -1 will remove this resource, the free_res_texture 1.51 + * destroy handler will be called, which will remove the node 1.52 + * from the list 1.53 + */ 1.54 + return -1; 1.55 + } 1.56 1.57 if(resman_get_res_result(texman, id) != 0 || !rdata) { 1.58 fprintf(stderr, "failed to load resource %d (%s)\n", id, resman_get_res_name(texman, id)); 1.59 @@ -240,14 +250,23 @@ 1.60 1.61 static void free_res_texture(int id, void *cls) 1.62 { 1.63 - struct thumbnail *rdata = resman_get_res_data(texman, id); 1.64 + struct thumbnail *thumb = resman_get_res_data(texman, id); 1.65 1.66 - if(rdata) { 1.67 - if(rdata->tex) { 1.68 - glDeleteTextures(1, &rdata->tex); 1.69 + if(thumb) { 1.70 + if(thumb->tex) { 1.71 + glDeleteTextures(1, &thumb->tex); 1.72 } 1.73 - if(rdata->img) { 1.74 - img_free(rdata->img); 1.75 + if(thumb->img) { 1.76 + img_free(thumb->img); 1.77 } 1.78 } 1.79 + 1.80 + /* remove from the list */ 1.81 + if(thumb->prev) { 1.82 + thumb->prev->next = thumb->next; 1.83 + } 1.84 + if(thumb->next) { 1.85 + thumb->next->prev = thumb->prev; 1.86 + } 1.87 + free(thumb); 1.88 }