📄 复件 freedirectdraw.cpp
字号:
// FreeDirectDraw.cpp : Implementation of CFreeDirectDraw
#include "stdafx.h"
#include "FreeDirectX.h"
#include "FreeDirectDraw.h"
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////开始工具函数区/////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*---------------------------------------------------------------------------------
功能:创建调色板
说明:
返回:成功,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;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////开始工具函数区/////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*---------------------------------------------------------------------------------
功能:初始化DirectDraw
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDirectDraw::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[] = {0, 0, m_n_width, m_n_height };
if( SetClipper( m_back_surface, 1, rect ) != 0 )
{
m_log.SaveMessage("设置剪切区失败!");
return 7;
}
m_log.SaveMessage( "DirectDraw组件初始化成功!" );
return 0;
}
/*---------------------------------------------------------------------------------
功能:清除DirectDraw资源
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDirectDraw::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 CFreeDirectDraw::SetClipper(int nNum, ULONG lp_rect)
{
int nRet;
nRet = SetClipper( m_back_surface, nNum, (LPRECT)lp_rect );
return nRet;
}
/*---------------------------------------------------------------------------------
功能:更新调色板
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDirectDraw::UpdatePalette(ULONG lp_palette_entry)
{
int nRet;
nRet = UpdatePalette( (PALETTEENTRY*)lp_palette_entry );
return nRet;
}
/*---------------------------------------------------------------------------------
功能:直接在副页面上画随机的点-------8位
说明:
返回:成功,0;失败,其它正数值
---------------------------------------------------------------------------------*/
STDMETHODIMP CFreeDirectDraw::DrawRandPixel_08(int nCount)
{
int x,y;
int mempitch = 0;
unsigned char* lp_buf = NULL;
HRESULT hr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -