Original Post
My DirectDraw7 Wrapper class:
When I use this class this way:
DDHANDLER DDHandler;
everything works fine, but when I do
DDHANDLER *DDHandler;
DDHandler = new DDHANDER;
calling DDHandler->SetupDirectDraw(16);
gives me error in:
PrimarySurface->GetAttachedSurface(&MainSurfCaps, &Backbuffer);
the return value is DDERR_NOTFOUND thus I am unable to use my Backbuffer.
Why does it matter whether or not it''s a pointer or an intance to that class?
#ifndef __DIRECTDRAWHANDLE_H__
#define __DIRECTDRAWHANDLE_H__
#define FASTCALL __fastcall //use register-based calling
///---Include main headers
#include <windows.h>
#include <ddraw.h>
///---
///---DirectDraw Version defines
#define DIRECTDRAW7
#ifdef DIRECTDRAW7
#define LPDIRECTDRAW LPDIRECTDRAW7
#define LPDIRECTDRAWSURFACE LPDIRECTDRAWSURFACE7
#define DDSURFACEDESC DDSURFACEDESC2
#define DDSCAPS DDSCAPS2
#endif
///---
///---DirectDraw Objects defines
#define LPDD LPDIRECTDRAW
#define LPDDSURF LPDIRECTDRAWSURFACE
#define DDSURFDESC DDSURFACEDESC
///---
//////////Start of DirectDrawHandler''s class
class DDHANDLER
{
private:
///--Main objects
HWND WindowHandle;
LPDD DDObj;
LPDDSURF PrimarySurface;
LPDDSURF Backbuffer;
DDSURFDESC MainSurfDesc;
DDSCAPS MainSurfCaps;
LPDIRECTDRAWCLIPPER Clipper;
///--
///--Description variables
int MainWidth, MainHeight;
int MainX, MainY;
int ColorDepth;
///--
///--Flags
BOOL FullScreenMode;
///--
public:
///--Constructors and Destructors
DDHANDLER();
~DDHANDLER();
///--
DWORD FASTCALL CreateMainWindow(HWND &rHandle,
char *ClassName, char *WindowName, void *MessageProc,
DWORD Style, HINSTANCE hInst, int Width, int Height,
BOOL FullScreen = true);
DWORD FASTCALL SetupDirectDraw(int ColorMode);
DWORD SetupFullScreenWindow(HWND &rHandle, char *ClassName,
char *WinName,
void *MessagePrc,
HINSTANCE hInst);
DWORD SetupDirectDrawFullScreen(int Width, int Height, int ColorMode);
DWORD FASTCALL FillBackbuffer(DWORD Color);
DWORD FASTCALL Flip();
DWORD FASTCALL RestoreSurfaces();
};
#endif
/////SetupDirectDraw/////
//
DWORD DDHANDLER::SetupDirectDraw(int ColorMode)
{
//--Create DirectDraw object--//
if (DirectDrawCreateEx(NULL, (void**)&DDObj, IID_IDirectDraw7, NULL) != DD_OK)
{
return(1);
}//end if
//--//
//--Set cooperative level and display mode--//
if (FullScreenMode) //Fullscreen mode
{
if (DDObj->SetCooperativeLevel(WindowHandle, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT) != DD_OK)
{
return(2);
}//end if
if (DDObj->SetDisplayMode(MainWidth, MainHeight, ColorMode, 0, 0) != DD_OK)
{
return(3);
}//end if
}
else //Windowed mode
{
if (DDObj->SetCooperativeLevel(WindowHandle, DDSCL_NORMAL) != DD_OK)
{
return(2);
}
}//end if
//--//
//--Create primary surface with backbuffer--//
ZeroMemory(&MainSurfDesc, sizeof(MainSurfDesc));
MainSurfDesc.dwSize = sizeof(MainSurfDesc);
MainSurfDesc.dwFlags = DDSD_CAPS;
MainSurfDesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if (FullScreenMode)
{
MainSurfDesc.ddsCaps.dwCaps |= DDSCAPS_FLIP | DDSCAPS_COMPLEX;
MainSurfDesc.dwFlags |= DDSD_BACKBUFFERCOUNT;
MainSurfDesc.dwBackBufferCount = 1;
}//end if
if (DDObj->CreateSurface (&MainSurfDesc, &PrimarySurface, NULL) != DD_OK)
{
return(4);
}
//Get the backbuffer depending on which mode
if (FullScreenMode)
{
MainSurfCaps.dwCaps = DDSCAPS_BACKBUFFER;
HRESULT ddrval;
/*
if (PrimarySurface->GetAttachedSurface(&MainSurfCaps, &Backbuffer) != DD_OK)
{
return(5);
}
*/
ddrval = PrimarySurface->GetAttachedSurface(&MainSurfCaps, &Backbuffer);
switch (ddrval)
{
case DDERR_INVALIDOBJECT:
return(6);
case DDERR_INVALIDPARAMS:
return(7);
case DDERR_NOTFOUND:
return(8);
case DDERR_SURFACELOST:
return(9);
}
}
else
{
MainSurfDesc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
MainSurfDesc.ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY;
MainSurfDesc.dwWidth = MainWidth;
MainSurfDesc.dwHeight = MainHeight;
if (DDObj->CreateSurface(&MainSurfDesc, &Backbuffer, NULL) != DD_OK)
{
return(5);
}
}//end if
//--Setup DD clipper--//
if (!FullScreenMode)
{
DDObj->CreateClipper(0, &Clipper, NULL);
Clipper->SetHWnd(0, WindowHandle);
PrimarySurface->SetClipper(Clipper);
}//end if
//--//
//--Test and set pixel format--//
//only need to test if 16bit is requested in the parameter
// since it could also be 15bit,
// anything else is equal to the parameter
if (ColorMode == 16)
{
DDPIXELFORMAT ddpixel;
ZeroMemory(&ddpixel, sizeof(ddpixel));
ddpixel.dwSize = sizeof(ddpixel);
PrimarySurface->GetPixelFormat(&ddpixel);
switch (ddpixel.dwRGBBitCount)
{
case 15:
ColorDepth = 15;
break;
case 16:
ColorDepth = 16;
break;
}//end switch
}
else
{
ColorDepth = ColorMode;
}//end if
//--//
//--//
return(0);
//--//
}
/////End SetupDirectDraw/////
|