# HG changeset patch # User John Tsiombikas # Date 1321869083 -7200 # Node ID c3e0bccd673ef105767ed33d4a7e7a685223e2a9 # Parent 0e781cc431780fea4380e22236e4603126232986 added texture mapping diff -r 0e781cc43178 -r c3e0bccd673e .hgignore --- a/.hgignore Mon Nov 21 10:16:09 2011 +0200 +++ b/.hgignore Mon Nov 21 11:51:23 2011 +0200 @@ -1,4 +1,5 @@ \.o$ \.obj$ \.d$ +\.swp$ ^test$ diff -r 0e781cc43178 -r c3e0bccd673e src/palman.c --- a/src/palman.c Mon Nov 21 10:16:09 2011 +0200 +++ b/src/palman.c Mon Nov 21 11:51:23 2011 +0200 @@ -44,6 +44,18 @@ return 0; } +int palm_color_index(unsigned char r, unsigned char g, unsigned char b) +{ + int i; + + for(i=0; iwidth, tex->height, tex->pixels); return 0; @@ -358,6 +371,10 @@ auto_rotate = !auto_rotate; break; + case 't': + texfile = argv[++i]; + break; + case 'v': use_vsync = !use_vsync; break; diff -r 0e781cc43178 -r c3e0bccd673e src/texture.c --- a/src/texture.c Mon Nov 21 10:16:09 2011 +0200 +++ b/src/texture.c Mon Nov 21 11:51:23 2011 +0200 @@ -16,13 +16,96 @@ along with this program. If not, see . */ +#include +#include #include #include "texture.h" #include "palman.h" -struct texture *tex_load(const char *fname) +struct texture *load_texture(const char *fname) { - return 0; /* TODO */ + int i, num_pixels; + struct texture *tex; + long fpos; + + if(!(tex = malloc(sizeof *tex))) { + return 0; + } + memset(tex, 0, sizeof *tex); + + if(!(tex->file = fopen(fname, "rb"))) { + fprintf(stderr, "failed to open texture: %s\n", fname); + free_texture(tex); + return 0; + } + if(fscanf(tex->file, "P6 %d %d 255 ", &tex->width, &tex->height) != 2) { + fprintf(stderr, "invalid pixmap: %s\n", fname); + free_texture(tex); + return 0; + } + fpos = ftell(tex->file); + + num_pixels = tex->width * tex->height; + for(i=0; ifile); + g = fgetc(tex->file); + b = fgetc(tex->file); + + if(feof(tex->file)) { + fprintf(stderr, "unexpected EOF while reading: %s\n", fname); + free_texture(tex); + return 0; + } + + if(palm_color_index(r, g, b) == -1) { + palm_add_color(r, g, b); + printf("adding color %d %d %d\n", r, g, b); + } + } + fseek(tex->file, fpos, SEEK_SET); + return tex; +} + +void free_texture(struct texture *tex) +{ + if(tex) { + if(tex->file) { + fclose(tex->file); + } + free(tex->pixels); + free(tex); + } +} + +unsigned char *get_texture_pixels(struct texture *tex) +{ + if(!tex->pixels && tex->file) { + int i, num_pixels = tex->width * tex->height; + + if(!(tex->pixels = malloc(num_pixels))) { + return 0; + } + + for(i=0; ifile); + g = fgetc(tex->file); + b = fgetc(tex->file); + + if((base = palm_color_base(r, g, b)) == -1) { + base = 0; + } + tex->pixels[i] = base; + } + + fclose(tex->file); + tex->file = 0; + } + + return tex->pixels; } struct texture *tex_gen_checker(int xsz, int ysz, int ush, int vsh, int c1, int c2) @@ -34,6 +117,8 @@ if(!(tex = malloc(sizeof *tex))) { return 0; } + memset(tex, 0, sizeof *tex); + if(!(tex->pixels = malloc(xsz * ysz))) { free(tex); return 0; diff -r 0e781cc43178 -r c3e0bccd673e src/texture.h --- a/src/texture.h Mon Nov 21 10:16:09 2011 +0200 +++ b/src/texture.h Mon Nov 21 11:51:23 2011 +0200 @@ -21,10 +21,21 @@ struct texture { int width, height; unsigned char *pixels; + + struct { + unsigned char r, g, b; + } *palette; + int num_colors; + + FILE *file; }; -struct texture *tex_load(const char *fname); +struct texture *load_texture(const char *fname); +void free_texture(struct texture *tex); struct texture *tex_gen_checker(int xsz, int ysz, int usub, int vsub, int c1, int c2); +unsigned char *get_texture_pixels(struct texture *tex); +int find_texture_color(struct texture *tex, int r, int g, int b); + #endif /* TEXTURE_H_ */