istereo
diff src/tex.c @ 26:862a3329a8f0
wohooo, added a shitload of code from zlib/libpng/libjpeg. When the good lord was raining shared libraries the iphone held a fucking umbrella...
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Thu, 08 Sep 2011 06:28:38 +0300 |
parents | 206348366635 |
children | fd39c0198935 |
line diff
1.1 --- a/src/tex.c Thu Sep 08 04:23:56 2011 +0300 1.2 +++ b/src/tex.c Thu Sep 08 06:28:38 2011 +0300 1.3 @@ -5,56 +5,19 @@ 1.4 #include "opengl.h" 1.5 #include "tex.h" 1.6 #include "config.h" 1.7 +#include "imago2.h" 1.8 1.9 unsigned int load_texture(const char *fname) 1.10 { 1.11 + int xsz, ysz; 1.12 unsigned int tex; 1.13 - FILE *fp; 1.14 - unsigned char *pixels; 1.15 - int xsz, ysz, sz, i; 1.16 - char buf[512]; 1.17 + void *pixels; 1.18 1.19 - if(!(fp = fopen(fname, "r"))) { 1.20 - fprintf(stderr, "failed to open texture: %s: %s\n", fname, strerror(errno)); 1.21 + if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) { 1.22 + fprintf(stderr, "failed to load image: %s\n", fname); 1.23 return 0; 1.24 } 1.25 1.26 - if(!fgets(buf, sizeof buf, fp) || buf[0] != 'P' || buf[1] != '6') { 1.27 - fprintf(stderr, "invalid format (1): %s\n", fname); 1.28 - fclose(fp); 1.29 - return 0; 1.30 - } 1.31 - if(!fgets(buf, sizeof buf, fp) || sscanf(buf, "%d %d", &xsz, &ysz) != 2) { 1.32 - fprintf(stderr, "invalid format (2): %s\n", fname); 1.33 - fclose(fp); 1.34 - return 0; 1.35 - } 1.36 - fgets(buf, sizeof buf, fp); 1.37 - 1.38 - sz = xsz * ysz * 4; 1.39 - if(!(pixels = malloc(sz))) { 1.40 - fprintf(stderr, "failed to allocate %d bytes\n", sz); 1.41 - fclose(fp); 1.42 - return 0; 1.43 - } 1.44 - for(i=0; i<sz; i++) { 1.45 - int c; 1.46 - 1.47 - if(i % 4 == 3) { 1.48 - pixels[i] = 255; 1.49 - continue; 1.50 - } 1.51 - 1.52 - if((c = fgetc(fp)) == -1) { 1.53 - fprintf(stderr, "partial read: %s\n", fname); 1.54 - free(pixels); 1.55 - fclose(fp); 1.56 - return 0; 1.57 - } 1.58 - pixels[i] = c; 1.59 - } 1.60 - fclose(fp); 1.61 - 1.62 glGenTextures(1, &tex); 1.63 glBindTexture(GL_TEXTURE_2D, tex); 1.64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 1.65 @@ -62,7 +25,7 @@ 1.66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.68 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 1.69 - free(pixels); 1.70 + img_free_pixels(pixels); 1.71 1.72 return tex; 1.73 }