EnableExplicit ;>----- START INCLUDE --------------------------------------- ; ;-[ antialiased PB syntax compatible drawing functions using GDI+ ]- ; ; by Danilo, July 2011 ; ; Version 0.5 ; Prototype DebugEventProc(level, *message.Ascii) Prototype NotificationHookProc(*token.Long) Prototype NotificationUnhookProc(token.l) ;-------- structures Structure GdiplusStartupInput GdiPlusVersion.l *DebugEventCallback.DebugEventProc SuppressBackgroundThread.l SuppressExternalCodecs.l EndStructure Structure GdiplusStartupOutput *NotificationHook.NotificationHookProc *NotificationUnhook.NotificationUnhookProc EndStructure Structure POINTF X.f Y.f EndStructure ;-------- enumerations Enumeration ; CompositingMode #CompositingModeSourceOver = 0 ; // 0 #CompositingModeSourceCopy = 1 ; // 1 EndEnumeration Enumeration ; QualityMode #QualityModeInvalid = -1 #QualityModeDefault = 0 #QualityModeLow = 1 ; Best performance #QualityModeHigh = 2 ; Best rendering quality EndEnumeration Enumeration ; CompositingQuality #CompositingQualityInvalid = #QualityModeInvalid #CompositingQualityDefault = #QualityModeDefault #CompositingQualityHighSpeed = #QualityModeLow #CompositingQualityHighQuality = #QualityModeHigh #CompositingQualityGammaCorrected #CompositingQualityAssumeLinear EndEnumeration Enumeration ; SmoothingMode #SmoothingModeInvalid = #QualityModeInvalid #SmoothingModeDefault = #QualityModeDefault #SmoothingModeHighSpeed = #QualityModeLow #SmoothingModeHighQuality = #QualityModeHigh #SmoothingModeNone #SmoothingModeAntiAlias ;#if (GDIPVER >= 0x0110) #SmoothingModeAntiAlias8x4 = #SmoothingModeAntiAlias #SmoothingModeAntiAlias8x8 ;#endif //(GDIPVER >= 0x0110) EndEnumeration Enumeration ; InterpolationMode #InterpolationModeInvalid = #QualityModeInvalid #InterpolationModeDefault = #QualityModeDefault #InterpolationModeLowQuality = #QualityModeLow #InterpolationModeHighQuality = #QualityModeHigh #InterpolationModeBilinear #InterpolationModeBicubic #InterpolationModeNearestNeighbor #InterpolationModeHighQualityBilinear #InterpolationModeHighQualityBicubic EndEnumeration Enumeration ; PixelOffsetMode #PixelOffsetModeInvalid = #QualityModeInvalid #PixelOffsetModeDefault = #QualityModeDefault #PixelOffsetModeHighSpeed = #QualityModeLow #PixelOffsetModeHighQuality = #QualityModeHigh #PixelOffsetModeNone ; No pixel offset #PixelOffsetModeHalf ; Offset by -0.5, -0.5 For fast anti-alias perf EndEnumeration Enumeration ; LineCap #LineCapFlat = 0 #LineCapSquare = 1 #LineCapRound = 2 #LineCapTriangle = 3 #LineCapNoAnchor = $10 ; corresponds To flat cap #LineCapSquareAnchor = $11 ; corresponds To square cap #LineCapRoundAnchor = $12 ; corresponds To round cap #LineCapDiamondAnchor = $13 ; corresponds To triangle cap #LineCapArrowAnchor = $14 ; no correspondence #LineCapCustom = $ff ; custom cap #LineCapAnchorMask = $f0 ; mask To check For anchor Or Not. EndEnumeration #gNOCOLOR = $1FFFFFFFF ;-------- function prototypes Prototype.l GdiplusStartup(*token.Long, *input.GdiplusStartupInput, *output.GdiplusStartupOutput) Prototype.l GdiplusShutdown(token.l) Prototype.l GdipCreateFromHDC(*hDC, *graphics) Prototype.l GdipDeleteGraphics(*graphics) Prototype.l GdipGraphicsClear(*graphics, color.l) Prototype.l GdipSetSmoothingMode (*graphics, smoothingMode.l) Prototype.l GdipSetCompositingMode (*graphics, compositingMode.l) Prototype.l GdipSetCompositingQuality (*graphics, compositingQuality.l) Prototype.l GdipSetInterpolationMode (*graphics, interpolationMode.l) Prototype.l GdipSetPixelOffsetMode (*graphics, pixelOffsetMode.l) Prototype.l GdipCreatePen1 (color.l, Width.f, unit.l, *pen) Prototype.l GdipDeletePen (*pen) Prototype.l GdipSetPenWidth (*pen, width.f) Prototype.l GdipSetPenStartCap(*pen, startCap.l) Prototype.l GdipSetPenEndCap (*pen, endCap.l) Prototype.l GdipCreateSolidFill(color.l, *brush) Prototype.l GdipDeleteBrush(*brush) Prototype.l GdipDrawRectangle (*graphics, *pen , x.f , y.f , width.f, height.f) Prototype.l GdipFillRectangle (*graphics, *brush, x.f , y.f , width.f, height.f) Prototype.l GdipDrawLine (*graphics, *pen , x1.f, y1.f, x2.f , y2.f ) Prototype.l GdipDrawPie (*graphics, *pen , x.f , y.f , width.f, height.f, startAngle.f, sweepAngle.f) Prototype.l GdipFillPie (*graphics, *brush, x.f , y.f , width.f, height.f, startAngle.f, sweepAngle.f) Prototype.l GdipDrawArc (*graphics, *pen , x.f , y.f , width.f, height.f, startAngle.f, sweepAngle.f) Prototype.l GdipDrawEllipse (*graphics, *pen , x.f , y.f , width.f, height.f) Prototype.l GdipFillEllipse (*graphics, *pen , x.f , y.f , width.f, height.f) Prototype.l GdipDrawBezier (*graphics, *pen , x1.f, y1.f, x2.f , y2.f , x3.f, y3.f, x4.f, y4.f) Prototype.l GdipDrawPolygon (*graphics, *pen , *points.POINTF, count.l) Prototype.l GdipFillPolygon2 (*graphics, *brush, *points.POINTF, count.l) ;-------- internal macros Macro DoubleQuote " EndMacro Macro setFunc(_name_) Global _name_#_._name_ _name_#_ = GetFunction(__dll,DoubleQuote#_name_#DoubleQuote) EndMacro Macro toARGB(color) ( (color & $FF00FF00) | ( (color & $FF) << 16 ) | ((color >> 16) & $FF) ) EndMacro Global __dll,__gdiplusToken.l,__drawingMode.l,__graphics,__lineCapStart,__lineCapEnd Global __currentPen.i, __currentBrush.i, __currentPenColor.l, __currentPenSize.f ;-------- init & end functions Procedure gInit() __dll = OpenLibrary(#PB_Any, "gdiplus.dll") If Not __dll : ProcedureReturn 0 : EndIf setFunc(GdiplusStartup) setFunc(GdiplusShutdown) setFunc(GdipCreateFromHDC) setFunc(GdipDeleteGraphics) setFunc(GdipGraphicsClear) setFunc(GdipSetSmoothingMode) setFunc(GdipSetCompositingMode) setFunc(GdipSetCompositingQuality) setFunc(GdipSetInterpolationMode) setFunc(GdipSetPixelOffsetMode) setFunc(GdipCreatePen1) setFunc(GdipDeletePen) setFunc(GdipSetPenWidth) setFunc(GdipSetPenStartCap) setFunc(GdipSetPenEndCap) setFunc(GdipCreateSolidFill) setFunc(GdipDeleteBrush) setFunc(GdipDrawRectangle) setFunc(GdipFillRectangle) setFunc(GdipDrawLine) setFunc(GdipDrawPie) SetFunc(GdipFillPie) setFunc(GdipDrawArc) setFunc(GdipDrawEllipse) SetFunc(GdipFillEllipse) SetFunc(GdipDrawBezier) SetFunc(GdipDrawPolygon) SetFunc(GdipFillPolygon2) ; Initialize GDI+. Protected gdiplusStartupInput.GdiplusStartupInput gdiplusStartupInput\GdiplusVersion = 1 gdiplusStartupInput\DebugEventCallback = #Null gdiplusStartupInput\SuppressBackgroundThread = #False gdiplusStartupInput\SuppressExternalCodecs = #False If Not GdiplusStartup_(@__gdiplusToken, @gdiplusStartupInput, #Null) ProcedureReturn #True EndIf CloseLibrary(__dll) ProcedureReturn #False EndProcedure Procedure gEnd() GdiplusShutdown_(__gdiplusToken) If __dll CloseLibrary(__dll) EndIf EndProcedure ;-------- internal functions Procedure __getPenInternal(color.q) Protected *pen If color = #gNOCOLOR ProcedureReturn __currentPen EndIf GdipCreatePen1_(toARGB(color),__currentPenSize,2,@*pen) GdipSetPenStartCap_(*pen, __lineCapStart) GdipSetPenEndCap_ (*pen, __lineCapEnd ) ProcedureReturn *pen EndProcedure Procedure __getBrushInternal(color.q) Protected *brush If color = #gNOCOLOR ProcedureReturn __currentBrush EndIf GdipCreateSolidFill_(toARGB(color),@*brush) ProcedureReturn *brush EndProcedure Procedure __createPenInternal() If __currentPen GdipDeletePen_(__currentPen) EndIf GdipCreatePen1_(toARGB(__currentPenColor),__currentPenSize,2,@__currentPen) GdipSetPenStartCap_(__currentPen, __lineCapStart) GdipSetPenEndCap_ (__currentPen, __lineCapEnd ) If __currentBrush GdipDeleteBrush_(__currentBrush) EndIf GdipCreateSolidFill_(toARGB(__currentPenColor),@__currentBrush) EndProcedure ;-------- drawing options Procedure gSetAntiAliasMode( newMode.l ) If __graphics GdipSetSmoothingMode_(__graphics,newMode) EndIf EndProcedure Procedure gSetPixelOffsetMode( newMode.l ) If __graphics GdipSetPixelOffsetMode_(__graphics,newMode) EndIf EndProcedure Procedure gSetInterpolationMode( newMode.l ) If __graphics GdipSetInterpolationMode_(__graphics,newMode) EndIf EndProcedure Procedure gSetCompositingMode( newMode.l ) If __graphics GdipSetCompositingMode_(__graphics,newMode) EndIf EndProcedure Procedure gSetCompositingQuality( newQuality.l ) If __graphics GdipSetCompositingQuality_(__graphics,newQuality) EndIf EndProcedure Procedure gSetPen(color.l,width.f) __currentPenColor = color __currentPenSize = width __createPenInternal() EndProcedure Procedure gSetPenColor(color.l) __currentPenColor = color __createPenInternal() EndProcedure Procedure gSetPenWidth(width.f) __currentPenSize = width If __currentPen GdipSetPenWidth_(__currentPen,width) EndIf EndProcedure Procedure gSetPenCaps( lineCapStart.l, lineCapEnd.l ) __lineCapStart = lineCapStart __lineCapEnd = lineCapEnd If __currentPen GdipSetPenStartCap_(__currentPen, __lineCapStart) GdipSetPenEndCap_ (__currentPen, __lineCapEnd ) EndIf EndProcedure Procedure gDrawingMode( newMode.l ) __drawingMode = newMode EndProcedure ;-------- drawing start & stop Procedure gStartDrawing( out ) Protected hDC hDC = StartDrawing(out) If Not hDC : ProcedureReturn 0 : EndIf GdipCreateFromHDC_(hDC ,@__graphics) If Not __graphics StopDrawing() ProcedureReturn 0 EndIf __drawingMode = #PB_2DDrawing_Default __lineCapStart = #LineCapFlat __lineCapEnd = #LineCapFlat __currentPenColor = RGBA($FF,$FF,$FF,$FF) __currentPenSize = 1.0 __createPenInternal() GdipSetSmoothingMode_ (__graphics,#SmoothingModeAntiAlias) GdipSetCompositingMode_ (__graphics,#CompositingModeSourceOver) GdipSetCompositingQuality_(__graphics,#CompositingQualityGammaCorrected) GdipSetInterpolationMode_ (__graphics,#InterpolationModeHighQualityBicubic) GdipSetPixelOffsetMode_ (__graphics,#PixelOffsetModeNone) ProcedureReturn hDC EndProcedure Procedure gStopDrawing() If __graphics : GdipDeleteGraphics_( __graphics ) : __graphics = 0 : EndIf If __currentPen : GdipDeletePen_ ( __currentPen ) : __currentPen = 0 : EndIf If __currentBrush : GdipDeleteBrush_ ( __currentBrush ) : __currentBrush = 0 : EndIf StopDrawing() EndProcedure ;-------- drawing functions Procedure gClear(color.l) If __graphics GdipGraphicsClear_(__graphics, toARGB(color)) EndIf EndProcedure Procedure gBox(x.f, y.f, width.f, height.f, color.q=#gNOCOLOR) If __graphics If __drawingMode & #PB_2DDrawing_Outlined Protected *pen = __getPenInternal(color) GdipDrawRectangle_(__graphics,*pen,x,y,width,height) If Not color=#gNOCOLOR : GdipDeletePen_(*pen) : EndIf Else Protected *brush = __getBrushInternal(color) GdipFillRectangle_(__graphics,*brush,x,y,width,height) If Not color=#gNOCOLOR : GdipDeleteBrush_(*brush) : EndIf EndIf EndIf EndProcedure Procedure gLineXY(x1.f, y1.f, x2.f, y2.f, color.q=#gNOCOLOR) If __graphics Protected *pen = __getPenInternal(color) GdipDrawLine_(__graphics,*pen,x1,y1,x2,y2) If Not color=#gNOCOLOR : GdipDeletePen_(*pen) : EndIf EndIf EndProcedure Procedure gLine(x.f, y.f, width.f, height.f, color.q=#gNOCOLOR) gLineXY(x,y,x+width,y+height,color) EndProcedure Procedure gPie(x.f, y.f, radiusX.f, radiusY.f, startAngle.f, sweepAngle.f, color.q=#gNOCOLOR) If __graphics If __drawingMode & #PB_2DDrawing_Outlined Protected *pen = __getPenInternal(color) GdipDrawPie_(__graphics,*pen,x-radiusX,y-radiusY,radiusX*2,radiusY*2,startAngle,sweepAngle) If Not color=#gNOCOLOR : GdipDeletePen_(*pen) : EndIf Else Protected *brush = __getBrushInternal(color) GdipFillPie_(__graphics,*brush,x-radiusX,y-radiusY,radiusX*2,radiusY*2,startAngle,sweepAngle) If Not color=#gNOCOLOR : GdipDeleteBrush_(*brush) : EndIf EndIf EndIf EndProcedure Procedure gArc(x.f, y.f, radiusX.f, radiusY.f, startAngle.f, sweepAngle.f, color.q=#gNOCOLOR) If __graphics Protected *pen = __getPenInternal(color) GdipDrawArc_(__graphics,*pen,x-radiusX,y-radiusY,radiusX*2,radiusY*2,startAngle,sweepAngle) If Not color=#gNOCOLOR : GdipDeletePen_(*pen) : EndIf EndIf EndProcedure Procedure gEllipse(x.f, y.f, radiusX.f, radiusY.f, color.q=#gNOCOLOR) If __graphics If __drawingMode & #PB_2DDrawing_Outlined Protected *pen = __getPenInternal(color) GdipDrawEllipse_(__graphics,*pen,x-radiusX,y-radiusY,radiusX*2,radiusY*2) If Not color=#gNOCOLOR : GdipDeletePen_(*pen) : EndIf Else Protected *brush = __getBrushInternal(color) GdipFillEllipse_(__graphics,*brush,x-radiusX,y-radiusY,radiusX*2,radiusY*2) If Not color=#gNOCOLOR : GdipDeleteBrush_(*brush) : EndIf EndIf EndIf EndProcedure Procedure gCircle(x.f, y.f, radius.f, color.q=#gNOCOLOR) gEllipse(x,y,radius,radius,color) EndProcedure Procedure gBezier(x1.f, y1.f, x2.f, y2.f, x3.f, y3.f, x4.f, y4.f, color.q=#gNOCOLOR) If __graphics Protected *pen = __getPenInternal(color) GdipDrawBezier_(__graphics,*pen,x1,y1,x2,y2,x3,y3,x4,y4) If Not color=#gNOCOLOR : GdipDeletePen_(*pen) : EndIf EndIf EndProcedure Procedure gTriangle(x1.f, y1.f, x2.f, y2.f, x3.f, y3.f, color.q=#gNOCOLOR) If __graphics Dim points.POINTF(3) points(0)\x = x1 points(0)\y = y1 points(1)\x = x2 points(1)\y = y2 points(2)\x = x3 points(2)\y = y3 If __drawingMode & #PB_2DDrawing_Outlined Protected *pen = __getPenInternal(color) GdipDrawPolygon_(__graphics,*pen,@points(),3) If Not color=#gNOCOLOR : GdipDeletePen_(*pen) : EndIf Else Protected *brush = __getBrushInternal(color) GdipFillPolygon2_(__graphics,*brush,@points(),3) If Not color=#gNOCOLOR : GdipDeleteBrush_(*brush) : EndIf EndIf EndIf EndProcedure Procedure gPoly(Array points.POINTF(1),color.q=#gNOCOLOR) If __graphics If __drawingMode & #PB_2DDrawing_Outlined Protected *pen = __getPenInternal(color) GdipDrawPolygon_(__graphics,*pen,@points(),ArraySize(points())+1) If Not color=#gNOCOLOR : GdipDeletePen_(*pen) : EndIf Else Protected *brush = __getBrushInternal(color) GdipFillPolygon2_(__graphics,*brush,@points(),ArraySize(points())+1) If Not color=#gNOCOLOR : GdipDeleteBrush_(*brush) : EndIf EndIf EndIf EndProcedure ;>----- END INCLUDE ----------------------------------------- Enumeration #Window #Image EndEnumeration gInit() Define n CreateImage(#Image, 500, 300, 32) gStartDrawing(ImageOutput(#Image)) gClear($FFFFFFFF) For n = 0 To 20 gSetPen($FF000000, n/10) gLineXY(n*20+20, 110, n*20+20, 190) Next gStopDrawing() OpenWindow(#Window, 0, 0, 500, 300, "") ImageGadget(#PB_Any, 0, 0, 500, 300, ImageID(#Image)) Repeat Until WaitWindowEvent() = #PB_Event_CloseWindow ; IDE Options = PureBasic 4.60 Beta 3 (Windows - x86) ; CursorPosition = 492 ; FirstLine = 474 ; Folding = ------ ; EnableXP