• Advertisement

Archived

This topic is now archived and is closed to further replies.

DrawIndexedPrimitive HEELLLPP!!

This topic is 5187 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I give up! lol I am doing a 2D game using 3D. My tile engine was fine until I decided to try my hands at batching. (my FPS wasn't that good) Anyway, all I get is a blank screen. Here is the code that I am using:
//---------------------------------------------------------------------------------

//	Constructor

//---------------------------------------------------------------------------------

CSprite_QUAD::CSprite_QUAD(IDirect3DDevice9 *d3dDevice,
						   CTexture_D3DX *pTexture,
						   D3DPRESENT_PARAMETERS pPresent,
						   int fwidth,
						   int fheight)
{

	texture = pTexture->GetTexture();

	//color key

	D3DCOLOR colorkey = 0xFFFF00FF;
	
    //Create vertex buffer and set as stream source

    d3dDevice->CreateVertexBuffer( sizeof(TLVERTEX) * BATCH_BUFFER_SIZE,
								   D3DUSAGE_WRITEONLY,		//write only..faster

								   D3DFVF_TLVERTEX,			//FVF format

								   D3DPOOL_MANAGED,			//DirectX manages memory

								   &SpriteVertexBuffer,
								   NULL );

	d3dDevice->CreateIndexBuffer(  BATCH_BUFFER_SIZE * 3,
								   D3DUSAGE_WRITEONLY,
								   D3DFMT_INDEX16,
								   D3DPOOL_MANAGED,
								   &indexBatchBuffer,
								   NULL );
	numBatchVertices = 0;

	//Fill the index buffer

    int index = 0;
    short* indices = NULL;

    //Lock index buffer

    indexBatchBuffer->Lock(0, BATCH_BUFFER_SIZE  * 3,(void**) &indices, 0);

    for (int vertex = 0; vertex < BATCH_BUFFER_SIZE; vertex += 4)
    {
        indices[index] = vertex;
        indices[index + 1] = vertex + 2;
        indices[index + 2] = vertex + 3;
        indices[index + 3] = vertex;
        indices[index + 4] = vertex + 1;
        indices[index + 5] = vertex + 2;
        index += 6;
    }

    //Unlock index buffer

    indexBatchBuffer->Unlock ();

	//get texture surface description

	texture->GetLevelDesc(0, &surfaceDesc);
	tex_width	= surfaceDesc.Width;
	tex_height	= surfaceDesc.Height;

	//setup dimensions

	FrameWidth = fwidth;
	FrameHeight = fheight;
	NumFramesWide = tex_width / FrameWidth;
	NumFramesTall = tex_height / FrameHeight;
	U = 1.0f / (float)NumFramesWide;
	V = 1.0f / (float)NumFramesTall;


    //Setup destination rectangle

    rDest.left		= x;
    rDest.right		= x + tex_width;
    rDest.top		= y;
    rDest.bottom	= y + tex_height;


	//Lock the vertex buffer

	SpriteVertexBuffer->Lock(0, 0, (void **)&vertices, NULL);
	
    //Setup vertices

    vertices[0].color = 0xFFFFFFFF;
    vertices[0].x = 0.0f;
    vertices[0].y = 0.0f;
    vertices[0].z = 1.0f;
    vertices[0].u = 0.0f;
    vertices[0].v = 0.0f;
	
    vertices[1].color = 0xFFFFFFFF;
    vertices[1].x = 1.0f;
    vertices[1].y = 0.0f;
    vertices[1].z = 1.0f;
    vertices[1].u = 1.0f;
    vertices[1].v = 0.0f;
	
    vertices[2].color = 0xFFFFFFFF;
    vertices[2].x = 1.0f;
    vertices[2].y = -1.0f;
    vertices[2].z = 1.0f;
    vertices[2].u = 1.0f;
    vertices[2].v = 1.0f;
	
    vertices[3].color = 0xFFFFFFFF;
    vertices[3].x = 0.0f;
    vertices[3].y = -1.0f;
    vertices[3].z = 1.0f;
    vertices[3].u = 0.0f;
    vertices[3].v = 1.0f;
	
	
	//Unlock the vertex buffer

	SpriteVertexBuffer->Unlock();

	//other variables

	scr_width = pPresent.BackBufferWidth;
	scr_height = pPresent.BackBufferHeight;


    d3dDevice->SetTexture(0, texture);

	//Defaults

	x = 0;
	y = 0;
	CurrentFrame = 0;
	xspeed = 0;
	yspeed = 0;
	rotation = 0.0f;

}

...

//---------------------------------------------------------------------------------

//	BeginBatchDrawing

//---------------------------------------------------------------------------------

//Get ready for batch drawing

void CSprite_QUAD::BeginBatchDrawing( IDirect3DDevice9 *d3dDevice )
{
    //Lock the batching vertex buffer

    numBatchVertices = 0;
    SpriteVertexBuffer->Lock(0,BATCH_BUFFER_SIZE * sizeof(TLVERTEX),(void **) &vertices, 0);

    //Set texture

    d3dDevice->SetTexture (0, texture);

    //Set world matrix to an identity matrix

    d3dDevice->SetTransform (D3DTS_WORLD, &matTransform);

    //Set stream source to batch buffer

    d3dDevice->SetStreamSource(0, SpriteVertexBuffer,0, sizeof(TLVERTEX));
}



//---------------------------------------------------------------------------------

//	EndBatchDrawing

//---------------------------------------------------------------------------------

//Finish batch drawing

void CSprite_QUAD::EndBatchDrawing(IDirect3DDevice9 *d3dDevice)
{
    //Unlock vertex buffer

    SpriteVertexBuffer->Unlock();

    //Draw the quads in the buffer if it wasn't just flushed

    if (numBatchVertices)
       d3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
										0,
										0,
										numBatchVertices,
										0,
										numBatchVertices / 2);


    //Set stream source to regular buffer

    d3dDevice->SetStreamSource(0, SpriteVertexBuffer, 0, sizeof(TLVERTEX));

    //Reset vertex count        

    numBatchVertices = 0;        
}



//---------------------------------------------------------------------------------

//	Draw

//---------------------------------------------------------------------------------

int CSprite_QUAD::Draw(IDirect3DDevice9 *d3dDevice)
{
    float X;
    float Y;
	
    //Get coordinates

	rDest.left		= x;
	rDest.top		= y;
	rDest.right		= x + FrameWidth;
	rDest.bottom	= y + FrameHeight;
	
    X = rDest.left - (float)(scr_width) / 2;
    Y = -rDest.top + (float)(scr_height) / 2; 
	
	CurrentFrame	= 16;

	rfSource.top	= ( CurrentFrame / NumFramesWide ) * V;
	rfSource.left	= ( CurrentFrame % NumFramesTall ) * V;
	tmp1 = rfSource.top;
	tmp2 = rfSource.left;
	rfSource.bottom	= rfSource.top + V;
	
    //Setup vertices

    vertices[numBatchVertices].color = 0xFFFFFFFF;				//top left

    vertices[numBatchVertices].x = 0.0f;
    vertices[numBatchVertices].y = 0.0f;
    vertices[numBatchVertices].z = 1.0f;
    vertices[numBatchVertices].u = rfSource.left;
    vertices[numBatchVertices].v = rfSource.top;
	
    vertices[numBatchVertices + 1].color = 0xFFFFFFFF;			//top right

    vertices[numBatchVertices + 1].x = 1.0f;
    vertices[numBatchVertices + 1].y = 0.0f;
    vertices[numBatchVertices + 1].z = 1.0f;
    vertices[numBatchVertices + 1].u = rfSource.right;
    vertices[numBatchVertices + 1].v = rfSource.top;
	
    vertices[numBatchVertices + 2].color = 0xFFFFFFFF;			//bottom right

    vertices[numBatchVertices + 2].x = 1.0f;
    vertices[numBatchVertices + 2].y = -1.0f;
    vertices[numBatchVertices + 2].z = 1.0f;
    vertices[numBatchVertices + 2].u = rfSource.right;
    vertices[numBatchVertices + 2].v = rfSource.bottom;
	
    vertices[numBatchVertices + 3].color = 0xFFFFFFFF;			//bottom left

    vertices[numBatchVertices + 3].x = 0.0f;
    vertices[numBatchVertices + 3].y = -1.0f;
    vertices[numBatchVertices + 3].z = 1.0f;
    vertices[numBatchVertices + 3].u = rfSource.left;
    vertices[numBatchVertices + 3].v = rfSource.bottom;

    //Increase vertex count

    numBatchVertices += 4;


    //Setup translation and scaling matrices

    D3DXMatrixScaling( &matScaling,
					   (float)(rDest.right - rDest.left),
					   (float)(rDest.bottom - rDest.top),
					   1.0f );									// scales the sprite

	
    D3DXMatrixTranslation (&matTranslation, X, Y, 0.0f);		// moves the sprite

    matTransform = matScaling * matTranslation;
	
    //Check if quad is rotated

    if(rotation)
    {
        D3DXMATRIX matRotate;
		
        //Create rotation matrix about the z-axis

        D3DXMatrixRotationZ (&matRotate, rotation);
		
        //Multiply matrices together

        matTransform *= matRotate;
    }


    //Flush buffer if it's full

    if (numBatchVertices == BATCH_BUFFER_SIZE)
    {
        //Unlock vertex buffer

        SpriteVertexBuffer->Unlock();
        
        //Draw quads in the buffer

       d3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
										0,
										0,
										numBatchVertices,
										0,
										numBatchVertices / 2);

        //Reset vertex count

        numBatchVertices = 0;        

        //Lock vertex buffer

        SpriteVertexBuffer->Lock( 0,
								  BATCH_BUFFER_SIZE * sizeof(TLVERTEX),
								  (void **) &vertices,
								  0 );
    }


	//Draw the quad

//	d3dDevice->SetTransform (D3DTS_WORLD, &matTransform);

//	d3dDevice->SetTexture (0, texture);

//	d3dDevice->SetStreamSource(0, SpriteVertexBuffer, 0, sizeof(TLVERTEX));

//	d3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);


	return true;
}

 
I *THINK* the problem is with the DrawIndexedPrimitive Any clues? THANKS! -cb

~ c o l ec o ~

Rock the cradle of love! You stupid WANKER! S i g n a l D E V .com Metroid Classic [edited by - coleco on February 6, 2004 7:51:59 PM] Come on people...I am at my wits end... :-/ thanks [edited by - coleco on February 7, 2004 11:55:24 AM]

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement