GLBasic forum

Main forum => Tutorials => Topic started by: spacefractal on 2011-Apr-22

Title: change hotspot for zoomsprite?
Post by: spacefractal on 2011-Apr-22
After trying to math how to math a sprite, based on topleft, center or rightcenter hotsots after about 10-15 hours, then I gave up.

Is is possible to change the hotspot on zoomsprite as well other sprite commands to set on the top left, which would been LOTS easier to math the placement of them? Something a missing command?

Here is the funtion I try to create. The box draw perfect, but zoomsprite is the issue, its not setting on the box as it should:

Code (glbasic) Select

FUNCTION PaintImage: Name$, x#, y#, alpha#, zoom#, xspot, yspot, yy#
zoom#=0.5
x=0
y=0
xspot=1
yspot=-1
IF alpha#=0 THEN RETURN
IF alpha#>0
alpha#=0-alpha#
ELSE
alpha#=-alpha#
ENDIF
LOCAL h#, w#, z#
LOCAL img, hh#, ww#
IF alpha#<>0 THEN ALPHAMODE alpha#
IF Name$=0
img=GetStr$(Name$, "Sprites")
ELSE
img=Name$
ENDIF
GETSPRITESIZE img, w, h

// rect maths
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#

// ALPHAMODE 0.2
DRAWRECT PX, PY, PW, PH, RGB(255, 255, 255)


SELECT ROTATE
CASE 180
PX=ScreenWidth-PX-PW
PY=ScreenHeight-PY-PH
CASE 0
ZOOMSPRITE img, PX, PY, zoom#, zoom#
DEFAULT
ENDSELECT
ENDFUNCTION


I use this for scaling graphics for any resolutions.

PS. I use name rather than number for ease programmering, so you can coomand the getspr$ out without issue.

PPS. YY# does nothing at all, and is just a placeholder to been removed later.
Title: Re: change hotspot for zoomsprite?
Post by: ampos on 2011-Apr-22
((1-zoom)*sprwidth)/2

Thats how you get the "new" x=0
Title: Re: change hotspot for zoomsprite?
Post by: spacefractal 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.