3736
Tutorials / Re: change hotspot for zoomsprite?
« on: 2011-Apr-22 »Code: (glbasic) [Select]
FUNCTION PaintImage: Name$, x#, y#, alpha#, zoom#, xspot, yspot, yy#
LOCAL h#, w#, img, hh#, ww#, xx#, yy#
x=20
y=20
xspot=1
yspot=1
IF alpha#=0 THEN RETURN
IF Name$=0
// img=GetStr$(Name$, "Sprites") ' Just a name based sprite function rather than number. Not need for test here at all.
ELSE
img=Name$
ENDIF
GETSPRITESIZE img, w, h
// rect maths <- This code math the box where the sprite should been placement
// That one works perfecty as its should. I does this because I might later using scaling#
// (different than zoom, which was FOR resolution scaling).
IF xspot=-1 THEN PX=x#
IF xspot=0 THEN PX=ScreenWidth/2.0-(w#*zoom#/2.0)+x#
IF xspot=1 THEN PX=ScreenWidth-w#*zoom#-x#*zoom#
IF yspot=-1 THEN PY=y#
IF yspot=0 THEN PY=ScreenHeight/2.0-(h*zoom#/2.0)+y#
IF yspot=1 THEN PY=ScreenHeight-h#*zoom#-y#*zoom#
PW=w*zoom#
PH=h*zoom#
// test box using drawrect, where the sprite should been draw directly over.
ALPHAMODE 0.2
DRAWRECT PX, PY, PW, PH, RGB(255, 255, 255)
// some alpha code in that way I prefer. Not a issue in the real alphamode, its just me :-D
IF alpha#>0
alpha#=0-alpha#
ELSE
alpha#=-alpha#
ENDIF
ALPHAMODE alpha#
// draw a sprite top on the drawrect box, but its slighty off. Also some rotation support, not finished, but dont think about it.
SELECT ROTATE
CASE 180
PX=ScreenWidth-PX-PW
PY=ScreenHeight-PY-PH
CASE 0
ZOOMSPRITE img, PX#-((1-zoom#)*w#)/2, PY#-((1-zoom#)*h#)/2, PW#/w#, PH#/h#
DEFAULT
ENDSELECT
ENDFUNCTION
Tried it, seen its works.
Its was somewhere the only one I diddent tried out (1-zoom#). So close :-D.
The only issue is its still sometimes off with one pixel, which should been gone with a real top/left hotspot to avoid that, but elsewise thanks.