[DX9 C++] Present problem

Started by
7 comments, last by ArgusMaker 12 years, 11 months ago
Hi, i'm a newbie here.
I have made a win32 application with DirectX9.

There's kinda an error in my rendering code, as it shows in my window stuff shown before, like other windows parts (menus,title bars, black rectangles..), when i should see a blank rectangle.
This is my rendering code:


g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(255,255,255), 1.0f, 0);
if(SUCCEEDED(g_pd3dDevice->BeginScene()))
{
g_pd3dDevice->EndScene();
}
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);

unledsvw.jpg
I'm pretty sure that the other application code does not affect rendering..
Do you know which the cause could be?
Advertisement
D3DCOLOR_XRGB(255,255,255)

when i should see a blank rectangle.[/quote]

Not quite, the color you specified is white. For me personally I also get such artifacts if I clear the backbuffer to anything but black. Try using
D3DCOLOR_XRGB(0,0,0).

For me personally I also get such artifacts if I clear the backbuffer to anything but black. Try using
D3DCOLOR_XRGB(0,0,0).

Nothing changes :(
Ok i re-wrote my rendering function and it is now correct!
My new task is different:
I'm able to create surfaces with DX9, but i haven't understood how to draw on them (device->draw methods only work in my "official" backbuffer, which is swapped to the frontbuffer with device->present).

What i want to do is to draw something in a separate surface, which content will not be rendered on my window by device->present, holding in the application memory this surface in order to let me use it later.
Is it possible? Thanks in advance
Good you figured it out. The function you are looking for to rendering to a surface is:

LPDIRECT3DSURFACE9 Surface;
device->SetRenderTarget(0, &Surface);
Surface->Release();


Then you render like normal and the content will be on the surface. Afterwards, you have to set the rendertarge back to the backbuffer:

LPDIRECT3DSURFACE9 Surface;
device->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&Surface);
device->SetRenderTarget(0, Surface);
Surface->Release();
Thank you.
Could you please give me one last hint?

LPDIRECT3DSURFACE9 Surfaces[20];
//create the first surface!
device->CreateRenderTarget(100,100,D3DFMT_A8R8G8B8,D3DMULTISAMPLE_NONE,0,false,&Surfaces[0],NULL);
device->SetRenderTarget(0,Surfaces[0]);
Surfaces[0]->Release();
POINT a;
a.x = 10;
a.y = 10;



This code seems to work, but later, in my render loop, when i try to draw Surface[0] on the screen by copying it on the back buffer , i get an error:

device->UpdateSurface(Surfaces[0],NULL,BackBuffer,&a);
device->Present(NULL,NULL,NULL,NULL);


the error message is:
Unhandled exception at 0x4fe13f3d in ProjDX.exe: 0xC0000005: Access violation in reading 0x00000000.
and it comes from the line with "UpdateSurface" method..
BackBuffer is obtained at the beginning of the application with this code:

IDirect3DSurface9* BackBuffer = NULL;
if(FAILED(g_pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&BackBuffer)))
{ MessageBox(0,"Error","BackBuffer Error",MB_OK);}

I never got that message..
Ah sry, my fault. You are only supposed to Release the surface right after setting it as a rendertarget if you don't need its information later on (like for the backbuffer). Try to delete Surface[0]->Release(); , but be sure to release it on programs exit. Tell me if it worked.

LPDIRECT3DSURFACE9 Surfaces[20];
device->CreateRenderTarget(100,100,D3DFMT_A8R8G8B8,D3DMULTISAMPLE_NONE,0,false,&Surface[0],NULL);
device->SetRenderTarget(0,Surfaces[0]);
//draw stuff on Surfaces[0]
device->DrawPrimitiveUP(...);
//reset the target
device->SetRenderTarget(0,BackBuffer);
POINT a;
a.x = 12;
a.y = 12;


//later, in render loop
device->UpdateSurface(Surfaces[0],NULL,BackBuffer,&a);



[color="#006666"]Gives me the same error at the same line
If this can help you to help me (coxian sentence)
here is the entire code of my win32 application:
[sup]

#include <d3d9.h>
#include <WindowsX.h>
#pragma comment (lib,"d3d9.lib")
#pragma comment (lib,"d3dx9.lib")
//Add this source to your project

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device

int mouse_x = 0;
int mouse_y = 0;

struct VERTEX
{
float x,y,z,rhw;
DWORD color;
};
VERTEX vertici[20];
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
LPDIRECT3DSURFACE9 Surface = NULL;
LPDIRECT3DSURFACE9 BackBuffer = NULL;
POINT punto;
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object, which is needed to create the D3DDevice.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;

// Set up the structure used to create the D3DDevice. Most parameters are
// zeroed out. We set Windowed to TRUE, since we want to do D3D in a
// window, and then set the SwapEffect to "discard", which is the most
// efficient method of presenting the back buffer to the display. And
// we request a back buffer format that matches the current desktop display
// format.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

// Create the Direct3D device. Here we are using the default adapter (most
// systems only have one, unless they have multiple graphics hardware cards
// installed) and requesting the HAL (which is saying we want the hardware
// device rather than a software one). Software vertex processing is
// specified since we know it will work on all cards. On cards that support
// hardware vertex processing, though, we would see a big performance gain
// by specifying hardware vertex processing.
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
return E_FAIL;

}

// Device state would normally be set here
//Creo una surface
g_pd3dDevice->CreateRenderTarget(100,100,D3DFMT_UNKNOWN,D3DMULTISAMPLE_NONE,0,FALSE,&Surface,NULL);


return S_OK;
}




//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();

if( g_pD3D != NULL)
g_pD3D->Release();
}




//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
if( NULL == g_pd3dDevice )
return;

// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{

g_pd3dDevice->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE);


// Rendering of scene objects can happen here
vertici[0].x = 0;
vertici[0].y = 0;
vertici[0].rhw = 1.0f;
vertici[0].z = 1.0f;
vertici[0].color = D3DCOLOR_XRGB(255,0,0);
vertici[1].x = 100;
vertici[1].y = 100;
vertici[1].rhw = 1.0f;
vertici[1].z = 1.0f;
vertici[1].color = D3DCOLOR_XRGB(255,0,0);
vertici[2].x = 0;
vertici[2].y = 100;
vertici[2].rhw = 1.0f;
vertici[2].z = 1.0f;
vertici[2].color = D3DCOLOR_XRGB(0,0,0);
vertici[3].x = 100;
vertici[3].y = 0;
vertici[3].rhw = 1.0f;
vertici[3].z = 1.0f;
vertici[3].color = D3DCOLOR_XRGB(0,0,0);
g_pd3dDevice->SetRenderTarget(0,Surface);
g_pd3dDevice->DrawPrimitiveUP(D3DPT_LINELIST,2,vertici,sizeof(VERTEX));
g_pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&BackBuffer);
g_pd3dDevice->SetRenderTarget(0,BackBuffer);
g_pd3dDevice->DrawPrimitiveUP(D3DPT_LINELIST,1,vertici,sizeof(VERTEX));
punto.x = mouse_x;
punto.y = mouse_y;
g_pd3dDevice->UpdateSurface(Surface,NULL,BackBuffer,&punto);
// End the scene
g_pd3dDevice->EndScene();
}

// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}




//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
case WM_MOUSEMOVE:
mouse_x = GET_X_LPARAM(lParam);
mouse_y = GET_Y_LPARAM(lParam);
break;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}




//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClassEx( &wc );

// Create the application's window
HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), NULL, wc.hInstance, NULL );

// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );

MSG mssg;

PeekMessage( &mssg, NULL, 0, 0, PM_NOREMOVE);
// run till completed
while (mssg.message!=WM_QUIT)
{
// is there a message to process?
if (PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE))
{
// dispatch the message
TranslateMessage(&mssg);
DispatchMessage(&mssg);
}
else
{
//No message to process?
// Then do your game stuff here
Render();
}
}
}

UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}
[/sup]

This topic is closed to new replies.

Advertisement