Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Here in the attached code strechrect method is getting failed and its returning the -ve value. I am doing operations as below. please give me what wrong i am doing here.

1. Getting the offscreenplain surface using CreateOffscreenPlainSurface method.
2. modifying the plain surface.
3. Getting the back buffer of the surface
4. Putting back plain surface to the back buffer using strechrect method.

void CTriangleRenderer::LockSurfaceExtra()
{
	DWORD     bkColor;
	D3DCOLOR Color;
	HDC hdc;
	HRESULT         hr;
	D3DLOCKED_RECT  lockedRect;
	DWORD           *pMem, colorVal;
	RECT          rc, wrc, mrc,destRect;
	int             i, j, k, m, linemin, linemax, linecnt, linesDrawn = 0,pitch;
	//This will hold the back buffer
	IDirect3DSurface9* backbuffer = NULL;
	// Get the surface details
			D3DSURFACE_DESC d3dsd;
	if( NULL == m_pd3dDevice )
		return;

	g_increase=g_increase+5;
	
	m_pd3dDevice->GetBackBuffer( 0,
		0,
		D3DBACKBUFFER_TYPE_MONO,
		&backbuffer );


    //m_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_ARGB(0, 0,0, 0),1.0f,0);


			// Get the back buffer
		
		
	
		m_pd3dDevice->CreateOffscreenPlainSurface(1400,
                                             1100,
                                             D3DFMT_X8R8G8B8,
                                             D3DPOOL_DEFAULT,
                                             &m_pd3dRTSBackup,
                                             NULL);

		

		

		//m_pd3dDevice->UpdateSurface(m_pd3dRTS, NULL, backbuffer, NULL);

		if (m_pd3dRTSBackup != NULL)
		{
			hr = m_pd3dRTSBackup->LockRect(&lockedRect, NULL, 0);
			if (SUCCEEDED(hr))
			{
			  pitch = lockedRect.Pitch / sizeof(DWORD); // DWORD pitch - 32 bits per pixel
			  pMem = (DWORD *)lockedRect.pBits;
			}
			GetClientRect(m_hwnd,&rc);
			
			m_pd3dRTSBackup->GetDesc(&d3dsd);

			for (WORD y = 0; y <d3dsd.Height ; ++y) 
			{
			for (WORD x = 0; x < d3dsd.Width; ++x) 
			{	
				
				pMem[(pitch*y)+x] = D3DCOLOR_ARGB(255, 0,0,0);
		
			}	
			}

			m_pd3dRTSBackup->UnlockRect(); 
		}
		 
		


		 //Copy the offscreen surface to the back buffer
		// Note the use of NULL values for the source and destination RECTs
		// This ensures a copy of the entire surface to the back buffer
		
		m_pd3dDevice->BeginScene();
		
		


		hr=m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,   

                                        D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );


		hr=	m_pd3dDevice->StretchRect(m_pd3dRTSBackup,
		NULL,
		backbuffer,
		NULL,
		D3DTEXF_NONE );

		

		
		//D3DXLoadSurfaceFromSurface(backbuffer,NULL,NULL,m_pd3dRTSBackup,NULL,NULL,D3DX_FILTER_NONE,0xFF000000);

		//m_pd3dDevice->UpdateSurface(m_pd3dRTSBackup, NULL, backbuffer, NULL);
		backbuffer->Release();	
		m_pd3dDevice->EndScene();
		// Present the back buffer contents to the display
		m_pd3dDevice->Present ( NULL, NULL, NULL, NULL );
}
Posted
Updated 25-Oct-10 4:25am
v2

Have you looked at the StretchRect restrictions section here[^]? It contains valuable information as to why this call may fail.
 
Share this answer
 
Hi,
Thanks for suggestions. I checked all the rules for stretchrect and following them also. Here after creating the plain surface i tried to write as image file, its also not coming correct as the main screen. some junk data is coming with the main screen data. Not getting where i am doing mistake. Below is the main device creation code.

HRESULT
CRenderer::Init(IDirect3D9 *pD3D, IDirect3D9Ex *pD3DEx, HWND hwnd, UINT uAdapter)
{
HRESULT hr = S_OK;

D3DPRESENT_PARAMETERS d3dpp;
/* ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.BackBufferCount=1;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferHeight = 1100;
d3dpp.BackBufferWidth = 1000;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;*/


memset(&d3dpp, 0, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = 0; // 0 for windowed mode
d3dpp.PresentationInterval = D3DPRESENT_DONOTWAIT;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.EnableAutoDepthStencil = FALSE;
d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
d3dpp.Windowed = TRUE;
d3dpp.BackBufferWidth = 1100;
d3dpp.BackBufferHeight = 1000;


D3DCAPS9 caps;
DWORD dwVertexProcessing;
IFC(pD3D->GetDeviceCaps(uAdapter, D3DDEVTYPE_HAL, &caps));

if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
dwVertexProcessing = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else
{
dwVertexProcessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}

if (pD3DEx)
{
IDirect3DDevice9Ex *pd3dDevice = NULL;
IFC(pD3DEx->CreateDeviceEx(
uAdapter,
D3DDEVTYPE_HAL,
hwnd,
dwVertexProcessing | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
&d3dpp,
NULL,
&m_pd3dDeviceEx
));

IFC(m_pd3dDeviceEx->QueryInterface(__uuidof(IDirect3DDevice9), reinterpret_cast<void**>(&m_pd3dDevice)));
}
else
{
assert(pD3D);

IFC(pD3D->CreateDevice(
uAdapter,
D3DDEVTYPE_HAL,
hwnd,
dwVertexProcessing | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
&d3dpp,
&m_pd3dDevice
));
}

Cleanup:
return hr;
}
 
Share this answer
 
Comments
Niklas L 26-Oct-10 13:46pm    
hr = m_pd3dRTSBackup->LockRect(&lockedRect, NULL, 0);
if (SUCCEEDED(hr))
{
pitch = lockedRect.Pitch / sizeof(DWORD); // DWORD pitch - 32 bits per pixel
pMem = (DWORD *)lockedRect.pBits;
}

If the LockRect call fails, pitch and pMem will be uninitialized.

Have you stepped through the code and watched all variables?
hi,
ss i checked the surface modification and i was able to see the changes by saving to the .jpg file also. ones i come to stretchrect method i am getting the error.
One more doubt I have if I take a plain surface object, is that object contains the updated surface or its an just a plain surface without any data.?
thanks for ur suggestiom.:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900