deepstone

diff src/texture.c @ 4:c3e0bccd673e

added texture mapping
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 11:51:23 +0200
parents 0e781cc43178
children c8ffdbc6139e
line diff
     1.1 --- a/src/texture.c	Mon Nov 21 10:16:09 2011 +0200
     1.2 +++ b/src/texture.c	Mon Nov 21 11:51:23 2011 +0200
     1.3 @@ -16,13 +16,96 @@
     1.4  along with this program.  If not, see <http://www.gnu.org/licenses/>.
     1.5  */
     1.6  
     1.7 +#include <stdio.h>
     1.8 +#include <string.h>
     1.9  #include <stdlib.h>
    1.10  #include "texture.h"
    1.11  #include "palman.h"
    1.12  
    1.13 -struct texture *tex_load(const char *fname)
    1.14 +struct texture *load_texture(const char *fname)
    1.15  {
    1.16 -	return 0;	/* TODO */
    1.17 +	int i, num_pixels;
    1.18 +	struct texture *tex;
    1.19 +	long fpos;
    1.20 +
    1.21 +	if(!(tex = malloc(sizeof *tex))) {
    1.22 +		return 0;
    1.23 +	}
    1.24 +	memset(tex, 0, sizeof *tex);
    1.25 +
    1.26 +	if(!(tex->file = fopen(fname, "rb"))) {
    1.27 +		fprintf(stderr, "failed to open texture: %s\n", fname);
    1.28 +		free_texture(tex);
    1.29 +		return 0;
    1.30 +	}
    1.31 +	if(fscanf(tex->file, "P6 %d %d 255 ", &tex->width, &tex->height) != 2) {
    1.32 +		fprintf(stderr, "invalid pixmap: %s\n", fname);
    1.33 +		free_texture(tex);
    1.34 +		return 0;
    1.35 +	}
    1.36 +	fpos = ftell(tex->file);
    1.37 +
    1.38 +	num_pixels = tex->width * tex->height;
    1.39 +	for(i=0; i<num_pixels; i++) {
    1.40 +		int r, g, b;
    1.41 +
    1.42 +		r = fgetc(tex->file);
    1.43 +		g = fgetc(tex->file);
    1.44 +		b = fgetc(tex->file);
    1.45 +
    1.46 +		if(feof(tex->file)) {
    1.47 +			fprintf(stderr, "unexpected EOF while reading: %s\n", fname);
    1.48 +			free_texture(tex);
    1.49 +			return 0;
    1.50 +		}
    1.51 +
    1.52 +		if(palm_color_index(r, g, b) == -1) {
    1.53 +			palm_add_color(r, g, b);
    1.54 +			printf("adding color %d %d %d\n", r, g, b);
    1.55 +		}
    1.56 +	}
    1.57 +	fseek(tex->file, fpos, SEEK_SET);
    1.58 +	return tex;
    1.59 +}
    1.60 +
    1.61 +void free_texture(struct texture *tex)
    1.62 +{
    1.63 +	if(tex) {
    1.64 +		if(tex->file) {
    1.65 +			fclose(tex->file);
    1.66 +		}
    1.67 +		free(tex->pixels);
    1.68 +		free(tex);
    1.69 +	}
    1.70 +}
    1.71 +
    1.72 +unsigned char *get_texture_pixels(struct texture *tex)
    1.73 +{
    1.74 +	if(!tex->pixels && tex->file) {
    1.75 +		int i, num_pixels = tex->width * tex->height;
    1.76 +
    1.77 +		if(!(tex->pixels = malloc(num_pixels))) {
    1.78 +			return 0;
    1.79 +		}
    1.80 +
    1.81 +		for(i=0; i<num_pixels; i++) {
    1.82 +			int r, g, b, base;
    1.83 +
    1.84 +			r = fgetc(tex->file);
    1.85 +			g = fgetc(tex->file);
    1.86 +			b = fgetc(tex->file);
    1.87 +
    1.88 +			if((base = palm_color_base(r, g, b)) == -1) {
    1.89 +				base = 0;
    1.90 +			}
    1.91 +			tex->pixels[i] = base;
    1.92 +		}
    1.93 +
    1.94 +		fclose(tex->file);
    1.95 +		tex->file = 0;
    1.96 +	}
    1.97 +
    1.98 +	return tex->pixels;
    1.99  }
   1.100  
   1.101  struct texture *tex_gen_checker(int xsz, int ysz, int ush, int vsh, int c1, int c2)
   1.102 @@ -34,6 +117,8 @@
   1.103  	if(!(tex = malloc(sizeof *tex))) {
   1.104  		return 0;
   1.105  	}
   1.106 +	memset(tex, 0, sizeof *tex);
   1.107 +
   1.108  	if(!(tex->pixels = malloc(xsz * ysz))) {
   1.109  		free(tex);
   1.110  		return 0;