#include <ddraw.h>
#include <windows.h>
#define WIN32_LEAN_AND_MEAN
LPDIRECTDRAW lpDD; // DirectDraw object defined in DDRAW.H
bool DirectDrawInit(HWND hwnd)
{
HRESULT ddrval;
ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
if( ddrval != DD_OK )
{
return(false);
}
ddrval = lpDD->SetCooperativeLevel( hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
if( ddrval != DD_OK )
{
lpDD->Release();
return(false);
}
ddrval = lpDD->SetDisplayMode( 640, 480, 8);
if( ddrval != DD_OK )
{
lpDD->Release();
return(false);
}
return(true);
}
LPDIRECTDRAWSURFACE lpDDSPrimary; // DirectDraw primary surface
LPDIRECTDRAWSURFACE lpDDSBack; // DirectDraw back surface
LPDIRECTDRAWSURFACE lpDDSOne;
bool CreatePrimarySurface()
{
DDSURFACEDESC ddsd;
DDSCAPS ddscaps;
HRESULT ddrval;
memset( &ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
if( ddrval != DD_OK )
{
lpDD->Release();
return(false);
}
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps, &lpDDSBack);
if( ddrval != DD_OK )
{
lpDDSPrimary->Release();
lpDD->Release();
return(false);
}
return true;
}
IDirectDrawSurface * CreateOffScreenSurface(IDirectDraw *pdd, int dx, int dy)
{
DDSURFACEDESC ddsd;
IDirectDrawSurface *pdds;
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT |DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = dx;
ddsd.dwHeight = dy;
if (pdd->CreateSurface(&ddsd, &pdds, NULL) != DD_OK)
{
return NULL;
} else {
return pdds;
}
}
HRESULT DDCopyBitmap(IDirectDrawSurface *pdds, HBITMAP hbm, int dx, int dy)
{
HDC hdcImage;
HDC hdc;
HRESULT hr;
HBITMAP hbmOld;
hdcImage = CreateCompatibleDC(NULL);
hbmOld = (HBITMAP)SelectObject(hdcImage, hbm);
if ((hr = pdds->GetDC(&hdc)) == DD_OK)
{
BitBlt(hdc, 0, 0, dx, dy, hdcImage, 0, 0, SRCCOPY);
pdds->ReleaseDC(hdc);
}
SelectObject(hdcImage, hbmOld);
DeleteDC(hdcImage);
return hr;
}
IDirectDrawSurface * DDLoadBitmap(IDirectDraw *pdd, LPCSTR szBitmap)
{
HBITMAP hbm;
BITMAP bm;
IDirectDrawSurface *pdds;
hbm = (HBITMAP)LoadImage(NULL, szBitmap, IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
if (hbm == NULL)
return NULL;
GetObject(hbm, sizeof(bm), &bm); // get size of bitmap
pdds = CreateOffScreenSurface(pdd, bm.bmWidth, bm.bmHeight);
if (pdds) {
DDCopyBitmap(pdds, hbm, bm.bmWidth, bm.bmHeight);
}
DeleteObject(hbm);
return pdds;
}
void updateFrame( void )
{
RECT rcRect;
HRESULT ddrval;
//IDirectDrawSurface *lpDDSMySprite;
IDirectDraw *lpDDSBMP;
LPDDSURFACEDESC SurfDesc;
lpDDSBMP->CreateSurface (SurfDesc, &lpDDSOne, NULL);
DDLoadBitmap (lpDDSBMP, "bitmap.bmp");
SetRect(&rcRect, 0, 0, 640, 480);
ddrval = lpDDSBack->BltFast( 0, 0, lpDDSOne, &rcRect,
DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
if( ddrval != DD_OK )
{
return;
}
ddrval = lpDDSPrimary->Flip( NULL, DDFLIP_WAIT );
}
void finiObjects( void )
{
if( lpDD != NULL )
{
if( lpDDSPrimary != NULL )
{
lpDDSPrimary->Release();
lpDDSPrimary = NULL;
}
if( lpDDSOne != NULL )
{
lpDDSOne->Release();
lpDDSOne = NULL;
}
lpDD->Release();
lpDD = NULL;
}
LRESULT CALLBACK WindowProc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message)
{
case WM_KEYDOWN:
{
switch (wparam)
{
case VK_ESCAPE:
PostMessage (hwnd, WM_CLOSE, 0, 0);
return (0);
break;
}
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}
break;
default:
break;
}
return (DefWindowProc(hwnd, message, wparam, lparam));
}
int WINAPI WinMain (HINSTANCE hinst, HINSTANCE hprevinst, LPSTR lpcmdline, int ncmdshow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG message;
DWORD loop_start_time;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinst;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_CROSS);
winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = "Class Name";
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&winclass))
return(0);
if (!(hwnd = CreateWindowEx(NULL, "Class Name", "GROW! BMP_TEST_BUILD", WS_POPUPWINDOW | WS_VISIBLE, 0, 0, 800, 600, NULL, NULL, hinst, NULL)))
return(0);
ShowWindow (hwnd, ncmdshow);
UpdateWindow (hwnd);
ShowCursor (false);
while(GetMessage(&message,NULL,0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
loop_start_time = GetTickCount ();
updateFrame ();
while ( ( GetTickCount ( ) - loop_start_time ) < 40 );
return message.wParam;
}