Skip to main content
gamedev.net
🔒 Locked

Rendering fields

Started by GreatGenius Jun 26, 2010 at 1:07 PM 2 replies 1k views
Original Post
GreatGenius
GreatGenius
Hi, Everyone.
I'm working on a tiled based game and right now everything has going well until now. I want my engine to render in different part of surfaces before it render to the backbuffer. For example

In surface g_mScoreBoard will score and amount life be drawed.
In surface g_mGameBoard will all items, player and enemies be drawed.

And after these surfaces will be drawed to the backbuffer. The score surface is going to be 800 x 200 in size and the game surface will be 800 x 400 in size.
Somehow I figured this out a long while ago but I have lost my codes for that :-( but I remember that rendertarget is the solution for this problem.
The only problem is that I don't remember how to put this thing right.
The game will use LPD3DXSPRITE as items and each sprite will have a texture too.

Wich solution should I choose?
1. Create surfaces with D3DPOOL_DEFAULT.
Create a RenderSurface with CreateRenderTarget() and draw score and game to it with StretchRect().
Then draw it to BackBuffer so it can display all of it to screen.

2. Create rendersurfaces with CreateRenderTarget() and let both score and game be rendertargets.
Then somehow set backbuffer as a rendertarget first and after that select ScoreBoard as a rendertarget so I can draw score stuff to its surface.
After that select GameBoard as a rendertarget so I can draw all items and players to its surface.
And finally select backbuffer as a rendertarget so it can render it to screen.

So any suggestion for my problem would be great. I also wonder if LPD3DXSPRITE works with rendertargets and how I make it work.
GreatGenius
GreatGenius
I spent some hour to figure out myself and know I want to know what you think of my solution:

if( bGameRunning && !bGamePaused ){	// Tell the Direct3D device to render to the first swap chain’s back buffer	hResult = g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &g_pd3dBackBuffer );	hResult = g_pd3dDevice->GetRenderTarget( 0, &g_pd3dBackBuffer );	// And then switch to the gamebox render...	hResult = g_pd3dDevice->SetRenderTarget(0, g_pd3dRenderGame );	// Clear the target buffer to little red...	g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,								 D3DCOLOR_COLORVALUE( 0.5f, 0.0f, 0.0f, 1.0f ), 1.0f, 0 );	//--------------------------------------------------------------------------	// BEGINSCENE HERE	hResult = g_pd3dDevice->BeginScene();	if (FAILED(hResult))	   return;	RECT srcRect;	srcRect.top    = 0;	srcRect.left   = 0;	srcRect.bottom = 32;	srcRect.right  = 32;	D3DXVECTOR3 vCenter( 0.0f, 0.0f, 0.0f );	D3DXVECTOR3 vPosition( 100.0f, 200.0f, 0.0f );	g_pTestSprite->Begin( D3DXSPRITE_ALPHABLEND );	g_pTestSprite->Draw( g_pTestTexture,                        &srcRect,                          &vCenter,                          &vPosition,                          D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,1.0f) );	g_pTestSprite->End();	g_pd3dDevice->EndScene();	//-------------------------------------------------------------------------	// ENDSCENE HERE	// Then switch to scorebox...	hResult = g_pd3dDevice->SetRenderTarget(0, g_pd3dRenderScore );	static wchar_t ScoreText[25] = L"Here is the scoreboard";	RECT dTextBox1;	SetRect( &dTextBox1, 160, 160, 0, 0 );	//--------------------------------------------------------------------------	// BEGINSCENE HERE	hResult = g_pd3dDevice->BeginScene();	if (FAILED(hResult))	    return;	// Paint the text to a sprite item so it will be drawed to the score box and	// not over the screen	g_pSpriteText->Begin( D3DXSPRITE_ALPHABLEND );	g_pd3dxFont->DrawText( g_pSpriteText, ScoreText, -1, &dTextBox1, DT_NOCLIP, 								   D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );	g_pSpriteText->End();	g_pd3dDevice->EndScene();	//--------------------------------------------------------------------------	// ENDSCENE HERE	// Now switch back to backbuffer	hResult = g_pd3dDevice->SetRenderTarget(0, g_pd3dBackBuffer );	g_pd3dBackBuffer->Release();	// Clear the target buffer to black...	g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,								 D3DCOLOR_COLORVALUE( 0.0f, 0.0f, 0.0f, 1.0f ), 1.0f, 0 );	// Stretchblit the score and game boxes to the backbuffer	RECT dRectGame;	SetRect( &dRectGame, 0, 200, 800, 600 );	hResult = g_pd3dDevice->StretchRect( g_pd3dRenderGame, NULL, g_pd3dBackBuffer, &dRectGame, D3DTEXF_NONE );	RECT dRectScore;	SetRect( &dRectScore, 0, 0, 800, 200 );	hResult = g_pd3dDevice->StretchRect( g_pd3dRenderScore, NULL, g_pd3dBackBuffer, &dRectScore, D3DTEXF_NONE );				g_pd3dDevice->Present( NULL, NULL, NULL, NULL );	// clear the backbuffer	g_pd3dBackBuffer->Release();	g_pd3dBackBuffer = NULL;}


Are StretchRect fast enough to use for 2D games? Please comment if this is a good solution for making a tiled based game.
Zahlman
Zahlman
Quote:
Original post by GreatGenius
Are StretchRect fast enough to use for 2D games?


Are they fast enough for your game? Did you try it?
GreatGenius
GreatGenius
Well, so far so good. My fps counter shows between 2500 - 2900 fps but it don't run all the items in my game yet. I still have to work on sounds and other items but it looks good right now.
I succeed to find all my old codes in a backup CD so right now I can work on it for a while but thanks for the reply.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.