deepstone

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