Drawing sprites with more complexity...

Monkey Forums/Monkey Programming/Drawing sprites with more complexity...

Raptisoft(Posted 2011) [#1]
Hi all,
I just asked a question about packing sprites, and it was answered. Unfortunately, the answers made more questions, which I'm addressing here!

When I pack sprites, I crop out blank space around the edges. This is because if I render a lot of rotations on, say, a wizard in 3DSMax, the actual area of graphics I need is often smaller than the render square (especially if the wizard is holding a long staff that points only in one direction in some renderings).

So I crop out the blank space, and I give my helper file an offset number, saying, when drawing this sprite, offset it x,y to make it act like it's the real size.

Easy enough to do in Monkey....

UNLESS! I want to rotate or scale that sprite. In Monkey, it's rotating and scaling from the center, so when I do this, my careful collection of rotated images in 3DSMax wobble all over the place, because their "center" is different, since I can't use that offset number to help rotate or scale.

So, is there (or can I request) a way to draw an image onto a quad? Usually what I do is I draw my sprite using a quad that extends from -spritewidth/2,-spriteheight/2 to spritewidth/2,spriteheight/2. I move that quad around via the offset so that it's always rotating around 0,0 where the original graphic was centered before cropping (that's how I get away with cropping the blank space off the image for texture packing, but still centering it as if it were not cropped).

Thanks!
John


skid(Posted 2011) [#2]
For my spritesheets I use a combination of GrabImage and SetHandle to extract the cells (pixels remain on sheet), and am then free to scale and rotate my cells any which way.


Raptisoft(Posted 2011) [#3]
Hi Simon,
Thanks... my only question would be, how expensive is SetHandle? It seems I'll be setting that handle... CONSTANTLY.


DruggedBunny(Posted 2011) [#4]
Here's the source code for SetHandle (see modules/mojo/graphics.monkey):

	Method SetHandle( tx#,ty# )
		Self.tx=tx
		Self.ty=ty
		Self.flags=Self.flags & ~MidHandle
	End


Not too expensive then!

You might also be able to do something with PushMatrix/Translate/Rotate/PopMatrix to get your rotated offset, though it'll depend on the target as to whether or not that's feasible. (See the Matrix Rocks sample.)


Raptisoft(Posted 2011) [#5]
Hrm... what's wrong with my syntax here, any idea?

mSpritePage=LoadImage("test.png",1,Image.DefaultFlags)
mCharacter=mSpritePage.GrabImage(128,128,50,50);
mCharacter.SetHandle(25,25);


It fails with a syntax error on the GrabImage call (also fails if I specify the image count and image flags).

Also, I assume GrabImage just makes a new pointer to the spritesheet, right? I.E. it's not making a whole new texture, and therefore I incur texture swap slowdowns, does it?


skid(Posted 2011) [#6]
what are those weird commas with spots on them ? :)

and yes


Raptisoft(Posted 2011) [#7]
Haha... sorry to waste your time. Those semicolons are completely automatic to me;


Wagenheimer(Posted 2011) [#8]
I also want to know! =)

"GrabImage just makes a new pointer to the spritesheet, right?"


Wagenheimer(Posted 2011) [#9]
Is it possible to Flip the image?


Wagenheimer(Posted 2011) [#10]
"Is it possible to Flip the image?"

Scale = -1 does the trick! I just need to adjust the X position to X+Image.Width()


Yahfree(Posted 2011) [#11]
Not sure if I understand the question but...
Function SetRotation (X:Float, Y:Float, angle:Float)
	PushMatrix
	Translate X, Y
	Rotate angle
	Translate -X, -Y
End

Function ResetRotation ()
	PopMatrix
End


Then.. do something like this:

Rotpointx = x+XOFFSET_TO_IMAGE_CENTER
Rotpointy = y+YOFFSET_TO_IMAGE_CENTER
SetRotation(Rotpointx,Rotpointy,angle)
DrawImageRect ... blahblah
ResetRotation()


would that work?


Wagenheimer(Posted 2011) [#12]
Thanks Yahfree, it works!

Well, I have created a texture packer program and I am creating some monkey classes To load and use this packed textures easyly, and I will relesse everything here soon! :)