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