#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <ddraw.h>
#define WIN_NAME "DDBitmapDisplay"
#define WIN_X 200
#define WIN_Y 200
#define WINPOS_X 0
#define WINPOS_Y 0
#define SIZE_X 800
#define SIZE_Y 600
#define BPP 24
#define NAME_OF_BITMAP "castle.bmp"
HWND hMainWin = NULL;
HINSTANCE hInstance = NULL;
LPDIRECTDRAW lpDDObject = NULL;
LPDIRECTDRAWSURFACE lpPrimary = NULL;
LPDIRECTDRAWSURFACE lpBackBuffer = NULL;
bool Init_DirectDraw( HWND hWin );
bool Load_Bitmap( LPDIRECTDRAWSURFACE lpSurface,
char *NameOfBitmap );
void Clean_Up();
LRESULT CALLBACK WindowProc( HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam );
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow) {
HWND hwnd;
MSG msg;
WNDCLASSEX winclass;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL,
IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL,
IDC_ARROW);
winclass.hbrBackground = (HBRUSH)
GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = "WIN_SHELL";
winclass.hIconSm = NULL;
if(!RegisterClassEx( &winclass ))
return FALSE;
if(!(hwnd = CreateWindow("WIN_SHELL",
WIN_NAME, WS_VISIBLE |
WS_POPUP,
WINPOS_X,WINPOS_Y,
WIN_X,WIN_Y,
NULL,
NULL,
hinstance,
NULL)))
return FALSE;
hMainWin = hwnd;
ShowCursor( FALSE );
if(!Init_DirectDraw( hMainWin ))
Clean_Up();
if(!Load_Bitmap( lpBackBuffer,NAME_OF_BITMAP ))
Clean_Up();
while ( true )
{
if( PeekMessage(&msg,NULL,0,0,PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
break;
TranslateMessage( &msg ); DispatchMessage ( &msg );
}
}
return(msg.wParam);
}
LRESULT CALLBACK WindowProc( HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam ) {
PAINTSTRUCT ps;
HDC hdc;
switch ( msg )
{
case WM_CREATE:
return 0;
case WM_PAINT:
hdc = BeginPaint( hwnd,&ps);
EndPaint( hwnd,&ps );
return 0;
case WM_DESTROY:
Clean_Up();
return 0;
case WM_KEYDOWN:
if ( ((int)wparam) == VK_RETURN )
Clean_Up();
else
lpPrimary->Flip( NULL,DDFLIP_WAIT );
return 0;
default:break;
}
return ( DefWindowProc( hwnd, msg, wparam, lparam ) );
}
bool Init_DirectDraw( HWND hWin ) {
DDSURFACEDESC ddsd;
ZeroMemory( &ddsd,sizeof(ddsd) );
if ( DirectDrawCreate( NULL,&lpDDObject,NULL ) != DD_OK )
{
MessageBox( hWin,"DDObject ERROR","ERROR",MB_OK );
return false;
}
if ( lpDDObject->SetCooperativeLevel(
hWin,DDSCL_EXCLUSIVE |
DDSCL_FULLSCREEN |
DDSCL_ALLOWREBOOT ) != DD_OK)
{
MessageBox( hWin,"Error Coop. level.","ERROR",MB_OK );
return false;
}
if ( lpDDObject->SetDisplayMode( SIZE_X,SIZE_Y,BPP ) !=
DD_OK )
{
MessageBox( hWin,"Error display mode.","ERROR",MB_OK );
return false;
}
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
if( lpDDObject->CreateSurface( &ddsd,&lpPrimary,NULL )!=
DD_OK )
{
MessageBox( hWin,"Error P. surface.","ERROR",MB_OK );
return false;
}
DDSCAPS ddscaps;
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
if ( lpPrimary->GetAttachedSurface(
&ddscaps,&lpBackBuffer ) != DD_OK )
{
MessageBox( hWin,"Error backbuffer.","ERROR",MB_OK );
return false;
}
return true;
}
void Clean_Up() {
if ( lpDDObject )
{
if ( lpBackBuffer )
{
lpBackBuffer->Release();
lpBackBuffer = NULL;
}
if ( lpPrimary )
{
lpPrimary->Release();
lpPrimary = NULL;
}
lpDDObject->Release();
lpDDObject = NULL;
}
PostQuitMessage( 0 );
}
bool Load_Bitmap( LPDIRECTDRAWSURFACE surface,
char *nameOfBitmap ) {
HBITMAP hBitmap = NULL;
HDC hdcSurface = NULL;
HDC hdcMain = NULL;
HRESULT result = NULL;
DDSURFACEDESC ddsd;
hBitmap = (HBITMAP)LoadImage(
NULL,nameOfBitmap,IMAGE_BITMAP,0,0,LR_LOADFROMFILE );
if(hBitmap == NULL) {
MessageBox( hMainWin,"Error bitmap.","ERROR",MB_OK );
return false;
}
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
surface->GetSurfaceDesc(&ddsd);
result = surface->GetDC(&hdcSurface);
hdcMain = CreateCompatibleDC(NULL);
SelectObject(hdcMain,hBitmap);
if(!BitBlt(hdcSurface,0,0,
ddsd.dwWidth,ddsd.dwHeight,
hdcMain,0,0,SRCCOPY)) {
MessageBox( hMainWin,"ERROR BLT bitmap.","ERROR",MB_OK );
return false;
}
surface->ReleaseDC(hdcSurface);
DeleteDC(hdcMain);
DeleteObject(hBitmap);
return true;
}
Mike L.G.
mlg400@blazemail.com