deepstone
view src/texture.c @ 29:5fa2983bfe61
watcom makefile changes
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 22 Sep 2013 18:09:44 +0300 |
parents | c8ffdbc6139e |
children |
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, hdrline = 0;
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 }
42 while(hdrline < 3) {
43 char buf[64];
44 if(!fgets(buf, sizeof buf, tex->file)) {
45 fprintf(stderr, "invalid pixmap: %s\n", fname);
46 free_texture(tex);
47 return 0;
48 }
50 if(buf[0] == '#') {
51 continue;
52 }
54 switch(hdrline) {
55 case 0:
56 if(buf[0] != 'P' || buf[1] != '6') {
57 fprintf(stderr, "invalid pixmap: %s\n", fname);
58 free_texture(tex);
59 return 0;
60 }
61 break;
63 case 1:
64 if(sscanf(buf, "%d %d", &tex->width, &tex->height) != 2) {
65 fprintf(stderr, "invalid pixmap: %s\n", fname);
66 free_texture(tex);
67 return 0;
68 }
69 break;
71 case 2:
72 if(atoi(buf) != 255) {
73 fprintf(stderr, "invalid pixmap: %s\n", fname);
74 free_texture(tex);
75 return 0;
76 }
77 break;
78 }
79 hdrline++;
80 }
81 fpos = ftell(tex->file);
83 num_pixels = tex->width * tex->height;
84 for(i=0; i<num_pixels; i++) {
85 int r, g, b;
87 r = fgetc(tex->file);
88 g = fgetc(tex->file);
89 b = fgetc(tex->file);
91 if(feof(tex->file)) {
92 fprintf(stderr, "unexpected EOF while reading: %s\n", fname);
93 free_texture(tex);
94 return 0;
95 }
97 if(palm_color_index(r, g, b) == -1) {
98 palm_add_color(r, g, b);
99 }
100 }
101 fseek(tex->file, fpos, SEEK_SET);
102 return tex;
103 }
105 void free_texture(struct texture *tex)
106 {
107 if(tex) {
108 if(tex->file) {
109 fclose(tex->file);
110 }
111 free(tex->pixels);
112 free(tex);
113 }
114 }
116 unsigned char *get_texture_pixels(struct texture *tex)
117 {
118 if(!tex->pixels && tex->file) {
119 int i, num_pixels = tex->width * tex->height;
121 if(!(tex->pixels = malloc(num_pixels))) {
122 return 0;
123 }
125 for(i=0; i<num_pixels; i++) {
126 int r, g, b, base;
128 r = fgetc(tex->file);
129 g = fgetc(tex->file);
130 b = fgetc(tex->file);
132 if((base = palm_color_base(r, g, b)) == -1) {
133 base = 0;
134 }
135 tex->pixels[i] = base;
136 }
138 fclose(tex->file);
139 tex->file = 0;
140 }
142 return tex->pixels;
143 }
145 struct texture *tex_gen_checker(int xsz, int ysz, int ush, int vsh, int c1, int c2)
146 {
147 int i, j;
148 struct texture *tex;
149 unsigned char *pptr;
151 if(!(tex = malloc(sizeof *tex))) {
152 return 0;
153 }
154 memset(tex, 0, sizeof *tex);
156 if(!(tex->pixels = malloc(xsz * ysz))) {
157 free(tex);
158 return 0;
159 }
160 tex->width = xsz;
161 tex->height = ysz;
163 pptr = tex->pixels;
164 for(i=0; i<ysz; i++) {
165 for(j=0; j<xsz; j++) {
166 int c = ((i >> vsh) & 1) == ((j >> ush) & 1) ? c1 : c2;
167 *pptr++ = c;
168 }
169 }
170 return tex;
171 }