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