TGC Codebase Backup



Shadows on a 2D display by Coin

15th Mar 2005 18:56
Summary

Creates see-through shadows of rectangular 2D objects



Description

This is a simple function that draws shadows of rectangular 2D objects to make them look like they are standing out from the screen. The shadows are adjustable in width and intensity so that you may vary the amount of colour showing through them from the original graphics below.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    function Shadow(x1,y1,x2,y2,width,perc)
` x1,y1,x2,y2 are coordinates of rectangle casting the shadow - not the shadow itself
` perc is the intensity percentage of the desired shadow; width is its width
  percent#=.01*perc
  pc#=percent#
	off=width+1
	for y=y2 TO y2+width
		if y=y2+width-2 then pc#=percent#*.75
		if y=y2+width-1 then pc#=percent#*.5
		if y=y2+width then pc#=percent#*.25
		p#=pc# 
		for x=x1+width TO x2+width-off
			if x=x1+width AND percent#*.25<=pc# then pc#=percent#*.25		
			if x=x1+width+1 AND percent#*.5<=pc# then pc#=percent#*.5		
			if x=x1+width+2 AND percent#*.75<=pc# then pc#=percent#*.75
			colour=POINT(x,y)
			red=RGBR(colour)
		  green=RGBG(colour)
		  blue=RGBB(colour)
		  red=red-red*pc# : if red<0 then red=0
		  green=green-green*pc# : if green<0 then green=0
		  blue=blue-blue*pc# : if blue<0 then blue=0
			DOT x,y,RGB(red,green,blue)
			pc#=p#
		next x
		DEC off,1
	next y
	pc#=percent#
	off=0
	for x=x2 TO x2+width
		if x=x2+width-2 then pc#=percent#*.75
		if x=x2+width-1 then pc#=percent#*.5
		if x=x2+width then pc#=percent#*.25
		p#=pc#		
		for y=y1+width TO y2+off
			if y=y1+width AND percent#*.25<=pc# then pc#=percent#*.25			
			if y=y1+width+1 AND percent#*.5<=pc# then pc#=percent#*.5		
			if y=y1+width+2 AND percent#*.75<=pc# then pc#=percent#*.75
			colour=POINT(x,y)
			red=RGBR(colour)
		  green=RGBG(colour)
		  blue=RGBB(colour)
		  red=red-red*pc# : if red<0 then red=0
		  green=green-green*pc# : if green<0 then green=0
		  blue=blue-blue*pc# : if blue<0 then blue=0
			DOT x,y,RGB(red,green,blue)
			pc#=p#
		next y
		INC off,1
		if x=x2+width then pc#=percent#
	next x
endfunction