nuclear@0: /* nuclear@0: 256-color 3D graphics hack for real-mode DOS. nuclear@0: Copyright (C) 2011 John Tsiombikas nuclear@0: nuclear@0: This program is free software: you can redistribute it and/or modify nuclear@0: it under the terms of the GNU General Public License as published by nuclear@0: the Free Software Foundation, either version 3 of the License, or nuclear@0: (at your option) any later version. nuclear@0: nuclear@0: This program is distributed in the hope that it will be useful, nuclear@0: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@0: GNU General Public License for more details. nuclear@0: nuclear@0: You should have received a copy of the GNU General Public License nuclear@0: along with this program. If not, see . nuclear@0: */ nuclear@0: nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include "texture.h" nuclear@0: #include "palman.h" nuclear@0: nuclear@0: struct texture *load_texture(const char *fname) nuclear@0: { nuclear@0: long i, num_pixels; nuclear@0: struct texture *tex; nuclear@0: long fpos; nuclear@0: nuclear@0: if(!(tex = malloc(sizeof *tex))) { nuclear@0: return 0; nuclear@0: } nuclear@0: memset(tex, 0, sizeof *tex); nuclear@0: nuclear@0: if(!(tex->file = fopen(fname, "rb"))) { nuclear@0: fprintf(stderr, "failed to open texture: %s\n", fname); nuclear@0: free_texture(tex); nuclear@0: return 0; nuclear@0: } nuclear@0: if(fscanf(tex->file, "P6 %d %d 255 ", &tex->width, &tex->height) != 2) { nuclear@0: fprintf(stderr, "invalid pixmap: %s\n", fname); nuclear@0: free_texture(tex); nuclear@0: return 0; nuclear@0: } nuclear@0: fpos = ftell(tex->file); nuclear@0: nuclear@0: num_pixels = tex->width * tex->height; nuclear@0: for(i=0; ifile); nuclear@0: g = fgetc(tex->file); nuclear@0: b = fgetc(tex->file); nuclear@0: nuclear@0: if(feof(tex->file)) { nuclear@0: fprintf(stderr, "unexpected EOF while reading: %s\n", fname); nuclear@0: free_texture(tex); nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: if(palm_color_index(r, g, b) == -1) { nuclear@0: palm_add_color(r, g, b); nuclear@0: } nuclear@0: } nuclear@0: fseek(tex->file, fpos, SEEK_SET); nuclear@0: return tex; nuclear@0: } nuclear@0: nuclear@0: void free_texture(struct texture *tex) nuclear@0: { nuclear@0: if(tex) { nuclear@0: if(tex->file) { nuclear@0: fclose(tex->file); nuclear@0: } nuclear@0: free(tex->pixels); nuclear@0: free(tex); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: unsigned char *get_texture_pixels(struct texture *tex) nuclear@0: { nuclear@0: if(!tex->pixels && tex->file) { nuclear@0: long i, num_pixels = tex->width * tex->height; nuclear@0: nuclear@0: if(!(tex->pixels = malloc(num_pixels))) { nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: for(i=0; ifile); nuclear@0: g = fgetc(tex->file); nuclear@0: b = fgetc(tex->file); nuclear@0: nuclear@0: if((base = palm_color_base(r, g, b)) == -1) { nuclear@0: base = 0; nuclear@0: } nuclear@0: tex->pixels[i] = base; nuclear@0: } nuclear@0: nuclear@0: fclose(tex->file); nuclear@0: tex->file = 0; nuclear@0: } nuclear@0: nuclear@0: return tex->pixels; nuclear@0: } nuclear@0: nuclear@0: struct texture *tex_gen_checker(int xsz, int ysz, int ush, int vsh, int c1, int c2) nuclear@0: { nuclear@0: int i, j; nuclear@0: struct texture *tex; nuclear@0: unsigned char *pptr; nuclear@0: nuclear@0: if(!(tex = malloc(sizeof *tex))) { nuclear@0: return 0; nuclear@0: } nuclear@0: memset(tex, 0, sizeof *tex); nuclear@0: nuclear@0: if(!(tex->pixels = malloc(xsz * ysz))) { nuclear@0: free(tex); nuclear@0: return 0; nuclear@0: } nuclear@0: tex->width = xsz; nuclear@0: tex->height = ysz; nuclear@0: nuclear@0: pptr = tex->pixels; nuclear@0: for(i=0; i> vsh) & 1) == ((j >> ush) & 1) ? c1 : c2; nuclear@0: *pptr++ = c; nuclear@0: } nuclear@0: } nuclear@0: return tex; nuclear@0: }