Original Post
Hey, I'm starting to get back into C++ again. I'v had a long enought break. I was playing SspiderMan and beat it in 2 day.
Any ways, I need some one to check over my code real quike. Its just a graphics class to initailize DirectDraw7 and setup a few offscreen surfaceses. Well, heres what I got so far.
I haven't included any of the error handeling, like MessageBoxes and log files yet. How would I get a MessageBox to show up while DirectDraw is running ?? I think I might of asked that before, but I'm not sure.
Thanx for any advice thats helpfull in advance.
[edited by - Sakky on August 1, 2002 2:26:54 PM]
// Header
#ifndef C_GRAPHICS2D_H
#define C_GRAPHICS2D_H
#include <afxwin.h>
#include <ddraw.h>
class CGraphics2D
{
IDirectDraw7 *lpDD;
IDirectDrawSurface7 *lpDDSPrimary;
IDirectDrawSurface7 *lpDDSBack;
DDSURFACEDESC2 ddsd;
DDSCAPS2 ddsCaps;
public:
CGraphics2D();
~CGraphics2D();
BOOL Init(HWND);
void Blit(IDirectDrawSurface7 *,RECT,RECT);
IDirectDrawSurface7 *CreateBitmapSurface(LPSTR,COLORREF);
void Flip(void);
void Clear(void);
void Destroy(void);
};
#endif // C_GRAPHICS2D_H
// Source File
#include <afxwin.h>
#include <ddraw.h>
#include "CGraphics2D.h"
CGraphics2D::CGraphics2D()
{
lpDD = NULL;
lpDDSPrimary = NULL;
lpDDSBack = NULL;
}
CGraphics2D::~CGraphics2D()
{
Destroy();
}
BOOL CGraphics2D::Init(HWND hWnd)
{
if(FAILED(DirectDrawCreateEx(0,(LPVOID*)&lpDD,IID_IDirectDraw7,0)))
return(FALSE);
if(FAILED(lpDD->SetCooperativeLevel(hWnd,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT)))
return(FALSE);
if(FAILED(lpDD->SetDisplayMode(640,480,32,0,0)))
{
if(FAILED(lpDD->SetDisplayMode(640,480,16,0,0)))
return(FALSE);
}
::ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_PRIMARYSURFACE;
ddsd.dwBackBufferCount = 1;
if(FAILED(lpDD->CreateSurface(&ddsd,&lpDDSPrimary,0)))
return(FALSE);
ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
if(FAILED(lpDDSPrimary->GetAttachedSurface(&ddsCaps,&lpDDSBack)))
return(FALSE);
return(TRUE);
}
void CGraphics2D::Clear()
{
DDBLTFX clear;
long hr = 0;
::ZeroMemory(&clear,sizeof(clear));
clear.dwSize = sizeof(clear);
clear.dwFillColor = 0;
while(TRUE)
{
hr = lpDDSBack-> Blt(NULL,NULL,NULL,DDBLT_COLORFILL,&clear);
if(hr == DD_OK)
break;
if(hr == DDERR_SURFACELOST)
lpDDSPrimary->Restore();
if(hr == DDERR_WASSTILLDRAWING)
return;
}
if(hr != DD_OK)
return;
}
void CGraphics2D::Flip()
{
long hr = 0;
while(TRUE)
{
hr = lpDDSPrimary->Flip(NULL,DDFLIP_WAIT);
if(hr == DD_OK)
break;
if(hr == DDERR_SURFACELOST)
lpDDSPrimary->Restore();
if(hr == DDERR_WASSTILLDRAWING)
return;
}
if(hr != DD_OK)
return;
}
void CGraphics2D::Destroy()
{
if(lpDD != NULL)
{
if(lpDDSPrimary != NULL)
{
lpDDSPrimary->Release();
lpDDSPrimary = NULL;
}
lpDD->Release();
lpDD = NULL;
}
}
void CGraphics2D::Blit(IDirectDrawSurface7 *lpDDS,RECT rcDest,RECT rcSrc)
{
long hr = 0;
while(TRUE)
{
hr = lpDDSBack-> Blt(&rcDest,lpDDS,&rcSrc,DDBLT_KEYSRC,NULL);
if(hr == DD_OK)
break;
if(hr == DDERR_SURFACELOST)
lpDDSPrimary->Restore();
if(hr == DDERR_WASSTILLDRAWING)
return;
}
if(hr != DD_OK)
return;
}
IDirectDrawSurface7 *CGraphics2D::CreateBitmapSurface(LPSTR szBitmap,COLORREF SrcKey)
{
DDCOLORKEY DDColorKey;
HBITMAP hbm = NULL;
BITMAP bm;
HDC hDC = NULL;
HDC hImageDC = NULL;
DDSURFACEDESC2 DDsd;
IDirectDrawSurface7 *pdds = NULL;
hbm = (HBITMAP) ::LoadImage(GetModuleHandle(NULL),szBitmap,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
if(hbm == NULL)
hbm = (HBITMAP) ::LoadImage(NULL,szBitmap,IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if(hbm == NULL)
return NULL;
::GetObject(hbm, sizeof(bm), &bm);
::ZeroMemory(&ddsd, sizeof(ddsd));
DDsd.dwSize = sizeof(ddsd);
DDsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_CKSRCBLT;
DDsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
DDsd.dwWidth = bm.bmWidth;
DDsd.dwHeight = bm.bmHeight;
DDColorKey.dwColorSpaceLowValue = (DWORD) SrcKey;
DDColorKey.dwColorSpaceHighValue = (DWORD) SrcKey;
DDsd.ddckCKSrcBlt = DDColorKey;
if(FAILED(lpDD->CreateSurface(&DDsd,&pdds,NULL)))
return(NULL);
pdds->GetDC(&hDC);
hDC = ::CreateCompatibleDC(hImageDC);
::SelectObject(hImageDC,hbm);
::BitBlt(hDC,0,0,bm.bmWidth,bm.bmHeight,hImageDC,0,0,SRCCOPY);
pdds->ReleaseDC(hDC);
::DeleteDC(hDC);
::DeleteDC(hImageDC);
::DeleteObject(hbm);
return(pdds);
}