Quote: "...and I need to rotate my sprite so it always looks at my cursor"
Just in case anyone was interested in how to do it in DBC, I wrote an image rotate function I posted on code snippets some time ago. Though the function creates a bit of unnecessary overhead (it could definitely be improved), the idea is still sound:
remstart
==============================================================
= Title : rotate sprite towards mouse
= Author : latch
= Date : 04/10/2012
= Update :
= Version: .01
==============================================================
Comments Rotate an image/sprite towards the mouse
==============================================================
remend
rem =============================================================
rem = SET UP DISPLAY
rem =============================================================
autocam off
set display mode 800,600,32
sync on
sync rate 60
rem =============================================================
rem = MAIN
rem =============================================================
gosub _flower_pot
cls 128
gosub _position_flower
do
gosub _point_sprite_at_mouse
sync
loop
end
rem =============================================================
rem = SUBROUTINES - PROCEDURES
rem =============================================================
_flower_pot:
ink rgb(64,64,64),0
box 0,0,128,128
ink rgb(200,200,255),0
box 4,4,124,124
ink rgb(0,160,0),0
box 60,38,68,124
for xr=0 to 11
for yr=0 to 6
ellipse 77,72,xr,yr
ellipse 51,72,xr,yr
next yr
next xr
anginc#=360.0/6.0
ang#=0-anginc#
ink RGB(255,255,0),0
for f=1 to 6
inc ang#,anginc#
x=16*cos(ang#)+64
y=16*sin(ang#)+34
for r=0 to 10
circle x,y,r
circle x,y-1,r
next r
next f
ink rgb(10,10,10),0
for r=0 to 11
circle 64,34,r
circle 63,34,r
next r
ink RGB(128,0,0),0
box 48,96,80,124
get image 1,0,0,129,129,1
sync
return
`================================================================
_position_flower:
sprite 1,-900,-900,1
spx=(screen width()/2)-(sprite width(1)/2)
spy=(screen height()/2)-(sprite height(1)/2)
sprite 1,spx,spy,1
return
`================================================================
_point_sprite_at_mouse:
rem get angle between image mouse and image
ang#=atanfull((sprite x(1)+(sprite width(1)/2))-mousex(),sprite y(1)-mousey())
rotate_img(1,2,3,1,2,1,ang#,2,xoffset,yoffset)
sprite 1,spx,spy,3
return
rem =============================================================
rem = FUNCTIONS
rem =============================================================
function rotate_img(imgin,imgdraw,imgout,mem1,mem2,bmp,angle#,matting,xoffset,yoffset)
remstart
rotate image
by latch
11/15/2008
imgin = the image to rotate - will be copied and not affected
imgdraw = the image to perform the drawing on so as not to affect
imgin or imgout
imgout = the number of the rotated image to create
mem1 = the source image memblock
mem2 = the target image memblock that is used to create imgout
bmp = the bitmap to draw on in the background
angle# = the angle to rotate the image (- clockwise / + counter)
matting = the background behind the rotated image.
0 is no matting. If the image is rotated beyond
imgin's original size, the corners will be clipped
1 is extra matting. The background of the created
image will be large enough to NOT clip the corners
if the image is rotated outside of imgin's original size
2 shrink image. Imgout will be small enough to fit
within imgin's original boundaries with no clipping
the xoffset and yoffset are used to change the rotation
center of the image. This can alos be used just to shift the
images position (will clip image areas out of bounds)
remend
make memblock from image mem1,imgin
wd1=memblock dword(mem1,0)
ht1=memblock dword(mem1,4)
dp=memblock dword(mem1,8)
bytes=dp/8
delete memblock mem1
rem if matting=1 create a larger image, if 2 use the values for later
rem now create a new black image that is the size of the diagonals
if matting >= 1
wd2=sqrt((wd1*wd1)+(ht1*ht1))
ht2=wd2
else
wd2=wd1
ht2=ht1
endif
create bitmap bmp,wd2+2,ht2+2
cls 0
rem center the old image onto the new black background
posx=(wd2-wd1)/2
posy=(ht2-ht1)/2
paste image imgin,posx,posy
get image imgdraw,0,0,wd2+1,ht2+1,1
sync
rem matting = 2 so shrink the matted image created for matting=1
if matting >=2
bmp2=bmp+1
while bitmap exist(bmp2)
inc bmp2
endwhile
create bitmap bmp2,wd1+1,ht1+1
copy bitmap bmp,0,0,wd2,ht2,bmp2,0,0,wd1,ht1
sync
endif
rem now make 2 memblocks. 1 for the original image, and one to copy to
if matting>=2
make memblock from bitmap mem1,bmp2
delete bitmap bmp2
else
make memblock from image mem1,imgdraw
endif
wd1=memblock dword(mem1,0)
ht1=memblock dword(mem1,4)
dp=memblock dword(mem1,8)
bytes=dp/8
make memblock mem2,12+((wd1*ht1)*bytes)
write memblock dword mem2,0,wd1
write memblock dword mem2,4,ht1
write memblock dword mem2,8,dp
rem rotate image one and copy the rotation information to memblock 2
cx=(wd1/2)+xoffset
cy=(ht1/2)+yoffset
if angle#=90 or angle#=270
cang#=0
else
cang#=cos(angle#)
endif
if angle#=180 or angle#=360
sang#=0
else
sang#=sin(angle#)
endif
for y=0 to ht1-1
for x=0 to wd1-1
rem target is the current x,y position of the image to draw as rotated
rem the source is the position from which to grab a color from
rem the original image. Because we never alter the original image,
rem errors will not accumulate in the rotation avoiding distorting
rem the image
targetx=x-cx
targety=y-cy
sourcex=((targetx*cang#)-(targety*sang#))+cx
sourcey=((targetx*sang#)+(targety*cang#))+cy
if sourcex < wd1 and sourcex >= 0 and sourcey < ht1 and sourcey >= 0
targetpixel=((x+(y*wd1))*bytes)+12
sourcepixel=((sourcex+(sourcey*wd1))*bytes)+12
copy memblock mem1,mem2,sourcepixel,targetpixel,bytes
else
if bytes=4
rem 32 bit
rem if the area is outside of the image make black
targetpixel=((x+(y*wd1))*bytes)+12
write memblock dword mem2,targetpixel,0
else
if bytes=2
rem 16 bit
rem if the area is outside of the image make black
targetpixel=((x+(y*wd1))*bytes)+12
write memblock word mem2,targetpixel,0
endif
endif
endif
next x
next y
make image from memblock imgout,mem2
delete memblock mem1
delete memblock mem2
delete bitmap bmp
endfunction
rem =============================================================
rem = DATA STATEMENTS
rem =============================================================
Enjoy your day.