Skip to main content
gamedev.net
🔒 Locked

The Problem with Pointer

Started by vbisme Oct 11, 2001 at 7:11 PM 6 replies 200+ views
Original Post
vbisme
vbisme
My DirectDraw7 Wrapper 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/////

  
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?
Shannon Barber
Shannon Barber
Check _every return value from _every function as well

Use a macro to make it easier to track down
  
#ifdef _DEBUG
#define TESTHR(Test_) \
if(S_OK!=(hr=Test_))\
{\
char szError[256];\
if(FAILED(hr))\
{\
sprintf(szError, "Error 0x%x\n%s\nOn line %i in %s\n\0", hr, #Test_, __LINE__, __FILE__);\
OutputDebugString("*****************\n");\
OutputDebugString(szError);\
return(hr);\
}\
else\
{\
sprintf(szError, "Warning 0x%x\n%s\nOn line %i in %s\n\0", hr, #Test_, __LINE__, __FILE__);\
OutputDebugString("*****************\n");\
OutputDebugString(szError);\
}\
}
#else
#define TESTHR(Test_) if(FAILED(hr=Test_)) return(hr);
#endif
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
vbisme
vbisme
I set every pointer to NULL. If there''s a problem and that errors are returned, I always test it. I don''t see why using the wrapper as an instance cause no problem while using it as a pointer caused the problem.

I''ve pin pointed to problem down to the line mention above about the Backbuffer with GetAttachedSurface returning DDERR_NOTFOUND. Up to that point there is no code related to the Backbuffer.
Gladiator
Gladiator
quote:

DDHANDLER *DDHandler;
DDHandler = new DDHANDLER;



Give this a try:

DDHANDLER *DDHandler = NULL;
DDHandler = new DDHANDLER();
vbisme
vbisme
Same problem, absolutely no change.
vbisme
vbisme
Would it be ok if you look at my project? It''s only 14kb.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.