dos3d

annotate src/texture.c @ 7:3239511d1a76

random change
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 12:12:23 +0200
parents c3e0bccd673e
children c10f62b2bd56
rev   line source
nuclear@3 1 /*
nuclear@3 2 256-color 3D graphics hack for real-mode DOS.
nuclear@3 3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@3 4
nuclear@3 5 This program is free software: you can redistribute it and/or modify
nuclear@3 6 it under the terms of the GNU General Public License as published by
nuclear@3 7 the Free Software Foundation, either version 3 of the License, or
nuclear@3 8 (at your option) any later version.
nuclear@3 9
nuclear@3 10 This program is distributed in the hope that it will be useful,
nuclear@3 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@3 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@3 13 GNU General Public License for more details.
nuclear@3 14
nuclear@3 15 You should have received a copy of the GNU General Public License
nuclear@3 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@3 17 */
nuclear@3 18
nuclear@4 19 #include <stdio.h>
nuclear@4 20 #include <string.h>
nuclear@3 21 #include <stdlib.h>
nuclear@3 22 #include "texture.h"
nuclear@3 23 #include "palman.h"
nuclear@3 24
nuclear@4 25 struct texture *load_texture(const char *fname)
nuclear@3 26 {
nuclear@4 27 int i, num_pixels;
nuclear@4 28 struct texture *tex;
nuclear@4 29 long fpos;
nuclear@4 30
nuclear@4 31 if(!(tex = malloc(sizeof *tex))) {
nuclear@4 32 return 0;
nuclear@4 33 }
nuclear@4 34 memset(tex, 0, sizeof *tex);
nuclear@4 35
nuclear@4 36 if(!(tex->file = fopen(fname, "rb"))) {
nuclear@4 37 fprintf(stderr, "failed to open texture: %s\n", fname);
nuclear@4 38 free_texture(tex);
nuclear@4 39 return 0;
nuclear@4 40 }
nuclear@4 41 if(fscanf(tex->file, "P6 %d %d 255 ", &tex->width, &tex->height) != 2) {
nuclear@4 42 fprintf(stderr, "invalid pixmap: %s\n", fname);
nuclear@4 43 free_texture(tex);
nuclear@4 44 return 0;
nuclear@4 45 }
nuclear@4 46 fpos = ftell(tex->file);
nuclear@4 47
nuclear@4 48 num_pixels = tex->width * tex->height;
nuclear@4 49 for(i=0; i<num_pixels; i++) {
nuclear@4 50 int r, g, b;
nuclear@4 51
nuclear@4 52 r = fgetc(tex->file);
nuclear@4 53 g = fgetc(tex->file);
nuclear@4 54 b = fgetc(tex->file);
nuclear@4 55
nuclear@4 56 if(feof(tex->file)) {
nuclear@4 57 fprintf(stderr, "unexpected EOF while reading: %s\n", fname);
nuclear@4 58 free_texture(tex);
nuclear@4 59 return 0;
nuclear@4 60 }
nuclear@4 61
nuclear@4 62 if(palm_color_index(r, g, b) == -1) {
nuclear@4 63 palm_add_color(r, g, b);
nuclear@4 64 }
nuclear@4 65 }
nuclear@4 66 fseek(tex->file, fpos, SEEK_SET);
nuclear@4 67 return tex;
nuclear@4 68 }
nuclear@4 69
nuclear@4 70 void free_texture(struct texture *tex)
nuclear@4 71 {
nuclear@4 72 if(tex) {
nuclear@4 73 if(tex->file) {
nuclear@4 74 fclose(tex->file);
nuclear@4 75 }
nuclear@4 76 free(tex->pixels);
nuclear@4 77 free(tex);
nuclear@4 78 }
nuclear@4 79 }
nuclear@4 80
nuclear@4 81 unsigned char *get_texture_pixels(struct texture *tex)
nuclear@4 82 {
nuclear@4 83 if(!tex->pixels && tex->file) {
nuclear@4 84 int i, num_pixels = tex->width * tex->height;
nuclear@4 85
nuclear@4 86 if(!(tex->pixels = malloc(num_pixels))) {
nuclear@4 87 return 0;
nuclear@4 88 }
nuclear@4 89
nuclear@4 90 for(i=0; i<num_pixels; i++) {
nuclear@4 91 int r, g, b, base;
nuclear@4 92
nuclear@4 93 r = fgetc(tex->file);
nuclear@4 94 g = fgetc(tex->file);
nuclear@4 95 b = fgetc(tex->file);
nuclear@4 96
nuclear@4 97 if((base = palm_color_base(r, g, b)) == -1) {
nuclear@4 98 base = 0;
nuclear@4 99 }
nuclear@4 100 tex->pixels[i] = base;
nuclear@4 101 }
nuclear@4 102
nuclear@4 103 fclose(tex->file);
nuclear@4 104 tex->file = 0;
nuclear@4 105 }
nuclear@4 106
nuclear@4 107 return tex->pixels;
nuclear@3 108 }
nuclear@3 109
nuclear@3 110 struct texture *tex_gen_checker(int xsz, int ysz, int ush, int vsh, int c1, int c2)
nuclear@3 111 {
nuclear@3 112 int i, j;
nuclear@3 113 struct texture *tex;
nuclear@3 114 unsigned char *pptr;
nuclear@3 115
nuclear@3 116 if(!(tex = malloc(sizeof *tex))) {
nuclear@3 117 return 0;
nuclear@3 118 }
nuclear@4 119 memset(tex, 0, sizeof *tex);
nuclear@4 120
nuclear@3 121 if(!(tex->pixels = malloc(xsz * ysz))) {
nuclear@3 122 free(tex);
nuclear@3 123 return 0;
nuclear@3 124 }
nuclear@3 125 tex->width = xsz;
nuclear@3 126 tex->height = ysz;
nuclear@3 127
nuclear@3 128 pptr = tex->pixels;
nuclear@3 129 for(i=0; i<ysz; i++) {
nuclear@3 130 for(j=0; j<xsz; j++) {
nuclear@3 131 int c = ((i >> vsh) & 1) == ((j >> ush) & 1) ? c1 : c2;
nuclear@3 132 *pptr++ = c;
nuclear@3 133 }
nuclear@3 134 }
nuclear@3 135 return tex;
nuclear@3 136 }