2D engine done. Advice on optimisation, please?
Here is how I handle sprite drawing.
Code:
SpriteHandle Renderer2dSprite::draw(SpriteHandle handle,const RectangleI &screenRectangle, const RectangleI &textureRectangle,
float scale,float rotate,const ColorQuad &tint)
{
SpriteHandle encodedOffset=handle;
DynamicRenderablePrimitive *sCollection;
Material *sMaterial;
size_t spriteOffset;
if(encodedOffset==InvalidSpriteHandle)
{
assert(mCurrentSprites!=0);
sCollection=mCurrentSprites;
sMaterial=mCurrentMaterial;
spriteOffset=mCurrentSprites->getItemCount();
encodedOffset=encodeOffset(mCurrentMaterialIndex,spriteOffset);
if(spriteOffset>=mCurrentSprites->getItemCapacityCount())
{
mCurrentSprites->setCapacity(mCurrentSprites->getItemCapacityCount()*2);
}
mCurrentSprites->increaseItemCount();
}
else
{
getMaterialSpriteCollection(mSprites,handle,&sMaterial,&sCollection);
spriteOffset=decodeOffset(handle);
}
assert(sMaterial->getTextureUnit(0).getTexture()!=0);
const float texWidth=(float)sMaterial->getTextureUnit(0).getTexture()->getWidth();
const float texHeight=(float)sMaterial->getTextureUnit(0).getTexture()->getHeight();
Vector4 uvs(((float)textureRectangle.mX1)/texWidth,1.0f-((float)textureRectangle.mY1)/texHeight,
((float)textureRectangle.mX2+1)/texWidth,1.0f-((float)textureRectangle.mY2+1)/texHeight);
float cos_rot=1.0f;
float sin_rot=0.0f;
if(rotate!=0)
{
rotate=-rotate;
MathUtils::SinCos(rotate,sin_rot,cos_rot);
}
float width = ((float)(screenRectangle.mX2+1-screenRectangle.mX1))/2;
float height =((float)(screenRectangle.mY2+1-screenRectangle.mY1))/2;
float mid_u = ((float)screenRectangle.mX1)+width;
float mid_v = ((float)screenRectangle.mY1)+height;
width*=scale;
height*=scale;
const float cos_rot_w = cos_rot * width;
const float cos_rot_h = cos_rot * height;
const float sin_rot_w = sin_rot * width;
const float sin_rot_h = sin_rot * height;
float *vertexFData=(float*)sCollection->getItemData(spriteOffset);
unsigned int *vertexIData;
short *vertexSData=(short*)sCollection->getItemData(spriteOffset);
assert(vertexFData!=0);
*vertexSData++=(short)mid_u - cos_rot_w - sin_rot_h;
*vertexSData++=(short)mid_v - sin_rot_w + cos_rot_h;
*vertexSData++=(short)mCurrDepth;
vertexSData++;
vertexFData=(float*)(vertexSData);
*vertexFData++ = uvs.x;
*vertexFData++ = uvs.w;
vertexIData=(unsigned int*)(vertexFData);
*vertexIData++=tint.mColors[0];
vertexSData=(short*)(vertexIData);
*vertexSData++=(short)mid_u + cos_rot_w - sin_rot_h;
*vertexSData++=(short)mid_v + sin_rot_w + cos_rot_h;
*vertexSData++=(short)mCurrDepth;
vertexSData++;
vertexFData=(float*)(vertexSData);
*vertexFData++ = uvs.z;
*vertexFData++ = uvs.w;
vertexIData=(unsigned int*)(vertexFData);
*vertexIData++=tint.mColors[1];
vertexSData=(short*)(vertexIData);
*vertexSData++=(short)mid_u + cos_rot_w + sin_rot_h;
*vertexSData++=(short)mid_v + sin_rot_w - cos_rot_h;
*vertexSData++=(short)mCurrDepth;
vertexSData++;
vertexFData=(float*)(vertexSData);
*vertexFData++ = uvs.z;
*vertexFData++ = uvs.y;
vertexIData=(unsigned int*)(vertexFData);
*vertexIData++=tint.mColors[2];
vertexSData=(short*)(vertexIData);
*vertexSData++=(short)mid_u - cos_rot_w + sin_rot_h;
*vertexSData++=(short)mid_v - sin_rot_w - cos_rot_h;
*vertexSData++=(short)mCurrDepth;
vertexSData++;
vertexFData=(float*)(vertexSData);
*vertexFData++ = uvs.x;
*vertexFData++ = uvs.y;
vertexIData=(unsigned int*)(vertexFData);
*vertexIData++=tint.mColors[3];
return encodedOffset;
}
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| new engine in works. advice? | godexsoft | 4 | 3,793 |
Apr 7, 2012 07:25 AM Last Post: godexsoft |
|
| Advice on 3D Room engine? | devGamer | 4 | 3,410 |
Mar 21, 2010 10:05 AM Last Post: Skorche |
|

