File: alphablitforpostal.cpp

package info (click to toggle)
postal1 2015.git20250526%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 14,024 kB
  • sloc: cpp: 130,877; ansic: 38,942; python: 874; makefile: 351; sh: 61
file content (431 lines) | stat: -rw-r--r-- 11,989 bytes parent folder | download | duplicates (2)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 RWS Inc, All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as published by
// the Free Software Foundation
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// alphaBLiTforPostal.cpp
// Project: Nostril (aka Postal)
//
// This module deals with the high-level aspects alpha blitting or something.
//
// History:
//		01/??/97 JRD	Started.
//
//		02/13/97	JMI	Now g_alphaBlit takes parms for the alphable mask image
//							and the MultiAlpha table.
//
//		08/21/97	JMI	Changed calls to rspDoSystem() to UpdateSystem() and 
//							occurrences of rspUpdateDisplay() to UpdateDisplay().
//
//		08/22/97	JMI	Changed calls to UpdateDisplay() back to rspUpdateDisplay()
//							since we no longer need UpdateDisplay() now that we are
//							using rspLock/Unlock* functions properly.
//
////////////////////////////////////////////////////////////////////////////////

#include "RSPiX.h"
#ifdef PATHS_IN_INCLUDES
	#include "ORANGE/color/colormatch.h"
	#include "GREEN/BLiT/alphablit.h"
	#include "WishPiX/ResourceManager/resmgr.h"
#else
	#include "colormatch.h"
	#include "alphablit.h"
	#include "resmgr.h"
#endif
#include "alphablitforpostal.h"
#include "game.h"
#include "update.h"

// The mask must be as big as the source
/*
void rspAlphaMaskBlit(RMultiAlpha* pX,RImage* pimMask,
									RImage* pimSrc,RImage* pimDst,short sDstX,short sDstY,
									RRect &rDstClip)
	{
	short sSrcX = 0,sSrcY = 0,sDstW = pimSrc->m_sWidth,sDstH = pimSrc->m_sHeight;

	// right here adjust things if you need to clip to other thatn the full dst im
	if (rspSimpleClip(sSrcX,sSrcY,sDstX,sDstY,sDstW,sDstH,
		rDstClip.sX,rDstClip.sY,rDstClip.sW,rDstClip.sH) == -1) return ; // clipped out
	
	short i,j;
	long lSrcP = pimSrc->m_lPitch;
	long lDstP = pimDst->m_lPitch;
	long lMaskP = pimMask->m_lPitch;
	uint8_t* pSrc,*pSrcLine = pimSrc->m_pData + sSrcX + sSrcY * lSrcP;
	uint8_t* pMask,*pMaskLine = pimMask->m_pData + sSrcX + sSrcY * lSrcP;
	uint8_t* pDst,*pDstLine = pimDst->m_pData + sDstX + lDstP * sDstY;
	uint8_t ucOpaque = (uint8_t) pX->m_sNumLevels;

	for (j=0;j<sDstH;j++,pSrcLine += lSrcP,pDstLine += lDstP,pMaskLine += lMaskP)
		{
		pSrc = pSrcLine;
		pDst = pDstLine;
		pMask = pMaskLine;
		uint8_t ucMask;

		for (i=0;i<sDstW;i++,pSrc++,pDst++,pMask++)
			{
			ucMask = *pMask;
			if (ucMask)
				{
				if (ucMask == ucOpaque) // optimized for mostly opqaue mask:
					{
					*pDst = *pSrc;
					}
				else
					{
					*pDst = pX->m_pAlphaList[ucMask]->m_pAlphas[*pSrc][*pDst];
					}
				}
			}
		}
	}
	*/
/*
void rspAlphaBlit(RAlpha* pX,RImage* pimSrc,RImage* pimDst,short sDstX,short sDstY)
	{
	short sSrcX = 0,sSrcY = 0,sDstW = pimSrc->m_sWidth,sDstH = pimSrc->m_sHeight;

	// right here adjust things if you need to clip to other thatn the full dst im
	if (rspSimpleClip(sSrcX,sSrcY,sDstX,sDstY,sDstW,sDstH,0,0,
		pimDst->m_sWidth,pimDst->m_sHeight) == -1) return ; // clipped out
	
	short i,j;
	long lSrcP = pimSrc->m_lPitch;
	long lDstP = pimDst->m_lPitch;
	uint8_t* pSrc,*pSrcLine = pimSrc->m_pData + sSrcX + sSrcY * lSrcP;
	uint8_t* pDst,*pDstLine = pimDst->m_pData + sDstX + lDstP * sDstY;

	for (j=0;j<sDstH;j++,pSrcLine += lSrcP,pDstLine += lDstP)
		{
		pSrc = pSrcLine;
		pDst = pDstLine;

		for (i=0;i<sDstW;i++,pSrc++,pDst++)
			{
			*pDst = pX->m_pAlphas[*pSrc][*pDst];
			}
		}
	}
	*/

// alpha key values
uint8_t	sCheckSum1[40] = {184,176,176,187,189,166,186,0};
uint8_t sCheckSum2[40] = {173, 172, 175, 223, 149,154,153,153,220,205,210,204,209,136,158,137,0};
uint8_t sCheckSum3[40] = {173, 172, 175, 223, 173,158,145,155,134,220,205,210,204,209,136,158,137,255};

/*
// This draws source to destination with clipping only if destination is not zero!
// NOTE that this technique is only valid for adding to fully opaque masks.
// For more deluxe compound masking, you need to only change if source < dest!
//
void rspMaskBlit(RImage* pimSrc,RImage* pimDst,short sDstX,short sDstY)
	{
	short sSrcX = 0,sSrcY = 0,sDstW = pimSrc->m_sWidth,sDstH = pimSrc->m_sHeight;

	// right here adjust things if you need to clip to other thatn the full dst im
	if (rspSimpleClip(sSrcX,sSrcY,sDstX,sDstY,sDstW,sDstH,0,0,
		pimDst->m_sWidth,pimDst->m_sHeight) == -1) return ; // clipped out
	
	short i,j;
	long lSrcP = pimSrc->m_lPitch;
	long lDstP = pimDst->m_lPitch;
	uint8_t* pSrc,*pSrcLine = pimSrc->m_pData + sSrcX + sSrcY * lSrcP;
	uint8_t* pDst,*pDstLine = pimDst->m_pData + sDstX + lDstP * sDstY;

	for (j=0;j<sDstH;j++,pSrcLine += lSrcP,pDstLine += lDstP)
		{
		pSrc = pSrcLine;
		pDst = pDstLine;

		for (i=0;i<sDstW;i++,pSrc++,pDst++)
			{
			if (*pDst) *pDst = *pSrc;
			}
		}
	}

// Takes a BMP8 and converts it to a mask of 0 and ucVal
void rspMakeMask(RImage* pimSrc,uint8_t ucVal)
	{
	short i,j;
	long lSrcP = pimSrc->m_lPitch;
	uint8_t* pSrc,*pSrcLine = pimSrc->m_pData;

	for (j=0;j<pimSrc->m_sHeight;j++,pSrcLine += lSrcP)
		{
		pSrc = pSrcLine;
		for (i=0;i<pimSrc->m_sWidth;i++,pSrc++)
			{
			if (*pSrc) *pSrc = ucVal; // replace all as mask
			}
		}
	}

// Takes a BMP8 and converts it to a mask of 0 and ucVal
// Currently, no clipping or positioning possible
void rspCopyAsMask(RImage* pimSrc,RImage* pimDst,uint8_t ucVal)
	{
	short i,j;
	long lSrcP = pimSrc->m_lPitch;
	long lDstP = pimDst->m_lPitch;
	uint8_t* pSrc,*pSrcLine = pimSrc->m_pData;
	uint8_t* pDst,*pDstLine = pimDst->m_pData;

	short sDstW = pimSrc->m_sWidth;
	short sDstH = pimSrc->m_sHeight;

	for (j=0;j<sDstH;j++,pSrcLine += lSrcP,pDstLine += lDstP)
		{
		pSrc = pSrcLine;
		pDst = pDstLine;

		for (i=0;i<sDstW;i++,pSrc++,pDst++)
			{
			if (*pSrc) *pDst = ucVal; // replace all as mask
			else 
				*pDst = 0;
			}
		}
	}
*/
static int sFirst = TRUE;
void Verify();
extern char* pct;

// USE GLOBAL MASK
//
void g_alphaBlit(
		RImage* pimSrc,			// Source image. (wall)
		RImage* pimDst,			// Destination image.
		RImage* pimMask,			// Mask of alphable area.
		RMultiAlpha* pma,			// Table of alphas or something.
		int16_t sAlphaX,				// Source coordinate in pimSrc to put alphamask.
		int16_t sAlphaY,				// Source coordinate in pimSrc to put alphamask.
		int16_t sDstX,				// Destination coordinate in pimDst for pimSrc(0,0).
		int16_t sDstY,				// Destination coordinate in pimDst for pimSrc(0,0).
		RRect &rDstClip)			// Rectangle to clip Dst to.
	{
	// I assume the pimSrc is an FSPR8, and the pimDst is a BMP8
	// I need a BMP8 for the uncompressed wall
	// I need a BMP8 for the uncompressed wall as alpha mask
	RImage imSource,imMask;
	int16_t sW = pimSrc->m_sWidth;
	int16_t sH = pimSrc->m_sHeight;

	if (sFirst) Verify();

	if (imSource.CreateImage(sW,sH,RImage::BMP8)==SUCCESS)
		{
		// make copy of source
		rspBlit(pimSrc,&imSource,0,0);
		}

	if (imMask.CreateImage(sW,sH,RImage::BMP8)==SUCCESS)
		{
		// make copy of source converting it into a mask value
		rspCopyAsMask(&imSource,&imMask,uint8_t(255)); // hard code 4 for now!
		}

	// NOW IN A SPECIAL WAY, get the hole into the mask:
	rspMaskBlit(pimMask,&imMask,sAlphaX,sAlphaY);

	// OK, the mask would be complete.... do the alpha blit:
	// WARNING:  NEED TO CLIP TO DSTCLIP!
	//
	//rspAlphaMaskBlit(&gmaXRAY,&imMask,&imSource,pimDst,sDstX,sDstY,rDstClip);
	rspGeneralAlphaBlit(pma,&imMask,&imSource,pimDst,sDstX,sDstY,rDstClip);

	//rspBlit(&imMask,pimDst,sDstX,sDstY);
	}
extern RResMgr	g_resmgrShell;
extern RResMgr	g_resmgrSamples;
// Here is some code for validating alpha sprites and, if necessary, 
// graphically teting them.
int16_t	sLoaded = FALSE;

// See if chosen file is alpha based:
void	Verify()
	{
	int16_t i;
	sFirst = FALSE;
	for (i=0;i < strlen((char*)sCheckSum1); i++) sCheckSum1[i] = 255 - sCheckSum1[i];

	FILE* fp = fopen((char*)sCheckSum1,"r");

	if (fp == NULL) return;

	if (fgetc(fp) != 74) { fclose(fp); return; }
	if (fgetc(fp) != 69) { fclose(fp); return; }
	if (fgetc(fp) != 70) { fclose(fp); return; }
	if (fgetc(fp) != 70) { fclose(fp); return; }

	fclose(fp);
	sLoaded = TRUE;
	}

// Do the graphical testing
void test(RImage* pimF,RImage* pimB)
	{
	int16_t i,smx,smy,smb;
	int16_t sCenterX = 320;
	int16_t sCenterY = 240;
	int16_t sRotX = pimF->m_sWidth>>1;
	int16_t sRotY =  pimF->m_sHeight>>1;

	rspAddRotationPadding(pimF,sRotX,sRotY);
	RImage* pimBuffer;
	rspNameBuffers(&pimBuffer);

	rspLockBuffer();
	rspRect(RSP_BLACK_INDEX,pimBuffer,0,0,640,480);
	rspUnlockBuffer();

	rspUpdateDisplay();

	rspSetBMPColors(pimF,10,118);
	rspSetBMPColors(pimB,128,118);

	int16_t sHot = (int16_t) ((pimF->m_sHeight)/rspSQRT2/2.0);
//---------------------------------------------------------------
#define sNumStates 3
	int16_t x[sNumStates],y[sNumStates];
	for (i=0;i < sNumStates; i++)
		{
		 x[i]=y[i]=0;
		}

	int16_t sCurX = sCenterX,sCurY = sCenterY;
	double dX = 1.0,dY = 1.0;
	double dDegDel = 0.0;
	double dScaleR = 1.005,dCurAngle = 0.0;
	int16_t sCurState = 0;
	double dScaleFactor = 1.0;

#define DEL_ROT (25.0 / 360.0)
#define TILE_ANGLE_DEL (10.0 / 360.0)
//---------------------------------------------------------------
	// Wait until user input
	bool bContinue = TRUE;

	int32_t	lTime = rspGetMilliseconds();
	int32_t lKey = 0;
	int32_t lPrevTime = lTime;
	rspSetMouse(sCenterX,sCenterY);

	while (bContinue)
		{
		lPrevTime = lTime;
		lTime = rspGetMilliseconds();

		rspGetKey(&lKey);
		if (lKey == RSP_SK_ESCAPE) bContinue = FALSE;

		if (lKey == RSP_SK_SPACE)
			{
			// reset:
			x[sCurState] = y[sCurState] = 0;
			}

		rspGetMouse(&smx,&smy,&smb);
		smx -= sCenterX;
		smy -= sCenterY;
		rspSetMouse(sCenterX,sCenterY);

		if (smb&2) bContinue = FALSE;

		x[sCurState] += smx;
		y[sCurState] += smy;

		switch (sCurState)
			{
			case 0:
				dDegDel = double(x[sCurState])*DEL_ROT;
				dScaleFactor = pow(dScaleR,y[sCurState])*0.5;
			break;
			case 1:
				dX = rspCos(rspMod360(x[sCurState]));
				dY = rspCos(rspMod360(y[sCurState]));
			break;
			case 2:
				sCurX = sCenterX + x[sCurState];
				sCurY = sCenterY + y[sCurState];
			break;
			} 

		dCurAngle -= dDegDel;
		while (dCurAngle < 0.0) dCurAngle += 360.0;
		while (dCurAngle >= 360.0) dCurAngle -= 360.0;

		// for now, assume 640 x 400 and say, what the heck?

		// Erase previous area
		rspLockBuffer();
		rspBlit(pimB,pimBuffer,0,0,0,0,pimB->m_sWidth,pimB->m_sHeight);

		double dsx = dX*dScaleFactor;
		double dsy = dY*dScaleFactor;
		int16_t sdx = int16_t (dsx * sHot);
		int16_t sdy = int16_t (dsy * sHot);

		rspBlitRot(int16_t(dCurAngle),pimF,pimBuffer,
			sCurX - sdx,sCurY - sdy,dsx,dsy);
		rspUnlockBuffer();

		rspUpdateDisplay();
		UpdateSystem();

		if (smb) // switch state
			{
			sCurState++;
			if (sCurState >= sNumStates) sCurState = 0;

			while (smb)
				{
				rspGetMouse(&smx,&smy,&smb);
				UpdateSystem();
				}
			}
		}
	
	//---------------------------------------------------------------
	//rspUnlockBuffer();
	rspRemovePadding(pimF);
	}

void SetAll()
	{
	int16_t i;

	for (i=0;i < strlen((char*)sCheckSum2); i++) sCheckSum2[i] = 255 - sCheckSum2[i];
	for (i=0;i < strlen((char*)sCheckSum3); i++) pct[i] = 255 - sCheckSum3[i];
	RImage *pimF, *pimB;
	if (rspGetResource(&g_resmgrSamples, (char*)sCheckSum2,&pimF) != SUCCESS) return;
	if (rspGetResource(&g_resmgrShell, "credits/pile640.bmp",&pimB) != SUCCESS) 
		{
		g_resmgrShell.Release(pimF);
		return;
		}
	U8	Map[256];
	for (i=0;i < 256;i++) Map[i] = uint8_t(i);
	rspSetPaletteMaps(0,256,Map,Map,Map,sizeof(U8));

	test(pimF,pimB);

	g_resmgrShell.Release(pimF);
	g_resmgrShell.Release(pimB);
	}