News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Misc Graphics Functions

Started by kevin, February 28, 2022, 06:50:25 PM

Previous topic - Next topic

kevin

  Misc Graphics Functions

    This thread is for those small graphic functions snippets that handy but really warrant their own thread..



    Related Threads:

            - Image Functions
            - Bordered Box






RainBoxCls()   Function


    This function alpha blend a pair of RGB colours drawing them to the current surface as a classic RainBow() effect.    

    Commands used:  RgbAlphaBlend, BoxC

PlayBASIC Code: [Select]
   setfps 10

StartRGB = $223344
EndRGB = $f000ff
frame =10

do

RainBowCls(StartRGB,EndRGB)

frame--
if frame<0
StartRGB = EndRGB
EndRGB = RndRGB()
Frame=10
endif


sync
loop spacekey()

explicit true

Function RainBowCLS(StartRGB,DestRGB)

local lp,w,h,Scaler#
w=getsurfacewidth()
h=getsurfaceheight()

Scaler#=100.0/h
for lp =0 to h-1
local NewRGB=RGBAlphaBlend(StartRGB,DestRGB, lp*Scaler#)
boxc 0,lp,w,lp+1,true,NewRGB
next

EndFunction

explicit false




    -- Get Source Code PasteBin

kevin




DrawCenteredImage()   Function


     This function allows the use to draw an image while controlling the X and Y handles as a pair scalers hidden in the FLAG parameter.  Since FLAGS is 32bit and we're only using the bottom bit (bit zero) to toggle if we're rendering the image solid or transparent; we can store other data in this parameter. 

     So what i've done is store the X scaler is bits 24-17   and Y Scaler in bits 16 to 8;  which means we can use the RGB commands to PACK and unPACK the separate values.  The only limitation is these values are limited to a range of 0 to 255.  So we're using them as scalers against the images size to control the handle.   

     Commands used:  DrawImage, RGB, RGBR,RGBB, CLS

PlayBASIC Code: [Select]
   cls $ff
getimage 1,0,0,128,128


do
cls

mx=mousex()
my=mouseY()


XOffset-=LeftKey()
XOffset+=RightKey()
XOffset=XOffset and 255

YOffset-=UpKey()
YOffset+=DownKey()
YOffset=YOffset and 255

print "X offset:"+str$(Xoffset)
print "Y offset:"+str$(Yoffset)

drawcenteredImage(1,mx,my, rgb(Xoffset,Yoffset,0))

sync
loop spacekey()




Function DrawCenteredImage(Thisimage,X,Y,Flag)
if getImageStatus(Thisimage)
iw=getimagewidth(ThisImage)
ih=getimageheight(ThisImage)
// Read Offsets X offset

XOffset_Scaler#=RgbR(Flag)/255.0
YOffset_Scaler#=RgbG(Flag)/255.0

X=X -(iw*XOffset_Scaler#)
Y=Y -(IH*YOffset_Scaler#)

Drawimage ThisImage ,X , y, flag and 1

endif
EndFunction