|
image - handle of the image to rotate angle# - angle to rotate by, in degrees |
|
Rotates an image by a number of degrees, replacing it in memory. This is not a real-time command. The rotation is done in software across every pixel of every frame, and calling it inside your game loop will bring the frame rate to its knees. The way to have a spinning ship or a turning turret is to pre-rotate: at startup, loop through the angles you need, take a CopyImage of the original for each one, rotate the copy, and store the handles in an array. Then drawing is just DrawImage ship[angle],x,y. Thirty-six copies at ten degrees apart looks smooth enough for most games and costs a fraction of a second to build. Rotation happens around the image's handle, so call MidHandle first unless you specifically want it to swing around a corner. The image also grows to fit its rotated corners, which means ImageWidth and ImageHeight change - something to keep in mind if you were relying on a fixed size. It is destructive and cumulative. Rotating the same handle repeatedly by small amounts blurs it away to nothing, because each pass resamples pixels that were already resampled. Always rotate a fresh copy of the original. TFormFilter decides whether the rotated result is smoothed. Smoothing looks better on soft artwork but blends edge pixels towards the mask colour, which can leave a halo or eat into transparency on pixel art - turn it off for sprites with a hard-edged mask. See also: ScaleImage, ResizeImage, TFormFilter, CopyImage, MidHandle. |
; RotateImage Example
; -------------------
Graphics 640,480,0,2
SetBuffer BackBuffer()
; Crisp rotation keeps the mask colour clean (try 1 for filtered edges)
TFormFilter 0
; RotateImage is too slow to call every frame, so we pre-rotate
; 36 copies of the ship BEFORE the loop and page through them
player=LoadImage("media/player.bmp")
Dim spin(35)
For i=0 To 35
spin(i)=CopyImage(player)
RotateImage spin(i),i*10 ; each copy turned 10 degrees further
MidHandle spin(i) ; draw each rotated copy from its centre
Next
frame=0
delay_ms=40
timer=MilliSecs()
While Not KeyDown(1)
Cls
; [ / ] change the spin speed
If KeyHit(26) And delay_ms<200 Then delay_ms=delay_ms+20
If KeyHit(27) And delay_ms>20 Then delay_ms=delay_ms-20
; Step to the next pre-rotated copy
If MilliSecs()>timer+delay_ms Then
timer=MilliSecs()
frame=(frame+1) Mod 36
End If
DrawImage spin(frame),320,240
Text 0,0,"[ / ] : spin speed Esc: exit"
Text 0,20,"Pre-rotated with RotateImage - showing the "+(frame*10)+" degree copy"
Flip
Wend
End