I simplified some things and added some more imports
cgcontext.pbi
Code:
Enumeration
#kCGLineJoinMiter
#kCGLineJoinRound
#kCGLineJoinBevel
EndEnumeration
Enumeration
#kCGLineCapButt
#kCGLineCapRound
#kCGLineCapSquare
EndEnumeration
Enumeration
#kCGPathFill
#kCGPathEOFill
#kCGPathStroke
#kCGPathFillStroke
#kCGPathEOFillStroke
EndEnumeration
Enumeration
#kCGBlendModeNormal
#kCGBlendModeMultiply
#kCGBlendModeScreen
#kCGBlendModeOverlay
#kCGBlendModeDarken
#kCGBlendModeLighten
#kCGBlendModeColorDodge
#kCGBlendModeColorBurn
#kCGBlendModeSoftLight
#kCGBlendModeHardLight
#kCGBlendModeDifference
#kCGBlendModeExclusion
#kCGBlendModeHue
#kCGBlendModeSaturation
#kCGBlendModeColor
#kCGBlendModeLuminosity
EndEnumeration
ImportC ""
; colorspace functions
CGColorSpaceCreateDeviceRGB()
CGColorSpaceRelease(colorspace)
; bitmapcontext functions
CGBitmapContextCreate(*bdata, width, height, bitsPerComponent, bytesPerRow, colorspace, bitmapInfo)
CGContextRelease(context)
; cgcontext functions to init and restore / release some things
CGContextSaveGState(context)
CGContextRestoreGState(context)
CGContextScaleCTM(context, sx.f, sy.f)
CGContextTranslateCTM(context, tx.f, ty.f)
CGContextSetStrokeColorSpace(context, colorspace)
CGContextSetFillColorSpace(context, colorspace)
CGContextSetStrokeColor(context, color)
CGContextSetFillColor(context, color)
; cgcontext drawing related functions
CGContextAddArc(context, x.f, y.f, radius.f, startAngle.f, endAngle.f, clockwise)
CGContextAddArcToPoint(context, x1.f, y1.f, x2.f, y2.f, radius.f)
CGContextAddCurveToPoint(context, cp1x.f, cp1y.f, cp2x.f, cp2y.f, x.f, y.f)
CGContextAddEllipseInRect(context, x.f, y.f, w.f, h.f)
CGContextAddLineToPoint(context, x.f, y.f)
CGContextAddQuadCurveToPoint(context, cpx.f, cpy.f, x.f, y.f)
CGContextAddRect(context, x.f, y.f, w.f, h.f)
CGContextBeginPath(context)
CGContextClearRect(context, x.f, y.f, w.f, h.f)
CGContextClosePath(context)
CGContextDrawImage(context, x.f, y.f, w.f, h.f, image)
CGContextDrawPath(context, mode)
CGContextDrawTiledImage(context, x.f, y.f, w.f, h.f, image)
CGContextFillEllipseInRect(context, x.f, y.f, w.f, h.f)
CGContextFillPath(context)
CGContextFillRect(context, x.f, y.f, w.f, h.f)
CGContextMoveToPoint(context, x.f, y.f)
CGContextSetAlpha(context, alpha.f)
CGContextSetBlendMode(context, mode)
CGContextSetLineCap(context, cap)
CGContextSetLineJoin(context, join)
CGContextSetLineWidth(context, width.f)
CGContextSetMiterLimit(context, limit.f)
CGContextSetShadow(context, x_offset.f, y_offset.f, blur.f)
CGContextSetShadowWithColor(context, x_offset.f, y_offset.f, blur.f, cgcolor)
CGContextStrokeEllipseInRect(context, x.f, y.f, w.f, h.f)
CGContextStrokePath(context)
CGContextStrokeRect(context, x.f, y.f, w.f, h.f)
CGContextStrokeRectWithWidth(context, x.f, y.f, w.f, h.f, width.f)
EndImport
Procedure CGContextCreate(flipped = #True)
Protected w, h, csRGB, ctx
w = OutputWidth()
h = OutputHeight()
csRGB = CGColorSpaceCreateDeviceRGB()
ctx = CGBitmapContextCreate(DrawingBuffer(), w, h, 8, w << 2, csRGB, 1)
If flipped
CGContextScaleCTM(ctx, 1, -1)
CGContextTranslateCTM(ctx, 0, -h)
EndIf
CGContextSetStrokeColorSpace(ctx, csRGB)
CGContextSetFillColorSpace(ctx, csRGB)
CGColorSpaceRelease(csRGB)
ProcedureReturn ctx
EndProcedure
Procedure CGContextSetStrokeColorRGB(ctx, r.f, g.f, b.f, a.f = 1.0)
CGContextSetStrokeColor(ctx, @r)
EndProcedure
Procedure CGContextSetFillColorRGB(ctx, r.f, g.f, b.f, a.f = 1.0)
CGContextSetFillColor(ctx, @r)
EndProcedure
Procedure CGContextClearShadow(ctx)
CGContextSetShadowWithColor(ctx, 0, 0, 0, #Null)
EndProcedure
test code
Code:
IncludeFile "cgcontext.pbi"
Procedure RenderOnCanvas(canvas)
Protected ctx
If StartDrawing(CanvasOutput(canvas))
; create a context
ctx = CGContextCreate()
; drawing
CGContextSetShadow(ctx, 2, -2, 2)
CGContextSetFillColorRGB(ctx, 1, 0.5, 0)
CGContextFillRect(ctx, 5, 5, 50, 50)
CGContextFillRect(ctx, 105, 105, 50, 50)
CGContextSetStrokeColorRGB(ctx, 0.9, 0.9, 0)
CGContextSetFillColorRGB(ctx, 0.5, 1, 0)
CGContextSetLineWidth(ctx, 2)
CGContextClearShadow(ctx)
CGContextBeginPath(ctx)
CGContextAddArc(ctx, 80, 80, 25, #PI, 0, #True)
CGContextAddQuadCurveToPoint(ctx, 130, 120, 105, 140)
CGContextAddQuadCurveToPoint(ctx, 80, 160, 40, 140)
CGContextDrawPath(ctx, #kCGPathFillStroke)
; release context and stop drawing
CGContextRelease(ctx)
StopDrawing()
EndIf
EndProcedure
If OpenWindow(0, 0, 0, 320, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(0, 10, 10, 300, 200)
RenderOnCanvas(0)
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf