dos3d

view 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
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 }
65 }
66 fseek(tex->file, fpos, SEEK_SET);
67 return tex;
68 }
70 void free_texture(struct texture *tex)
71 {
72 if(tex) {
73 if(tex->file) {
74 fclose(tex->file);
75 }
76 free(tex->pixels);
77 free(tex);
78 }
79 }
81 unsigned char *get_texture_pixels(struct texture *tex)
82 {
83 if(!tex->pixels && tex->file) {
84 int i, num_pixels = tex->width * tex->height;
86 if(!(tex->pixels = malloc(num_pixels))) {
87 return 0;
88 }
90 for(i=0; i<num_pixels; i++) {
91 int r, g, b, base;
93 r = fgetc(tex->file);
94 g = fgetc(tex->file);
95 b = fgetc(tex->file);
97 if((base = palm_color_base(r, g, b)) == -1) {
98 base = 0;
99 }
100 tex->pixels[i] = base;
101 }
103 fclose(tex->file);
104 tex->file = 0;
105 }
107 return tex->pixels;
108 }
110 struct texture *tex_gen_checker(int xsz, int ysz, int ush, int vsh, int c1, int c2)
111 {
112 int i, j;
113 struct texture *tex;
114 unsigned char *pptr;
116 if(!(tex = malloc(sizeof *tex))) {
117 return 0;
118 }
119 memset(tex, 0, sizeof *tex);
121 if(!(tex->pixels = malloc(xsz * ysz))) {
122 free(tex);
123 return 0;
124 }
125 tex->width = xsz;
126 tex->height = ysz;
128 pptr = tex->pixels;
129 for(i=0; i<ysz; i++) {
130 for(j=0; j<xsz; j++) {
131 int c = ((i >> vsh) & 1) == ((j >> ush) & 1) ? c1 : c2;
132 *pptr++ = c;
133 }
134 }
135 return tex;
136 }