⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gp_alpha.cpp

📁 国产的RPG源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//==============================Additive==========================
//	additive混合相关函数
//================================================================

//****************************
//功能:两个点的Additive混合
//参数:源点,目标点
//返回:混合后的点
WORD Additive_Pixel(WORD sour, WORD dest)
{
	//C++版
	//简单了,明白了alpha混合,这个就不算什么了
	//不懂的话,恩,看看alpha混合的注释先
	rgbTemp = (((sour<<16)|sour) & rgbMask) + (((dest<<16)|dest) & rgbMask);
	rgbTemp = rgbTemp & rgbMask;
	return (WORD)((rgbTemp>>16)|rgbTemp);
}

//***************************
//功能:两个表面的Additive混合
//参数:(SS:目标表面 DS:源表面 Color_Key:透明色[0:无])
void AdditiveBlt(LPDIRECTDRAWSURFACE SS,int x,int y, LPDIRECTDRAWSURFACE DS,RECT rcRect, WORD Color_Key)
{
	WORD *Dest,*Sour;
	int t1,t2;
	int DestWidth, SourWidth;
	
	//取目标页面指针
	if( BeginDraw(DS) )
	{
		Dest=(WORD *)GraphBuffer;
		DestWidth=GraphPitch;
		EndDraw(DS);
	}

	//取目标页面指针
	if( BeginDraw(SS) )
	{
		Sour=(WORD *)GraphBuffer;
		SourWidth=GraphPitch;
		EndDraw(SS);
	}

	//边界检查
	if( x<0 ) 
	{  
		rcRect.left = rcRect.left - x; 
		x=0; 
	}

	if( y<0 ) 
	{  
		rcRect.top  = rcRect.top - y; 
		y=0; 
	}

	if( x+ rcRect.right - rcRect.left > ScreenWidth ) 
	{ 
		rcRect.right = rcRect.left + ScreenWidth - x; 
	}

	if( y+ rcRect.bottom - rcRect.top > ScreenHeight ) 
	{ 
		rcRect.bottom = rcRect.top + ScreenHeight - y; 
	}
	//看上面的吧
	t1=SourWidth*y+x;
	t2=DestWidth*rcRect.top+rcRect.left;
	int rectWidth=rcRect.right-rcRect.left;
	int SW=SourWidth-rectWidth;					//没有语言了
	int DW=DestWidth-rectWidth;

	if( Color_Key == 0 )	//无透明色检查
	{
		for(int i=0; i<rcRect.bottom-rcRect.top; i++)
		{
			for( int j=0; j<rectWidth; j++)
			{
				Sour[t1]=_Additive_Pixel(Sour[t1], Dest[t2]);
				t1++;
				t2++;
			}
			t1+=SW;
			t2+=DW;
		}
	}
	else	//透明色检查
	{
		for(int i=0; i<rcRect.bottom-rcRect.top; i++)
		{
			for( int j=0; j<rectWidth; j++)
			{
				if( Dest[t2] != Color_Key )
					Sour[t1]=_Additive_Pixel(Sour[t1], Dest[t2]);
				t1++;
				t2++;
			}
			t1+=SW;
			t2+=DW;
		}
	}
}

//==============================Gray=============================
//	'RGB->灰度'相关函数
//================================================================

//****************************
//功能:把一个RGB点的变成灰度
//参数:源点
//返回:混合后的点
WORD Gray_Pixel(WORD sour)
{
	WORD t;						//看看那个inline版的注释……
	WORD r, g, b;
	r= sour >> RMove;
	g= (GMask & sour) >> GMove;
	b= BMask & sour;
	t= (r*3 + g*6 + b)/10;

	/*
	//汇编版					//同样给个注释吧,本来我是想把这段给删掉的
	_asm{
		mov ax, sour			//把sour放到ax中
		mov bx, ax				//放到bx中
		mov dx, ax				//放到dx中
		
		mov cl, RMove			//移位数目
		shr ax, cl				//逻辑右移
		
		and bx, GMask			//与上绿色的掩码
		mov cl,GMove			//……
		shr bx, cl				//……
		
		and dx, BMask			//与上蓝色的掩码
		
		add ax, bx				
		add ax, dx				//加到一起
		mov bl, 3
		div bl					//除以3:和上面的算法不一样
		xor ah, ah				//ah放的是余数,不需要的,清0就是了
		mov t, ax				//放到t中
	}
*/	
	return (t<<RMove)|(t<<GMove)|t;	//合成
}

//****************************
//功能:把图片变成灰色后BLT
//参数:(SS:目标表面 DS:目标表面 Color_Key:透明色[0:无])
void Gray_Blt(LPDIRECTDRAWSURFACE SS,int x,int y, LPDIRECTDRAWSURFACE DS,RECT rcRect,WORD Color_Key)
{
  	WORD *Dest,*Sour;
	int DestWidth,SourWidth;
	int t1,t2;
	
	//取目标页面指针
	if( BeginDraw(DS) )
	{
		Dest=(WORD *)GraphBuffer;
		DestWidth=GraphPitch;
		EndDraw(DS);
	}
	
	//取目标页面指针
	if( BeginDraw(SS) )
	{
		Sour=(WORD *)GraphBuffer;
		SourWidth=GraphPitch;
		EndDraw(SS);
	}

	//边界检查
	if( x<0 ) 
	{  
		rcRect.left = rcRect.left - x; 
		x=0; 
	}

	if( y<0 ) 
	{  
		rcRect.top  = rcRect.top - y; 
		y=0; 
	}

	if( x+ rcRect.right - rcRect.left > ScreenWidth ) 
	{ 
		rcRect.right = rcRect.left + ScreenWidth - x; 
	}

	if( y+ rcRect.bottom - rcRect.top > ScreenHeight ) 
	{ 
		rcRect.bottom = rcRect.top + ScreenHeight - y; 
	}
	//………………
	t1=SourWidth*y+x;
	t2=DestWidth*rcRect.top+rcRect.left;
	int rectWidth=rcRect.right-rcRect.left;
	int SW=SourWidth-rectWidth;				
	int DW=DestWidth-rectWidth;

	if( Color_Key == 0 )	//没有透明色
	{
		for(int i=0; i<rcRect.bottom-rcRect.top; i++)
		{
			for( int j=0; j<rectWidth; j++)
			{
				Sour[t1]=_Gray_Pixel(Dest[t2] );
				t1++;
				t2++;
			}
			t1+=SW;
			t2+=DW;
		}
	}
	else	//带透明色
	{
		for(int i=0; i<rcRect.bottom-rcRect.top; i++)
		{
			for( int j=0; j<rectWidth; j++)
			{
				if( Dest[t2] != Color_Key )
					Sour[t1]=_Gray_Pixel(Dest[t2] );
				t1++;
				t2++;
			}
			t1+=SW;
			t2+=DW;
		}
	}
}

//==============================单色=============================
//	单色处理相关函数
//================================================================

//**************************************
//功能:把图片变成单色(color)后BLT Color=颜色
//参数:(SS:目标表面 DS:目标表面 Color_Key:透明色[0:无] Color=颜色)
void Color_Blt(LPDIRECTDRAWSURFACE SS,int x,int y, LPDIRECTDRAWSURFACE DS,RECT rcRect,WORD Color_Key, WORD Color)
{
  	WORD *Dest,*Sour;
	int DestWidth,SourWidth;
	int t1,t2;
	
	//取目标页面指针
	if( BeginDraw(DS) )
	{
		Dest=(WORD *)GraphBuffer;
		DestWidth=GraphPitch;
		EndDraw(DS);
	}
	
	//取目标页面指针
	if( BeginDraw(SS) )
	{
		Sour=(WORD *)GraphBuffer;
		SourWidth=GraphPitch;
		EndDraw(SS);
	}

	//边界检查
	if( x<0 ) 
	{  
		rcRect.left = rcRect.left - x; 
		x=0; 
	}

	if( y<0 ) 
	{  
		rcRect.top  = rcRect.top - y; 
		y=0; 
	}

	if( x+ rcRect.right - rcRect.left > ScreenWidth ) 
	{ 
		rcRect.right = rcRect.left + ScreenWidth - x;
	}

	if( y+ rcRect.bottom - rcRect.top > ScreenHeight ) 
	{ 
		rcRect.bottom = rcRect.top + ScreenHeight - y; 
	}
	//…………
	t1=SourWidth*y+x;
	t2=DestWidth*rcRect.top+rcRect.left;
	int rectWidth=rcRect.right-rcRect.left;

	int SW=SourWidth-rectWidth;
	int DW=DestWidth-rectWidth;

	for(int i=0; i<rcRect.bottom-rcRect.top; i++)
	{
		for( int j=0; j<rectWidth; j++)
		{
			if( Dest[t2] != Color_Key )		//只要不是透明色就变成所要求的单色
				Sour[t1]=Color;
			t1++;
			t2++;
		}
		t1+=SW;
		t2+=DW;
	}
}

////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////
//**************************************
//功能:查找精灵边缘
//参数:精灵页面,边缘色,透明色
void FindEdge(LPDIRECTDRAWSURFACE lpDDSPict, WORD Color, WORD CK)
{
	int n, i, j;

	if( BeginDraw(lpDDSPict) )
	{
		WORD *Pict=GraphBuffer;
		int Width=GraphWidth;
		int Height=GraphHeight;
		int Pitch=GraphPitch;

		for(i=0; i<Height; i++)
			for(j=0; j<Width; j++)
			{
				n=Pitch*i+j;			//按顺序扫描点
				if( Pict[n]!=CK && (Pict[n+1]==CK || Pict[n-1]==CK 
						|| Pict[n+Pitch]==CK || Pict[n-Pitch]==CK ) )
				{
					Pict[n]=Color;			//这个点不是透明的,但是他的周围四个点有一个
				}							//是透明的。
				else						//左右是加减一
					Pict[n]=CK;				//上下是加减Pitch
			}								//边缘变成指定的颜色
		EndDraw(lpDDSPict);					//其他都变成透明色
	}
}

//**************************************
//功能:Blt精灵边缘
//参数:(SS:目标表面 DS:源表面 CK:透明色 Color:边缘颜色)
void Edge_Blt(LPDIRECTDRAWSURFACE SS,int x,int y, LPDIRECTDRAWSURFACE DS,RECT rcRect,WORD CK, WORD Color)
{
  	WORD *Dest,*Sour;
	int DestWidth,SourWidth;
	int t1,t2;
	
	//取源页面指针
	if( BeginDraw(DS) )
	{
		Dest=(WORD *)GraphBuffer;
		DestWidth=GraphPitch;
		EndDraw(DS);
	}
	
	//取目标页面指针
	if( BeginDraw(SS) )
	{
		Sour=(WORD *)GraphBuffer;
		SourWidth=GraphPitch;
		EndDraw(SS);
	}

	//边界检查
	if( x<0 ) 
	{  
		rcRect.left = rcRect.left - x; 
		x=0; 
	}

	if( y<0 ) 
	{  
		rcRect.top  = rcRect.top - y; 
		y=0;
	}

	if( x+ rcRect.right - rcRect.left > ScreenWidth ) 
	{ 
		rcRect.right = rcRect.left + ScreenWidth - x; 
	}

	if( y+ rcRect.bottom - rcRect.top > ScreenHeight ) 
	{ 
		rcRect.bottom = rcRect.top + ScreenHeight - y; 
	}
	//这样的用法在这个文件里有很多了
	t1=SourWidth*y+x;
	t2=DestWidth*rcRect.top+rcRect.left;
	int rectWidth=rcRect.right-rcRect.left;

	int SW=SourWidth-rectWidth;
	int DW=DestWidth-rectWidth;

	for(int i=0; i<rcRect.bottom-rcRect.top; i++)
	{
		for( int j=0; j<rectWidth; j++)
		{
			//和上个函数一样的逻辑,只不过改变的对象不同而已
			if( Dest[t2]!=CK && (Dest[t2+1]==CK || Dest[t2-1]==CK 
						|| Dest[t2+DestWidth]==CK || Dest[t2-DestWidth]==CK ) )
				Sour[t1]=Color;
			t1++;
			t2++;
		}
		t1+=SW;
		t2+=DW;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -