?? directdraw.cpp
字號:
/*****************************************************************************\
* Copyright (c), Future Entertainment World / Seoul, Republic of Korea *
* All Rights Reserved. *
* *
* This document contains proprietary and confidential information. No *
* parts of this document or the computer program it embodies may be in *
* any way copied, duplicated, reproduced, translated into a different *
* programming language, or distributed to any person, company, or *
* corporation without the prior written consent of Future Entertainment World *
\*****************************************************************************/
#include "stdafx.h"
#include <Stdio.h>
#include "dragon.h"
#include "directdraw.h"
#include "MouseCursor.h"
///////////////////////////////////////////////////////////////////////////////
//
BOOL InitDirectDraw( HWND hWnd, LPDIRECTDRAWINFO lpDirectDrawInfo );
void CleanupDirectDraw( LPDIRECTDRAWINFO lpDirectDrawInfo );
LPDIRECTDRAWSURFACE CreateSurface( LPDIRECTDRAW lpDirectDraw, DWORD dwWidth, DWORD dwHeight );
void CleanupSurface( LPDIRECTDRAWSURFACE lpSurface );
BOOL RestoreSurface( LPDIRECTDRAWSURFACE lpSurface );
BOOL RestoreAllSurfaces( LPDIRECTDRAWINFO lpDirectDrawInfo );
void FlipScreen( LPDIRECTDRAWINFO lpDirectDrawInfo );
void EraseScreen( LPDIRECTDRAWINFO lpDirectDrawInfo, WORD color );
BOOL DDLoadBitmap( LPDIRECTDRAW lpDirectDraw, LPSURFACEINFO lpSurfaceInfo, char* lpszFilePath );
BOOL DDReLoadBitmap( LPDIRECTDRAWSURFACE lpSurface, char* lpszFilePath );
BOOL DDCopyBitmap( LPDIRECTDRAWSURFACE lpSurface, HBITMAP hBitmap, int x, int y, int dx, int dy );
DWORD DDColorMatch( LPDIRECTDRAWSURFACE lpSurface, COLORREF rgb );
HRESULT DDSetColorKey( LPDIRECTDRAWSURFACE lpSurface, COLORREF rgb );
void StretchBltScreen( LPDIRECTDRAWSURFACE lpSurfaceDst, LPRECT lpRectDst, LPDIRECTDRAWSURFACE lpSurfaceSrc, LPRECT lpRectSrc, DWORD dwColorFill, int nDirection );
void BltGrid( LPDIRECTDRAWSURFACE lpSurfaceDst, LPRECT lpRectDst, LPDIRECTDRAWSURFACE lpSurfaceSrc, LPRECT lpRectSrc );
void BltTrans( LPDIRECTDRAWSURFACE lpSurfaceDst, LPRECT lpRectDst, LPDIRECTDRAWSURFACE lpSurfaceSrc, LPRECT lpRectSrc );
int TransAlpha( LPDIRECTDRAWSURFACE lpSurfaceDst, LPDIRECTDRAWSURFACE lpSurfaceSrc, LONG x, LONG y, RECT rectSrc, WORD wAlphaValue );
void InitGammaControl( void );
void ResetGammaCtrl( void );
int ColorCtrlBrightness( long v );
///////////////////////////////////////////////////////////////////////////////
//
BOOL
InitDirectDraw( HWND hWnd, LPDIRECTDRAWINFO lpDirectDrawInfo )
{
LPDIRECTDRAW lpDirectDraw = NULL;
DDSCAPS ddscaps;
DDSURFACEDESC ddsd;
CLIPLIST tClipList;
RECT rectClipper;
HRESULT hResult;
if(lpDirectDrawInfo->lpDirectDraw != NULL) return TRUE;
hResult = DirectDrawCreate( NULL, &lpDirectDrawInfo->lpDirectDraw, NULL );
if( hResult != DD_OK )
{
return ShowErrorMessage( "DirectDrawCreate Failed!" );
}
// hResult = lpDirectDraw->QueryInterface( IID_IDirectDraw4, ( LPVOID* )&lpDirectDrawInfo->lpDirectDraw );
// if ( hResult != DD_OK )
// {
// return ShowErrorMessage( "QueryInterface Failed!" );
// }
// lpDirectDraw->Release( );
// lpDirectDraw = NULL;
if ( lpDirectDrawInfo->bFullscreen )
{
hResult = lpDirectDrawInfo->lpDirectDraw->SetCooperativeLevel( hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
if ( hResult != DD_OK )
{
return ShowErrorMessage( "SetCooperativeLevel Failed!" );
}
#ifdef ALT_TAB_BLOCK
while( lpDirectDrawInfo->lpDirectDraw->SetDisplayMode( SCREEN_WIDTH, SCREEN_HEIGHT, 16 ) != DD_OK )
{
Sleep( 1000 );
//count++;
//if( count > 100 ) return false;
}
#else
hResult = lpDirectDrawInfo->lpDirectDraw->SetDisplayMode( SCREEN_WIDTH, SCREEN_HEIGHT, 16 );
if ( hResult != DD_OK )
{
return ShowErrorMessage( "SetDisplayMode Failed!" );
}
#endif
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_SYSTEMMEMORY;
ddsd.dwBackBufferCount = 1;
hResult = lpDirectDrawInfo->lpDirectDraw->CreateSurface( &ddsd, &lpDirectDrawInfo->lpDirectDrawSurfacePrimary, NULL );
if ( hResult != DD_OK )
{
return ShowErrorMessage( "CreateSurface(Primary) Failed!" );
}
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
hResult = lpDirectDrawInfo->lpDirectDrawSurfacePrimary->GetAttachedSurface( &ddscaps, &lpDirectDrawInfo->lpDirectDrawSurfaceBack );
if ( hResult != DD_OK )
{
return ShowErrorMessage( "GetAttachedSurface Failed!" );
}
/*
DDSURFACEDESC DDSDesc;
DDSDesc.dwSize = sizeof(DDSDesc2);
lpDirectDrawInfo->lpDirectDrawSurfacePrimary->Lock(NULL, &DDSDesc, 0, NULL); //
lpDirectDrawInfo->lpDirectDrawSurfacePrimary->Unlock(NULL);
DxSize = DDSDesc.lPitch;
*/
}
else
{
hResult = lpDirectDrawInfo->lpDirectDraw->SetCooperativeLevel( hWnd, DDSCL_NORMAL );
if ( hResult != DD_OK )
{
return ShowErrorMessage( "SetCooperativeLevel Failed!" );
}
/*
DWORD dwStyle;
RECT rectWork, rectClient;
dwStyle = GetWindowStyle( hWnd );
dwStyle &= ~WS_POPUP;
dwStyle |= WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX;
SetWindowLong( hWnd, GWL_STYLE, dwStyle );
SetRect( &rectClient, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT );
AdjustWindowRectEx( &rectClient, GetWindowStyle( hWnd ), GetMenu( hWnd ) != NULL, GetWindowExStyle( hWnd ) );
SetWindowPos( hWnd, NULL, 0, 0, rectClient.right - rectClient.left, rectClient.bottom - rectClient.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
SetWindowPos( hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
SystemParametersInfo( SPI_GETWORKAREA, 0, &rectWork, 0 );
GetWindowRect( hWnd, &rectClient );
if ( rectClient.left < rectWork.left )
{
rectClient.left = rectWork.left;
}
if ( rectClient.top < rectWork.top )
{
rectClient.top = rectWork.top;
}
SetWindowPos( hWnd, NULL, rectClient.left, rectClient.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
*/
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY;
hResult = lpDirectDrawInfo->lpDirectDraw->CreateSurface( &ddsd, &lpDirectDrawInfo->lpDirectDrawSurfacePrimary, NULL );
if ( hResult != DD_OK )
{
return ShowErrorMessage( "CreateSurface(Primary Failed!" );
}
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
ddsd.dwWidth = SCREEN_WIDTH;
ddsd.dwHeight = SCREEN_HEIGHT;
hResult = lpDirectDrawInfo->lpDirectDraw->CreateSurface( &ddsd, &lpDirectDrawInfo->lpDirectDrawSurfaceBack, NULL );
if ( hResult != DD_OK )
{
return ShowErrorMessage( "CreateSurface(Back) Failed!" );
}
}
hResult = lpDirectDrawInfo->lpDirectDraw->CreateClipper( 0, &lpDirectDrawInfo->lpClipper, NULL );
if ( hResult != DD_OK )
{
return ShowErrorMessage( "CreateClipper Failed!" );
}
SetRect( &rectClipper, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT );
tClipList.rgnheader.dwSize = sizeof( RGNDATAHEADER );
tClipList.rgnheader.iType = RDH_RECTANGLES;
tClipList.rgnheader.nCount = 1;
tClipList.rgnheader.nRgnSize = 0;
memcpy( &tClipList.rgnheader.rcBound, &rectClipper, sizeof( RECT ) );
memcpy( &tClipList.rect, &rectClipper, sizeof( RECT ) );
hResult = lpDirectDrawInfo->lpClipper->SetClipList( ( LPRGNDATA )&tClipList, 0 );
if ( hResult != DD_OK )
{
return ShowErrorMessage( "SetClipList Failed!" );
}
InitGammaControl();
return TRUE;
}
void
CleanupDirectDraw( LPDIRECTDRAWINFO lpDirectDrawInfo )
{
if ( !lpDirectDrawInfo->bFullscreen && lpDirectDrawInfo->lpClipper )
{
lpDirectDrawInfo->lpClipper->SetClipList( NULL, 0 );
lpDirectDrawInfo->lpClipper->Release( );
lpDirectDrawInfo->lpClipper = NULL;
}
if ( !lpDirectDrawInfo->bFullscreen && ( lpDirectDrawInfo->lpDirectDrawSurfaceBack != NULL ) )
{
lpDirectDrawInfo->lpDirectDrawSurfaceBack->Release( );
lpDirectDrawInfo->lpDirectDrawSurfaceBack = NULL;
}
if ( lpDirectDrawInfo->lpDirectDrawSurfacePrimary != NULL )
{
lpDirectDrawInfo->lpDirectDrawSurfacePrimary->Release( );
lpDirectDrawInfo->lpDirectDrawSurfacePrimary = NULL;
}
if( lpDirectDrawInfo->lpDirectDraw != NULL )
{
lpDirectDrawInfo->lpDirectDraw->Release( );
lpDirectDrawInfo->lpDirectDraw = NULL;
}
// ResetGammaCtrl();
}
LPDIRECTDRAWSURFACE
CreateSurface( LPDIRECTDRAW lpDirectDraw, DWORD dwWidth, DWORD dwHeight )
{
LPDIRECTDRAWSURFACE lpSurface;
DDSURFACEDESC ddsd;
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
ddsd.dwWidth = dwWidth;
ddsd.dwHeight = dwHeight;
if ( lpDirectDraw->CreateSurface( &ddsd, &lpSurface, NULL ) != DD_OK )
{
return NULL;
}
return lpSurface;
}
void
CleanupSurface( LPDIRECTDRAWSURFACE lpSurface )
{
if ( lpSurface )
{
lpSurface->Release( );
lpSurface = NULL;
}
}
BOOL
RestoreSurface( LPDIRECTDRAWSURFACE lpSurface )
{
if ( lpSurface->Restore( ) != DD_OK )
{
return FALSE;
}
return TRUE;
}
BOOL
RestoreAllSurfaces( LPDIRECTDRAWINFO lpDirectDrawInfo )
{
if ( !RestoreSurface( lpDirectDrawInfo->lpDirectDrawSurfacePrimary ) )
{
char temp[30];
sprintf(temp, lan->OutputMessage(7,61));//010215 lsw
MessageBox( g_hwndMain, temp, 0, MB_OK);
return FALSE;
}
if ( !RestoreSurface( lpDirectDrawInfo->lpDirectDrawSurfaceBack ) )
{
char temp[30];
sprintf(temp, lan->OutputMessage(7,62));//010215 lsw
MessageBox( g_hwndMain, temp, 0, MB_OK);
return FALSE;
}
return TRUE;
}
void
FlipScreen( LPDIRECTDRAWINFO lpDirectDrawInfo )
{
HRESULT hResult;
extern bool chinese_input;
if ( lpDirectDrawInfo->bFullscreen )
{
if( chinese_input )
{
lpDirectDrawInfo->lpDirectDrawSurfacePrimary->SetClipper( lpDirectDrawInfo->lpClipper );
hResult = lpDirectDrawInfo->lpDirectDrawSurfacePrimary->Blt( &lpDirectDrawInfo->rectPrimarySurface,
lpDirectDrawInfo->lpDirectDrawSurfaceBack, NULL, DDBLT_WAIT, NULL );
}
else
while( 1 )
{
hResult = lpDirectDrawInfo->lpDirectDrawSurfacePrimary->Flip( NULL, DDFLIP_WAIT );
if ( hResult == DD_OK )
{
break;
}
if ( hResult == DDERR_SURFACELOST )
{
hResult = RestoreAllSurfaces( lpDirectDrawInfo );
if ( hResult != DD_OK )
{
return;
}
}
if ( hResult != DDERR_WASSTILLDRAWING )
{
break;
}
}
}
else
{
// lpDirectDrawInfo->lpDirectDrawSurfacePrimary->SetClipper( lpDirectDrawInfo->lpClipper );
hResult = lpDirectDrawInfo->lpDirectDrawSurfacePrimary->Blt( &lpDirectDrawInfo->rectPrimarySurface,
lpDirectDrawInfo->lpDirectDrawSurfaceBack, NULL, DDBLT_WAIT, NULL );
}
}
void
EraseScreen( LPDIRECTDRAWINFO lpDirectDrawInfo, WORD color )
{
DDBLTFX ddbltfx;
HRESULT hResult;
ddbltfx.dwSize = sizeof( ddbltfx );
ddbltfx.dwFillColor = color;// RGB16( 0, 0, 255 );
if( lpDirectDrawInfo == NULL ) return;
while( 1 )
{
hResult = lpDirectDrawInfo->lpDirectDrawSurfaceBack->Blt( NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx );
if ( hResult == DD_OK )
{
break;
}
if ( hResult == DDERR_SURFACELOST )
{
hResult = RestoreAllSurfaces( lpDirectDrawInfo );
if ( hResult != DD_OK )
{
return;
}
}
if ( hResult != DDERR_WASSTILLDRAWING )
{
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -