Original Post
Hi!
I'm making a graphics engine for 2D graphics using direct 3D. Below is my DrawSprite function. As you can see you can specify a source rectangle to tell which part of a bitmap (texture) you want to render (or use NULL to use the whole bitmap). You can also specify a destination rectangle to tell where on the screen it should be rendered (or use NULL and it will be calculated automatically).
The function works fine if both fields are set to NULL.
The problem I have is when the source rectangle is set to something. The result will be some weird "cracks" in the resulting image (hard to explain, but you can see a cross-like crack if the image is moving). What causes this?
Also, if the source rectangle is set to NULL and the destination rectangle is something, the resulting picture is weirdly scaled (mainly smaller than what it should be). I've checked though the code over and over unable to find the cause. Can some of you find any errors?
Thanks in advance!
BOOL GRAPHICS::DrawSprite(INT X, INT Y, INT SpriteNr, RECT* SrcRect, RECT* DestRect, DWORD VertexColor)
{
//Data
INT TextureWidth;
INT TextureHeight;
RECT TempRect1;
RECT TempRect2;
VERTEX* Vertices;
D3DSURFACE_DESC SurfaceDesc;
//Aborts if the sprite nr is out of bounds
if((SpriteNr < 0) || (SpriteNr >= NrOfTextures))
{
return FALSE;
}
//Get the texture width and height
TextureBuffer[SpriteNr]->GetLevelDesc(0, &SurfaceDesc);
TextureWidth = SurfaceDesc.Width;
TextureHeight = SurfaceDesc.Height;
//Prevent division by 0
if((TextureWidth == 0) || (TextureHeight == 0))
{
return FALSE;
}
//Locks the vertex buffer
VertexBuffer->Lock(0, 0, (void**)&Vertices, NULL);
//Upper left vertex
Vertices[0].Color = VertexColor;
Vertices[0].Z = 0.0f;
Vertices[0].RHW = 1.0f;
//Upper right vertex
Vertices[1].Color = VertexColor;
Vertices[1].Z = 0.0f;
Vertices[1].RHW = 1.0f;
//Lower right vertex
Vertices[2].Color = VertexColor;
Vertices[2].Z = 0.0f;
Vertices[2].RHW = 1.0f;
//Lower left vertex
Vertices[3].Color = VertexColor;
Vertices[3].Z = 0.0f;
Vertices[3].RHW = 1.0f;
//If no source rectangle is specified, assume the whole texture
if(SrcRect == NULL)
{
TempRect1.left = 0;
TempRect1.right = TextureWidth;
TempRect1.top = 0;
TempRect1.bottom = TextureHeight;
SrcRect = &TempRect1
//Upper left vertex
Vertices[0].TexX = 0.0f;
Vertices[0].TexY = 0.0f;
//Upper right vertex
Vertices[1].TexX = 1.0f;
Vertices[1].TexY = 0.0f;
//Lower right vertex
Vertices[2].TexX = 1.0f;
Vertices[2].TexY = 1.0f;
//Lower left vertex
Vertices[3].TexX = 0.0f;
Vertices[3].TexY = 1.0f;
}
else
{
//Upper left vertex
Vertices[0].TexX = (FLOAT)SrcRect->left / (FLOAT)TextureWidth;
Vertices[0].TexY = (FLOAT)SrcRect->top / (FLOAT)TextureHeight;
//Upper right vertex
Vertices[1].TexX = (FLOAT)SrcRect->right / (FLOAT)TextureWidth;
Vertices[1].TexY = (FLOAT)SrcRect->top / (FLOAT)TextureHeight;
//Lower right vertex
Vertices[2].TexX = (FLOAT)SrcRect->right / (FLOAT)TextureWidth;
Vertices[2].TexY = (FLOAT)SrcRect->bottom / (FLOAT)TextureHeight;
//Lower left vertex
Vertices[3].TexX = (FLOAT)SrcRect->left / (FLOAT)TextureWidth;
Vertices[3].TexY = (FLOAT)SrcRect->bottom / (FLOAT)TextureHeight;
}
//If no destination rectangle is specified, use the X & Y position to create one
if(DestRect == NULL)
{
TempRect2.left = X;
TempRect2.right = X + (SrcRect->right - SrcRect->left);
TempRect2.top = Y;
TempRect2.bottom = Y + (SrcRect->bottom - SrcRect->top);
DestRect = &TempRect2
}
//Upper left vertex
Vertices[0].X = (FLOAT)DestRect->left;
Vertices[0].Y = (FLOAT)DestRect->top;
//Upper right vertex
Vertices[1].X = (FLOAT)DestRect->right;
Vertices[1].Y = (FLOAT)DestRect->top;
//Lower right vertex
Vertices[2].X = (FLOAT)DestRect->right;
Vertices[2].Y = (FLOAT)DestRect->bottom;
//Lower left vertex
Vertices[3].X = (FLOAT)DestRect->left;
Vertices[3].Y = (FLOAT)DestRect->bottom;
//Unlocks the vertex buffer
VertexBuffer->Unlock();
//Sets the texture
if(FAILED(Direct3DDevice->SetTexture(0, TextureBuffer[SpriteNr])))
{
return FALSE;
}
//Draws the sprite
if(FAILED(Direct3DDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2)))
{
return FALSE;
}
//Return success
return TRUE;
}
EDIT: It's SOURCE tags, not CODE ^^
Games and Programming
http://www20.brinkster.com/magos818
[edited by - Magos on August 23, 2003 1:33:00 PM]
[edited by - Magos on August 23, 2003 1:33:38 PM]
[edited by - Magos on August 23, 2003 1:34:42 PM]