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

📄 game2.cpp

📁 一个简单的rpg游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <AfxWin.h>
#include <AfxExt.h>
#include <AfxTempl.h>

#include "game.h"

#include <mmsystem.h>
#pragma comment (lib,"winmm.lib")



void FadeIn(LPDIRECTDRAWSURFACE to,
			LPDIRECTDRAWSURFACE from,
			LPRECT lprc,LPDIRECTDRAW lpdd)
{
	DDSURFACEDESC desctest,descfrom;
	USHORT *lptest,*lpfrom;
	RECT rc;
	long i,j,n;
	long w=lprc->right-lprc->left;
	long h=lprc->bottom-lprc->top;

	SetRect(&rc,0,0,w,h);

	LPDIRECTDRAWSURFACE test=0;
	test=DDGetSurface(lpdd,w,h);
	if(from==0)
		return;
	test->BltFast(0,0,from,&rc,DDBLTFAST_WAIT);
	FillSurface( from,0 );

	long z=256;
	while(z>1)
	{
		desctest.dwSize=sizeof(desctest);
		descfrom.dwSize=sizeof(descfrom);

		test->Lock(0,&desctest,DDLOCK_WAIT,0);
		from->Lock(0,&descfrom,DDLOCK_WAIT,0);

		lptest=(USHORT *)desctest.lpSurface;
		lpfrom=(USHORT *)descfrom.lpSurface;

		for(j=0;j<h;j+=z)
			for(i=0;i<w;i+=z)
			{
				n=j*w+i;
				lpfrom[n]=lptest[n];
			}

		test->Unlock(0);
		from->Unlock(0);

		z/=2;

		CopySurfaceWithHdc(to,from,rc);
	}

	test->Release();
}



void FadeOut(LPDIRECTDRAWSURFACE to,
			LPDIRECTDRAWSURFACE from,
			LPRECT lprc,LPDIRECTDRAW lpdd)
{
	DDSURFACEDESC desctest,descfrom;
	USHORT *lptest,*lpfrom;
	RECT rc;
	long i,j,n;
	long w=lprc->right-lprc->left;
	long h=lprc->bottom-lprc->top;

	SetRect(&rc,0,0,w,h);
	CopySurfaceWithHdc(to,from,rc);

	LPDIRECTDRAWSURFACE test=0;
	test=DDGetSurface(lpdd,w,h);
	if(from==0)
		return;
	test->BltFast(0,0,from,&rc,DDBLTFAST_WAIT);

	long z=256;
	while(z>0)
	{
		from->BltFast(0,0,test,&rc,DDBLTFAST_WAIT);

		desctest.dwSize=sizeof(desctest);
		descfrom.dwSize=sizeof(descfrom);

		test->Lock(0,&desctest,DDLOCK_WAIT,0);
		from->Lock(0,&descfrom,DDLOCK_WAIT,0);

		lptest=(USHORT *)desctest.lpSurface;
		lpfrom=(USHORT *)descfrom.lpSurface;

		for(j=0;j<h;j+=z)
			for(i=0;i<w;i+=z)
			{
				n=j*w+i;
				lpfrom[n]=0;
			}

		test->Unlock(0);
		from->Unlock(0);

		z/=2;

		CopySurfaceWithHdc(to,from,rc);
	}

	test->Release();
}



void Tr(LPDIRECTDRAWSURFACE to,RECT trect,RECT ttest,
		LPDIRECTDRAWSURFACE from,RECT frect,RECT ftest,
		BYTE percent,USHORT colorkey,USHORT color)
{
	if( trect.right<0
		|| trect.bottom<0
		|| trect.left>ttest.right
		|| trect.top>ttest.bottom )
		return;

	//	set rect
	if(trect.left<0)
	{
		frect.left-=trect.left;
		trect.left=0;
	}

	if(trect.top<0)
	{
		frect.top-=trect.top;
		trect.top=0;
	}

	if(trect.right>ttest.right)
	{
		frect.right-=trect.right-ttest.right;
		trect.right=ttest.right;
	}

	if(trect.bottom>ttest.bottom)
	{
		frect.bottom-=trect.bottom-ttest.bottom;
		trect.bottom=ttest.bottom;
	}

	//	init
	static int x,y;
	static int m,n;
	static int tk,fk;

	x=trect.right-trect.left;
	y=trect.bottom-trect.top;

	DDSURFACEDESC desc_from,desc_to;
	USHORT *lp_from,*lp_to;

	//	to
	desc_to.dwSize=sizeof(desc_to);
	to->Lock(0,&desc_to,DDLOCK_WAIT|DDLOCK_WRITEONLY,0);
	lp_to=(USHORT *)desc_to.lpSurface;

	//	from
	if(from!=0)
	{
		desc_from.dwSize=sizeof(desc_from);
		from->Lock(0,&desc_from,DDLOCK_WAIT|DDLOCK_WRITEONLY,0);
		lp_from=(USHORT *)desc_from.lpSurface;
	}

	//	do
	for(n=0;n<y;n++)
		for(m=0;m<x;m++)
		{
			if(from==0)
			{
				tk=ttest.right*(trect.top+n)+trect.left+m;

				lp_to[tk]=TrApp(color,lp_to[tk],percent);
			}
			else
			{
				tk=ttest.right*(trect.top+n)+trect.left+m;
				fk=ftest.right*(frect.top+n)+frect.left+m;

				if(lp_from[fk]!=colorkey)
				{
					lp_to[tk]=TrApp(lp_from[fk],lp_to[tk],percent);
				}
			}
		}

	//	deinit
	to->Unlock(0);
	if(from!=0) from->Unlock(0);
}



USHORT TrApp(USHORT d,USHORT s,BYTE percent)
{
	USHORT r,g,b;
	USHORT rr,gg,bb;

	float alpha=(float)( (float)percent/(float)100 );

	//R 5
	r=d>>11;
	rr=s>>11;

	r=(USHORT)( alpha*rr + (1-alpha)*r );

	r=r<<11;

	//G 6
	g=(d>>5) & 63;
	gg=(s>>5) & 63;

	g=(USHORT)( alpha*gg + (1-alpha)*g );

	g=g<<5;

	//B 5
	b=d & 31;
	bb=s & 31;

	b=(USHORT)( alpha*bb + (1-alpha)*b );

	return 	(r ^ g ^ b);

}



BOOL FillSurface( LPDIRECTDRAWSURFACE surf, DWORD clr, RECT* rect )
{
	if (surf==0)
		return FALSE;

	DDBLTFX bltfx;
	ZeroMemory( &bltfx, sizeof(bltfx) );
	bltfx.dwSize = sizeof(bltfx);
	bltfx.dwFillColor = clr;

	HRESULT r;
	r=surf->Blt( rect, 0, 0, DDBLT_COLORFILL | DDBLT_WAIT, &bltfx );
	if (r==DD_OK)
		return TRUE;

	if (r==DDERR_NOBLTHW && clr==0 && rect==0)
	{
		DDSURFACEDESC desc;
		ZeroMemory( &desc, sizeof(desc) );
		desc.dwSize = sizeof(desc);
		desc.dwFlags=DDSD_WIDTH | DDSD_HEIGHT;
		if (surf->GetSurfaceDesc( &desc )!=DD_OK)
			return FALSE;
		int w=desc.dwWidth;
		int h=desc.dwHeight;

		r=surf->Lock( 0, &desc, DDLOCK_WAIT  | DDLOCK_WRITEONLY, 0 );
		if (r!=DD_OK)
			return FALSE;

		BYTE* surfbits = (BYTE*)desc.lpSurface;

		for (int i=0;i<h;i++)
		{
			ZeroMemory( surfbits, desc.lPitch );
			surfbits += desc.lPitch;
		}
		surf->Unlock( 0 );
	}
	
	return FALSE;
}



bool CopySurface( LPDIRECTDRAWSURFACE to,RECT trect,RECT test,
				 LPDIRECTDRAWSURFACE from,RECT frect,int colorkey)
{
	if (to==0 || from==0)
		return FALSE;

	if( trect.right<0
		|| trect.bottom<0
		|| trect.left>test.right
		|| trect.top>test.bottom )
		return FALSE;

	if(trect.left<0)
	{
		frect.left-=trect.left;
		trect.left=0;
	}

	if(trect.top<0)
	{
		frect.top-=trect.top;
		trect.top=0;
	}

	if(trect.right>test.right)
	{
		frect.right-=trect.right-test.right;
		trect.right=test.right;
	}

	if(trect.bottom>test.bottom)
	{
		frect.bottom-=trect.bottom-test.bottom;
		trect.bottom=test.bottom;
	}

	DWORD flags;
	flags=DDBLTFAST_WAIT;
	if (colorkey)
		flags |= DDBLTFAST_SRCCOLORKEY;
	to->BltFast( trect.left, trect.top, from, &frect, flags );

	return TRUE;
}



void CopySurfaceWithHdc(LPDIRECTDRAWSURFACE to,
						LPDIRECTDRAWSURFACE from,
						RECT rect)
{
	static	HDC	pdc,sdc;

	if( to->GetDC(&pdc) == DD_OK )
	{
		if( from->GetDC(&sdc) == DD_OK )
		{
			StretchBlt(pdc,
						rect.left,rect.top,
						rect.right,rect.bottom,
						sdc,
						rect.left,rect.top,
						rect.right,rect.bottom,
						SRCCOPY);

			from->ReleaseDC(sdc);
		}

		to->ReleaseDC(pdc);
	}
}



void AddString(char *to,char from[8],char *first,char *last)
{
	int i;
	int pt=0;

	memset(to,0,strlen(to));
	strcpy(to,first);
	pt=strlen(first);

	for(i=0;i<8;i++)
	{
		if(from[i]==' ')
			break;
	}
	strncpy(&to[pt],from,i);
	pt+=i;

	strcpy(&to[pt],last);
}



void ChangeRes(OBJECT_RES *res,OBJECT_RES_ASCII *ares)
{
	res->number=atol(ares->number);

	strncpy( res->objname,ares->objname,8 );
	strncpy( res->filename,ares->filename,8 );

	SetRect(&res->rect,0,0,atol(ares->rright),atol(ares->rbottom));

	SetRect(&res->apprect,
		0,0,atol(ares->arright),atol(ares->arbottom));

	res->load=atol(ares->load);
	res->colorkey=atol(ares->colorkey);
	res->rgb=RGB( atol(ares->r),atol(ares->g),atol(ares->b) );
}



void ChangeRes2(OBJECT_RES2 *res2,OBJECT_RES2_ASCII *ares2)
{
	res2->number=atol(ares2->number);

	res2->x=atol(ares2->x);
	res2->y=atol(ares2->y);
}



void ChangeAccRes(OBJECT *obj,long from,long to,
				  char *accmem,long acclength)
{
	long i;
	ACC_RES_ASCII accres;

	for(i=0;i<acclength;i++)
	{
		if(accmem[i]=='`')
		{
			memcpy(&accres,&accmem[i],sizeof(ACC_RES_ASCII));

			if( atol(accres.fromnumber)==from
				&& atol(accres.tonumber)==to )
			{
				break;
			}

		}
	}

	obj->res.number=atol(accres.number);
	obj->res2.number=obj->res.number;

	strncpy(obj->res.objname,accres.objname,8);
	strncpy(obj->res.filename,accres.filename,8);
	
	obj->res2.x=atol(accres.x);
	obj->res2.y=atol(accres.y);

}

⌨️ 快捷键说明

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