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

📄 freedraw.cpp

📁 游戏编程很好的东西大家看看啊可以学到很多东西
💻 CPP
字号:
// FreeDraw.cpp : Implementation of CFreeDraw
#include "stdafx.h"
#include "GameEngineATL.h"
#include "FreeDraw.h"

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////开始工具函数区/////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*---------------------------------------------------------------------------------
功能:创建调色板
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
int CFreeDraw::CreatePalatte()
{
	memset( &m_palatte_entry, 0, sizeof( PALETTEENTRY ) * MAX_PALETTE_COLORS );

	for( int i = 1; i < MAX_PALETTE_COLORS; i++ )
	{
		if( i < 64 )
		{
			m_palatte_entry[i].peRed = i * 4;
		}
		else if( i >= 64 && i < 128 )
		{
			m_palatte_entry[i].peGreen = ( i - 64 ) * 4;
		}
		else if( i >= 128 && i > 192 )
		{
			m_palatte_entry[i].peBlue = ( i - 128 ) * 4;
		}
		else
		{
			m_palatte_entry[i].peRed =
			m_palatte_entry[i].peRed =
			m_palatte_entry[i].peRed = ( i - 192 ) * 4;
		}

		m_palatte_entry[i].peFlags = PC_NOCOLLAPSE;
	}

	m_palatte_entry[0].peRed	= 0;
	m_palatte_entry[0].peGreen	= 0;
	m_palatte_entry[0].peBlue	= 0;
	m_palatte_entry[0].peFlags	= PC_NOCOLLAPSE;
	m_palatte_entry[255].peRed	= 255;
	m_palatte_entry[255].peGreen= 255;
	m_palatte_entry[255].peBlue	= 255;
	m_palatte_entry[255].peFlags= PC_NOCOLLAPSE;



	HRESULT hr;

	hr = m_dd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_INITIALIZE | DDPCAPS_ALLOW256,m_palatte_entry,&m_palette,NULL);

	if( hr != DD_OK )
	{
		return 1;
	}

	hr = m_prim_surface->SetPalette( m_palette );

	if( hr != DD_OK )
	{
		return 2;
	}

	return 0;
}

/*---------------------------------------------------------------------------------
功能:剪切页面
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
int CFreeDraw::SetClipper(LPDIRECTDRAWSURFACE7 lpdds, int nNum, LPRECT clip_list )
{
	LPDIRECTDRAWCLIPPER		lp_clip		= NULL;

	int						nIndex;
	LPRGNDATA				region;

	if( FAILED(m_dd->CreateClipper(0, &lp_clip, NULL)) )
	{
		return 1;
	}

	region = (LPRGNDATA)malloc( sizeof(RGNDATAHEADER) + nNum * sizeof( RECT ) );

	memcpy(region->Buffer,clip_list,sizeof(RECT)*nNum);

	region->rdh.dwSize			= sizeof( RGNDATAHEADER );
	region->rdh.iType			= RDH_RECTANGLES;
	region->rdh.nCount			= nNum;
	region->rdh.nRgnSize		= nNum * sizeof( RECT );
	region->rdh.rcBound.left	= 6400;
	region->rdh.rcBound.top		= 6400;
	region->rdh.rcBound.right	= -6400;
	region->rdh.rcBound.bottom	= -6400;

	for( nIndex = 0; nIndex < nNum; nIndex++ )
	{
		if( region->rdh.rcBound.left > clip_list[nIndex].left )
		{
			region->rdh.rcBound.left = clip_list[nIndex].left;
		}

		if( region->rdh.rcBound.top > clip_list[nIndex].top )
		{
			region->rdh.rcBound.top = clip_list[nIndex].top;
		}

		if( region->rdh.rcBound.right < clip_list[nIndex].right )
		{
			region->rdh.rcBound.right = clip_list[nIndex].right;
		}

		if( region->rdh.rcBound.bottom < clip_list[nIndex].bottom )
		{
			region->rdh.rcBound.bottom = clip_list[nIndex].bottom;
		}
	}

	if( FAILED( lp_clip->SetClipList( region, 0 ) ) )
	{
		free( region );

		return 2;
	}

	if( FAILED( lpdds->SetClipper( lp_clip ) ))
	{
		free( region );

		return 3;
	}

	free( region );

	m_clipper = lp_clip;

	return 0;
}

int CFreeDraw::UpdatePalette(PALETTEENTRY* lp_palette_entry)
{
	if ( FAILED( m_palette->SetEntries( 0, 0, MAX_PALETTE_COLORS, lp_palette_entry ) ) )
	{
		return 1;
	}

	return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////开始工具函数区/////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/*---------------------------------------------------------------------------------
功能:初始化DirectDraw
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDraw::Init(ULONG hWnd, int nWidth, int nHeight, int nBit)
{
	m_h_wnd		= (HWND)hWnd;

	m_n_bit		= nBit;
	m_n_width	= nWidth;
	m_n_height	= nHeight;

	/////////////////////////////////////////////////////////

	HRESULT					result;
	DDSCAPS2				caps;
	DDSURFACEDESC2			desc;

	result = DirectDrawCreateEx( NULL, (void**)&m_dd, IID_IDirectDraw7, NULL );

	if( result != DD_OK )
	{
		m_log.SaveMessage("创建DirectDraw对象失败!");

		return 1;
	}


	result = m_dd->SetCooperativeLevel(m_h_wnd,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT | DDSCL_ALLOWMODEX );

	if( result != DD_OK )
	{
		m_log.SaveMessage( "设置协调层级失败!" );

		return 2;
	}


	result = m_dd->SetDisplayMode( m_n_width, m_n_height, m_n_bit, 0, 0 );

	if( result != DD_OK )
	{
		m_log.SaveMessage( "设置显示模式失败!" );

		return 3;
	}

	//创建主页面
	INIT_STRUCT( desc );
	desc.dwFlags			= DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
	desc.dwBackBufferCount  = 1;
	desc.ddsCaps.dwCaps		= DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;

	result = m_dd->CreateSurface( &desc, &m_prim_surface, NULL );

	if( result != DD_OK )
	{
		m_log.SaveMessage( "创建主页面失败!" );

		return 4;
	}

	//副缓冲区
	memset( &caps, 0, sizeof( caps ) );

	caps.dwCaps = DDSCAPS_BACKBUFFER;

	result = m_prim_surface->GetAttachedSurface( &caps, &m_back_surface );

	if( result != DD_OK )
	{
		m_log.SaveMessage( "创建次页面失败!" );

		return 5;
	}

	//如果是8bit,就要创建调色板
	if( m_n_bit == 8 )
	{
		if(CreatePalatte() != 0)
		{
			m_log.SaveMessage( "创建调色板失败!" );

			return 6;
		}
	}

	//设置剪切区
	RECT rect[] = {100, 100, m_n_width - 100, m_n_height - 100 };
	if( SetClipper( m_back_surface, 1, rect ) != 0 )
	{
		m_log.SaveMessage("设置剪切区失败!");

		return 7;
	}

	m_log.SaveMessage( "DirectDraw组件初始化成功!" );

	return 0;	
}

/*---------------------------------------------------------------------------------
功能:清除DirectDraw资源
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDraw::UnInit()
{
	SAFE_RELEASE( m_clipper );
	SAFE_RELEASE( m_palette );
	SAFE_RELEASE( m_back_surface );
	SAFE_RELEASE( m_prim_surface );
	SAFE_RELEASE( m_dd );

	return 0;
}

/*---------------------------------------------------------------------------------
功能:剪切页面
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDraw::SetClipper(ULONG lp_surface, int nNum, ULONG lp_rect)
{
	int nRet;

	nRet = SetClipper( (LPDIRECTDRAWSURFACE7)lp_surface, nNum, (LPRECT)lp_rect );

	return nRet;
}

/*---------------------------------------------------------------------------------
功能:更新调色板
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDraw::UpdatePalette(ULONG lp_palette_entry)
{
	int nRet;

	nRet = UpdatePalette( (PALETTEENTRY*)lp_palette_entry );

	return nRet;
}

/*---------------------------------------------------------------------------------
功能:直接在副页面上画随机的点-------8位
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDraw::DrawRandPixel_008(int nCount)
{
	int x,y;

	int				mempitch	= 0;
	unsigned char*	lp_buf		= NULL;

	HRESULT hr;

	int count = nCount;

	DDSURFACEDESC2	dd;


	INIT_STRUCT( dd );

	hr = m_back_surface->Lock(NULL,&dd,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);

	if(hr != DD_OK)
	{
		m_log.SaveMessage("锁定表面失败!");

		return 1;
	}


	mempitch	= dd.lPitch;

	lp_buf		= (unsigned char*)dd.lpSurface;

	int	 nBitSize = dd.lPitch * m_n_height;

	__asm
	{
		CLD							; clear direction of copy to forward
		MOV EAX, 0					; color goes here
		MOV ECX, nBitSize			; number of DWORDS goes here
		MOV EDI, lp_buf				; address of line to move data
		REP STOSB					; send the pentium X on its way
	}

	srand( GetTickCount() );

	for(int i = 0; i < count; i++ )
	{
		UCHAR color = rand() % MAX_PALETTE_COLORS;

		x = rand() % m_n_width;

		y = rand() % m_n_height;

		lp_buf[ x + y * mempitch ] = color;
	}

	hr = m_back_surface->Unlock(NULL);

	if(hr != DD_OK)
	{
		m_log.SaveMessage("解锁定表面失败!");

		return 2;
	}

	return 0;
}

/*---------------------------------------------------------------------------------
功能:切换副页面为主页面
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDraw::Flip()
{
	while( m_prim_surface->Flip( NULL, DDFLIP_WAIT ) != DD_OK );

	return 0;
}

⌨️ 快捷键说明

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