// set frontSurface to lpDDSurface if lpDDSurface is primary // returns true if frontSurface is set // returns false if frontSurface is NULL and lpDDSurface is not primary bool getFrontSurface(LPDIRECTDRAWSURFACE7 lpDDSurface) { //logOutput << CurrentTimeString() << "called getFrontSurface" << endl; if (!lpDDSurface) { //logOutput << CurrentTimeString() << "lpDDSurface null" << endl; return false; } if (!g_ddInterface) { LPDIRECTDRAWSURFACE7 dummy; if (lpDDSurface->QueryInterface(IID_IDirectDrawSurface7, (LPVOID*)&dummy) == S_OK) { IUnknown* Unknown; HRESULT err; if (FAILED(err = dummy->GetDDInterface((LPVOID*)&Unknown))) { logOutput << CurrentTimeString() << "getFrontSurface: could not get DirectDraw interface" << endl; printDDrawError(err, "getFrontSurface"); } else { if (Unknown->QueryInterface(IID_IDirectDraw7, (LPVOID*)&g_ddInterface) == S_OK) { logOutput << CurrentTimeString() << "Got DirectDraw interface pointer" << endl; } else { logOutput << CurrentTimeString() << "Query of DirectDraw interface failed" << endl; } } ddrawSurfaceRelease.Unhook(); dummy->Release(); ddrawSurfaceRelease.Rehook(); } } if (!bTargetAcquired) { DDSCAPS2 caps; if (SUCCEEDED(lpDDSurface->GetCaps(&caps))) { //logOutput << CurrentTimeString() << "checking if surface is primary" << endl; if (caps.dwCaps & DDSCAPS_PRIMARYSURFACE) { logOutput << CurrentTimeString() << "found primary surface" << endl; g_frontSurface = lpDDSurface; if (!SetupDDraw()) { return false; } else { bTargetAcquired = true; } } } else { logOutput << CurrentTimeString() << "could not retrieve caps" << endl; } } return lpDDSurface == g_frontSurface; }
HRESULT CTextureHolder::Restore( LPDIRECT3DDEVICE7 pd3dDevice ) { // Release any previously created objects SAFE_RELEASE( m_pddsSurface ); // Check params if( NULL == pd3dDevice ) return DDERR_INVALIDPARAMS; // Get the device caps D3DDEVICEDESC7 ddDesc; if( FAILED( pd3dDevice->GetCaps( &ddDesc) ) ) return E_FAIL; // Setup the new surface desc DDSURFACEDESC2 ddsd; D3DUtil_InitSurfaceDesc( ddsd ); ddsd.dwFlags = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH| DDSD_PIXELFORMAT|DDSD_TEXTURESTAGE; ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE; ddsd.dwTextureStage = 0; //m_dwStage; ddsd.dwWidth = m_dwWidth; ddsd.dwHeight = m_dwHeight; // Turn on texture management for hardware devices if( ddDesc.deviceGUID == IID_IDirect3DHALDevice ) ddsd.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE; else if( ddDesc.deviceGUID == IID_IDirect3DTnLHalDevice ) ddsd.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE; else ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY; // Adjust width and height to be powers of 2, if the device requires it if( ddDesc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2 ) { for( ddsd.dwWidth=1; m_dwWidth>ddsd.dwWidth; ddsd.dwWidth<<=1 ); for( ddsd.dwHeight=1; m_dwHeight>ddsd.dwHeight; ddsd.dwHeight<<=1 ); } // Limit max texture sizes, if the driver can't handle large textures DWORD dwMaxWidth = ddDesc.dwMaxTextureWidth; DWORD dwMaxHeight = ddDesc.dwMaxTextureHeight; ddsd.dwWidth = min( ddsd.dwWidth, ( dwMaxWidth ? dwMaxWidth : 256 ) ); ddsd.dwHeight = min( ddsd.dwHeight, ( dwMaxHeight ? dwMaxHeight : 256 ) ); // Make the texture square, if the driver requires it if( ddDesc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_SQUAREONLY ) { if( ddsd.dwWidth > ddsd.dwHeight ) ddsd.dwHeight = ddsd.dwWidth; else ddsd.dwWidth = ddsd.dwHeight; } // Setup the structure to be used for texture enumration. TEXTURESEARCHINFO tsi; tsi.bFoundGoodFormat = FALSE; tsi.pddpf = &ddsd.ddpfPixelFormat; tsi.dwDesiredBPP = m_dwBPP; tsi.bUsePalette = ( m_dwBPP <= 8 ); tsi.bUseAlpha = m_bHasMyAlpha; if( m_dwFlags & D3DTEXTR_16BITSPERPIXEL ) tsi.dwDesiredBPP = 16; else if( m_dwFlags & D3DTEXTR_32BITSPERPIXEL ) tsi.dwDesiredBPP = 32; if( m_dwFlags & (D3DTEXTR_TRANSPARENTWHITE|D3DTEXTR_TRANSPARENTBLACK) ) { if( tsi.bUsePalette ) { if( ddDesc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE ) { tsi.bUseAlpha = TRUE; tsi.bUsePalette = TRUE; } else { tsi.bUseAlpha = TRUE; tsi.bUsePalette = FALSE; } } } // Enumerate the texture formats, and find the closest device-supported // texture pixel format pd3dDevice->EnumTextureFormats( TextureSearchCallback, &tsi ); // If we couldn't find a format, let's try a default format if( FALSE == tsi.bFoundGoodFormat ) { tsi.bUsePalette = FALSE; tsi.dwDesiredBPP = 16; pd3dDevice->EnumTextureFormats( TextureSearchCallback, &tsi ); // If we still fail, we cannot create this texture if( FALSE == tsi.bFoundGoodFormat ) return E_FAIL; } // Get the DirectDraw interface for creating surfaces LPDIRECTDRAW7 pDD; LPDIRECTDRAWSURFACE7 pddsRender; pd3dDevice->GetRenderTarget( &pddsRender ); pddsRender->GetDDInterface( (VOID**)&pDD ); pddsRender->Release(); // Create a new surface for the texture HRESULT hr = pDD->CreateSurface( &ddsd, &m_pddsSurface, NULL ); // Done with DDraw pDD->Release(); if( FAILED(hr) ) return hr; // For bitmap-based textures, copy the bitmap image. if( m_hbmBitmap ) return CopyBitmapToSurface(); // At this point, code can be added to handle other file formats (such as // .dds files, .jpg files, etc.). return S_OK; }