shithub: candycrisis

Download patch

ref: 56147227bf950b6bd6d16d033ad0e75e31df470d
parent: 0e77bca8d56825756c965f9788d3758722caf3a0
author: Iliyas Jorio <[email protected]>
date: Tue Jul 28 19:26:07 EDT 2020

Make 1-bit masks 8-bit for nvidia compat on linux

--- a/src/SDLU.cpp
+++ b/src/SDLU.cpp
@@ -22,7 +22,6 @@
 static SDL_Surface* s_acquireList[k_acquireMax];
 
 // for initsurface
-static SDL_Palette* s_oneBitPalette;
 static SDL_Palette* s_grayscalePalette;
 
 // for button and getmouse
@@ -145,14 +144,6 @@
 {
     SDL_SetEventFilter(SDLUi_EventFilter, NULL);
 
-    // Initialize one bit palette.
-    static const SDL_Color  k_oneBitColors[2] = { { 0xFF, 0xFF, 0xFF, 0x00 },
-                                                  { 0x00, 0x00, 0x00, 0x00 }  };
-
-    s_oneBitPalette = SDL_AllocPalette(2);
-    
-    SDL_SetPaletteColors(s_oneBitPalette, k_oneBitColors, 0, arrsize(k_oneBitColors));
-
     // Initialize eight bit grayscale ramp palette.
     SDL_Color  grayscaleColors[256];
     for (int index=0; index<256; index++)
@@ -168,49 +159,6 @@
 }
 
 
-static void	SDLUi_Blit8BitTo1Bit( SDL_Surface* surface8, SDL_Rect* rect8,
-                                  SDL_Surface* surface1, SDL_Rect* rect1  )
-{
-	// NOTE: for now this copy assumes that we're copying the whole thing.
-	// That's true for everything I'm doing.
-	
-	int          x, y, across, down;
-	SDL_Color*   palette8;
-
-	(void) rect8; // is unused for now
-	
-//	ASSERTN( surface8->format->BitsPerPixel == 8, surface8->format->BitsPerPixel );
-//	ASSERTN( surface1->format->BitsPerPixel == 1, surface8->format->BitsPerPixel );
-//	ASSERT( rect8->w == rect1->w );
-//	ASSERT( rect8->h == rect1->h );
-	
-	palette8 = surface8->format->palette->colors;
-	down     = rect1->h;
-	across   = (rect1->w + 7) & ~7;
-	
-	for( y=0; y<down; y++ )
-	{
-		unsigned char* src = (unsigned char*) surface8->pixels + (y * surface8->pitch);
-		unsigned char* dst = (unsigned char*) surface1->pixels + (y * surface1->pitch);
-		
-		for( x=0; x<across; x+=8 )
-		{
-			*dst = (palette8[src[0]].r? 0: 0x80) |
-			       (palette8[src[1]].r? 0: 0x40) |
-			       (palette8[src[2]].r? 0: 0x20) |
-			       (palette8[src[3]].r? 0: 0x10) |
-			       (palette8[src[4]].r? 0: 0x08) |
-			       (palette8[src[5]].r? 0: 0x04) |
-			       (palette8[src[6]].r? 0: 0x02) |
-			       (palette8[src[7]].r? 0: 0x01)   ;
-			
-			dst += 1;
-			src += 8;
-		}
-	}
-}
-
-
 SDL_Rect* SDLU_MRectToSDLRect( const MRect* in, SDL_Rect* out )
 {
 	int t = in->top, l = in->left, b = in->bottom, r = in->right;
@@ -240,14 +188,6 @@
 int SDLU_BlitSurface( SDL_Surface* src, SDL_Rect* srcrect,
 			          SDL_Surface* dst, SDL_Rect* dstrect  )
 {
-	if( src->format->BitsPerPixel == 8 && dst->format->BitsPerPixel == 1 )
-	{
-		// SDL BUG!! SDL cannot blit 8-bit to 1-bit surfaces.
-		SDLUi_Blit8BitTo1Bit( src, srcrect,
-		                      dst, dstrect  );
-		return 0;
-	}
-   
 	// Let SDL handle this.
 	return SDL_BlitSurface( src, srcrect,
 	                        dst, dstrect  );
@@ -324,17 +264,6 @@
 							0, 0, 0, 0 );
 
             SDL_SetSurfacePalette(surface, s_grayscalePalette);
-			break;
-		
-		case 1:
-			surface = SDL_CreateRGBSurface( 
-							SDL_SWSURFACE, 
-							rect->w, 
-							rect->h, 
-							1, 
-							0, 0, 0, 0 );
-			
-            SDL_SetSurfacePalette(surface, s_oneBitPalette);
 			break;
             
         default:
--- a/src/SDLU.h
+++ b/src/SDLU.h
@@ -21,6 +21,8 @@
 #define BITS_PER_3CHANNELS 24
 
 #define BYTES_PER_PIXEL    4
+#define BYTES_PER_MASK_PIXEL 1
+#define MASK_DEPTH 8
 
 void         SDLU_Init();
 SDL_Rect*    SDLU_MRectToSDLRect( const MRect* in, SDL_Rect* out );
--- a/src/blitter.cpp
+++ b/src/blitter.cpp
@@ -117,7 +117,6 @@
                       const MRect*  objectRect, const MRect*  maskRect, const MRect*  destRect )
 {
 	int            startX = 0, startY = 0, endX, endY, x, y, srcRowBytes, mskRowBytes, dstRowBytes;
-	unsigned int   bit, startBit, maskBits;
 	unsigned char *src, *msk, *dst;
 	MRect          destBounds;
 
@@ -142,7 +141,7 @@
 	dstRowBytes = dest->pitch;
 	
 	src += (objectRect->top * srcRowBytes) + (objectRect->left * BYTES_PER_PIXEL);
-	msk += (maskRect->top   * mskRowBytes) + (maskRect->left   / 8);
+	msk += (maskRect->top   * mskRowBytes) + (maskRect->left   * BYTES_PER_MASK_PIXEL);
 	dst += (destRect->top   * dstRowBytes) + (destRect->left   * BYTES_PER_PIXEL);
 	
 	if( destRect->left   < destBounds.left   ) startX -= destRect->left - destBounds.left;
@@ -150,8 +149,7 @@
 	if( destRect->top    < destBounds.top    ) startY -= destRect->top - destBounds.top;
 	if( destRect->bottom > destBounds.bottom ) endY   -= destRect->bottom - destBounds.bottom;
 	
-	startBit = 0x80000000 >> (startX & 31);
-	msk += (mskRowBytes * startY) + ((startX & ~31) / 8);
+	msk += (mskRowBytes * startY) + (startX * BYTES_PER_MASK_PIXEL);
 	src += (srcRowBytes * startY) + (startX * BYTES_PER_PIXEL);
 	dst += (dstRowBytes * startY) + (startX * BYTES_PER_PIXEL);
 
@@ -159,18 +157,16 @@
 	{
 		unsigned char *tSrc = src, *tDst = dst, *tMsk = msk;
 		
-		maskBits = SDL_SwapBE32( *(unsigned int *)msk );
-		bit = startBit;
-		
 		for( x=startX; x<endX; x++ )
 		{
-			if( maskBits & bit )
+			if( *msk )
 			{
 				*(COLOR_T*)dst = *(COLOR_T*)src;
 			}
 			
-			if( !(bit >>= 1) ) { msk += 4; maskBits = SDL_SwapBE32( *(unsigned int *)msk ); bit = 0x80000000; }
-			src += BYTES_PER_PIXEL; dst += BYTES_PER_PIXEL;
+			src += BYTES_PER_PIXEL;
+			dst += BYTES_PER_PIXEL;
+			msk += BYTES_PER_MASK_PIXEL;
 		}
 		
 		src = tSrc + srcRowBytes;
@@ -192,7 +188,6 @@
                        int r, int g, int b, int weight )
 {
 	int            startX = 0, startY = 0, endX, endY, x, y, mskRowBytes, dstRowBytes;
-	unsigned int   bit, startBit, maskBits;
 	unsigned char *msk, *dst;
 	MRect          destBounds;
 	
@@ -214,7 +209,7 @@
 	mskRowBytes = mask->pitch;
 	dstRowBytes = dest->pitch;
 	
-	msk += (maskRect->top * mskRowBytes) + (maskRect->left / 8);
+	msk += (maskRect->top * mskRowBytes) + (maskRect->left * BYTES_PER_MASK_PIXEL);
 	dst += (destRect->top * dstRowBytes) + (destRect->left * BYTES_PER_PIXEL);
 	
 	if( destRect->left   < destBounds.left   ) startX -= destRect->left - destBounds.left;
@@ -222,8 +217,7 @@
 	if( destRect->top    < destBounds.top    ) startY -= destRect->top - destBounds.top;
 	if( destRect->bottom > destBounds.bottom ) endY   -= destRect->bottom - destBounds.bottom;
 	
-	startBit = 0x80000000 >> (startX & 31);
-	msk += (mskRowBytes * startY) + ((startX & ~31) / 8);
+	msk += (mskRowBytes * startY) + (startX * BYTES_PER_MASK_PIXEL);
 	dst += (dstRowBytes * startY) + (startX * BYTES_PER_PIXEL);
 
 	r *= weight;
@@ -236,12 +230,9 @@
 		unsigned char *tMsk = msk, *tDst = dst;
 		int work, workB, workG, workR;
 		
-		maskBits = SDL_SwapBE32( *(unsigned int *)msk );
-		bit = startBit;
-				
 		for( x=startX; x<endX; x++ )
 		{
-			if( maskBits & bit )
+			if( *msk )
 			{
 				work = *(COLOR_T*)dst;
                 workB = ((((work                    ) & CHANNEL_MASK)*weight) + b) >> BITS_PER_1CHANNEL;
@@ -251,8 +242,8 @@
                 *(COLOR_T*)dst = (workR & RED_MASK) | (workG & GREEN_MASK) | (workB & BLUE_MASK);
 			}
 			
-			if( !(bit >>= 1) ) { msk += 4; maskBits = SDL_SwapBE32( *(unsigned int *)msk ); bit = 0x80000000; }
 			dst += BYTES_PER_PIXEL;
+			msk += BYTES_PER_MASK_PIXEL;
 		}
 		
 		msk = tMsk + mskRowBytes;
@@ -356,7 +347,6 @@
 {
 	int startX = 0, startY = 0, endX, endY, x, y,
 	    srcRowBytes, alfRowBytes, mskRowBytes, dstRowBytes, bckRowBytes;
-	unsigned int   bit, startBit, maskBits;
 	unsigned char *bck, *src, *alf, *msk, *dst;
 	MRect destBounds;
 
@@ -389,7 +379,7 @@
 	src += (sourceRect->top * srcRowBytes) + (sourceRect->left * BYTES_PER_PIXEL);
 	alf += (alphaRect->top  * alfRowBytes) + (alphaRect->left  * BYTES_PER_PIXEL);
 	dst += (destRect->top   * dstRowBytes) + (destRect->left   * BYTES_PER_PIXEL);
-	msk += (maskRect->top   * mskRowBytes) + (maskRect->left   / 8);
+	msk += (maskRect->top   * mskRowBytes) + (maskRect->left   * BYTES_PER_MASK_PIXEL);
 	
 	if( destRect->left   < destBounds.left   ) startX -= destRect->left - destBounds.left;
 	if( destRect->right  > destBounds.right  ) endX   -= destRect->right - destBounds.right;
@@ -400,8 +390,7 @@
 	src += (srcRowBytes * startY) + (startX * BYTES_PER_PIXEL);
 	alf += (alfRowBytes * startY) + (startX * BYTES_PER_PIXEL);
 	dst += (dstRowBytes * startY) + (startX * BYTES_PER_PIXEL);
-	msk += (mskRowBytes * startY) + ((startX & ~31) / 8);
-	startBit = 0x80000000 >> (startX & 31);
+	msk += (mskRowBytes * startY) + (startX * BYTES_PER_MASK_PIXEL);
 	
 	for( y=startY; y<endY; y++ )
 	{
@@ -408,12 +397,9 @@
 		unsigned char *tSrc = src, *tAlf = alf, *tDst = dst, *tBck = bck, *tMsk = msk;
 		int pixS, pixB, weightS, weightB, work, workB, workG, workR;
 		
-		maskBits = SDL_SwapBE32( *(unsigned int *)msk );
-		bit = startBit;
-		
 		for( x=startX; x<endX; x++ )
 		{
-			if( maskBits & bit )
+			if( *msk )
 			{
 				work = *(COLOR_T*)alf;
 				workB = ((((work                    ) & CHANNEL_MASK)*inWeight) ) >> BITS_PER_1CHANNEL;
@@ -445,11 +431,11 @@
 				}
 			}
 
-			if( !(bit >>= 1) ) { msk += 4; maskBits = SDL_SwapBE32( *(unsigned int *)msk ); bit = 0x80000000; }
 			src += BYTES_PER_PIXEL;
 			alf += BYTES_PER_PIXEL;
 			bck += BYTES_PER_PIXEL;
 			dst += BYTES_PER_PIXEL;
+			msk += BYTES_PER_MASK_PIXEL;
 		}
 		
 		bck = tBck + bckRowBytes;
--- a/src/gworld.cpp
+++ b/src/gworld.cpp
@@ -40,10 +40,9 @@
 		boardSurface[1] = LoadPICTAsSurface( picBoard, 32 );
 	
 	// Get blob worlds
-
 	blobSurface = LoadPICTAsSurface( picBlob, 32 );
-	maskSurface = LoadPICTAsSurface( picBlobMask, 1 );
-	charMaskSurface = LoadPICTAsSurface( picCharMask, 1 );
+	maskSurface = LoadPICTAsSurface( picBlobMask, MASK_DEPTH );
+	charMaskSurface = LoadPICTAsSurface( picCharMask, MASK_DEPTH );
 
 	// Get blast worlds
 	
--- a/src/opponent.cpp
+++ b/src/opponent.cpp
@@ -37,7 +37,7 @@
 	opponentSurface     = SDLU_InitSurface( SDLU_MRectToSDLRect( &bigRect, &sdlRect ), 32 );
 
 	bigRect.bottom *= kGlows + 1;
-	opponentMaskSurface = SDLU_InitSurface( SDLU_MRectToSDLRect( &bigRect, &sdlRect ), 1 );
+	opponentMaskSurface = SDLU_InitSurface( SDLU_MRectToSDLRect( &bigRect, &sdlRect ), MASK_DEPTH );
 	
 	opponentWindowZRect.top = opponentWindowZRect.left = 0;
 	opponentWindowZRect.bottom = opponentWindowZRect.right = 64;
--- a/src/pause.cpp
+++ b/src/pause.cpp
@@ -1040,7 +1040,7 @@
 	// Get some graphics that we're going to need
 	logoSurface      = LoadPICTAsSurface( picLogo, 32 );
 	logoAlphaSurface = LoadPICTAsSurface( picLogoAlpha, 32 );
-	logoMaskSurface  = LoadPICTAsSurface( picLogoMask, 1 );
+	logoMaskSurface  = LoadPICTAsSurface( picLogoMask, MASK_DEPTH );
 
 	// Get a copy of the current game window contents
 	backSurface      = SDLU_InitSurface( &fullSDLRect, 32 );
--- a/src/score.cpp
+++ b/src/score.cpp
@@ -48,7 +48,7 @@
 	
 	numberSurface = LoadPICTAsSurface( picNumber, 32 );
 
-	numberMaskSurface = LoadPICTAsSurface( picNumberMask, 1 );
+	numberMaskSurface = LoadPICTAsSurface( picNumberMask, MASK_DEPTH );
 	
 	displayedScore[0] = displayedScore[1] = 0;
 	score[0]          = score[1]          = 0;