libresman

diff examples/imgthumbs/src/thumbs.c @ 20:c6073bf9fd38

finally! async loading works. Now on to inotify...
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 12 Feb 2014 16:02:12 +0200
parents 711698580eb0
children fe0dbdfbe403
line diff
     1.1 --- a/examples/imgthumbs/src/thumbs.c	Wed Feb 12 06:54:52 2014 +0200
     1.2 +++ b/examples/imgthumbs/src/thumbs.c	Wed Feb 12 16:02:12 2014 +0200
     1.3 @@ -9,19 +9,12 @@
     1.4  #include "thumbs.h"
     1.5  #include "resman.h"
     1.6  
     1.7 -#undef DBG_SYNC
     1.8 -
     1.9 -#ifndef GL_COMPRESSED_RGB
    1.10 -#define GL_COMPRESSED_RGB	0x84ed
    1.11 -#endif
    1.12 -
    1.13 -struct resman *texman;
    1.14 -struct thumbnail *dbg;
    1.15 -
    1.16  static int load_res_texture(const char *fname, int id, void *cls);
    1.17  static int done_res_texture(int id, void *cls);
    1.18  static void free_res_texture(int id, void *cls);
    1.19  
    1.20 +struct resman *texman;
    1.21 +int dbg_load_async = 1;
    1.22  
    1.23  struct thumbnail *create_thumbs(const char *dirpath)
    1.24  {
    1.25 @@ -29,7 +22,6 @@
    1.26  	struct dirent *dent;
    1.27  	/* allocate dummy head node */
    1.28  	struct thumbnail *list = calloc(1, sizeof *list);
    1.29 -	dbg = list;
    1.30  
    1.31  	if(!texman) {
    1.32  		texman = resman_create();
    1.33 @@ -44,9 +36,7 @@
    1.34  	}
    1.35  
    1.36  	while((dent = readdir(dir))) {
    1.37 -#ifdef DBG_SYNC
    1.38  		struct img_pixmap img;
    1.39 -#endif
    1.40  		struct thumbnail *node;
    1.41  
    1.42  		if(!(node = malloc(sizeof *node))) {
    1.43 @@ -67,32 +57,34 @@
    1.44  
    1.45  		node->aspect = 1.0;
    1.46  
    1.47 -#ifndef DBG_SYNC
    1.48 -		resman_lookup(texman, node->fname, node);
    1.49 -#else
    1.50 -		img_init(&img);
    1.51 -		if(img_load(&img, node->fname) == -1) {
    1.52 +		if(dbg_load_async) {
    1.53 +			resman_lookup(texman, node->fname, node);
    1.54 +		} else {
    1.55 +			img_init(&img);
    1.56 +			if(img_load(&img, node->fname) == -1) {
    1.57 +				img_destroy(&img);
    1.58 +				free(node->fname);
    1.59 +				free(node);
    1.60 +				continue;
    1.61 +			}
    1.62 +
    1.63 +			printf("loaded image: %s\n", node->fname);
    1.64 +
    1.65 +			node->aspect = (float)img.width / (float)img.height;
    1.66 +
    1.67 +			glGenTextures(1, &node->tex);
    1.68 +			glBindTexture(GL_TEXTURE_2D, node->tex);
    1.69 +			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.70 +			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.71 +			glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(&img), img.width, img.height, 0, img_glfmt(&img), img_gltype(&img), img.pixels);
    1.72  			img_destroy(&img);
    1.73 -			free(node->fname);
    1.74 -			free(node);
    1.75 -			continue;
    1.76 +
    1.77 +			node->prev = list;
    1.78 +			node->next = list->next;
    1.79 +			if(list->next) list->next->prev = node;
    1.80 +			list->next = node;
    1.81  		}
    1.82 -
    1.83 -		printf("loaded image: %s\n", node->fname);
    1.84 -
    1.85 -		node->aspect = (float)img.width / (float)img.height;
    1.86 -
    1.87 -		glGenTextures(1, &node->tex);
    1.88 -		glBindTexture(GL_TEXTURE_2D, node->tex);
    1.89 -		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.90 -		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.91 -		glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(&img), img.width, img.height, 0, img_glfmt(&img), img_gltype(&img), img.pixels);
    1.92 -		img_destroy(&img);
    1.93 -#endif
    1.94 -
    1.95 -		node->next = list->next;
    1.96 -		node->prev = list;
    1.97 -		list->next = node;
    1.98 +		node->list = list;
    1.99  	}
   1.100  	closedir(dir);
   1.101  
   1.102 @@ -138,7 +130,6 @@
   1.103  
   1.104  	thumbs = thumbs->next;	/* skip dummy node */
   1.105  	while(thumbs) {
   1.106 -		printf("drawing thumb: %s\n", thumbs->fname);
   1.107  		glPushMatrix();
   1.108  		glTranslatef(x, y, 0);
   1.109  
   1.110 @@ -225,7 +216,7 @@
   1.111  
   1.112  static int done_res_texture(int id, void *cls)
   1.113  {
   1.114 -	struct thumbnail *rdata = resman_get_res_data(texman, id);
   1.115 +	struct thumbnail *thumb = resman_get_res_data(texman, id);
   1.116  	int load_result = resman_get_res_result(texman, id);
   1.117  
   1.118  	if(load_result == -1) {
   1.119 @@ -236,21 +227,27 @@
   1.120  		return -1;
   1.121  	}
   1.122  
   1.123 -	if(resman_get_res_result(texman, id) != 0 || !rdata) {
   1.124 +	if(resman_get_res_result(texman, id) != 0 || !thumb) {
   1.125  		fprintf(stderr, "failed to load resource %d (%s)\n", id, resman_get_res_name(texman, id));
   1.126  	} else {
   1.127  		printf("done loading resource %d (%s)\n", id, resman_get_res_name(texman, id));
   1.128  	}
   1.129  
   1.130 -	if(!rdata->tex) {
   1.131 -		glGenTextures(1, &rdata->tex);
   1.132 +	if(!thumb->tex) {
   1.133 +		glGenTextures(1, &thumb->tex);
   1.134  	}
   1.135 -	glBindTexture(GL_TEXTURE_2D, rdata->tex);
   1.136 +	glBindTexture(GL_TEXTURE_2D, thumb->tex);
   1.137  	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   1.138  	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   1.139 -	glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(rdata->img),
   1.140 -			rdata->img->width, rdata->img->height, 0, img_glfmt(rdata->img),
   1.141 -			img_gltype(rdata->img), rdata->img->pixels);
   1.142 +	glTexImage2D(GL_TEXTURE_2D, 0, img_glintfmt(thumb->img),
   1.143 +			thumb->img->width, thumb->img->height, 0, img_glfmt(thumb->img),
   1.144 +			img_gltype(thumb->img), thumb->img->pixels);
   1.145 +
   1.146 +	/* and add it to the list of thumbnails */
   1.147 +	thumb->prev = thumb->list;
   1.148 +	thumb->next = thumb->list->next;
   1.149 +	if(thumb->list->next) thumb->list->next->prev = thumb;
   1.150 +	thumb->list->next = thumb;
   1.151  	return 0;
   1.152  }
   1.153