int DD_Shutdown(void)
{
// this function release all the resources directdraw
// allocated, mainly to com objects

// release the clipper first
if (lpddclipper)
    lpddclipper->Release();

// release the palette
if (lpddpal)
   lpddpal->Release();

// release the secondary surface
if (lpddsback)
    lpddsback->Release();

// release the primary surface
if (lpddsprimary)
   lpddsprimary->Release();

// finally, the main dd object
if (lpdd)
    lpdd->Release();

// return success
return(1);
} // end DD_Shutdown
示例#2
0
void DirectDrawDisplay::cleanup()
{
  if(pDirectDraw != NULL) {
    if(ddsClipper != NULL) {
      ddsClipper->Release();
      ddsClipper = NULL;
    }

    if(ddsFlip != NULL) {
      ddsFlip->Release();
      ddsFlip = NULL;
    }

    if(ddsOffscreen != NULL) {
      ddsOffscreen->Release();
      ddsOffscreen = NULL;
    }
    
    if(ddsPrimary != NULL) {
      ddsPrimary->Release();
      ddsPrimary = NULL;
    }
    
    pDirectDraw->Release();
    pDirectDraw = NULL;
  }

  if(ddrawDLL != NULL) {
    AfxFreeLibrary(ddrawDLL);
    ddrawDLL = NULL;
  }
  width = 0;
  height = 0;
}
示例#3
0
void CleanUp()
{
	g_surfCar.Destroy();

	if(g_pDDSBack)
		g_pDDSBack->Release();

	if(g_pDDSFront)
		g_pDDSFront->Release();

	if(g_pDD)
		g_pDD->Release();
}
示例#4
0
int Game_Shutdown(void *parms = NULL, int num_parms = 0)
{
// this is called after the game is exited and the main event
// loop while is exited, do all you cleanup and shutdown here


// first the palette
if (lpddpal)
   {
   lpddpal->Release();
   lpddpal = NULL;
   } // end if


// now the lpddsbackground surface
if (lpddsbackground)
   {
   lpddsbackground->Release();
   lpddsbackground = NULL;
   } // end if

// now the lpddsback surface
if (lpddsback)
   {
   lpddsback->Release();
   lpddsback = NULL;
   } // end if


// now the primary surface
if (lpddsprimary)
   {
   lpddsprimary->Release();
   lpddsprimary = NULL;
   } // end if

// now blow away the IDirectDraw4 interface
if (lpdd)
   {
   lpdd->Release();
   lpdd = NULL;
   } // end if

// unload the bitmap file, we no longer need it
Unload_Bitmap_File(&bitmap);

// return success or failure or your own return code here
return(1);

} // end Game_Shutdown
示例#5
0
/*****************************Private*Routine******************************\
* DDSurfEnumFunc
*
\**************************************************************************/
HRESULT WINAPI
DDSurfEnumFunc(
    LPDIRECTDRAWSURFACE7 pdds,
    DDSURFACEDESC2* pddsd,
    void* lpContext
    )
{
    LPDIRECTDRAWSURFACE7* ppdds = (LPDIRECTDRAWSURFACE7*)lpContext;

    DDSURFACEDESC2 ddsd;
    INITDDSTRUCT(ddsd);

    HRESULT hr = pdds->GetSurfaceDesc(&ddsd);
    if(SUCCEEDED(hr))
    {
        if(ddsd.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
        {
            *ppdds = pdds;
            return DDENUMRET_CANCEL;

        }
    }

    pdds->Release();
    return DDENUMRET_OK;
}
int Game_Shutdown(void *parms = NULL, int num_parms = 0)
{
// this is called after the game is exited and the main event
// loop while is exited, do all you cleanup and shutdown here

// kill all the surfaces


// first the palette
if (lpddpal)
   {
   lpddpal->Release();
   lpddpal = NULL;
   } // end if

// now the primary surface
if (lpddsprimary)
   {
   lpddsprimary->Release();
   lpddsprimary = NULL;
   } // end if

// now blow away the IDirectDraw4 interface
if (lpdd)
   {
   lpdd->Release();
   lpdd = NULL;
   } // end if

// return success or failure or your own return code here
return(1);

} // end Game_Shutdown
示例#7
0
//! Releases the primary surface
void DestroyPrimary()
{
    if (g_pDDSPrimary)
    {
        g_pDDSPrimary->Release();
        g_pDDSPrimary = NULL;
    }
}
示例#8
0
void DDEnd(void)
{
        if (m_pDD)
        {
                if (m_pddsFrontBuffer != NULL)
                {
                        if (m_pddsFrame) m_pddsFrame->Release();
                        m_pddsFrame = NULL;

                        if (pcClipper) pcClipper->Release();
                        pcClipper = NULL;

                        if (m_pddsFrontBuffer) m_pddsFrontBuffer->Release();
                        m_pddsFrontBuffer = NULL;
                }
                m_pDD->Release();
                m_pDD = NULL;
        }
}
示例#9
0
//-----------------------------------------------------------------------------
// Name: ShutDown()
// Desc: cleans up evreything. releases mem used
//-----------------------------------------------------------------------------
int ShutDown()
{
	// now release the primary surface
	if (lpddsPrimary!=NULL)
		lpddsPrimary->Release();
       
	// release the directdraw object
	if (lpddObj!=NULL)
		lpddObj->Release();

	return(1);
}
示例#10
0
//CFileMapping g_filemapping;
HRESULT WINAPI EnumSurfacesCallback7(
									 LPDIRECTDRAWSURFACE7 lpDDSurface,
									 LPDDSURFACEDESC2 lpDDSurfaceDesc,
									 LPVOID lpContext
									 )
{
	//When you use the DDENUMSURFACES_DOESEXIST flag, an enumerated surface's reference count is incremented
	if(lpDDSurfaceDesc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
	{
		*(IDirectDrawSurface7**)lpContext = lpDDSurface;
		return DDENUMRET_CANCEL;
	}
	lpDDSurface->Release();
	return DDENUMRET_OK ;
}
示例#11
0
/**
 * vdraw_ddraw_free_all(): Free all DirectDraw objects.
 * @param scl If true, sets the cooperative level of lpDD before freeing it.
 */
static void WINAPI vdraw_ddraw_free_all(bool scl)
{
	if (lpDDC_Clipper)
	{
		lpDDC_Clipper->Release();
		lpDDC_Clipper = NULL;
	}
	
	if (lpDDS_Back)
	{
		lpDDS_Back->Release();
		lpDDS_Back = NULL;
	}
	
	if (lpDDS_Flip)
	{
		lpDDS_Flip->Release();
		lpDDS_Flip = NULL;
	}
	
	if (lpDDS_Primary)
	{
		lpDDS_Primary->Release();
		lpDDS_Primary = NULL;
	}
	
	if (lpDD)
	{
		if (scl)
			lpDD->SetCooperativeLevel(gens_window, DDSCL_NORMAL);
		lpDD->Release();
		lpDD = NULL;
	}
	
	lpDDS_Blit = NULL;
}
示例#12
0
BOOL Test_PrivateData (INT* passed, INT* failed)
{
	LPDIRECTDRAWSURFACE7 Surface;
    DWORD size, dummy = 0xBAADF00D;
    GUID guid = { 0 };
    GUID guid2 = { 0x1 };

    if(!CreateSurface(&Surface))
        return FALSE;

    // General test
    TEST(Surface->SetPrivateData(guid, NULL, 0, 0) == DDERR_INVALIDPARAMS);
    TEST(Surface->SetPrivateData(guid, (LPVOID)&dummy, 0, 0) == DDERR_INVALIDPARAMS);
    TEST(Surface->SetPrivateData(guid, (LPVOID)0xdeadbeef, sizeof(DWORD), 0) == DDERR_INVALIDPARAMS);
    TEST(Surface->SetPrivateData(guid, (LPVOID)&dummy, sizeof(DWORD), 0) == DD_OK);

    TEST(Surface->GetPrivateData(guid, NULL, 0) == DDERR_INVALIDPARAMS);
    TEST(Surface->GetPrivateData(guid, &dummy, 0) == DDERR_INVALIDPARAMS);
    size = 0;
    TEST(Surface->GetPrivateData(guid, &dummy, &size) == DDERR_MOREDATA && size == sizeof(DWORD));
    size = 2;
    TEST(Surface->GetPrivateData(guid, NULL, &size) == DDERR_MOREDATA && size == sizeof(DWORD));
    TEST(Surface->GetPrivateData(guid, NULL, &size) == DDERR_INVALIDPARAMS);
    TEST(Surface->GetPrivateData(guid, &dummy, &size) == DD_OK && dummy == 0xBAADF00D);
    TEST(Surface->GetPrivateData(guid2, NULL, 0) == DDERR_NOTFOUND);

    TEST(Surface->FreePrivateData(guid) == DD_OK);
    TEST(Surface->FreePrivateData(guid) == DDERR_NOTFOUND);

    // Test for DDSPD_VOLATILE flag
    TEST(Surface->SetPrivateData(guid, (LPVOID)&dummy, sizeof(DWORD), DDSPD_VOLATILE) == DD_OK);
    size = 0;
    TEST(Surface->GetPrivateData(guid, NULL, &size) == DDERR_MOREDATA && size == sizeof(DWORD));
    TEST(Surface->GetPrivateData(guid, &dummy, &size) == DD_OK && dummy == 0xBAADF00D);

	DDBLTFX	 bltfx;
	bltfx.dwSize = sizeof(DDBLTFX);
	bltfx.dwFillColor = RGB(0, 0, 0);
	if(Surface->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &bltfx) != DD_OK)
        printf("ERROR: Failed to draw to surface !");
    TEST(Surface->GetPrivateData(guid, &dummy, &size) == DDERR_EXPIRED);

    // TODO: Test for DDSPD_IUNKNOWNPOINTER (see http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/ddraw7/directdraw7/ddref_5qyf.asp)

    Surface->Release();
    return TRUE;
}
示例#13
0
ULONG STDMETHODCALLTYPE Release(LPDIRECTDRAWSURFACE7 surface)
{
    //logOutput << CurrentTimeString() << "Hooked Release()" << endl;

    ddrawSurfaceRelease.Unhook();
    /*
    if(surface == g_frontSurface)
    {
    logOutput << CurrentTimeString() << "Releasing primary surface 0x" << surface << endl;
    surface->AddRef();
    ULONG refCount = surface->Release();

    if(refCount == 1)
    {
    logOutput << CurrentTimeString() << "Freeing primary surface" << endl;
    g_frontSurface = NULL;
    bTargetAcquired = false;
    CleanUpDDraw();
    }
    }
    */

    ULONG refCount = surface->Release();
    ddrawSurfaceRelease.Rehook();

    if (surface == g_frontSurface)
    {
        if (refCount == 0)
        {
            logOutput << CurrentTimeString() << "Freeing primary surface" << endl;
            CleanUpDDraw();
        }
    }

    return refCount;
}
示例#14
0
int Game_Shutdown(void *parms = NULL, int num_parms = 0)
{
   // this is called after the game is exited and the main event
   // loop while is exited, do all you cleanup and shutdown here

   MessageBox(main_window_handle,
              "game shutdown...",
              "game shutdown...",
              MB_OK | MB_ICONEXCLAMATION );


   if (lpddsprimary)
   {
      lpddsprimary->Release();
      lpddsprimary = NULL;
   }
   
   // simply blow away the IDirectDraw4 interface
   if (lpdd)
   {
      lpdd->Release();
      lpdd = NULL;
   } // end if

   if (g_ddStepFailure != DDSF_OK)
   {
      ostrstream txtout;
      txtout << "Critical Error in DirectX initialization!"
             << "(";
             
      switch (g_ddStepFailure)
      {
         case DDSF_DDACCESS:
            {
               txtout <<"DDSF_DDACCESS";
            }
            break;
         case DDSF_DDPALETTECREATE:
            {
               txtout <<"DDSF_DDPALETTECREATE";
            }
            break;
         case DDSF_DDPALETTEATTACH:
            {
               txtout <<"DDSF_DDPALETTEATTACH";
            }
            break;
         case DDSF_SURFACECREATE:
            {
               txtout <<"DDSF_SURFACECREATE";
            }
            break;
         case DDSF_CLIPPERCREATE:
            {
               txtout <<"DDSF_CLIPPERCREATE";
            }
            break;
         case DDSF_SETDISPLAYMODE:
            {
               txtout <<"DDSF_SETDISPLAYMODE";
            }
            break;
         default:
            {
               txtout <<"DDSF_UNKNOWN";
            }
            break;
      }
             
      txtout << ")"
             << ends;
      MessageBox(main_window_handle,
                 txtout.str(),
                 "Critical Error in DirectX initialization, application is now ending!",
                 MB_OK | MB_ICONEXCLAMATION );
      txtout.freeze(0);                 
   }

   delete [] colors16;
   // return success or failure or your own return code here
   return(1);
   
} // end Game_Shutdown
示例#15
0
ALERROR CGImageCache::LoadBitmapImage (DWORD dwImageUNID, DWORD dwTransparencyUNID, DWORD dwDepthUNID, int *retiIndex)

//	LoadBitmapImage
//
//	Loads an image into the cache. This should only be called once
//	when the cache is initialized or when an image is first requested.

	{
	ALERROR error;
	HRESULT result;
	HBITMAP hBitmap;
	DDSURFACEDESC ddsd;
	BITMAP bm;
	LPDIRECTDRAWSURFACE7 pSurface;
	HDC hDC, hBitmapDC;
	HBITMAP hOldBitmap;
	HBITMAP hDepth;
	CGChannelStruct *pTrans = NULL;

	//	Load the bitmap from the image db

	if (error = m_pMediaDb->LoadBitmap(dwImageUNID, &hBitmap))
		return error;

	//	Get some info from bitmap

	GetObject(hBitmap, sizeof(bm), &bm);

	//	Create a surface of the proper size

	utlMemSet(&ddsd, sizeof(ddsd), 0);
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
	ddsd.dwWidth = bm.bmWidth;
	ddsd.dwHeight = bm.bmHeight;

	if (m_pScreen->GetDD()->CreateSurface(&ddsd, &pSurface, NULL) != DD_OK)
		{
		DeleteObject(hBitmap);
		return ERR_FAIL;
		}

	if (result = pSurface->GetDC(&hDC) != DD_OK)
		{
		pSurface->Release();
		DeleteObject(hBitmap);
		return ERR_FAIL;
		}

	//	Blt the bitmap onto the surface

	hBitmapDC = CreateCompatibleDC(NULL);
	hOldBitmap = (HBITMAP)SelectObject(hBitmapDC, hBitmap);

	BitBlt(hDC,
			0,
			0,
			bm.bmWidth,
			bm.bmHeight,
			hBitmapDC,
			0,
			0,
			SRCCOPY);

	SelectObject(hBitmapDC, hOldBitmap);
	DeleteDC(hBitmapDC);
	DeleteObject(hBitmap);

	pSurface->ReleaseDC(hDC);

	//	Load the transparency bitmap

	if (dwTransparencyUNID)
		{
		HBITMAP hTransparency;

		if (error = m_pMediaDb->LoadBitmap(dwTransparencyUNID, &hTransparency))
			{
			pSurface->Release();
			return error;
			}

		//	Convert this bitmap into an 8-bit map

		error = ConvertToChannel(hTransparency, &pTrans);
		DeleteObject(hTransparency);
		if (error)
			{
			pSurface->Release();
			return error;
			}
		}

	//	Load the depth bitmap

	if (dwDepthUNID)
		{
		if (error = m_pMediaDb->LoadBitmap(dwDepthUNID, &hDepth))
			{
			if (pTrans)
				MemFree(pTrans);
			pSurface->Release();
			return error;
			}
		}
	else
		hDepth = NULL;

	//	Add the surface to our cache

	if (error = AddImage(dwImageUNID, pSurface, pTrans, hDepth, retiIndex))
		{
		if (hDepth)
			DeleteObject(hDepth);
		if (pTrans)
			MemFree(pTrans);
		pSurface->Release();
		return ERR_FAIL;
		}

	return NOERROR;
	}
示例#16
0
HRESULT CTextureHolder::CopyBitmapToSurface(){

    // Get a DDraw object to create a temporary surface
    LPDIRECTDRAW7 pDD;
    m_pddsSurface->GetDDInterface( (VOID**)&pDD );

    // Get the bitmap structure (to extract width, height, and bpp)
    BITMAP bm;
    GetObject( m_hbmBitmap, sizeof(BITMAP), &bm );

    // Setup the new surface desc
    DDSURFACEDESC2 ddsd;
    ddsd.dwSize = sizeof(ddsd);
    m_pddsSurface->GetSurfaceDesc( &ddsd );
    ddsd.dwFlags          = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|
                            DDSD_TEXTURESTAGE;
    ddsd.ddsCaps.dwCaps   = DDSCAPS_TEXTURE|DDSCAPS_SYSTEMMEMORY;
    ddsd.ddsCaps.dwCaps2  = 0L;
    ddsd.dwWidth          = bm.bmWidth;
    ddsd.dwHeight         = bm.bmHeight;

    // Create a new surface for the texture
    LPDIRECTDRAWSURFACE7 pddsTempSurface;
    HRESULT hr;
    if( FAILED( hr = pDD->CreateSurface( &ddsd, &pddsTempSurface, NULL ) ) )
    {
        pDD->Release();
        return hr;
    }

    // Get a DC for the bitmap
    HDC hdcBitmap = CreateCompatibleDC( NULL );
    if( NULL == hdcBitmap )
    {
        pddsTempSurface->Release();
        pDD->Release();
        return hr; // bug? return E_FAIL?
    }
    SelectObject( hdcBitmap, m_hbmBitmap );

    // Handle palettized textures. Need to attach a palette
    if( ddsd.ddpfPixelFormat.dwRGBBitCount == 8 )
    {
        LPDIRECTDRAWPALETTE  pPalette;
        DWORD dwPaletteFlags = DDPCAPS_8BIT|DDPCAPS_ALLOW256;
        DWORD pe[256];
        WORD  wNumColors     = GetDIBColorTable( hdcBitmap, 0, 256, (RGBQUAD*)pe );

        // Create the color table
        for( WORD i=0; i<wNumColors; i++ )
        {
            pe[i] = RGB( GetBValue(pe[i]), GetGValue(pe[i]), GetRValue(pe[i]) );

            // Handle textures with transparent pixels
            if( m_dwFlags & (D3DTEXTR_TRANSPARENTWHITE|D3DTEXTR_TRANSPARENTBLACK) )
            {
                // Set alpha for opaque pixels
                if( m_dwFlags & D3DTEXTR_TRANSPARENTBLACK )
                {
                    if( pe[i] != 0x00000000 )
                        pe[i] |= 0xff000000;
                }
                else if( m_dwFlags & D3DTEXTR_TRANSPARENTWHITE )
                {
                    if( pe[i] != 0x00ffffff )
                        pe[i] |= 0xff000000;
                }
            }
        }
        // Add DDPCAPS_ALPHA flag for textures with transparent pixels
        if( m_dwFlags & (D3DTEXTR_TRANSPARENTWHITE|D3DTEXTR_TRANSPARENTBLACK) )
            dwPaletteFlags |= DDPCAPS_ALPHA;

        // Create & attach a palette
        pDD->CreatePalette( dwPaletteFlags, (PALETTEENTRY*)pe, &pPalette, NULL );
        pddsTempSurface->SetPalette( pPalette );
        m_pddsSurface->SetPalette( pPalette );
        SAFE_RELEASE( pPalette );
    }

    // Copy the bitmap image to the surface.
    HDC hdcSurface;
    if( SUCCEEDED( pddsTempSurface->GetDC( &hdcSurface ) ) )
    {
        BitBlt( hdcSurface, 0, 0, bm.bmWidth, bm.bmHeight, hdcBitmap, 0, 0,
                SRCCOPY );
        pddsTempSurface->ReleaseDC( hdcSurface );
    }
    DeleteDC( hdcBitmap );

    // Copy the temp surface to the real texture surface
    m_pddsSurface->Blt( NULL, pddsTempSurface, NULL, DDBLT_WAIT, NULL );

    // Done with the temp surface
    pddsTempSurface->Release();

    // For textures with real alpha (not palettized), set transparent bits
    if( ddsd.ddpfPixelFormat.dwRGBAlphaBitMask )
    {
        if( m_dwFlags & (D3DTEXTR_TRANSPARENTWHITE|D3DTEXTR_TRANSPARENTBLACK) )
        {
            // Lock the texture surface
            DDSURFACEDESC2 ddsd;
            ddsd.dwSize = sizeof(ddsd);
            while( m_pddsSurface->Lock( NULL, &ddsd, 0, NULL ) ==
                   DDERR_WASSTILLDRAWING );

            DWORD dwAlphaMask = ddsd.ddpfPixelFormat.dwRGBAlphaBitMask;
            DWORD dwRGBMask   = ( ddsd.ddpfPixelFormat.dwRBitMask |
                                  ddsd.ddpfPixelFormat.dwGBitMask |
                                  ddsd.ddpfPixelFormat.dwBBitMask );
            DWORD dwColorkey  = 0x00000000; // Colorkey on black
            if( m_dwFlags & D3DTEXTR_TRANSPARENTWHITE )
                dwColorkey = dwRGBMask;     // Colorkey on white

            // Add an opaque alpha value to each non-colorkeyed pixel
            for( DWORD y=0; y<ddsd.dwHeight; y++ )
            {
                WORD*  p16 =  (WORD*)((BYTE*)ddsd.lpSurface + y*ddsd.lPitch);
                DWORD* p32 = (DWORD*)((BYTE*)ddsd.lpSurface + y*ddsd.lPitch);

                for( DWORD x=0; x<ddsd.dwWidth; x++ )
                {
                    if( ddsd.ddpfPixelFormat.dwRGBBitCount == 16 )
                    {
                        if( ( *p16 &= dwRGBMask ) != dwColorkey )
                            *p16 |= dwAlphaMask;
                        p16++;
                    }
                    if( ddsd.ddpfPixelFormat.dwRGBBitCount == 32 )
                    {
                        if( ( *p32 &= dwRGBMask ) != dwColorkey )
                            *p32 |= dwAlphaMask;
                        p32++;
                    }
                }
            }
            m_pddsSurface->Unlock( NULL );
        }
				else if( m_bHasMyAlpha ){
					
					DDSURFACEDESC2 ddsd;
					ddsd.dwSize = sizeof(ddsd);
					while( m_pddsSurface->Lock( NULL, &ddsd, 0, NULL ) ==
						DDERR_WASSTILLDRAWING );
					
					DWORD dwRGBMask   = ( ddsd.ddpfPixelFormat.dwRBitMask |
						ddsd.ddpfPixelFormat.dwGBitMask |
						ddsd.ddpfPixelFormat.dwBBitMask );

					DWORD rMask = ddsd.ddpfPixelFormat.dwRBitMask;
					DWORD gMask = ddsd.ddpfPixelFormat.dwGBitMask;
					DWORD bMask = ddsd.ddpfPixelFormat.dwBBitMask;
					DWORD aMask = ddsd.ddpfPixelFormat.dwRGBAlphaBitMask;

					DWORD rShift = GetShift( rMask );
					DWORD gShift = GetShift( gMask );
					DWORD bShift = GetShift( bMask );
					DWORD aShift = GetShift( aMask );

					DWORD maxRVal = rMask >> rShift;
					DWORD maxGVal = gMask >> gShift;
					DWORD maxBVal = bMask >> bShift;
					DWORD maxAVal = aMask >> aShift;

					DWORD rVal, gVal, bVal, aVal;
					FLOAT min, max;
					
					// Add an opaque alpha value to each non-colorkeyed pixel
					for( DWORD y=0; y<ddsd.dwHeight; y++ ){

						WORD*  p16 =  (WORD*)((BYTE*)ddsd.lpSurface + y*ddsd.lPitch);
						DWORD* p32 = (DWORD*)((BYTE*)ddsd.lpSurface + y*ddsd.lPitch);
						
						for( DWORD x=0; x<ddsd.dwWidth; x++ ){

							if( ddsd.ddpfPixelFormat.dwRGBBitCount == 32 ){
								
								*p32 &= dwRGBMask; // set alpha to zero

								if( *p32 == 0 ){} // black is 100% transparent, so leave alpha at 0%

								else if( *p32 == dwRGBMask ){ // white is opaque, so set alpha to 100%
									*p32 |= aMask;
								}

								else{ // set alpha to equal intensity of brightest hue

									rVal = ( *p32 & rMask ) >> rShift;
									gVal = ( *p32 & gMask ) >> gShift;
									bVal = ( *p32 & bMask ) >> bShift;

									max = max( (FLOAT)rVal / maxRVal, max( (FLOAT)gVal / maxGVal, (FLOAT)bVal / maxBVal ) );
//									min = min( (FLOAT)rVal / maxRVal, min( (FLOAT)gVal / maxGVal, (FLOAT)bVal / maxBVal ) );
									
									aVal = max * 255;

									//if( rVal == gVal && gVal == bVal ){ // white fading to black
									//	*p32 = dwRGBMask; // set color to white
									//}

									// maximize luminosity and saturation
									rVal /= max;
									gVal /= max;
									bVal /= max;

									*p32 = ( aVal << aShift ) | ( rVal << rShift ) | ( gVal << gShift ) | ( bVal << bShift );
								}
								p32++;
							}
							else if( ddsd.ddpfPixelFormat.dwRGBBitCount == 16 ){
								
								*p16 &= dwRGBMask; // set alpha to zero

								if( *p16 == 0 ){} // black is 100% transparent, so leave alpha at 0%

								else if( *p16 == dwRGBMask ){ // white is opaque, so set alpha to 100%
									*p16 |= aMask;
								}

								else{ // set alpha to equal intensity of brightest hue

									rVal = ( *p16 & rMask ) >> rShift;
									gVal = ( *p16 & gMask ) >> gShift;
									bVal = ( *p16 & bMask ) >> bShift;

									aVal = STATSTEXTURE_ALPHA * max( (FLOAT)rVal / maxRVal, max( (FLOAT)gVal / maxGVal, (FLOAT)bVal / maxBVal ) );

									if( aVal < STATSTEXTURE_ALPHA ){ // semi-tranparent white
										*p16 = dwRGBMask;
									}

									*p16 |= aVal << aShift;
								}
								p16++;
							}

						}
					}
示例#17
0
// 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;
}
示例#18
0
void DDAccurateInit(int resize)
{
        DDPIXELFORMAT DDpf;
        DDSURFACEDESC2 ddsd;

        float OrigW, OrigH, ScaleW, ScaleH;

        OrigW=Form1->ClientWidth;
        OrigH=Form1->ClientHeight;
        if (Form1->StatusBar1->Visible) OrigH -= Form1->StatusBar1->Height;

        if (Form1->BaseWidth==0) Form1->BaseWidth= NoWinR-NoWinL;
        if (Form1->BaseHeight==0) Form1->BaseHeight= NoWinT-NoWinB;

        ScaleW = OrigW / Form1->BaseWidth;
        ScaleH = OrigH / Form1->BaseHeight;

        RasterX=0;
        RasterY=random(256);

        //fill the DDpf structure and get the BytesPerPixel
        ZeroMemory (&DDpf, sizeof(DDpf));
        DDpf.dwSize = sizeof(DDpf);
        m_pddsFrontBuffer->GetPixelFormat(&DDpf);
        BPP = DDpf.dwRGBBitCount/8;

        Paletteised = (BPP==1) ? true:false;
        Scale= tv.AdvancedEffects?2:1;

        //ScanLen=460*BPP;
        ScanLen=(2+machine.tperscanline*2)*BPP;

        switch(zx81.bordersize)
        {
        case BORDERNONE:
                WinL=BlWinL; WinR=BlWinR; WinT=BlWinT; WinB=BlWinB;
                if (zx81.NTSC) { WinT-=24; WinB-=24; }
                break;
        case BORDERSMALL:
                WinL=SmWinL; WinR=SmWinR; WinT=SmWinT; WinB=SmWinB;
                if (zx81.NTSC) { WinT-=24; WinB-=24; }
                break;
        case BORDERNORMAL:
                WinL=NoWinL; WinR=NoWinR; WinT=NoWinT; WinB=NoWinB;
                if (zx81.NTSC) { WinT-=24; WinB-=24; }
                break;
        case BORDERLARGE:
                WinL=LaWinL; WinR=LaWinR; WinT=LaWinT; WinB=LaWinB;
                if (zx81.NTSC) { WinB-=24; }
                break;
        case BORDERFULL:
                WinL=FuWinL; WinR=FuWinR; WinT=FuWinT; WinB=FuWinB;
                if (zx81.NTSC) WinB-=51;
                break;
        }

        ZeroMemory( &ddsd, sizeof( ddsd ) );
        ddsd.dwSize = sizeof( ddsd );
        // Create the backbuffer surface
        ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
        ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;

        if (tv.AdvancedEffects)
        {
                WinL*=2; WinR*=2; WinT*=2; WinB*=2; ScanLen*=2;
                TVW=ddsd.dwWidth = 1024;
                TVH=ddsd.dwHeight = 768;
                HSYNC_TOLLERANCE=HTOL*2; HSYNC_MINLEN=10;
                VSYNC_TOLLERANCE=VTOL*2; VSYNC_MINLEN=350;
        }
        else
        {
                TVW=ddsd.dwWidth = 520;
                TVH=ddsd.dwHeight = 380;
                HSYNC_TOLLERANCE=HTOL; HSYNC_MINLEN=10;
                VSYNC_TOLLERANCE=VTOL; VSYNC_MINLEN=350;
        }

        m_pddsFrame->Release(); m_pddsFrame = NULL;
        m_pDD->CreateSurface(&ddsd, &m_pddsFrame, NULL);

        if (zx81.NTSC) VSYNC_TOLLERANCE-=60;

        if ((resize) && (!Form1->FullScreen))
        {
                Form1->BaseWidth=WinR-WinL;
                Form1->BaseHeight=WinB-WinT;

                OrigW = Form1->BaseWidth * ScaleW;
                OrigH = Form1->BaseHeight * ScaleH;
                if (Form1->StatusBar1->Visible) OrigH += Form1->StatusBar1->Height;

                Form1->ClientWidth = OrigW;
                Form1->ClientHeight = OrigH;
        }

        DDFrame=m_pddsFrame;

        ZeroMemory(&DDFrameSurface, sizeof(DDFrameSurface));
        DDFrameSurface.dwSize = sizeof(DDFrameSurface);
        DDFrame->Lock(NULL, &DDFrameSurface, DDLOCK_WAIT |  DDLOCK_NOSYSLOCK, NULL);
        dest=buffer= (BYTE*)DDFrameSurface.lpSurface;
        TVP=DDFrameSurface.lPitch;

        RecalcPalette();
        RecalcWinSize();
}
示例#19
0
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;
}