Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Draw a rotated or flipped image using the Surface API

11 views
Skip to first unread message

Guillermo Rodriguez Garcia

unread,
Jan 15, 2024, 7:04:56 PM1/15/24
to PlayN
Hi all,

What is the "standard" way to draw a rotated or flipped image using the surface API?
I am currently doing something like this:

surf.saveTx();
surf.translate(x, y);
surf.rotate(angle);
surf.drawCentered(texture, x, y);
surf.restoreTx();

but this seems a bit verbose (and not very gc friendly due to the saving and restoring of the transform). So I am wondering if there is there a better way?

Guillermo

Michael Bayne

unread,
Jan 15, 2024, 7:25:15 PM1/15/24
to pl...@googlegroups.com
Unfortunately there are no shortcuts when it comes to rotation. When an image is drawn, we have to include the full affine transform matrix that defines how it is drawn. You can avoid the GC by not saving the transform (which does indeed make a whole new Transform object) but by just undoing the changes you made after drawing the image:

surf.translate(x, y);
surf.rotate(angle);
surf.drawCentered(texture, 0, 0); // I assume you want 0, 0 here because you already translated to the origin
surf.rotate(-angle);
surf.translate(-x, -y);

That will just make the needed changes to the Surface's current transform, do the draw (which adds the image to an accumulating batch of graphics calls, saving the data from the current state of the transform matrix along with it), and then undoing those changes. So you're back to where you started and did no allocations (other than potentially those associated with the drawing batch, but batches also auto-expand (and then reuse) their internal buffers to try to avoid allocations every frame).

You can of course make a helper function to do this, but Surface itself tries to be fairly low-level and doesn't have a zillion helper functions that accomplish every combination of scaling, translating and rotating that you might want to do before rendering an image.


--

---
You received this message because you are subscribed to the Google Groups "PlayN" group.
To unsubscribe from this group and stop receiving emails from it, send an email to playn+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/playn/3da57101-a66b-49a8-9183-83c148c199a9n%40googlegroups.com.


--

Guillermo Rodriguez Garcia

unread,
Jan 15, 2024, 8:47:13 PM1/15/24
to PlayN
Hi Michael,

Thanks for the hint about undoing the transform.

Re. the Surface API: since there are a number of draw() methods that take w/h, this already takes care of scaling and flipping (by passing negative width and/or height values). So I guess it's only the rotation that is missing. I understand that you want to avoid adding a zillion new methods but I wonder if adding overloads with an "angle" parameter would be helpful in general. I'm looking at other libraries such as libsdl or Lua's Love2D:


Best,

Guillermo
Reply all
Reply to author
Forward
0 new messages