# Help with this optimization [closed]

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

Essentially, imagine you have a plane. That plane starts at 0,0 which will be the center of the target bitmap. By target bitmapo I mean render target / framebuffer object. I'm drawing avatar components into a bitmap, then I render that bitmap to the screen.

The way it currently works is by finding the furthest the plane will extend. So the longest extent might be at 600,700 on the plane. In order for an object at 0,0 to land at the center of this bitmap while still being able to see everything, the dimensions must be longest extent * 2, so the bitmap that everything is drawn into is 1200,1400. The longest extent may also be on the other side of the plane. If the longest extent on the left was -800,-800, then the target bitmap should actually be 1600,1600 in size.

The problem is this, that tends to waste a lot of video memory. You might have a character whose a lot on 1 side of the plane but barely on the other. Thus, if you have an object at 100,100 and an object at -200,-200, then the effective space you need is roughly 300,300 to display this and not 400,400 which the current method does. To correctly display this, I need to change the size of the target bitmap, offset the position of the components in the bitmap, and offset where the final result is drawn on screen so that it appears the object at 0,0 is at the center of the screen.

-
Draw it on a piece of paper, that helps surprisingly often ) – Krom Stern Aug 29 '12 at 5:02
and please show that drawing to us, i can't even imagine what is the problem you are facing. – Gajoo Aug 29 '12 at 21:55
I too am having trouble understanding the exact problem. Are you trying to only render the intersection between the two bitmaps being drawn at 500,500 and 300,300? What do you exactly mean by "Target bitmap only needs to be 200,200"? If you can edit the question we all may be able to provide more help. – Dean Knight Aug 30 '12 at 12:28
@Dean Knight see edit. – Milo Aug 30 '12 at 16:09
"I need to change the size of the target bitmap..." It sounds you already know what you need to do, so what exactly is the problem? – CiscoIPPhone Aug 30 '12 at 16:54
show 1 more comment

## closed as too localized by Sean Middleditch, bummzack, Trevor Powell, Gajoo, Mr. BeastMar 16 at 0:10

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.