File: zbuffer.cpp

package info (click to toggle)
sludge 2.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,852 kB
  • sloc: cpp: 32,432; sh: 1,237; makefile: 634; xml: 284
file content (234 lines) | stat: -rw-r--r-- 6,578 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <stdio.h>
#include <stdint.h>

#include "tga.h"
#include "interface.h"
#include "moreio.h"
#include "sprites.h"
#include "zbuffer.h"


bool loadZBufferFromTGA (const char * fileName, spriteBank *loadhere) {
	unsigned char * pointer;
	unsigned short int t1, t2;
	palCol thePalette[256];
	int numPanels = 1;
	int panels[16];
	int cutoff[16];
	int color, panel;
	unsigned char n;
	unsigned char * data;

	// Open the file
	FILE * fp = fopen (fileName, "rb");
	if (fp == NULL) {
		errorBox ("Error", "Can't open that image file!");
		return false;
	}
	
	// Grab the header
	TGAHeader imageHeader;
	const char * errorBack;
	errorBack = readTGAHeader (imageHeader, fp, thePalette);
	if (errorBack) {
		errorBox ("Error reading TGA file", errorBack);
		return false;		
	}
	
	unsigned short (* readColFunction) (FILE * fp, int bpc, palCol thePalette[], int x, int y) =
		imageHeader.compressed ? readCompressedColour : readAColour;
	
	data = new unsigned char [imageHeader.width * imageHeader.height];
	panels[0] = cutoff[0] = 0;
	
	for (t2 = imageHeader.height; t2; t2 --) {
		pointer = data + (t2-1)*imageHeader.width;
		for (t1 = 0; t1 < imageHeader.width; t1 ++) {
			color = readColFunction (fp, imageHeader.pixelDepth, thePalette, 0, 0);
			if (color) {
				for (n = 1; n < numPanels; n ++) {
					if (panels[n] == color) break;
				}
				if (n == numPanels) {
					if (n < 16) {
						panels[n] = color;
						numPanels ++;
						if (n) cutoff[n] = t2;
						
					} else {
						delete data;
						errorBox ("Error reading TGA file", "Too many colours. Max 16 are allowed for making z-buffers. Each separate colour will become a z-buffer. If you need more, use sprites instead.");						
						return false;
					}
				}
				panel = n;
			} else {
				panel = 0;
			}
			* (pointer ++) = panel;
		}
	}
	fclose (fp);
	
	if (numPanels<2) {
		delete data;
		errorBox ("Error reading TGA file", "Can't find any z-buffers in the file.");						
		return false;
	}
	
	loadhere->total = numPanels;
	loadhere->sprites = new sprite [loadhere->total];
	
	for (n = 0; n < loadhere->total; n ++) {
		loadhere->sprites[n].width = imageHeader.width;
		loadhere->sprites[n].height = -imageHeader.height;
		loadhere->sprites[n].xhot = 0;
		loadhere->sprites[n].yhot = 0;
		loadhere->sprites[n].special = cutoff[n];
		loadhere->sprites[n].tex_x = 0;
		loadhere->sprites[n].texNum = n;
		loadhere->myPalette.tex_names[n] = 0;
		loadhere->myPalette.tex_h[n] = imageHeader.height;
		loadhere->myPalette.tex_w[n] = imageHeader.width;
		
		if (n)
			loadhere->sprites[n].data = new unsigned char [imageHeader.width * imageHeader.height];
		else
			loadhere->sprites[n].data = data;
	}
		
	for (int y = 0; y < imageHeader.height; y ++) {
		for (int x = 0; x < imageHeader.width; x ++) {
			n = loadhere->sprites[0].data[y*imageHeader.width+x];
			for (int i = 1; i < loadhere->total; i ++) {
				loadhere->sprites[i].data[y*imageHeader.width+x] = (n == i) ? 255: 0;
			}
		}
	}	
	
	return true;
}


bool loadZBufferFile (const char * name, spriteBank *loadhere) {
	int n, i, x, y, zbWidth, zbHeight;
	uint32_t stillToGo = 0;
	
	FILE * fp = fopen (name, "rb");
	if (! fp) return false;
	if (fgetc (fp) != 'S') { fclose (fp); return false; }
	if (fgetc (fp) != 'z') { fclose (fp); return false; }
	if (fgetc (fp) != 'b') { fclose (fp); return false; }
	switch (fgetc (fp)) {
		case 0:
		zbWidth = 640;
		zbHeight = 480;
		break;
		
		case 1:
		zbWidth = get2bytes (fp);
		zbHeight = get2bytes (fp);
		break;
		
		default:
		fclose (fp);
		return false;
	}

	loadhere->total = fgetc (fp);
	loadhere->sprites = new sprite [loadhere->total];

	for (n = 0; n < loadhere->total; n ++) {
		loadhere->sprites[n].width = zbWidth;
		loadhere->sprites[n].height = -zbHeight;
		loadhere->sprites[n].xhot = 0;
		loadhere->sprites[n].yhot = 0;
		loadhere->sprites[n].tex_x = 0;
		loadhere->sprites[n].special = get2bytes (fp);
		loadhere->sprites[n].texNum = n;
		loadhere->myPalette.tex_names[n] = 0;
		loadhere->myPalette.tex_h[n] = zbHeight;
		loadhere->myPalette.tex_w[n] = zbWidth;
		
		loadhere->sprites[n].data = new unsigned char [zbWidth * zbHeight];
	}
	
	for (y = 0; y < zbHeight; y ++) {
		for (x = 0; x < zbWidth; x ++) {
			if (stillToGo == 0) {
				n = fgetc (fp);
				stillToGo = n >> 4;
				if (stillToGo == 15) stillToGo = get2bytes (fp) + 16l;
				else stillToGo ++;
				n &= 15;
			}
			loadhere->sprites[0].data[y*zbWidth+x] = n;
			for (i = 1; i < loadhere->total; i ++) {
				loadhere->sprites[i].data[y*zbWidth+x] = (n == i) ? 255: 0;
			}
			stillToGo --;
		}
	}
	fclose (fp);
	return true;
}

void loadZTextures (spriteBank *loadhere){
	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
	glGenTextures (loadhere->total, loadhere->myPalette.tex_names);	

	for (int i = 1; i < loadhere->total; i ++) {
		loadhere->sprites[i].texNum = i;
		glBindTexture (GL_TEXTURE_2D, loadhere->myPalette.tex_names[loadhere->sprites[i].texNum]);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA8, loadhere->sprites[i].width, -loadhere->sprites[i].height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, loadhere->sprites[i].data);
	}	
}	

bool saveZBufferFile (const char * name, spriteBank *buffers) {
	FILE * fp = fopen (name, "wb");
	int n;
	uint32_t totalPixels = buffers->sprites[0].width * (-buffers->sprites[0].height);
	uint32_t thisPixel = 0;
	uint32_t countPixels = 0;
	unsigned short thisColour;

	fputc ('S', fp);
	fputc ('z', fp);
	fputc ('b', fp);
	fputc (1, fp);
	put2bytes (buffers->sprites[0].width, fp);
	put2bytes (-buffers->sprites[0].height, fp);
	fputc (buffers->total, fp);
	for (n = 0; n < buffers->total; n ++) {
		put2bytes (buffers->sprites[n].special, fp);
	}
		
	while (thisPixel < totalPixels) {
		thisColour = buffers->sprites[0].data[thisPixel];

		// Find how many pixels of the same colour follow this
		countPixels = thisPixel + 1;
		while (thisColour == buffers->sprites[0].data[countPixels]) {
			countPixels ++;
			if (countPixels == totalPixels) break;
			if (countPixels - thisPixel == 65551) break;
		}
		countPixels -= thisPixel;
		
		if (thisColour > 15) n = 0;
		if (countPixels < 16) {
			fputc (thisColour + ((unsigned char) ((countPixels - 1) << 4)), fp);
		} else {
			fputc (thisColour + 240, fp);
			put2bytes (countPixels - 16, fp);
		}
		thisPixel += countPixels;
	}
	fclose (fp);
	return true;
}