simple transform stack question

You are viewing a static copy of the old 2DBoy forum, which closed in 2010. It is preserved here for historical interest, but it is not possible to reply to topics. For more recent discussion about World of Goo, visit our new forum.
simple transform stack questiongmayer07/15/2009 - 10:46

hi all

I'm just messing around with this in my (very limited) spare time.
(HUGE thanks and hats off the to 2dboy guys for releasing this)

I have a quick question about the transform stack when drawing:

Just to get something running, I drew a line from roughly the screen center to a point I was moving around with the left stick.  Worked great.

Code looked like this:

  <br />    x0 = (int)(mOldPos.x);<br />    y0 = (int)(mOldPos.y);<br />    x1 = (int)(mPos.x);<br />    y1 = (int)(mPos.y);<br />    <br />    g->drawLine(x0, y0, x1, y1);

mOldPos never changes (for now - always 300,300)
mPos changes based on what you're doing with the stick - changed in ::update

Then I wanted to draw a sprite, so I loaded an image, and did this:
<br />    if( mTestImage )<br />    {        <br />        g->pushTransform();<br />        g->translate(mPos.x, mPos.y);<br />        g->drawImage(mTestImage);<br />        g->popTransform();<br />        <br />    }<br /><br />    int x0, x1, y0, y1;    <br />  <br />    x0 = (int)(mOldPos.x);<br />    y0 = (int)(mOldPos.y);<br />    x1 = (int)(mPos.x);<br />    y1 = (int)(mPos.y);<br />    <br />    g->drawLine(x0, y0, x1, y1);<br />    <br /> 
So I have the image drawing above the line, wrapped with the push and pop.

I would have expected the image to be moving around with the end point of the line, drawn overtop of it.

Instead, the image is moved around as expected, but the entire line seems to be translated on the screen about 300,300 (the non-changing value of mOldPos) down to roughly the bottom right corner.

I feel like I'm missing something simple and obvious here.

Any help would be much appreciated.

Re: simple transform stack questiongmayer07/15/2009 - 10:51

So... a few seconds after this post, I changed the order of the line and the sprite rendering - it makes sense that things drawn subsequently go over top of things drawn prior [doh!].

So now I indeed have my line getting drawn first, then the sprite on top, both where I expect.

But I still do not understand in the earlier case why the line moved - I would have expected only the draw order to change?  ???


Re: simple transform stack questionSoultaker07/15/2009 - 11:24

This could well be a bug in the framework. The way I understand drawLine() was supposed to work is that it draws a line on screen using absolute screen coordinates, bypassing the transform stack completely. The Windows implementation doesn't seem to clear the current transformation, which is probably what causes the behaviour you observed. (This occurs despite your call to popTransform() because changes to the transform stack don't take effect until WinGraphics::updateTransform() is called, which isn't done by drawLine() because it wasn't supposed to go through the transform stack anyway!)

In World of Goo the line drawing functionality is only used to visualize debug information. I'm not sure why that works; maybe the transformation has been reset as a side-effect of an earlier drawing call.

Re: simple transform stack questiongmayer07/15/2009 - 14:58

Ahhhh.... makes sense.

Something to keep in mind.  Thanks for the reply.