BadMoonFall

I can't get my code work. I'm attempting to make some 2D rendering using device and memory vertex buffers and device index buffer.

I post here all code with initialization and drawing in such order as it goes in program execution. Maybe someone could give me an advise about what is wrong with it.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool Init(int _BuffSize)

{

CSprite::DeviceVB_size = _BuffSize;

if( FAILED( RenderDevice->CreateVertexBuffer( _BuffSize * sizeof(CUSTOMVERTEX),

D3DUSAGE_WRITEONLY , D3DFVF_CUSTOMVERTEX,

D3DPOOL_DEFAULT, &DeviceVB, NULL ) ) )

{

MessageBox( NULL , L"Can't create VB" , L"Error creating VB" , MB_OK);

return false;

}

if( FAILED( RenderDevice->CreateIndexBuffer( 6*sizeof( int ) , D3DUSAGE_WRITEONLY , D3DFMT_INDEX16 ,

D3DPOOL_DEFAULT , &CSprite::QuadIndexBuffer , NULL) ))

{

MessageBox( NULL , L"Can't create IB" , L"Error creating IB" , MB_OK);

return false;

}

VOID* pIndices;

int IndexesDevil = {0 , 1 , 2 , 3 , 0 , 2 };

if( FAILED( QuadIndexBuffer->Lock( 0, sizeof(Indexes), (void**)&pIndices, 0 ) ))

return false;

memcpy( pIndices, Indexes, sizeof(Indexes) );

QuadIndexBuffer->Unlock();

return true;

};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Draw(const RECT* _TexRect , const Vector2* _Position , DWORD _Col)

{

int SpriteX = (int)_Position->x;

int SpriteY = (int)_Position->y;

int SpriteWidth = TextureWidth;

int SpriteHeight = TextureHeight;

float TextureX = 0.0f;

float TextureY = 0.0f;

float TexturePortionWidth = 1.0f;

float TexturePortionHeight = 1.0f;

if(_TexRect != NULL)

{

SpriteWidth = _TexRect->right - _TexRect->left;

SpriteHeight = _TexRect->bottom - _TexRect->top ;

TextureX = (float)_TexRect->left / TextureWidth;

TextureY = (float)_TexRect->top / TextureHeight;

TexturePortionWidth = (float)_TexRect->right / TextureWidth;

TexturePortionHeight = (float)_TexRect->bottom / TextureHeight;

}

MemoryVB[PrimitiveCounter].x = SpriteX;

MemoryVB[PrimitiveCounter].y = SpriteHeight;

MemoryVB[PrimitiveCounter].z = 0.0f;

MemoryVB[PrimitiveCounter].tu = TextureX;

MemoryVB[PrimitiveCounter].tv = TexturePortionHeight;

MemoryVB[PrimitiveCounter].color = _Col;

MemoryVB[PrimitiveCounter+1].x = SpriteX;

MemoryVB[PrimitiveCounter+1].y = SpriteY;

MemoryVB[PrimitiveCounter+1].z = 0.0f;

MemoryVB[PrimitiveCounter+1].tu = TextureX;

MemoryVB[PrimitiveCounter+1].tv = TextureY;

MemoryVB[PrimitiveCounter+1].color = _Col;

MemoryVB[PrimitiveCounter+2].x = SpriteWidth;

MemoryVB[PrimitiveCounter+2].y = SpriteY;

MemoryVB[PrimitiveCounter+2].z = 0.0f;

MemoryVB[PrimitiveCounter+2].tu = TexturePortionWidth;

MemoryVB[PrimitiveCounter+2].tv = TextureY;

MemoryVB[PrimitiveCounter+2].color = _Col;

MemoryVB[PrimitiveCounter+3].x = SpriteWidth;

MemoryVB[PrimitiveCounter+3].y = SpriteHeight;

MemoryVB[PrimitiveCounter+3].z = 0.0f;

MemoryVB[PrimitiveCounter+3].tu = TexturePortionWidth;

MemoryVB[PrimitiveCounter+3].tv = TexturePortionHeight;

MemoryVB[PrimitiveCounter+3].color = _Col;

PrimitiveCounter += 4; // Global static vertex counter

RenderBlock.LastPrimNumber +=4 ; // Local counter for vertices, for each instance of CSprite

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Render ()

{

RBlock *RB = RenderBlockQueue.front();

LPDIRECT3DTEXTURE9 Tex = RB->Tex;

int LastVertex = RB->LastPrimNumber;

VOID* pVertices;

if( FAILED( CSprite::DeviceVB->Lock( 0, LastVertex * sizeof(CUSTOMVERTEX), (void**)&pVertices, 0 ) ) )

{

MessageBox(NULL , L"Error locking VB" , L"VB error" , MB_OK);

return;

}

memcpy( pVertices, MemoryVB , /* PrimitiveCounter */ LastVertex * sizeof(CUSTOMVERTEX) );

CSprite::DeviceVB->Unlock();

if( RenderDevice->SetStreamSource( 0 , DeviceVB , 0 , sizeof(CUSTOMVERTEX)) != D3D_OK )

{

MessageBox(NULL , L"Error setting steam source" , L"Steam error" , MB_OK);

return;

}

else

if( RenderDevice->SetIndices(CSprite::QuadIndexBuffer) != D3D_OK )

{

MessageBox(NULL , L"Error setting index source" , L"Index error" , MB_OK);

return;

}

else

{

RenderDevice->SetFVF( D3DFVF_CUSTOMVERTEX );

for(int i=0;i<=LastVertex;i+=4)

RenderDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST , i , 0 , 4 , 0 , 2 ); // Draw quad

}

RenderBlockQueue.front()->LastPrimNumber = 0;

PrimitiveCounter = 0;




Re: Game Technologies: Graphics Can't Draw indexed primitive.

BadMoonFall

up




Re: Game Technologies: Graphics Can't Draw indexed primitive.

ProfEclipse

Posting a big pile of code and then saying "Can someone fix this " isn't the best way to get help.

What exactly is the code doing that it isn't supposed to do Or what exactly is the code not doing that it is supposed to do What thenigs have you tried to solve the problem What were the results of those attempts

Nobody wants to sift through a pile of code with no idea what your problem is, trying to fix it for you. Help us to help you.

(And an hour and 15 minutes isn't nearly long enough to wait before bumping your post.)