Post by Rod on Dec 20, 2018 9:40:15 GMT -5
This has been discussed recently. There is a complex SetWorldTransform() api call that rotates graphics or we can do it natively, well sort of. This code scans the graphic using simple circular trig functions and then redraws it at a different angle. I sample twice to get rid of the moire pattern interference.
The graphic has to be centred and has to be contained within a square box and has to be rotatable within that square box. It saves twelve rotations at the moment. This could be improved, the background color of the original graphic need not be black, the code could be improved to use any color and output a black/white mask.
The graphic has to be centred and has to be contained within a square box and has to be rotatable within that square box. It saves twelve rotations at the moment. This could be improved, the background color of the original graphic need not be black, the code could be improved to use any color and output a black/white mask.
nomainwin
WindowWidth=800
WindowHeight=600
UpperLeftX=(DisplayWidth-WindowWidth)/2
UpperLeftY=(DisplayHeight-WindowHeight)/2
menu #1, "&File", "&Open Sprite",[openSprite],_
"&Save As...",[saveAs],|,"E&xit",[quit]
open "Rotate and Mask Sprite" for graphics_nsb as #1
#1 "down ; trapclose [quit]"
hWindow=hwnd(#1)
CallDll #user32, "GetDC",_
hWindow as ulong,_
hDC as ulong
wait
[quit]
CallDll #user32, "ReleaseDC",_
hWindow as ulong,_
hDC as ulong,_
r as long
close #1
end
[openSprite]
if hBitmap<>0 then
unloadbmp ("bm")
#1 "cls"
end if
filedialog "Open Sprite","*.bmp",bitmap$
if bitmap$="" then wait
loadbmp "bm" , bitmap$
hBitmap=hbmp("bm")
bmpheight=HeightBitmap(bitmap$)
bmpwidth=WidthBitmap(bitmap$)
midx=int(bmpwidth/2)
midy=int(bmpheight/2)+bmpheight
radi=int(bmpwidth/2)
#1 "fill black"
#1 "drawbmp bm 0 ";bmpheight
for angle= 30 to 330 step 30
newx=newx+bmpwidth
call rotate angle,newx,midx,midy,radi, hWindow, hDC
next
call MakeMask bmpwidth, bmpheight, hWindow, hDC
wait
[saveAs]
#1 "getbmp SpriteMask 0 0 ";bmpwidth;" ";2*bmpheight
filedialog "Save As... ","*.bmp",savefile$
if savefile$="" then wait
newx=0
for a=0 to 330 step 30
#1 "getbmp SpriteMask ";newx;" 0 ";bmpwidth;" ";2*bmpheight
bmpsave "SpriteMask",left$(savefile$,len(savefile$)-4)+str$(a)+".bmp"
newx=newx+bmpwidth
next
wait
'************FUNCTIONS******************
function WidthBitmap(name$)
open name$ for input as #pic
pic$=input$(#pic,29)
close #pic
WidthBitmap = asc(mid$(pic$,19,1)) + _
(asc(mid$(pic$,20,1)) * 256)
end function
function HeightBitmap(name$)
open name$ for input as #pic
pic$=input$(#pic,29)
close #pic
HeightBitmap = asc(mid$(pic$,23,1)) + _
(asc(mid$(pic$,24,1)) * 256)
end function
sub rotate angle, newx, midx, midy, radi, hWindow, hDC
for d = 0 to radi
for n= 1 to 360 step .5
scan
x=int(midx-(d*sin(n/57.29577951)))
y=int(midy-(d*cos(n/57.29577951)))
CallDll #gdi32, "GetPixel",_
hDC as ulong,_
x as long,_
y as long,_
pColor as long
a=n+angle mod 360
x=int(midx-(d*sin(a/57.29577951)))
y=int(midy-(d*cos(a/57.29577951)))
x=x+newx
CallDll #gdi32, "SetPixel",_
hDC as ulong,_
x as long,_
y as long,_
pColor as long,_
r as long
next n
next d
end sub
sub MakeMask wide, high, hWnd,hDC
white=(255*256*256)+(255*256)+255
black=0
for x = 0 to 12*wide-1
for y = high to 2*high-1
CallDll #gdi32, "GetPixel",_
hDC as ulong,_
x as long,_
y as long,_
pColor as long
if pColor=black then
newColor=white
else
newColor=black
end if
j=y-high
CallDll #gdi32, "SetPixel",_
hDC as ulong,_
x as long, _
j as long, _
newColor as long, _
r as long
next y
next x
end sub