Hi all,
I've been working on some directdraw code from Andre Lamothe's book Tricks of the windows games programming gurus. I'm having a bit of trouble locking the back buffer. When I call lock() on the surface, it throws an error DDERR_SURFACEBUSY, apparently another thread has already locked the surface. However, I tried unlocking the surface first and I get an error saying that the surface is not locked. This confuses me.
I've checked my code a few times and the main and init methods look the same as Andre's, except I did not define a custom palette. I have no idea why the back surface would be locked - can anyone help please?
My initiailisation method (I'm pretty sure this is not where the problem is):
int Game_Init()
{
if( FAILED( DirectDrawCreateEx( NULL, (void **)&lpDD,
IID_IDirectDraw7, NULL ) ) )
return(0); //Error
if( FAILED( lpDD->SetCooperativeLevel( hMainWnd, DDSCL_FULLSCREEN
| DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE
| DDSCL_ALLOWREBOOT ) ) )
return(0); //Error
if( FAILED( lpDD->SetDisplayMode( SCREEN_WIDTH, SCREEN_HEIGHT,
SCREEN_BPP, 0, 0 ) ) )
return(0); //Error
//Setup direct draw surface description for primary surface
memset( &ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount = 1;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX
| DDSCAPS_FLIP;
if( FAILED( lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL ) ) )
return(0); //Error
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
if( FAILED( lpDDSPrimary->GetAttachedSurface( &ddsd.ddsCaps,
&lpDDSBack ) ) )
return(0);
return(1);
}
And my main method looks like (the error is thrown calling lock()):
int Game_Main()
{
if( window_closed )
return(0);
if( KEYDOWN( VK_ESCAPE ) )
{
SendMessage( hMainWnd, WM_CLOSE, 1, 0 );
window_closed = 1;
}
memset( &ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof(ddsd);
int dderrcode;
if( FAILED( dderrcode = lpDDSBack->Lock( NULL, &ddsd,
DLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, 0 ) ) )
{
return(dderrcode);
}
...
}