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

📄 新建 文本文档.txt

📁 游戏编程很好的东西大家看看啊可以学到很多东西
💻 TXT
📖 第 1 页 / 共 2 页
字号:
	nHeight	= lp_bitmap->m_info_head.biHeight;

	src_buf	= lp_bitmap->m_mem_buf;

	if( FAILED( lp_plain->m_off_screen->Lock( NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL ) ) )
	{
		return 2;
	}

	nPitch	= ddsd.lPitch;
	dest_buf= (UCHAR*)ddsd.lpSurface;

	if( nPitch == nWidth )
	{
		memcpy( dest_buf, src_buf, lp_bitmap->m_info_head.biSizeImage );
	}
	else
	{
		for( y = 0; y < nHeight; y++ )
		{
			src_pos		= y * nWidth;
			dest_pos	= y * nPitch;

			memcpy( dest_buf + dest_pos, src_buf + src_pos, nPitch );
		}
	}

	lp_plain->m_off_screen->Unlock( NULL );

	return 0;
}

/*---------------------------------------------------------------------------------
功能:显示一个图像
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDirectDraw::ShowUnit(ULONG plain_ptr, int x_pos, int y_pos )
{
	OFFSCREEN_PTR	lp_screen = (OFFSCREEN_PTR)plain_ptr;

	RECT			rect,dest_rect;

	rect.left	= 0 ;
	rect.top	= 0 ;
	rect.right	= lp_screen->m_n_width;
	rect.bottom	= lp_screen->m_n_height;

	dest_rect	= rect;

	dest_rect.left   += x_pos;
	dest_rect.top    += y_pos;
	dest_rect.right  += x_pos;
	dest_rect.bottom += y_pos;

	if( FAILED( m_back_surface->Blt( &dest_rect, lp_screen->m_off_screen, &rect, DDBLT_WAIT, NULL ) ) )
	{
		return 1;
	}

	return 0;
}

/*---------------------------------------------------------------------------------
功能:创建一个离屏区
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDirectDraw::CreateOffScreenPlain(ULONG plain_ptr, int nWidth, int nHeight, ULONG dwFlags)
{
	char	buf[1024];

	sprintf( buf, "width = %d, height = %d", nWidth, nHeight );

	m_log.SaveMessage( buf );

	OFFSCREEN_PTR	lp_screen = (OFFSCREEN_PTR)plain_ptr;

	DDSURFACEDESC2		ddsd;

	INIT_STRUCT(ddsd);

	ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;

	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | dwFlags;

	ddsd.dwWidth  = nWidth;
	ddsd.dwHeight = nHeight;
	
	if( FAILED( m_dd->CreateSurface(&ddsd, &lp_screen->m_off_screen, NULL ) ) )
	{
		m_log.SaveMessage("创建离屏区失败!");
		
		return 1;
	}

	lp_screen->m_n_width  = nWidth;
	lp_screen->m_n_height = nHeight;

	return 0;
}

/*---------------------------------------------------------------------------------
功能:卸载位图
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDirectDraw::UnloadBitmapFile(ULONG bitmap_file_ptr)
{
	BITMAP_FILE_PTR lp_bitmap = (BITMAP_FILE_PTR)bitmap_file_ptr;

	if( lp_bitmap == NULL )
	{
		return 0;
	}

	if( lp_bitmap->m_mem_buf == NULL )
	{
		return 0;
	}

	delete [] lp_bitmap->m_mem_buf;

	lp_bitmap->m_mem_buf = NULL;

	return 0;
}

/*---------------------------------------------------------------------------------
功能:清除离屏区
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDirectDraw::ReleaseOffScreenPlain(ULONG plain_ptr)
{
	OFFSCREEN_PTR lp_plain = (OFFSCREEN_PTR)plain_ptr;

	SAFE_RELEASE( lp_plain->m_off_screen );

	return 0;
}

/*---------------------------------------------------------------------------------
功能:从配置文件载入位图的配置信息
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDirectDraw::LoadFromConfigFile(ULONG config_struct_ptr)
{
	CONFIG_PTR	lp_config = (CONFIG_PTR)config_struct_ptr;

	m_ini.SetIniFileName( lp_config->m_file_name );

	m_ini.ReadIniInt( &lp_config->m_n_bit,     "DIRECTDRAW", "BITCOUNT" );
	m_ini.ReadIniInt( &lp_config->m_n_width,   "DIRECTDRAW", "WIDTH"  );
	m_ini.ReadIniInt( &lp_config->m_n_height,  "DIRECTDRAW", "HEIGHT" );
	m_ini.ReadIniString( lp_config->m_file_name, 20, "DIRECTDRAW", "FILENAME" );

	m_ini.GetFullFileName( lp_config->m_file_name );

	m_log.SaveMessage( lp_config->m_file_name );

	return 0;
}


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

		[id(1), helpstring("method Init")] HRESULT Init([in]ULONG hWnd,[in]int nWidth,[in]int nHeight,[in]int nBit);
		[id(2), helpstring("method UnInit")] HRESULT UnInit();
		[id(3), helpstring("method SetClipper")] HRESULT SetClipper(/*[in]*/int nNum,/*[in]*/ULONG lp_rect);
		[id(4), helpstring("method UpdatePalette")] HRESULT UpdatePalette([in]ULONG lp_palette_entry);
		[id(5), helpstring("method DrawRandPixel_08")] HRESULT DrawRandPixel_08([in]int nCount);
		[id(6), helpstring("method Flip")] HRESULT Flip();
		[id(7), helpstring("method Start_Clock")] HRESULT Start_Clock();
		[id(8), helpstring("method Wait_Clock")] HRESULT Wait_Clock([in]double count);
		[id(9), helpstring("method LoadBitmapFile")] HRESULT LoadBitmapFile([in]ULONG bitmap_file_ptr,[in]ULONG bitmap_file_name);
		[id(10), helpstring("method CopyBitmapToPlain08")] HRESULT CopyBitmapToPlain08([in]ULONG bitmap_file_ptr,[in]ULONG plain_ptr);
		[id(11), helpstring("method ShowUnit")] HRESULT ShowUnit(/*[in]*/ULONG plain_ptr,/*[in]*/int x_pos,/*[in]*/int y_pos);
		[id(12), helpstring("method CreateOffScreenPlain")] HRESULT CreateOffScreenPlain([in]ULONG plain_ptr,[in]int nWidth,[in]int nHeight,[in]ULONG dwFlags);
		[id(13), helpstring("method UnloadBitmapFile")] HRESULT UnloadBitmapFile([in]ULONG bitmap_file_ptr);
		[id(14), helpstring("method ReleaseOffScreenPlain")] HRESULT ReleaseOffScreenPlain([in]ULONG plain_ptr);
		[id(15), helpstring("method LoadFromConfigFile")] HRESULT LoadFromConfigFile([in]ULONG config_struct_ptr);

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

/*---------------------------------------------------------------------------------
功能:创建调色板
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
int CFreeDirectDraw::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 CFreeDirectDraw::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 = new RGNDATA[ 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 ) ) )
	{
		delete [] region ;

		return 2;
	}

	if( FAILED( lpdds->SetClipper( lp_clip ) ))
	{
		delete [] region ;

		return 3;
	}

	delete [] region ;

	m_clipper = lp_clip;

	return 0;
}

/*---------------------------------------------------------------------------------
功能:更新调色板内容
说明:
返回:成功,0;失败,其它数值
---------------------------------------------------------------------------------*/
int CFreeDirectDraw::UpdatePalette(PALETTEENTRY* lp_palette_entry)
{
	memcpy( m_palatte_entry, lp_palette_entry, sizeof( PALETTEENTRY ) * MAX_PALETTE_COLORS );

	if ( FAILED( m_palette->SetEntries( 0, 0, MAX_PALETTE_COLORS, m_palatte_entry ) ) )
	{
		return 1;
	}

	return 0;
}

/*---------------------------------------------------------------------------------
功能:翻转位图
说明:
返回:成功,0;失败,其它数值
---------------------------------------------------------------------------------*/
int CFreeDirectDraw::Flip_Bitmap( BITMAP_FILE_PTR lp_bitmap )
{
	if( lp_bitmap == NULL )
	{
		return 1;
	}

	if( lp_bitmap->m_mem_buf == NULL )
	{
		return 2;
	}

	//如果位图高度是负数,就不用反转了
	if( lp_bitmap->m_info_head.biHeight < 0 )
	{
		return 0;
	}

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

	int		nPitch;
	int		nWidth,nHeight,nHalfHeight;
	int 	y,src_pos,dest_pos;
	UCHAR*	temp_buf,*buf;

	nWidth		= lp_bitmap->m_info_head.biWidth;
	nHeight		= lp_bitmap->m_info_head.biHeight;
	nHalfHeight	= nHeight / 2;

	nPitch		= nWidth * lp_bitmap->m_info_head.biBitCount / 8;

	buf			= lp_bitmap->m_mem_buf;
	temp_buf	= new UCHAR[nPitch];

	//反转图像
	for( y = 0; y < nHalfHeight; y++ )
	{
		src_pos	= nPitch * y;
		dest_pos= nPitch * ( nHeight - 1 - y );

		memcpy( temp_buf, buf + src_pos, nPitch );
		memcpy( buf + src_pos, buf + dest_pos, nPitch );
		memcpy( buf + dest_pos, temp_buf, nPitch );
	}

	delete [] temp_buf;

	temp_buf	= NULL;

	return 0;
}

⌨️ 快捷键说明

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