/*******************************************************************
// Copyright (c) Tom Yutian Tang 2007-2008 huarong_china@sohu.com
//
*******************************************************************/
http://www.hackchina.com/r/151193/overlay.cpp__html
#define NAME "Overlay"
#define TITLE "DirectDraw Overlays"
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include "resource.h"
#include "OverlayManager.h"
OverlayManager gOverlayManager;
LPDIRECTDRAW lpDD; // DirectDraw object.
LPDIRECTDRAWSURFACE lpDDSPrimary; // DirectDraw primary surface.
LPDIRECTDRAWSURFACE lpDDSOverlay; // DirectDraw overlay surface.
BOOL bVisible = FALSE; // Overlay on or off.
RECT rSrc, rDest; // Overlay rectangles.
DDCAPS ddcaps; // Device caps.
DWORD AlignDown( DWORD dwNumber, DWORD dwAlignment )
{
DWORD dwResult;
if ( dwNumber == 1 ) return dwNumber;
dwResult = dwNumber - ( dwNumber % dwAlignment );
return dwResult;
}
DWORD AlignUp( DWORD dwNumber, DWORD dwAlignment )
{
DWORD dwResult;
if ( dwNumber == 1 ) return dwNumber;
dwResult = dwNumber + ( dwAlignment - 1 );
dwResult -= dwNumber % dwAlignment;
return dwResult;
}
BOOL LoadImage( LPDIRECTDRAWSURFACE lpDDS, LPSTR szImage )
{
HBITMAP hbm;
HDC hdcImage = NULL;
HDC hdcSurf = NULL;
BOOL bReturn = FALSE;
DDSURFACEDESC ddsd;
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
if ( FAILED( lpDDS->GetSurfaceDesc( &ddsd ) ) )
{
goto Exit;
}
// If the pixel format isn't some flavor of RGB, we can't handle it.
if ( ( ddsd.ddpfPixelFormat.dwFlags != DDPF_RGB ) ||
( ddsd.ddpfPixelFormat.dwRGBBitCount < 16 ) )
{
OutputDebugString( "Non-palettized RGB mode required./n" );
goto Exit;
}
// Try loading the image.
hbm = ( HBITMAP )LoadImage( NULL, szImage,
IMAGE_BITMAP, ddsd.dwWidth,
ddsd.dwHeight, LR_LOADFROMFILE | LR_CREATEDIBSECTION );
if ( hbm == NULL )
{
OutputDebugString( " Couldn't find the resource./n" );
goto Exit;
}
// Create a DC and select the image into it.
hdcImage = CreateCompatibleDC( NULL );
SelectObject( hdcImage, hbm );
// Get a DC for the surface.
if ( FAILED( lpDDS->GetDC( &hdcSurf ) ) )
{
OutputDebugString( "Couldn't get a DC./n" );
goto Exit;
}
// The BitBlt will perform format conversion as necessary.
if ( BitBlt( hdcSurf, 0, 0, ddsd.dwWidth, ddsd.dwHeight,
hdcImage, 0, 0, SRCCOPY ) == FALSE )
{
OutputDebugString( "Blt failed./n" );
goto Exit;
}
// Success.
bReturn = TRUE;
Exit:
// Clean up everything.
if ( hdcSurf )
lpDDS->ReleaseDC( hdcSurf );
if ( hdcImage )
DeleteDC( hdcImage );
if ( hbm )
DeleteObject( hbm );
return bReturn;
}
BOOL LoadImage_1( LPDIRECTDRAWSURFACE lpDDS, LPSTR szImage )
{
HBITMAP hbm;
HDC hdcImage = NULL;
HDC hdcSurf = NULL;
BOOL bReturn = FALSE;
DDSURFACEDESC ddsd;
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize = sizeof( ddsd );
if ( FAILED( lpDDS->GetSurfaceDesc( &ddsd ) ) )
{
goto Exit;
}
// If the pixel format isn't some flavor of RGB, we can't handle it.
if (ddsd.ddpfPixelFormat.dwFlags==DDPF_FOURCC)
{
if (ddsd.ddpfPixelFormat.dwFourCC== MAKEFOURCC('U', 'Y', 'V', 'Y'))
{
int tyt=0;
}
}
else if ( ( ddsd.ddpfPixelFormat.dwFlags != DDPF_RGB ) ||
( ddsd.ddpfPixelFormat.dwRGBBitCount < 16 ) )
{
OutputDebugString( "Non-palettized RGB mode required./n" );
goto Exit;
}
// Try loading the image.
hbm = ( HBITMAP )LoadImage( NULL, szImage,
IMAGE_BITMAP,
0,
0, LR_LOADFROMFILE | LR_CREATEDIBSECTION );
if ( hbm == NULL )
{
OutputDebugString( " Couldn't find the resource./n" );
goto Exit;
}
BITMAP Bitmap;
GetObject(hbm, sizeof(Bitmap), (LPSTR)&Bitmap);
// Create a DC and select the image into it.
hdcImage = CreateCompatibleDC( NULL );
SelectObject( hdcImage, hbm );
// Get a DC for the surface.
if ( FAILED( lpDDS->GetDC( &hdcSurf ) ) )
{
OutputDebugString( "Couldn't get a DC./n" );
goto Exit;
}
// The BitBlt will perform format conversion as necessary.
//if ( BitBlt( hdcSurf, 0, 0, ddsd.dwWidth, ddsd.dwHeight,
if ( BitBlt( hdcSurf, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight,
hdcImage, 0, 0, SRCCOPY ) == FALSE )
{
OutputDebugString( "Blt failed./n" );
goto Exit;
}
// Success.
bReturn = TRUE;
Exit:
// Clean up everything.
if ( hdcSurf )
lpDDS->ReleaseDC( hdcSurf );
if ( hdcImage )
DeleteDC( hdcImage );
if ( hbm )
DeleteObject( hbm );
return bReturn;
}
void ReleaseObjects( void )
{
if ( lpDD != NULL )
{
if ( lpDDSPrimary != NULL )
{
if ( lpDDSOverlay != NULL )
{
lpDDSOverlay->Release();
lpDDSOverlay = NULL;
}
lpDDSPrimary->Release();
lpDDSPrimary = NULL;
}
lpDD->Release();
lpDD = NULL;
}
}
BOOL Fail( HWND hwnd, char *szMsg )
{
ReleaseObjects();
OutputDebugString( szMsg );
EndDialog( hwnd, FALSE );
return FALSE;
}
DDPIXELFORMAT PixelFormatToDdPixelFormat(PixelFormat format)
{
const DDPIXELFORMAT formats[] =
{
// PIXEL_FORMAT_32BPP
{ sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 32, 0xFF0000, 0x0000FF00, 0x000000FF, 0},
// PIXEL_FORMAT_24BPP
{ sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 24, 0xFF0000, 0x0000FF00, 0x000000FF, 0},
// PIXEL_FORMAT_16BPP_R5G6B5
{ sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0xF800, 0x07E0, 0x001F, 0},
// PIXEL_FORMAT_16BPP_R5G5B5
{ sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x7C00, 0x03e0, 0x001F, 0},
// PIXEL_FORMAT_UYVY
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_YUY2
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_IMC1
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('I', 'M', 'C', '1'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_IMC2
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('I', 'M', 'C', '2'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_IMC4
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('I', 'M', 'C', '4'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_YV12
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'V', '1', '2'), 0, 0, 0, 0, 0},
// PIXEL_FORMAT_NV12
{ sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('N', 'V', '1', '2'), 0, 0, 0, 0, 0}
};
switch(format)
{
case PIXEL_FORMAT_32BPP:
return formats[0];
case PIXEL_FORMAT_24BPP:
return formats[1];
case PIXEL_FORMAT_16BPP_R5G6B5:
return formats[2];
case PIXEL_FORMAT_16BPP_R5G5B5:
return formats[3];
case PIXEL_FORMAT_UYVY:
return formats[4];
case PIXEL_FORMAT_YUY2:
return formats[5];
case PIXEL_FORMAT_IMC1:
return formats[6];
case PIXEL_FORMAT_IMC2:
return formats[7];
case PIXEL_FORMAT_IMC4:
return formats[8];
case PIXEL_FORMAT_YV12:
return formats[9];
case PIXEL_FORMAT_NV12:
return formats[10];
}
DDPIXELFORMAT tmp={0};
return tmp;
}
UINT AAA(LPVOID pParam)
{
HDC hdcRender;
if ( SUCCEEDED( lpDDSPrimary->GetDC( &hdcRender ) ) )
{
MessageBox(NULL,"OK!",NULL,MB_OK);
}
else
{
MessageBox(NULL,"OK!",NULL,MB_OK);
}
return 0;
}
LRESULT CALLBACK DialogProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
DDSURFACEDESC ddsd_primary,ddsd_overlay;
DDCOLORKEY ddckey; // For SetColorKey.
DWORD dwFlags; // For updating the overlay.
switch ( message )
{
case WM_INITDIALOG:
#if 0
gOverlayManager.Reset(400,400);
//IDirectDrawSurface *lpDDSPrimary=;
gOverlayManager.primarySurface_->QueryInterface(IID_IDirectDrawSurface,(void **)&lpDDSPrimary);
//IDirectDrawSurface *lpDDSOverlay=gOverlayManager.overlaySurface_;
gOverlayManager.overlaySurface_->QueryInterface(IID_IDirectDrawSurface,(void **)&lpDDSOverlay);
#else
// Create the DirectDraw object -- we just need an IDirectDraw
// interface so we won't bother to query an IDirectDraw2.
if ( FAILED( DirectDrawCreate( NULL, &lpDD, NULL ) ) )
{
return Fail( hwnd, "Couldn't create DirectDraw object./n" );
}
// Set normal cooperative levl.
if ( FAILED( lpDD->SetCooperativeLevel( hwnd, DDSCL_NORMAL ) ) )
{
return Fail( hwnd, "Couldn't set cooperative level./n" );
}
// Store the caps in our global and check for overlay cap bit.
ddcaps.dwSize = sizeof( ddcaps );
if ( FAILED( lpDD->GetCaps( &ddcaps, NULL ) ) )
{
return Fail( hwnd, "Couldn't get caps./n" );
}
if ( !( ddcaps.dwCaps & DDCAPS_OVERLAY ) )
{
MessageBox(NULL,"No overlay capability",NULL,MB_OK);
return Fail( hwnd, "No overlay capability./n" );
}
// Check to make sure that an overlay is available for use.
if ( ddcaps.dwCurrVisibleOverlays ==
ddcaps.dwMaxVisibleOverlays )
{
return Fail( hwnd, "No overlays available./n" );
}
// Create the primary surface.
ddsd_primary.dwSize = sizeof( ddsd_primary );
ddsd_primary.dwFlags = DDSD_CAPS;
ddsd_primary.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if ( FAILED( lpDD->CreateSurface( &ddsd_primary, &lpDDSPrimary, NULL ) ) )
{
return Fail( hwnd, "Couldn't create primary surface./n" );
}
//CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)AAA,NULL,0,NULL);
//break;
if (lpDDSPrimary->GetSurfaceDesc(&ddsd_primary)==DD_OK)
{
// ����ɫ��
/*
char tmp[512];
sprintf(tmp,"lpDDSPrimary, dwRGBBitCount=%d dwYUVBitCount=%d /r/n",
ddsd_primary.ddpfPixelFormat.dwRGBBitCount,ddsd_primary.ddpfPixelFormat.dwYUVBitCount);
MessageBox(NULL,tmp,NULL,MB_OK);*/
}
// Now we'll try to create an overlay surface. We have to guess
// at the format, as DirectDraw doesn't provide us with a list of
// possibilities. YUV formats are most common, but we'll try RGB
// so we don't have to convert our image.
ddsd_overlay.dwSize = sizeof( ddsd_overlay );
ddsd_overlay.dwFlags = DDSD_CAPS;
ddsd_overlay.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY ;//| DDSCAPS_FLIP;// | DDSCAPS_COMPLEX;
ddsd_overlay.dwFlags= DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
ddsd_overlay.dwHeight = 64;
ddsd_overlay.dwWidth = 64;
memset(&ddsd_overlay.ddpfPixelFormat,0,sizeof(DDPIXELFORMAT));
ddsd_overlay.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
#if 1 // Try to create overlay
{
BOOL bOverlay_Created=FALSE;
PixelFormat pixelFormat_ = PIXEL_FORMAT_UNKNOWN;
for(int format = PIXEL_FORMAT_FIRST; format <= PIXEL_FORMAT_LAST; format++)
{
// Attempt to create the surface with this pixel format
ddsd_overlay.ddpfPixelFormat = PixelFormatToDdPixelFormat(static_cast<PixelFormat>(format));
if ( lpDD->CreateSurface(&ddsd_overlay, &lpDDSOverlay, 0)==S_OK)
{
pixelFormat_ =static_cast<PixelFormat>(format);
char msg[1024];
switch (pixelFormat_)
{
case PIXEL_FORMAT_32BPP:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_32BPP created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_24BPP:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_24BPP created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_16BPP_R5G6B5:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_16BPP_R5G6B5 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_16BPP_R5G5B5:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_16BPP_R5G5B5 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_UYVY:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_UYVY created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_YUY2:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_YUY2 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_IMC1:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_IMC1 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_IMC2:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_IMC2 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_IMC4:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_IMC4 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_YV12:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_YV12 created sucessfully , use this overlay ?");
break;
case PIXEL_FORMAT_NV12:
sprintf(msg,"The overlay with pixel format PIXEL_FORMAT_NV12 created sucessfully , use this overlay ?");
break;
default:
sprintf(msg,"The pixel format Unknown of overlay created sucessfully ???!!!");
break;
}
int res=MessageBox(NULL,msg,NULL,MB_YESNO);
if (res==IDYES)
{
bOverlay_Created=TRUE;
break;
}
}
}
if (!bOverlay_Created)
{
MessageBox(NULL,"Failed to create overlay, exit !",NULL,MB_OK);
exit(1);
}
}
#else
ddsd_overlay.ddpfPixelFormat.dwFourCC = 0; //MAKEFOURCC('U', 'Y', 'V', 'Y')
ddsd_overlay.ddpfPixelFormat.dwRGBAlphaBitMask = 0;
// Try the current color depth firstly.
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_RGB;
ddsd_overlay.ddpfPixelFormat.dwRGBBitCount = 24;
ddsd_overlay.ddpfPixelFormat.dwRBitMask = 0x00ff0000;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x0000ff00;
ddsd_overlay.ddpfPixelFormat.dwBBitMask = 0x000000ff;
// Try the first format.
if ( FAILED( lpDD->CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
OutputDebugString( "Overlay format DDPF_RGB current color depth failed./n" );
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_RGB;
ddsd_overlay.ddpfPixelFormat.dwRGBBitCount = 16;
ddsd_overlay.ddpfPixelFormat.dwRBitMask = 0x7C00;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x03E0;
ddsd_overlay.ddpfPixelFormat.dwBBitMask = 0x001F;
if ( FAILED( lpDD->CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
OutputDebugString( "Overlay format RGB555 failed./n" );
// RGB565.
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_RGB;
ddsd_overlay.ddpfPixelFormat.dwRGBBitCount = 16;
ddsd_overlay.ddpfPixelFormat.dwRBitMask = 0xF800;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x07E0;
ddsd_overlay.ddpfPixelFormat.dwBBitMask = 0x001F;
if ( FAILED( lpDD->CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
OutputDebugString( "Overlay format RGB565 failed./n" );
// RGB888
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_RGB;
ddsd_overlay.ddpfPixelFormat.dwRGBBitCount = 24;
ddsd_overlay.ddpfPixelFormat.dwRBitMask = 0x00ff0000;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x0000ff00;
ddsd_overlay.ddpfPixelFormat.dwBBitMask = 0x000000ff;
if ( FAILED( lpDD->CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
// YUV -- 16
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_YUV;
ddsd_overlay.ddpfPixelFormat.dwRGBBitCount = 16;
ddsd_overlay.ddpfPixelFormat.dwRBitMask = 0x7C00;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x03E0;
ddsd_overlay.ddpfPixelFormat.dwBBitMask = 0x001F;
if ( FAILED( lpDD->CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
OutputDebugString( "Overlay format YUV -- 16 failed./n" );
// YUV -- 24
ddsd_overlay.ddpfPixelFormat.dwFlags = DDPF_YUV;
ddsd_overlay.ddpfPixelFormat.dwRGBBitCount = 24;
ddsd_overlay.ddpfPixelFormat.dwRBitMask = 0x00ff0000;
ddsd_overlay.ddpfPixelFormat.dwGBitMask = 0x0000ff00;
ddsd_overlay.ddpfPixelFormat.dwBBitMask = 0x000000ff;
if ( FAILED( lpDD->CreateSurface( &ddsd_overlay, &lpDDSOverlay, NULL ) ) )
{
MessageBox(NULL,"Could not create overlay surface in current display mode !",NULL,MB_OK);
return Fail( hwnd, "Couldn't create overlay surface./n" );
}
else
{
MessageBox(NULL,"create overlay surface with YUV-24",NULL,MB_OK);
}
}
else
{
MessageBox(NULL,"create overlay surface with YUV-16",NULL,MB_OK);
}
}
else
{
MessageBox(NULL,"create overlay surface with RGB888",NULL,MB_OK);
}
}
else
{
MessageBox(NULL,"create overlay surface with RGB565",NULL,MB_OK);
}
}
else
{
MessageBox(NULL,"create overlay surface with RGB555",NULL,MB_OK);
}
}
else
{
MessageBox(NULL,"create overlay surface with DDPF_RGB current color depth !",NULL,MB_OK);
}
#endif // Try to create overlay
#endif
// If source color key is available for overlays, set a
// color key of pure green and enable the check box. Use
// the green bit mask that was successfully created.
// Load a bitmap into our overlay.
// Our bitmap is 64 pixels wide, which will satisfy
// the common alignment requirements of 4 and 8 pixels.
LoadImage_1( lpDDSOverlay, "xrules.bmp" );
// Load a bitmap into primary surface.
LoadImage_1( lpDDSPrimary, "bluesq_1.bmp" );
// Set Color key of primary surface
DDCOLORKEY ddckey;
ddckey.dwColorSpaceLowValue = ddsd_primary.ddpfPixelFormat.dwBBitMask; // ��ɫ
ddckey.dwColorSpaceHighValue = ddsd_primary.ddpfPixelFormat.dwBBitMask;
if (lpDDSPrimary->SetColorKey(
//DDCKEY_SRCOVERLAY,
DDCKEY_DESTOVERLAY,
&ddckey )==DD_OK)
{
MessageBox(NULL,"Create_DDraw_Surface(): Set Color Key of the primary surface successful !/r/n",NULL,MB_OK);
}
else
{
MessageBox(NULL,"Create_DDraw_Surface(): Set Color Key of the primary surface FAILED !/r/n",NULL,MB_OK);
}
// Set Color key of overlay surface
if ( ddcaps.dwCKeyCaps & DDCKEYCAPS_SRCOVERLAY )
{
ddckey.dwColorSpaceLowValue = ddsd_overlay.ddpfPixelFormat.dwGBitMask;
ddckey.dwColorSpaceHighValue = ddsd_overlay.ddpfPixelFormat.dwGBitMask;
if (lpDDSOverlay->SetColorKey(
DDCKEY_SRCOVERLAY,
&ddckey )==DD_OK)
{
MessageBox(NULL,"Create_DDraw_Surface(): Set Color Key of the overlay surface successful !/r/n",NULL,MB_OK);
}
else
{
MessageBox(NULL,"Create_DDraw_Surface(): Set Color Key of the overlay surface FAILED !/r/n",NULL,MB_OK);
}
EnableWindow( GetDlgItem( hwnd, IDC_COLORKEY ),
TRUE );
}
else
{
MessageBox(NULL,"Create_DDraw_Surface(): Dont find DDCKEYCAPS_SRCOVERLAY support !/r/n",NULL,MB_OK);
}
/*
RECT rect_1;
rect_1.left=0; rect_1.top=0; rect_1.right=64; rect_1.bottom=64;
lpDDSOverlay->Blt(&rect_1,lpDDSPrimary,&rect_1,DDBLT_WAIT,NULL);
LoadImage_1( lpDDSPrimary, "xrules.bmp" );*/
// Prepare the overlay for display by setting the source
// and destination rectangles. This is the tricky part.
// We'll start with the upper left corners of both
// source and destination. Using zeros is the easy
// way out -- zero aligns with everything.
rSrc.left = 0;
rSrc.top = 0;
rDest.left = 0;
rDest.top = 0;
// To show the entire sprite rectangle, we need a bottom
// right coordinates of 64. Alignment only applies to the right
// x coordinates if DDCAPS_ALIGNSIZESRC is set in DDCAPS.
rSrc.bottom = 64;
rSrc.right = 64;
// A value of 64 will probably work, since it aligns with
// both 4 and 8. If it doesn't, we have to go
// smaller.
if ( ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC )
{
rSrc.right = AlignDown( rSrc.right, ddcaps.dwAlignSizeSrc );
}
// The destination rectangle must comply both with
// alignment and scaling limitations. Again, it's the
// right X coord that we're concerned with. If you
// want the aspect ratio to remain correct, you should
// adjust the bottom or Y coordinates as well.
// Stretching is specified "times 1000" so that it can be
// expressed as an integer value. A value of 1000 indicates
// no stretching. We'd like 4x if we can get it. A value of 0
// for dwMaxOverlayStretch indicates no maximum limit.
if ( ( ddcaps.dwMinOverlayStretch <= 4000 ) &&
( ( ddcaps.dwMaxOverlayStretch >= 4000 ) ||
ddcaps.dwMaxOverlayStretch == 0 ) )
{
// Add 999 to compensate for integer truncation.
rDest.right = ( ( rSrc.right * 4000 +
999 ) / 1000 );
}
else
// Take the minimum allowable if we can't get 4000.
{
// Add 999 to compensate for integer truncation.
rDest.right = ( ( rSrc.right * ddcaps.dwMinOverlayStretch +
999 ) / 1000 );
if ( rDest.right == 0 ) rDest.right = rSrc.right;
}
// Align the result, if necessary. Align upward to avoid
// messing up our minimum stretch adjustment.
if ( ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST )
{
rDest.right = AlignUp( rDest.right, ddcaps.dwAlignSizeDest );
}
// Our image is square, so adjust the bottom as well.
rDest.bottom = rDest.right;
rDest=rSrc;
break;
case WM_COMMAND:
switch ( LOWORD( wParam ) )
{
case IDC_SHOW:
if ( bVisible )
{
// Hide the overlay.
if ( FAILED( lpDDSOverlay->UpdateOverlay( NULL,
lpDDSPrimary, NULL, DDOVER_HIDE, NULL ) ) )
{
return Fail( hwnd,
"Couldn't hide the overlay./n" );
}
bVisible = FALSE;
SetDlgItemText( hwnd, IDC_SHOW, "Show" );
}
else
{
// If the Use ColorKey check box is checked,
// include the appropriate flag.
if ( SendDlgItemMessage( hwnd, IDC_COLORKEY,
BM_GETCHECK, 0, 0 ) == BST_CHECKED )
{
dwFlags = DDOVER_SHOW
//| DDOVER_KEYSRC;
| DDOVER_KEYDEST;
}
else
{
dwFlags = DDOVER_SHOW;
}
// Show the overlay.
if ( FAILED( lpDDSOverlay->UpdateOverlay( &rSrc,
lpDDSPrimary, &rDest, dwFlags, NULL ) ) )
{
lpDDSOverlay->UpdateOverlayZOrder(DDOVERZ_SENDTOFRONT, 0);
char tmp[1024];
sprintf(tmp,"Couldn't show the overlay. rSrc(%d,%d,%d,%d) rDest(%d,%d,%d,%d) Err=%d /n",
rSrc.left,rSrc.top,rSrc.right,rSrc.bottom,
rDest.left,rDest.top,rDest.right,rDest.bottom,
GetLastError());
MessageBox(NULL,tmp,NULL,MB_OK);
//return Fail( hwnd,
// "Couldn't show the overlay./n" );
}
else
{
bVisible = TRUE;
SetDlgItemText( hwnd, IDC_SHOW, "Hide" );
}
}
break;
case IDCANCEL:
EndDialog( hwnd, FALSE );
return TRUE;
}
break;
case WM_CLOSE:
EndDialog( hwnd, TRUE );
return TRUE;
break;
}
return FALSE;
}
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
DialogBox( hInstance, MAKEINTRESOURCE( IDD_MAIN ),
NULL, ( DLGPROC )DialogProc );
return TRUE;
}
...
UpdateOverlay GetDc
最新推荐文章于 2020-11-03 11:34:49 发布