Blitz3D+ Command Reference

AutoMidHandle enable

Parameters

enable - True to give images loaded from now on a centred handle, False to leave them anchored at their top left corner

Description

Makes images that are loaded from now on take their handle from the middle of the image.

An image's handle is the point inside the picture that lines up with the x,y you pass to DrawImage. Normally that point is the top left corner, so a 50x50 sprite drawn at 200,200 covers 200,200 to 250,250. Turn AutoMidHandle on and newly loaded images get their handle at width/2,height/2 instead, so the same call centres the sprite on 200,200.

That is almost always what you want for things that move: bullets, explosions, the player ship, pickups. Positioning by the centre means you can rotate a sprite, compare distances between sprites, or scale one, without redoing your maths. Call it once near the top of your program, before you load your sprites, and forget about it.

The catch: it only affects images loaded or created after the call. Images already in memory keep the handle they have, so if you flip this on halfway through you will need MidHandle on the ones you already loaded. It starts off (False) when your program begins, and it applies to LoadImage, LoadAnimImage and CreateImage alike.

Turn it back off before loading art you want anchored at the top left, such as HUD panels, backgrounds and tile sets, where 0,0 is the easier corner to think in.

Two different things get called a "handle" in these pages. This one is a position inside an image. The other is the number a load command hands back to identify the image itself.

See also: MidHandle, HandleImage, ImageXHandle, ImageYHandle, LoadImage, DrawImage.

Example

; AutoMidHandle Example
; ---------------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; AutoMidHandle affects images loaded AFTER it is called
AutoMidHandle False
corner=LoadImage("media/player.bmp")    ; handle at the default 0,0

AutoMidHandle True
centred=LoadImage("media/player.bmp")   ; handle at the image centre

angle#=0

While Not KeyDown(1)

    Cls

    ; Both ships are drawn at exactly the crosshair positions
    angle=angle+2
    y=240+30*Sin(angle)
    DrawImage corner,200,y
    DrawImage centred,440,y

    ; Crosshairs mark the draw positions
    Color 255,255,0
    Line 190,y,210,y
    Line 200,y-10,200,y+10
    Line 430,y,450,y
    Line 440,y-10,440,y+10

    Text 200,320,"AutoMidHandle False",True
    Text 440,320,"AutoMidHandle True",True

    Text 0,0,"Esc: exit"
    Text 0,20,"Same DrawImage position - only the automatic handle differs"

    Flip

Wend

End

Index