As you know, The new 107 commands allow the manipulation of images using the getImage() command.
I've been having a play around with this to see what can be done.
Before we can get to complex stuff, we need to create some primitive shapes, so I've whipped up some routines to create polygons, masks and gradients of various types.
The code is in the form of a number of self contained functions, each of which does a specific thing.
Combined, you can do all sorts of neat things with them
Absolutely no media was harmed in the making of this.
// Get Device Screen Dimensions
thisWidth# = getDeviceWidth()
thisHeight# = getDeviceHeight()
// Set Resolution to match
setVirtualResolution( thisWidth# , thisHeight# )
// Find Smallest Edge
if thisWidth# < thisHeight#
thisSmall# = thisWidth#
else
thisSmall# = thisHeight#
endif
CreateResultSprites( thisSmall# , 18 , 6 ) ` Create sprites to show results
// Create images for various types and combinations
squareOutline = CreatePolyOutlineImage( 0 , thisSmall# , thisSmall# * .95 )
` Create an outline Image of a square
circleImage = CreateCircleImage( thisSmall# ) ` Create a Circle Image
circleOutline = CreatePolyOutlineImage( circleImage , thisSmall# , thisSmall# * .95 )
` Create an outline Image of the circle
circleMask = CreateMaskImage( circleImage , thisSmall# ) ` Create a mask negative of the circle
triangImage = CreateRotPolyImage( thisSmall# , 3 ) ` Create a Triangle Image
triangOutline = CreatePolyOutlineImage( triangImage , thisSmall# , thisSmall# * .95 )
triangMask = CreateMaskImage( triangImage , thisSmall# ) ` Create a mask negative of the Triangle
rsquareImage = CreateRotPolyImage( thisSmall# , 4 )
rsquareOutline = CreatePolyOutlineImage( rsquareImage , thisSmall# , thisSmall# * .95 )
rsquareMask = CreateMaskImage( rsquareImage , thisSmall# ) ` Create a mask negative of the Triangle
rPentImage = CreateRotPolyImage( thisSmall# , 5 )
rPentOutline = CreatePolyOutlineImage( rPentImage , thisSmall# , thisSmall# * .95 )
rPentMask = CreateMaskImage( rPentImage , thisSmall# ) ` Create a mask negative of the Triangle
gradImage = CreateGradientImage( thisSmall# , 0) ` Create an opaque gradient image
transGradImage = CreateGradientImage( thisSmall# , 1 ) ` Create a transparent gradient image
transRevGradImage = CreateRevGradientImage( thisSmall# ) ` Create a reverse transparent gradient image
gradPentImage = CreateMaskedGradientImage( transGradImage , rPentMask , thisSmall# , 0 )
` Create a Gradient Pentagon - using the mask
transGradPentImage = CreateMaskedGradientImage( transGradImage , rPentMask , thisSmall# , 1 )
` Create another with the gradient transprency
// Apply created images to sample sprites
setSpriteImage( sprite[ 1 ] , squareOutline )
setSpriteImage( sprite[ 2 ] , circleImage )
setSpriteImage( sprite[ 3 ] , circleOutline )
setSpriteImage( sprite[ 4 ] , circleMask )
setSpriteImage( sprite[ 5 ] , triangImage )
setSpriteImage( sprite[ 6 ] , triangOutline )
setSpriteImage( sprite[ 7 ] , triangMask )
setSpriteImage( sprite[ 8 ] , rsquareImage )
setSpriteImage( sprite[ 9 ] , rsquareOutline )
setSpriteImage( sprite[ 10 ] , rsquareMask )
setSpriteImage( sprite[ 11 ] , rPentImage )
setSpriteImage( sprite[ 12 ] , rPentOutline )
setSpriteImage( sprite[ 13 ] , rPentMask )
setSpriteImage( sprite[ 14 ] , gradImage )
setSpriteImage( sprite[ 15 ] , transGradImage )
setSpriteImage( sprite[ 16 ] , transRevGradImage )
setSpriteImage( sprite[ 17 ] , gradPentImage )
setSpriteImage( sprite[ 18 ] , transGradPentImage )
setClearColor( 0 , 0 , 96 ) ` Set background to be not black to show transparency
dummy = createSprite( triangImage )
setSpriteSize( dummy , thisSmall# , thisSmall# )
setSpriteOffset( dummy , thisSmall# / 2.0 , thisSmall# / 2.0 )
do
if getRawKeyState( 27) = 1 then exit
thisAngle# = timer() * 20.0 ` calculate an angle from the timer
setSpriteAngle( sprite[ 0 ] , thisAngle# ) ` Rotate sample sprite 0 to show transparency
setSpriteAngle( dummy , thisAngle# ) ` Rotate sample sprite 0 to show transparency
Sync()
loop
end
// *** Creates an image of a Circle of a specified size ***
function CreateCircleImage( thisSize# )
setClearColor( 0 , 0 , 0 ) ` Set screen to black
clearscreen()
thisRadius# = thisSize# / 2.0 ` Radius of circle is half the size
thisSquare# = sqrt( thisRadius# * thisRadius# * 2.0 ) ` Calculate size of a square that fits in circle
thisHalf# = thisSquare# / 2.0
thisSprite = createSprite(0) ` Blank sprite for the square
setSpriteSize( thisSprite , thisSquare# , thisSquare# )
setSpriteOffset( thisSprite , thisHalf# , thisHalf# )
setSpritePositionByOffset( thisSprite , thisRadius# , thisRadius# )
` Position sprite in middle of circle
thisStep# = 90.0 / thisRadius# ` Work out rotation step based on size
thisAngle# = 0.0 ` Set initial angle to zero
while thisAngle# < 90.0 ` Repeat for 90 degrees
setSpriteAngle( thisSprite , thisAngle# ) ` Set sprite angle
drawSprite( thisSprite ) ` Draw the sprite to the buffer
thisAngle# = thisAngle# + thisStep# ` Increase angle
endwhile
thisInt = getImage( 0 , 0 , thisSize# , thisSize# ) ` Grab the resulting Image
setImageMask( thisInt , thisInt , 4 , 1 , 0 , 0 ) ` Add transparency based on colour
deleteSprite( thisSprite )
clearscreen()
endfunction thisInt
// Creates a Polygon for Rotation within a Specified Size
function CreateRotPolyImage( thisSize# , thisSides )
setClearColor( 255 , 255 , 255 ) ` Set screen colour to white
clearscreen()
thisStep# = 360.0 / thisSides
thisRadius# = thisSize# * 0.5
thisOffset# = thisRadius# * cos( thisStep# / 2.0 )
thisSpriteTop# = thisRadius# + thisOffset#
thisAngle# = 0.0
thisSprite = createSprite(0) ` Blank sprite
setSpriteSize( thisSprite , thisSize# , thisSize# )
setSpriteOffset( thisSprite , thisRadius# , -thisOffset# )
setSpritePosition( thisSprite , 0.0 , thisSpriteTop# )
` position below the center
setSpriteColor( thisSprite , 0 , 0 , 0 , 255 ) ` Set sprite colour to black
for i=1 to thisSides
setSpriteAngle( thisSprite , thisAngle# )
drawSprite( thisSprite ) ` Draw the sprite to the buffer
thisAngle# = thisAngle# + thisStep#
next i
thisInt = getImage( 0 , 0 , thisSize# , thisSize# ) ` Grab the resulting Image
setImageMask( thisInt , thisInt , 4 , 1 , 0 , 0 ) ` Add transparency based on colour
deleteSprite( thisSprite )
setClearColor( 0 , 0 , 0 ) ` Restore screen colour and clear backbuffer
clearscreen()
endfunction thisInt
// Creates an outline from a polygon image
function CreatePolyOutlineImage( thisImage , thisSize# , thisInner# )
setClearColor( 0 , 0 , 0 ) ` Set screen to black
clearscreen()
thisSprite = createSprite( thisImage ) ` Create a sprite from the image
setSpriteSize( thisSprite , thisSize# , thisSize# )
setSpriteOffset( thisSprite , thisSize# / 2.0 , thisSize# / 2.0 )
setSpritePositionByOffset( thisSprite , thisSize# / 2.0 , thisSize# / 2.0 )
drawSprite( thisSprite ) ` Draw the sprite to the buffer
setSpriteColor( thisSprite , 0 , 0 , 0 , 255 ) ` Set sprite colour to black
setSpriteSize( thisSprite , thisInner# , thisInner# ) ` Change size to inner size
setSpriteOffset( thisSprite , thisInner# / 2.0 , thisInner# / 2.0 )
drawSprite( thisSprite ) ` Draw the sprite to the buffer
thisInt = getImage( 0 , 0 , thisSize# , thisSize# ) ` Grab the resulting Image
setImageMask( thisInt , thisInt , 4 , 1 , 0 , 0 ) ` Add transparency based on colour
deleteSprite( thisSprite )
clearscreen()
endfunction thisInt
// *** Creates an inverse Mask from an Image ***
function CreateMaskImage( thisImage , thisSize# )
setClearColor( 255 , 255 , 255 ) ` Set screen colour to white
clearscreen()
thisSprite = createSprite( thisImage ) ` Create a sprite from the image
setSpriteSize( thisSprite , thisSize# , thisSize# )
setSpriteColor( thisSprite , 0 , 0 , 0 , 255 ) ` Set sprite colour to black
drawSprite( thisSprite ) ` Draw the sprite to the buffer
thisInt = getImage( 0 , 0 , thisSize# , thisSize# ) ` Grab the resulting Image
setImageMask( thisInt , thisInt , 4 , 1 , 0 , 0 ) ` Add transparency based on colour
deleteSprite( thisSprite )
setClearColor( 0 , 0 , 0 ) ` Restore screen colour and clear backbuffer
clearscreen()
endfunction thisInt
// ** Creates a gradient square from black (top) to white
function CreateGradientImage( thisSize# , thisTrans )
setClearColor( 0 , 0 , 0 ) ` Set screen to black
clearscreen()
thisSprite = createSprite( 0 ) ` Blank sprite to make the gradient
thisStep# = thisSize# / 256.0 ` Calculate the step size
setSpriteSize( thisSprite , thisSize# , thisStep# ) ` Sprite is full width and step high
thisY# = 0.0 ` Set initial Y position
thisColour = 0 ` Set initial colour to black
while thisColour < 256
setSpritePosition( thisSprite , 0.0 , thisY# ) ` Position the sprite at the current stripe
setSpriteColor( thisSprite , thisColour , thisColour , thisColour , 255 )
` Set sprite colour (shades of grey)
drawSprite( thisSprite ) ` Draw the sprite to the buffer
thisY# = thisY# + thisStep#
inc thisColour , 1
endwhile
thisInt = getImage( 0 , 0 , thisSize# , thisSize# ) ` Grab the resulting Image
if thisTrans then setImageMask( thisInt , thisInt , 4 , 1 , 0 , 0 )
deleteSprite( thisSprite )
clearscreen()
endfunction thisInt
// ** Creates a gradient square from black (top) to white with reversed transparenacy (white is transparent)
function CreateRevGradientImage( thisSize# )
setClearColor( 0 , 0 , 0 ) ` Set screen to black
clearscreen()
thisInt = CreateGradientImage( thisSize# , 1 ) ` Create a transparent gradient image
thisSprite = createSprite( thisInt ) ` Create a sprite from the image
setSpriteSize( thisSprite , thisSize# , thisSize# )
setSpriteFlip( thisSprite , 0 , 1 ) ` Flip the sprite vertically
drawSprite( thisSprite ) ` Draw the sprite to the buffer
thisFlipImage = getImage( 0 , 0 , thisSize# , thisSize# ) ` Grab the resulting image
setImageMask( thisInt , thisFlipImage , 4 , 1 , 0 , 0 ) ` Use new image for the transparency of original
deleteSprite( thisSprite )
deleteImage( thisFlipImage )
clearscreen()
endfunction thisInt
// ** Creates a gradient shape by combining a gradient with a mask
function CreateMaskedGradientImage( thisGradImage , thisMask , thisSize#, thisTrans )
setClearColor( 0 , 0 , 0 ) ` Set screen to black
clearscreen()
if thisGradImage < 1
thisGradImage = CreateGradientImage( thisSize# , 1 ) ` If no gradient specified, create one
newImage = 1
else
newImage = 0
endif
thisSprite = createSprite( thisGradImage ) ` Create a sprite from the image
setSpriteSize( thisSprite , thisSize# , thisSize# )
drawSprite( thisSprite ) ` Draw the sprite to the buffer
setSpriteImage( thisSprite , thisMask ) ` Use the same sprite fo the mask image
if newImage = 1 then deleteImage( thisGradImage )
` If a new image was created - delete it
setSpriteColor( thisSprite , 0 , 0 , 0 , 255 ) ` Make the mask Black
drawSprite( thisSprite ) ` Draw the sprite to the buffer
thisInt = getImage( 0 , 0 , thisSize# , thisSize# ) ` Grab the resulting Image
if thisTrans
// If transparency was required use the image as is
setImageMask( thisInt , thisInt , 4 , 1 , 0 , 0 ) ` Use colour as transparency
else
// Otherwise create an opaque mask
setClearColor( 255 , 255 , 255 ) ` Set screen to white to create an alpha mask
clearscreen() ` - this will create a pure white version of the masked image
drawSprite( thisSprite ) ` Draw the sprite to the buffer again - remember it's still black
thisTransImage = getImage( 0 , 0 , thisSize# , thisSize# ) ` Grab the resulting Image - in pure white for opaqueness
setImageMask( thisInt , thisTransImage , 4 , 1 , 0 , 0 ) ` Use new image for the transparency of original
deleteImage( thisTransImage ) ` Delete the new image
endif
deleteSprite( thisSprite )
clearscreen()
endfunction thisInt
//** Creates a number of sample sprites used to show results
function CreateResultSprites( thisSize# , numSprites , numPerRow )
spriteSize# = thisSize# / numPerRow
spriteHalf# = spriteSize# / 2.0
dim sprite[ numSprites ]
thisIsLand = ( getDeviceWidth() > getDeviceHeight() )
for i=1 to numSprites
// Create Sprite
sprite[ i ] = createSprite( 0 )
setSpriteSize( sprite[ i ] , spriteSize# - 4 , spriteSize# - 4)
setSpriteOffset( sprite[ i ] , spriteHalf# - 2 , spriteHalf# - 2 )
// Get position -
thisCol = mod( ( i - 1 ) , numPerRow )
thisRow = ( i - 1 ) / numPerRow
thisX# = 0.0 + thisCol * spriteSize# + 2
thisY# = thisSize# + thisRow * spriteSize# + 2
if thisIsLand
// Landscape - swap coordinates
setSpritePosition( sprite[ i ] , thisY# , thisX# )
else
// Portrait
setSpritePosition( sprite[ i ] , thisX# , thisY# )
endif
next i
// Use Sprite 0 to show transparency
sprite[ 0 ] = createSprite( 0 )
setSpriteDepth( sprite[ 0 ] , 100 )
if thisIsLand
setSpriteSize( sprite[ 0 ] , spriteHalf# , thisSize# )
setSpritePosition( sprite[ 0 ] , thisSize# + numPerRow * spriteHalf# / 2.0 , 0.0 )
else
setSpriteSize( sprite[ 0 ] , thisSize# , spriteHalf# )
setSpritePosition( sprite[ 0 ] , 0.0 , thisSize# + numPerRow * spriteHalf# / 2.0 )
endif
setSpriteOffset( sprite[ 0 ] , getSpriteWidth( sprite[ 0 ] ) / 2.0 , getSpriteHeight( sprite[ 0 ] ) / 2.0 )
setSpriteColor( sprite[ 0 ] , random( 128 , 255 ) , random( 128 , 255 ) , random( 128 , 255 ) , 255 )
endfunction