Some more drawing commands for CE

BlitzMax Forums/BlitzMax Programming/Some more drawing commands for CE

Grey Alien(Posted 2007) [#1]
These drawing commands *may* be useful for the Community Edition or whatever it's called.

' -----------------------------------------------------------------------------
' ccBezier: Draws a Bezier curve with 2 control points
' -----------------------------------------------------------------------------
Function ccBezier(sx:Float,sy:Float,ex:Float,ey:Float,p1x:Float,p1y:Float,p2x:Float,p2y:Float, ..
                  t:Float,pointx:Float Var, pointy:Float Var)
	'Pass in start and end coord, and also 2 control points and a time from 0 to 1
	'and this will return the coordinates of the relevant point in the vars provided.
	Local neg#=1-t
	pointx# = sx*(neg)^3 + 3*p1x*(neg)^2*t + 3*p2x*(neg)*t^2 + ex*t^3
	pointy# = sy*(neg)^3 + 3*p1y*(neg)^2*t + 3*p2y*(neg)*t^2 + ey*t^3
End Function


This is for use with ccDrawImageArea:

' -----------------------------------------------------------------------------
' ccClipImageToViewport: Clips an image into a "safe" viewport (doesn't use BMax viewport)
' -----------------------------------------------------------------------------
Function ccClipImageToViewport(image:TImage, imagex#, imagey#, ViewportX#, ViewPortY#, ViewPortW#, ViewPortH#, offsetx=0, offsety=0)
	'Perform basic clipping first by checking to see if the image is completely
	'outside of the viewport.
	'Note that images are drawn from the top left, not midhandled or anything else.
	Local w = ImageWidth(image)
	Local h = ImageHeight(image)
	
	If imagex+w>=ViewportX And imagex-w<ViewportX+ViewportW And..
		imagey+h>=ViewportY And imagey-h<ViewportY+ViewportH Then
		'Clip left and top
		Local startx#=ViewportX-imagex
		Local starty#=ViewportY-imagey
		If startx<0 Then startx=0 'clamp normal values
		If starty<0 Then starty=0 'clamp normal values
		'Clip right and bottom
		Local endx#=(imagex+w)-(ViewportX+ViewportW)
		Local endy#=(imagey+h)-(ViewportY+ViewportH)
		If endx<0 Then endx=0 'clamp normal values
		If endy<0 Then endy=0 'clamp normal values
		ccDrawImageArea(Image, imageX+startX+offsetx, imagey+starty+offsety, startx, starty, w-startx-endx, h-starty-endy,0)
	EndIf
End Function


' -----------------------------------------------------------------------------
' ccDimArea: Draws an alpha blended rectangle over an area to dim it out
' -----------------------------------------------------------------------------
Function ccDimArea(Alpha#=0.5,x,y,w,h)
	SetBlend ALPHABLEND
	SetColor 0,0,0
	SetAlpha Alpha
	DrawRect x,y,w,h
	SetColor 255,255,255				
	SetAlpha 1
End Function

' -----------------------------------------------------------------------------
' ccDimScreen: Draws an alpha blended rectangle over the screen to dim it out
' -----------------------------------------------------------------------------
Function ccDimScreen(Alpha#=0.5)
	SetBlend ALPHABLEND
	SetColor 0,0,0
	SetAlpha Alpha
	DrawRect 0,0,screenwidth, screenheight
	SetColor 255,255,255				
	SetAlpha 1
End Function


' -----------------------------------------------------------------------------
' ccImageRectCollide: See if an image (pixel perfect) collides with a rectangle
' -----------------------------------------------------------------------------
Function ccImageRectCollide%(image:TImage, ix%, iy%, iframe%, rx%, ry%, rwidth%, rheight%)
   ResetCollisions
   CollideImage image, ix, iy, iframe, 0, 1
   
   If CollideRect(rx, ry, rwidth, rheight, 1, 0) Then Return True Else Return False
End Function


' -----------------------------------------------------------------------------
' ccRectsOverLap: Detect if two rectangles overlap
' -----------------------------------------------------------------------------
Function ccRectsOverlap%(x0, y0, w0, h0, x2, y2, w2, h2)
	'Don't think this works with negative numbers, so be careful.
	If x0 >= (x2 + w2) Or (x0 + w0) <= x2 Then Return False
	If y0 >= (y2 + h2) Or (y0 + h0) <= y2 Then Return False
	Return True
End Function


All from my framework.