Beispiel #1
0
RageSurface* RageDisplay_D3D::CreateScreenshot()
{
#if defined(XBOX)
	return NULL;
#else
	RageSurface * result = NULL;

	// Get the back buffer.
	IDirect3DSurface9* pSurface;
	if( SUCCEEDED( g_pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pSurface ) ) )
	{
		// Get the back buffer description.
		D3DSURFACE_DESC desc;
		pSurface->GetDesc( &desc );

		// Copy the back buffer into a surface of a type we support.
		IDirect3DSurface9* pCopy;
		if( SUCCEEDED( g_pd3dDevice->CreateOffscreenPlainSurface( desc.Width, desc.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pCopy, NULL ) ) )
		{
			if( SUCCEEDED( D3DXLoadSurfaceFromSurface( pCopy, NULL, NULL, pSurface, NULL, NULL, D3DX_FILTER_NONE, 0) ) )
			{
				// Update desc from the copy.
				pCopy->GetDesc( &desc );

				D3DLOCKED_RECT lr;

				{
					RECT rect;
					rect.left = 0;
					rect.top = 0;
					rect.right = desc.Width;
					rect.bottom = desc.Height;

					pCopy->LockRect( &lr, &rect, D3DLOCK_READONLY );
				}

				RageSurface *surface = CreateSurfaceFromPixfmt( FMT_RGBA8, lr.pBits, desc.Width, desc.Height, lr.Pitch);
				ASSERT( surface != NULL );

				// We need to make a copy, since lr.pBits will go away when we call UnlockRect().
				result = 
					CreateSurface( surface->w, surface->h,
						surface->format->BitsPerPixel,
						surface->format->Rmask, surface->format->Gmask,
						surface->format->Bmask, surface->format->Amask );
				RageSurfaceUtils::CopySurface( surface, result );
				delete surface;

				pCopy->UnlockRect();
			}

			pCopy->Release();
		}

		pSurface->Release();
	}

	return result;
#endif
}
LPDIRECT3DSURFACE9 LoadSurface(char *filename, D3DCOLOR transcolor)
{
    LPDIRECT3DSURFACE9 image = NULL;
    D3DXIMAGE_INFO info;
    HRESULT result;
    
    result = D3DXGetImageInfoFromFile(filename, &info);

    result = d3ddev->CreateOffscreenPlainSurface(
        info.Width,
        info.Height,
        D3DFMT_X8R8G8B8,
        D3DPOOL_DEFAULT,
        &image,
        NULL);

    result = D3DXLoadSurfaceFromFile(
        image,
        NULL,
        NULL,
        filename,
        NULL,
        D3DX_DEFAULT,
        transcolor,
        NULL);

    return image;
}
/*This Function creates a surface object from a bitmap file. Find the dimensions of the bitmap,
  create the plain surface, then load the bitmap into the new surface object*/
LPDIRECT3DSURFACE9 LoadSurface(string filename) {
	LPDIRECT3DSURFACE9 image = NULL;

	//Get width and Height of bitmap 
	D3DXIMAGE_INFO info;
	HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info);
	if (result != D3D_OK) return NULL;

	//creates a new surface object
	result = d3ddev->CreateOffscreenPlainSurface(
		info.Width,
		info.Height,
		D3DFMT_X8R8G8B8,
		D3DPOOL_DEFAULT,
		&image,
		NULL);
	if (result != D3D_OK) return NULL;

	//load the surface from the file into the newley created surface object
	result = D3DXLoadSurfaceFromFile(
		image,
		NULL,
		NULL,
		filename.c_str(),
		NULL,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0, 0, 0),
		NULL);
	if (result != D3D_OK) return NULL;

	return image;
}
HRESULT InitD3D( HWND hWnd ) 
{ 
	HRESULT hr ;

	// Create the D3D object, which is needed to create the D3DDevice. 
	if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) 
	{ 
		MessageBox(NULL, "Create D3D9 object failed!", "Error", 0) ; 
		return E_FAIL; 
	} 

	D3DDISPLAYMODE	ddm;
	hr = g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&ddm) ;
	if(FAILED(hr))
	{
		MessageBox(NULL, "Unable to Get Adapter Display Mode", "Error", 0) ; 
		return E_FAIL;
	}

	ZeroMemory( &d3dpp, sizeof(d3dpp) ); 

	d3dpp.Windowed = TRUE;  
	d3dpp.BackBufferCount = 1 ;
	d3dpp.BackBufferWidth = ddm.Width ;
	d3dpp.BackBufferHeight = ddm.Height ;
	d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP; 
	d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; 

	// Create device 
	if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
		D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
		&d3dpp, &g_pd3dDevice ) ) ) 
	{ 
		MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ; 
		return E_FAIL; 
	} 

	// Create Semaphore variable
	g_hMutex = CreateMutex(NULL,FALSE,NULL);
	g_hFullSemaphore = CreateSemaphore(NULL, g_PoolSize - 1, g_PoolSize - 1, NULL);
	g_hEmptySemaphore = CreateSemaphore(NULL, 0, g_PoolSize - 1, NULL);

	// Get back buffer
	g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &g_pBackBuffer) ;

	// Create surface
	for (int i = 0; i < g_PoolSize; ++i)
	{
		g_SurfPool[i] = NULL ;
		if (FAILED(hr = g_pd3dDevice->CreateOffscreenPlainSurface(d3dpp.BackBufferWidth, 
			d3dpp.BackBufferHeight, d3dpp.BackBufferFormat, D3DPOOL_SYSTEMMEM, &g_SurfPool[i], NULL)))
		{
			return hr;
		}
	}

	return S_OK; 
} 
Beispiel #5
0
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void InitGraphics(int width, int height )
{
	InitWindow(width, height);

	D3DPRESENT_PARAMETERS d3dp;
	ZeroMemory(&d3dp, sizeof(d3dp));
	d3dp.BackBufferWidth = width;
	d3dp.BackBufferHeight = height;
	d3dp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dp.BackBufferCount = 1;      
	d3dp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dp.Windowed = TRUE;
	d3dp.hDeviceWindow = (HWND)GetHandle();
	d3dp.EnableAutoDepthStencil = TRUE;
    d3dp.AutoDepthStencilFormat = D3DFMT_D16;

#if __PerformanceCheckMode
	d3dp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
#endif

	g_d3d = Direct3DCreate9(D3D_SDK_VERSION);
	
	g_d3d->CreateDevice( 
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		(HWND) GetHandle(),
		D3DCREATE_HARDWARE_VERTEXPROCESSING,
		&d3dp,
		&g_d3d_device );

	
	{// 市松模様の背景画像を作る
		g_d3d_device->CreateOffscreenPlainSurface( width, height, 
			D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &g_d3d_clearing_image, NULL );
		D3DLOCKED_RECT lockedRect;
		g_d3d_clearing_image->LockRect( &lockedRect, NULL, 0 );
		CreateCheckeredPattern( width, height, (uint32_t*)lockedRect.pBits );
		g_d3d_clearing_image->UnlockRect();
	}

	g_renderer = ::EffekseerRendererDX9::Renderer::Create( g_d3d_device, 2000 );
	g_renderer->SetProjectionMatrix( ::Effekseer::Matrix44().PerspectiveFovRH( 90.0f / 180.0f * 3.14f, (float)width / (float)height, 1.0f, 50.0f ) );
	g_renderer->SetDistortingCallback( new DistortingCallback( 
		(EffekseerRendererDX9::Renderer*)g_renderer, g_d3d_device, width, height ) );
	
	g_manager->SetSpriteRenderer( g_renderer->CreateSpriteRenderer() );
	g_manager->SetRibbonRenderer( g_renderer->CreateRibbonRenderer() );
	g_manager->SetRingRenderer( g_renderer->CreateRingRenderer() );
	g_manager->SetModelRenderer( g_renderer->CreateModelRenderer() );
	g_manager->SetTrackRenderer( g_renderer->CreateTrackRenderer() );

	g_manager->SetCoordinateSystem( ::Effekseer::CoordinateSystem::RH );

	g_manager->SetTextureLoader( g_renderer->CreateTextureLoader() );
	g_manager->SetModelLoader( g_renderer->CreateModelLoader() );
}
Beispiel #6
0
bool Surface::loadSurface(LPDIRECT3DDEVICE9 device, std::string filename)
{
	imageScale = 100; //set our image scale to 100%
	//record the height and width

	HRESULT hResult;
	// Get the width and height of the image
	D3DXIMAGE_INFO imageInfo;
	hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
	if FAILED (hResult){
		return false;
	}

	height = imageInfo.Height;
	width = imageInfo.Width;

	//create the Off Screen Surface
	hResult = device->CreateOffscreenPlainSurface(width, //surface width
		height, //surface height
		D3DFMT_A8R8G8B8, //surface format, D3DFMT_A8R8G8B8 is a 32 bit format with 8 alpha bits
		D3DPOOL_DEFAULT, //create it in the default memory pool
		&surface, //the pointer to our surface 
		NULL
		);

	if (FAILED(hResult))
		return false;

	//load the surface from the a file
	hResult = D3DXLoadSurfaceFromFile(surface, //the surface we just created
		NULL, //palette entry, NULL for non 256 color formats
		NULL, //dest rect, NULL for the whole surface
		filename.c_str(), // the file we wish to load
		NULL, // Source Rect, NULL for the whole file
		D3DX_DEFAULT, //Filter 
		0, // Color key, color that should be used as transparent.
		NULL // pointer to a D3DXIMAGE_INFO structure, for holding info about the image.
		);

	if (FAILED(hResult))
		return false;

	//set rects
	destRect.left = 0;
	destRect.top = 0;
	destRect.bottom = destRect.top + height;
	destRect.right = destRect.left + width;

	srcRect.left = 0;
	srcRect.top = 0;
	srcRect.bottom = destRect.top + height;
	srcRect.right = destRect.left + width;

	return true;
}
HRESULT HookIDirect3DDevice9::CreateOffscreenPlainSurface(LPVOID _this,
														  UINT Width,
														  UINT Height,
														  D3DFORMAT Format,
														  D3DPOOL Pool,
														  IDirect3DSurface9** ppSurface,
														  HANDLE* pSharedHandle)
{
	LOG_API();
	return pD3Dev->CreateOffscreenPlainSurface(Width, Height, Format, Pool, ppSurface, pSharedHandle);
}
LPDIRECT3DSURFACE9 LoadSurface(char* fileName, D3DCOLOR transColor) {
    LPDIRECT3DSURFACE9 image = NULL;
    D3DXIMAGE_INFO info;
    HRESULT hr;

    hr = D3DXGetImageInfoFromFile(fileName, &info);
    if (hr != D3D_OK)
        return NULL;

    hr = d3dDev->CreateOffscreenPlainSurface(info.Width, info.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &image, NULL);
    if (hr != D3D_OK)
        return NULL;

    hr = D3DXLoadSurfaceFromFile(image, NULL, NULL, fileName, NULL, D3DX_DEFAULT, transColor, NULL);
    if (hr != D3D_OK)
        return NULL;

    return image;
}
LPDIRECT3DSURFACE9 LoadSurface(string filename)
{
    LPDIRECT3DSURFACE9 image = NULL;
    
    //get width and height from bitmap file
    D3DXIMAGE_INFO info;
    HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info);
    if (result != D3D_OK)
	{
        return NULL;
	}

    //create surface
    result = d3ddev->CreateOffscreenPlainSurface(
        info.Width,
        info.Height,
        D3DFMT_X8R8G8B8,
        D3DPOOL_DEFAULT,
        &image,
        NULL);  

    if (result != D3D_OK) return NULL;

    //load surface from file into newly created surface
    result = D3DXLoadSurfaceFromFile(
        image, 
        NULL,
        NULL,  
        filename.c_str(),
        NULL,    
        D3DX_DEFAULT,
        D3DCOLOR_XRGB(0,0,0),
        NULL);

    //make sure file was loaded okay
    if (result != D3D_OK)
	{
		return NULL;
	}

    return image;
}
Beispiel #10
0
bool Background::load(LPDIRECT3DDEVICE9 Device, std::wstring filename)
{
	imageScale = 100;

	HRESULT hr;
	D3DXIMAGE_INFO imageInfo;
	hr = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
	if(FAILED(hr))
	{
		return false;
	}

	height = imageInfo.Height;
	width = imageInfo.Width;

	hr = Device->CreateOffscreenPlainSurface(width, height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &background, NULL);
	if(FAILED(hr))
	{
		return false;
	}

	//load from file
	hr = D3DXLoadSurfaceFromFile(background, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL);
	if(FAILED(hr))
	{
		return false;
	}

	//Set Rects
	scrRect.left = 0;
	scrRect.top = 0;
	scrRect.bottom = 0;
	scrRect.top = 0;

	destRect.left = 0;
	destRect.top = 0;
	destRect.bottom = 0;
	destRect.right = 0;

	return true;
}
Beispiel #11
0
String screengrab(String filename)
{
	if(filename=="" || filename == " () ")
	{
		filename.sprintf("%s_%5.5d_.png",(const char*)screengrabprefix,screengrabcount);		
		screengrabcount++;
	}
	if(!IsOneOf('.',filename)) 
		filename << ".png";
	HRESULT hr;
	// get display dimensions
	// this will be the dimensions of the front buffer
	D3DDISPLAYMODE mode;
	if (FAILED(hr=g_pd3dDevice->GetDisplayMode(0,&mode)))
	{
		return "fail getdisplaymode";
	}
	// create the image surface to store the front buffer image
	// note that call to GetFrontBuffer will always convert format to A8R8G8B8
	static LPDIRECT3DSURFACE9 surf=NULL;
	if (!surf && FAILED(hr=g_pd3dDevice->CreateOffscreenPlainSurface(mode.Width,mode.Height,
		D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&surf,NULL)))
	{
		return "fail createimagesurface";
	}
 	// Next, this surface is passed to the GetFrontBuffer() method of the device, which will copy the entire screen into our image buffer:
	// read the front buffer into the image surface
	if (FAILED(hr=g_pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&surf))) 
	{
		surf->Release();
		return "fail getfrontbuffer";
	}
	//Finally, we call D3DXSaveSurfaceToFile() to create the BMP file, and release the temporary image surface:
	// write the entire surface to the requested file
	hr=D3DXSaveSurfaceToFile(filename,D3DXIFF_PNG,surf,NULL,NULL);
	// release the image surface
	// surf->Release();
	// return status of save operation to caller
	return (hr==D3D_OK)? filename + " exported!" : "something failed";
}
Beispiel #12
0
LPDIRECT3DSURFACE9 CreateSurfaceFromFile(LPDIRECT3DDEVICE9 d3ddv, LPWSTR FilePath)
{
	D3DXIMAGE_INFO info; 

	HRESULT result = D3DXGetImageInfoFromFile(FilePath,&info);
	if (result!=D3D_OK)
	{
		trace(L"[ERROR] Failed to get image info '%s'",FilePath);
		return NULL;
	}

	LPDIRECT3DSURFACE9 surface;

	d3ddv->CreateOffscreenPlainSurface(
			info.Width,				// width
			info.Height,			// height
			D3DFMT_X8R8G8B8,		// format
			D3DPOOL_DEFAULT,		
			&surface,
			NULL);

	result = D3DXLoadSurfaceFromFile(
			surface, 		// surface
			NULL,			// destination palette	
			NULL,			// destination rectangle 
			FilePath,			
			NULL,			// source rectangle
			D3DX_DEFAULT, 	// filter image
			D3DCOLOR_XRGB(0,0,0),				// transparency (0 = none)
			NULL);			// reserved

	if (result!=D3D_OK)
	{
		trace(L"[ERROR] D3DXLoadSurfaceFromFile() failed");
		return NULL;
	}

	return surface;
}
Beispiel #13
0
LPDIRECT3DSURFACE9 LoadSurface(string filename)
{
    LPDIRECT3DSURFACE9 image = NULL;
    
    //get width and height from bitmap file
    D3DXIMAGE_INFO info;
    HRESULT result = D3DXGetImageInfoFromFile(filename.c_str(), &info);
    if (result != D3D_OK)
        return NULL;

    //create surface
    result = d3ddev->CreateOffscreenPlainSurface(
        info.Width,         //width of the surface
        info.Height,        //height of the surface
        D3DFMT_X8R8G8B8,    //surface format
        D3DPOOL_DEFAULT,    //memory pool to use
        &image,             //pointer to the surface
        NULL);              //reserved (always NULL)

    if (result != D3D_OK) return NULL;

    //load surface from file into newly created surface
    result = D3DXLoadSurfaceFromFile(
        image,                  //destination surface
        NULL,                   //destination palette
        NULL,                   //destination rectangle
        filename.c_str(),       //source filename
        NULL,                   //source rectangle
        D3DX_DEFAULT,           //controls how image is filtered
        D3DCOLOR_XRGB(0,0,0),   //for transparency (0 for none)
        NULL);                  //source image info (usually NULL)

    //make sure file was loaded okay
    if (result != D3D_OK) return NULL;

    return image;
}
Beispiel #14
0
//-----------------------------------------------------------------------------
// Name: D3DUtil_SetDeviceCursor
// Desc: Gives the D3D device a cursor with image and hotspot from hCursor.
//-----------------------------------------------------------------------------
HRESULT D3DUtil_SetDeviceCursor( LPDIRECT3DDEVICE9 pd3dDevice, HCURSOR hCursor,
                                 BOOL bAddWatermark )
{
    HRESULT hr = E_FAIL;
    ICONINFO iconinfo;
    BOOL bBWCursor;
    LPDIRECT3DSURFACE9 pCursorSurface = NULL;
    HDC hdcColor = NULL;
    HDC hdcMask = NULL;
    HDC hdcScreen = NULL;
    BITMAP bm;
    DWORD dwWidth;
    DWORD dwHeightSrc;
    DWORD dwHeightDest;
    COLORREF crColor;
    COLORREF crMask;
    UINT x;
    UINT y;
    BITMAPINFO bmi;
    COLORREF* pcrArrayColor = NULL;
    COLORREF* pcrArrayMask = NULL;
    DWORD* pBitmap;
    HGDIOBJ hgdiobjOld;

    ZeroMemory( &iconinfo, sizeof(iconinfo) );
    if( !GetIconInfo( hCursor, &iconinfo ) )
        goto End;

    if (0 == GetObject((HGDIOBJ)iconinfo.hbmMask, sizeof(BITMAP), (LPVOID)&bm))
        goto End;
    dwWidth = bm.bmWidth;
    dwHeightSrc = bm.bmHeight;

    if( iconinfo.hbmColor == NULL )
    {
        bBWCursor = TRUE;
        dwHeightDest = dwHeightSrc / 2;
    }
    else 
    {
        bBWCursor = FALSE;
        dwHeightDest = dwHeightSrc;
    }

    // Create a surface for the fullscreen cursor
    if( FAILED( hr = pd3dDevice->CreateOffscreenPlainSurface( dwWidth, dwHeightDest, 
        D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pCursorSurface, 0 ) ) )
    {
        goto End;
    }

    pcrArrayMask = new DWORD[dwWidth * dwHeightSrc];

    ZeroMemory(&bmi, sizeof(bmi));
    bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
    bmi.bmiHeader.biWidth = dwWidth;
    bmi.bmiHeader.biHeight = dwHeightSrc;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;

    hdcScreen = GetDC( NULL );
    hdcMask = CreateCompatibleDC( hdcScreen );
    if( hdcMask == NULL )
    {
        hr = E_FAIL;
        goto End;
    }
    hgdiobjOld = SelectObject(hdcMask, iconinfo.hbmMask);
    GetDIBits(hdcMask, iconinfo.hbmMask, 0, dwHeightSrc, 
        pcrArrayMask, &bmi, DIB_RGB_COLORS);
    SelectObject(hdcMask, hgdiobjOld);

    if (!bBWCursor)
    {
        pcrArrayColor = new DWORD[dwWidth * dwHeightDest];
        hdcColor = CreateCompatibleDC( hdcScreen );
        if( hdcColor == NULL )
        {
            hr = E_FAIL;
            goto End;
        }
        SelectObject(hdcColor, iconinfo.hbmColor);
        GetDIBits(hdcColor, iconinfo.hbmColor, 0, dwHeightDest, 
            pcrArrayColor, &bmi, DIB_RGB_COLORS);
    }

    // Transfer cursor image into the surface
    D3DLOCKED_RECT lr;
    pCursorSurface->LockRect( &lr, NULL, 0 );
    pBitmap = (DWORD*)lr.pBits;
    for( y = 0; y < dwHeightDest; y++ )
    {
        for( x = 0; x < dwWidth; x++ )
        {
            if (bBWCursor)
            {
                crColor = pcrArrayMask[dwWidth*(dwHeightDest-1-y) + x];
                crMask = pcrArrayMask[dwWidth*(dwHeightSrc-1-y) + x];
            }
            else
            {
                crColor = pcrArrayColor[dwWidth*(dwHeightDest-1-y) + x];
                crMask = pcrArrayMask[dwWidth*(dwHeightDest-1-y) + x];
            }
            if (crMask == 0)
                pBitmap[dwWidth*y + x] = 0xff000000 | crColor;
            else
                pBitmap[dwWidth*y + x] = 0x00000000;

            // It may be helpful to make the D3D cursor look slightly 
            // different from the Windows cursor so you can distinguish 
            // between the two when developing/testing code.  When
            // bAddWatermark is TRUE, the following code adds some
            // small grey "D3D" characters to the upper-left corner of
            // the D3D cursor image.
            if( bAddWatermark && x < 12 && y < 5 )
            {
                // 11.. 11.. 11.. .... CCC0
                // 1.1. ..1. 1.1. .... A2A0
                // 1.1. .1.. 1.1. .... A4A0
                // 1.1. ..1. 1.1. .... A2A0
                // 11.. 11.. 11.. .... CCC0

                const WORD wMask[5] = { 0xccc0, 0xa2a0, 0xa4a0, 0xa2a0, 0xccc0 };
                if( wMask[y] & (1 << (15 - x)) )
                {
                    pBitmap[dwWidth*y + x] |= 0xff808080;
                }
            }
        }
    }
    pCursorSurface->UnlockRect();

    // Set the device cursor
    if( FAILED( hr = pd3dDevice->SetCursorProperties( iconinfo.xHotspot, 
        iconinfo.yHotspot, pCursorSurface ) ) )
    {
        goto End;
    }

    hr = S_OK;

End:
    if( iconinfo.hbmMask != NULL )
        DeleteObject( iconinfo.hbmMask );
    if( iconinfo.hbmColor != NULL )
        DeleteObject( iconinfo.hbmColor );
    if( hdcScreen != NULL )
        ReleaseDC( NULL, hdcScreen );
    if( hdcColor != NULL )
        DeleteDC( hdcColor );
    if( hdcMask != NULL )
        DeleteDC( hdcMask );
    SAFE_DELETE_ARRAY( pcrArrayColor );
    SAFE_DELETE_ARRAY( pcrArrayMask );
    SAFE_RELEASE( pCursorSurface );
    return hr;
}
Beispiel #15
0
/*
 * Game initialization function
 */
bool Game_Init(HWND window)
{
    //initialize Direct3D
    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    if (!d3d)
    {
        MessageBox(window, "Error initializing Direct3D", "Error", MB_OK);
        return false;
    }

    //set Direct3D presentation parameters
    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferWidth = SCREENW;
    d3dpp.BackBufferHeight = SCREENH;
    d3dpp.hDeviceWindow = window;

    //create Direct3D device
    d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

    if (!d3ddev)
    {
        MessageBox(window, "Error creating Direct3D device", "Error", MB_OK);
        return 0;
    }

    //clear the backbuffer to black
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
    
    //create surface
    HRESULT result = d3ddev->CreateOffscreenPlainSurface(
        SCREENW,            //width of the surface
        SCREENH,            //height of the surface
        D3DFMT_X8R8G8B8,    //surface format
        D3DPOOL_DEFAULT,    //memory pool to use
        &surface,           //pointer to the surface
        NULL);              //reserved (always NULL)

    if (!SUCCEEDED(result)) return false;

    //load surface from file into newly created surface
    result = D3DXLoadSurfaceFromFile(
        surface,            //destination surface
        NULL,               //destination palette
        NULL,               //destination rectangle
        "legotron.bmp",     //source filename
        NULL,               //source rectangle
        D3DX_DEFAULT,       //controls how image is filtered
        0,                  //for transparency 
        NULL);              //source image info
	
    //make sure file was loaded okay
    if (!SUCCEEDED(result)) return false;

    return true;
}
void* CDX9TextureObject::Read( int level, bool bForceUpdateRead )
{
	ReadyData();

	//level currently ignored, read cannot read mipmaps
	if( m_Texture )
	{
		LPDIRECT3DDEVICE9 pDevice;
		if( !m_Renderer )
		{
			return NULL;
		}
		pDevice = (LPDIRECT3DDEVICE9)m_Renderer->GetAPIDevice();
		if( !pDevice )
		{
			return NULL;
		}

		int iTextureSize = (GetColorDepth() / 8) * GetWidth() * GetHeight();
		// delete the old buffer if our current one is the wrong size
		if (m_pLocalBuffer && (iTextureSize != m_iLocalBufferSize))
		{
			delete[] m_pLocalBuffer;
			m_pLocalBuffer = NULL;
			m_iLocalBufferSize = 0;
		}
		// allocate a new buffer if we don't have one
		if (!m_pLocalBuffer)
		{
			m_pLocalBuffer = new unsigned char[iTextureSize];
			m_iLocalBufferSize = iTextureSize;
		}
		else
		{
			// return current buffer if we aren't forcing a read
			if (!bForceUpdateRead)
				return m_pLocalBuffer;
		}
		//check what kind of a surface it is, we have to get the surface if it's a rendertarget!
		if( !m_bRenderTarget )
		{
			if( m_Compressed )
			{
				LPDIRECT3DSURFACE9 Src = NULL;
				m_Texture->GetSurfaceLevel( 0, &Src );				
				//convert format to argb first
				LPDIRECT3DSURFACE9 tempSurf = NULL;
				HRESULT hr = pDevice->CreateOffscreenPlainSurface( GetWidth(), GetHeight(),
									D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tempSurf, NULL );
				if( hr != D3D_OK ||
					tempSurf == NULL ||
					Src == NULL )
				{
					SAFE_RELEASE( Src );
					SAFE_RELEASE( tempSurf );
					return NULL;
				}

				hr = D3DXLoadSurfaceFromSurface( tempSurf, NULL, NULL, Src, NULL, NULL, D3DX_DEFAULT, 0 );			
				if( hr != D3D_OK )
				{
					return NULL;
				}

				//lock the texture and read the data
				D3DLOCKED_RECT  lockrect;
				hr = tempSurf->LockRect( &lockrect, NULL, D3DLOCK_READONLY );
				if( hr == D3D_OK )
				{
					UINT pixelsize = (GetColorDepth()/8);
					BYTE * srcbits = (BYTE*)lockrect.pBits;
					BYTE * destbits = (BYTE*)m_pLocalBuffer;
					//write the texture to the buffer
					for( UINT i = 0; i < m_Height; i++ )
					{
						memcpy( destbits, srcbits, m_Width*pixelsize );
						//move by pitch
						srcbits  +=  lockrect.Pitch;
						destbits += m_Width*pixelsize;
					}
				}
				tempSurf->UnlockRect();
				SAFE_RELEASE( tempSurf );
				SAFE_RELEASE( Src );
				return m_pLocalBuffer;
			}
			else
			{
				//lock the texture and read the data
				D3DLOCKED_RECT  lockrect;
				HRESULT hr = m_Texture->LockRect( 0, &lockrect, NULL, D3DLOCK_READONLY );
				if( hr == D3D_OK )
				{
					UINT pixelsize = (GetColorDepth()/8);
					BYTE * srcbits = (BYTE*)lockrect.pBits;
					BYTE * destbits = (BYTE*)m_pLocalBuffer;
					//write the texture to the buffer
					for( UINT i = 0; i < m_Height; i++ )
					{
						memcpy( destbits, srcbits, m_Width*pixelsize );
						//move by pitch
						srcbits  +=  lockrect.Pitch;
						destbits += m_Width*pixelsize;
					}
				}
				m_Texture->UnlockRect( 0 );
				return m_pLocalBuffer;
			}
		}else//we are render target, need surface
		{			
			LPDIRECT3DSURFACE9 RenderSurf = NULL;
			LPDIRECT3DSURFACE9 OffSurf = NULL;
			m_Texture->GetSurfaceLevel( 0, &RenderSurf );
			
			if( RenderSurf )
			{
				D3DSURFACE_DESC tDesc;
				m_Texture->GetLevelDesc(0, &tDesc );
				if( FAILED(pDevice->CreateOffscreenPlainSurface( tDesc.Width,
													tDesc.Height,
													tDesc.Format,
													D3DPOOL_SYSTEMMEM,													
													&OffSurf,
													NULL )))
				{
					RenderSurf->Release();
					return NULL;
				}
				if( FAILED( pDevice->GetRenderTargetData( RenderSurf, OffSurf ) ))
				{
					RenderSurf->Release();
					OffSurf->Release();
					return NULL;
				}
				//No need for rendertarget surface anymore
				RenderSurf->Release();
				//lock the texture and read the data
				D3DLOCKED_RECT  lockrect;
				HRESULT hr = OffSurf->LockRect( &lockrect, NULL, D3DLOCK_READONLY );
				if( hr == D3D_OK )
				{
					UINT pixelsize = (GetColorDepth()/8);
					BYTE * srcbits = (BYTE*)lockrect.pBits;
					BYTE * destbits = (BYTE*)m_pLocalBuffer;
					//write the texture to the buffer
					for( UINT i = 0; i < m_Height; i++ )
					{
						memcpy( destbits, srcbits, m_Width*pixelsize );
						//move by pitch
						srcbits  +=  lockrect.Pitch;
						destbits += m_Width*pixelsize;
					}
				}
				OffSurf->UnlockRect();
				OffSurf->Release();
			}
			return m_pLocalBuffer;

		}
	}
	return NULL;
}
Beispiel #17
0
void BackBuffer()
{
	IDirect3DSurface9 *ppBackBuffer, *ppOffScreen;
	HRESULT back;
D3DXIMAGE_INFO pSrcInfo;
	if (SUCCEEDED(back = g_pd3dDevice->CreateOffscreenPlainSurface(
		256, 256, D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &ppOffScreen, NULL)))    	
	{	
		
		if (SUCCEEDED( back = D3DXLoadSurfaceFromFile(
			ppOffScreen,
			NULL,
			NULL,
			"bana.bmp",
			NULL,
			D3DX_FILTER_TRIANGLE,
			0,
			&pSrcInfo)))
		{
			back = NULL;
		}
		else if (back == D3DERR_INVALIDCALL)
		{
			back = 0;
		}
		else if (back == D3DXERR_INVALIDDATA)
		{
			pSrcInfo.Width;
			back = 1;
		}
		else
		{
			back = 2;
		}
	}

	//if (SUCCEEDED(g_pd3dDevice->GetRenderTarget(0, &ppBackBuffer)))
	if (SUCCEEDED(g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &ppBackBuffer)))
	{
		D3DSURFACE_DESC pDesc;
		ppBackBuffer->GetDesc(&pDesc);
		POINT pDestinationPoint;
	
		for (int i=0; i<4; i++)
		{
			pDestinationPoint.x = i*pSrcInfo.Width;
			for (int j=0; j<4; j++)
			{
				pDestinationPoint.y = j*pSrcInfo.Height;


				if (SUCCEEDED(back = g_pd3dDevice->UpdateSurface(ppOffScreen, NULL, ppBackBuffer, &pDestinationPoint)))
				{
					back = 0;
				}	
			}
		}
	}
	ppBackBuffer->Release();
	ppOffScreen->Release();
	

	

	/*
	if (SUCCEEDED(back =  g_pd3dDevice->CreateRenderTarget(
		256,
		256,
		D3DFMT_R8G8B8,
		D3DMULTISAMPLE_NONE,
		0,
		true,
		&ppBackBuffer,
		NULL)))
		*/

	/*if (SUCCEEDED(back = g_pd3dDevice->CreateDepthStencilSurface(
		256,
		256,
		D3DFMT_D16,
		D3DMULTISAMPLE_NONE,
		0,
		true,
		&ppBackBuffer,
		NULL)))*/
	//if (SUCCEEDED(g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &ppBackBuffer)))
/*

		
		D3DLOCKED_RECT pLockedRect;
		if (SUCCEEDED(back = ppBackBuffer->LockRect(&pLockedRect, NULL, D3DLOCK_READONLY)))
		{
			back=1;
		}
		
		

*/




				
/*

		HDC phdc;

		if (SUCCEEDED(back = ppBackBuffer->GetDC(&phdc)))

		//if (SUCCEEDED(back = ppBackBuffer->LockRect(&pLockedRect, NULL, D3DLOCK_READONLY)))
		{
			BYTE* pSrcTopRow = (BYTE*)pLockedRect.pBits;
			DWORD dwSrcPitch = (DWORD)pLockedRect.Pitch;
			*pSrcTopRow =1;
			//for(int i=0;i<1;i++)
			//	*(pSrcTopRow+(i*dwSrcPitch)) = 1;

		}
		else if (back == D3DERR_INVALIDCALL)
		{
			back = 0;
		}
		else if(back == D3DERR_WASSTILLDRAWING)
		{
			back = 0;
		}

*/
//		ppBackBuffer->UnlockRect();


}