SetRotation-Compliant DrawImageSubRect?

BlitzMax Forums/BlitzMax Programming/SetRotation-Compliant DrawImageSubRect?

zoqfotpik(Posted 2015) [#1]
I'm drawing out of a spritesheet. I would like the images to be rotated as expected by SetRotation.

The DrawImageSubRect routine I am using is from someone's code in the archives, I forget who did it. Any thoughts?

Is there any point to using spritesheets instead of individual images in animation strips on modern machines?

Is that the simplest way of doing this?

As it is, there is a viewport clipping out the unwanted parts of the spritesheet, but as the image rotates, it rotates in and out of the viewport. Do I want setorigin and sethandle?

Function DrawImageSubRect(Image:TImage, DrawX#, DrawY#, PartX#, PartY#, PartWidth#, PartHeight#, Frame# = 0)
' Not my code, someone else here did this a long time ago.  Draws a sub-rectangle of a given
' image to the screen.
	Local OldX:Int
	Local OldY:Int
	Local OldWidth:Int
	Local OldHeight:Int
	
	Local ViewportX:Int = DrawX
	Local ViewportY:Int = DrawY
	
	' Save current viewport settings
	GetViewport(OldX, OldY, OldWidth, OldHeight)
	
	' Calculate viewport coordinates based on image's handle	
	If Image.Handle_X Then
		Local PercentX:Float
		PercentX = Float(Image.Handle_X) / Float(Image.Width)
		ViewportX = DrawX - (PercentX * PartWidth)
	EndIf
	If Image.Handle_Y Then
		Local PercentY:Float
		PercentY = Float(Image.Handle_Y) / Float(Image.Height)
		ViewportY = DrawY - (PercentY * PartHeight)
	EndIf
	
	SetViewport(ViewportX, ViewportY, PartWidth, PartHeight)
	DrawImage(Image, DrawX-PartX, DrawY-PartY, Frame)
	
	' Restore old viewport settings
	SetViewport(OldX, OldY, OldWidth, OldHeight)
End Function



GW(Posted 2015) [#2]
Image frames are lightweight and you can construct them manually using pixmap.window()
Take a look at the code for LoadImage, LoadAnimImage, and http://www.blitzbasic.com/codearcs/codearcs.php?code=2002

Alternatively, you can use the textured poly code from the archive to texture a rectangle explicitly.