So, following on from the requests thread I mentioned its easily possible to do 2D shadows in AppGameKit and I was asked for some example code.
I rushed this together tonight
... so it may have a few bugs but it should be obvious how it works to anyone who tries it out or takes a look at it.
// Create2DQuickShadow(SpriteID as integer, Darkness as integer)
// This function creates a 2D sheer shadow sprite (45 degrees to right) from the sprite passed to it
// SpriteID - this is the sprite that you want to create a shadow for - it must contain alpha values
// Darkness - 255 makes a totally black shadow - 0 gives a totally transparent shadow - 180 looks good
// it returns the ID of the shadow...
Function Create2DQuickShadow(SpriteID as integer, Darkness as integer)
shadow = CloneSprite(SpriteID) // clone the riginal sprite
SetSpriteSize(shadow,GetSpriteWidth(shadow)*2,GetSpriteHeight(shadow)) // Set its size
SetSpriteDepth(shadow,GetSpriteDepth(SpriteID)+1) // set its depth
SetSpriteColor(shadow,0,0,0,Darkness) // make it a shadow
SetSpriteUV(shadow,-1,0,0,1,1,0,2,1)
endfunction shadow
// more complex and capable version !!
// Create2DSheerShadow(SpriteID as integer,Angle as float,HRatio as float, Darkness as integer)
// This function creates a 2D shadow sprite from the sprite passed to it
// SpriteID - this is the sprite that you want to create a shadow for - it must contain alpha
// Angle - This is the angle the shadow will cast (typically 0 to 60 degrees) - this is to the right
// HRatio - ratio of shadow height to sprite height - leave at 1 (unless you want to cast a short shadow (<1.0) or LONG shadow(>1.0))
// Darkness - 255 makes a totally black shadow - 0 gives a totally transparent shadow (pointless!)- 180 works well
// it returns the ID of the shadow...this MUST have its position set with its parent be moved with its parent sprite
Function Create2DSheerShadow(SpriteID as integer,Angle as float,HRatio as float, Darkness as integer)
Ratio# = Tan(angle) // Calculate a width of the shadow sprite relative to the original
shadow = CloneSprite(SpriteID) // clone the riginal sprite
SetSpriteSize(shadow,GetSpriteWidth(shadow)*(1+Abs(Ratio#)),GetSpriteHeight(shadow)*HRatio) // Set its size
SetSpriteDepth(shadow,GetSpriteDepth(SpriteID)+1) // set its depth
SetSpriteColor(shadow,0,0,0,Darkness) // make it a shadow
if Ratio# >= 0
SetSpriteUV(shadow,-Ratio#,0,0,1,1,0,1+Ratio#,1)
SetSpritePosition(shadow,GetSpriteX(shadow),GetSpriteY(shadow)+((1-HRatio)*GetSpriteHeight(SpriteID)))
else
SetSpriteUV(shadow,0,0,Ratio#,1,1-Ratio#,0,1,1)
SetSpritePosition(shadow,GetSpriteX(shadow)+(Ratio#*GetSpriteWidth(SpriteID)),GetSpriteY(shadow)+((1-HRatio)*GetSpriteHeight(SpriteID)))
endif
endfunction shadow
// Simple rotation based solution
// This function creates a 2D rotation shadow sprite from the sprite passed to it
// SpriteID - this is the sprite that you want to create a shadow for - it must contain alpha
// Angle - This is the angle the shadow will cast (typically 0 to 60 degrees) - this is to the right
// Darkness - 255 makes a totally black shadow - 0 gives a totally transparent shadow (pointless!)- 180 works well
// it returns the ID of the shadow...this MUST have its position set with its parent be moved with its parent sprite
Function Create2DRotationShadow(SpriteID as integer,Angle as float, Darkness as integer)
shadow = CloneSprite(SpriteID) // clone the riginal sprite
SetSpriteDepth(shadow,GetSpriteDepth(SpriteID)+1) // set its depth
SetSpriteColor(shadow,0,0,0,Darkness) // make it a shadow
SetSpriteOffset(shadow,GetSpriteWidth(shadow)/2,GetSpriteHeight(shadow))
SetSpriteAngle(shadow, Angle)
endfunction shadow
There are 3 functions:
Create2DQuickShadow(ID,180)
This creates a sheer shadow at 45 degrees to the right with an alpha of 180
Create2DSheerShadow(ID,30,1.2,200)
This creates a shadow with an angle of 30 degrees off vertical to the right with a shadow length of 1.2 x the original height with an alpha of 200
Create2DRotationShadow(ID,30,150)
This creates a shadow rotated by 30 degrees with an alpha of 150
please note:
1) This only works on images that have alpha values (obviously) So thats png's with transparent pixels in them.
2) A one pixel border of completely transparent pixels is needed at left and right side of the image or you will get shadow smear due to texture wrapping effects
3) I set the depth of the shadow sprite to just just '1' deeper than the original sprite... you will have to set this to whatever you need t to be (typically just slightly less than the background/ground sprite
4) All these functions assume the base of your image is the feet of the character...if there is space between his feet and the bottomof the image then he will appear to float unless you move his shadow appropriately.
Gives results similar to
Ohh...and im aware that it probably should be 'SHEAR' not 'SHEER' but ...ummm...whatever