Help with this optimization
Posted
by
Milo
on Game Development
See other posts from Game Development
or by Milo
Published on 2012-08-28T22:29:31Z
Indexed on
2012/08/29
3:51 UTC
Read the original article
Hit count: 341
Here is what I do: I have bitmaps which I draw into another bitmap.
The coordinates are from the center of the bitmap, thus on a 256 by 256 bitmap, an object at 0.0,0.0 would be drawn at 128,128 on the bitmap.
I also found the furthest extent and made the bitmap size 2 times the extent.
So if the furthest extent is 200,200 pixels, then the bitmap's size is 400,400.
Unfortunately this is a bit inefficient.
If a bitmap needs to be drawn at 500,500 and the other one at 300,300, then the target bitmap only needs to be 200,200 in size.
I cannot seem to find a correct way to draw in the components correctly with a reduced size.
I figure out the target bitmap size like this:
float AvatarComposite::getFloatWidth(float& remainder) const
{
float widest = 0.0f;
float widestNeg = 0.0f;
for(size_t i = 0; i < m_components.size(); ++i)
{
if(m_components[i].getSprite() == NULL)
{
continue;
}
float w = m_components[i].getX() +
( ((m_components[i].getSprite()->getWidth() / 2.0f) *
m_components[i].getScale()) / getWidthToFloat());
float wn = m_components[i].getX() -
( ((m_components[i].getSprite()->getWidth() / 2.0f) *
m_components[i].getScale()) / getWidthToFloat());
if(w > widest)
{
widest = w;
}
if(wn > widest)
{
widest = wn;
}
if(w < widestNeg)
{
widestNeg = w;
}
if(wn < widestNeg)
{
widestNeg = wn;
}
}
remainder = (2 * widest) - (widest - widestNeg);
return widest - widestNeg;
}
And here is how I position and draw the bitmaps:
int dw = m_components[i].getSprite()->getWidth() * m_components[i].getScale();
int dh = m_components[i].getSprite()->getHeight() * m_components[i].getScale();
int cx = (getWidth() + (m_remainderX * getWidthToFloat())) / 2;
int cy = (getHeight() + (m_remainderY * getHeightToFloat())) / 2;
cx -= m_remainderX * getWidthToFloat();
cy -= m_remainderY * getHeightToFloat();
int dx = cx + (m_components[i].getX() * getWidthToFloat()) - (dw / 2);
int dy = cy + (m_components[i].getY() * getHeightToFloat()) - (dh / 2);
g->drawScaledSprite(m_components[i].getSprite(),0.0f,0.0f,
m_components[i].getSprite()->getWidth(),m_components[i].getSprite()->getHeight(),dx,dy,
dw,dh,0);
I basically store the difference between the original 2 * longest extent bitmap and the new optimized one, then I translate by that much which I would think would cause me to draw correctly but then some of the components look cut off.
Any insight would help.
Thanks
© Game Development or respective owner