#include once "window9.bi"

Using GDIPLUS
Dim shared ULONG_PTR_01 AS ULONG_PTR
DIM GDIPLUSSTARTUPINPUT_01 AS GDIPLUSSTARTUPINPUT
GDIPLUSSTARTUPINPUT_01.GdiplusVersion = 1
IF (GDIPLUSSTARTUP(@ULONG_PTR_01, @GDIPLUSSTARTUPINPUT_01, NULL) <> 0) THEN
	PRINT "FAIL"
EndIf

Dim Shared As PVOID GpGraphics,GpGraphics2,hbitmapMaindrawGP
Dim Shared As Integer FlagDrawOnWindow
Dim Shared Window9RectPos As RECT

Function WindowStartDrawA(ByVal window_ As HWND,ByVal x As Integer=0, ByVal y As Integer=0, ByVal w As Integer=0, ByVal h As Integer=0, ByVal ColorFlag As Integer=0, ByVal Color_ As Integer=&hFFFFFFFF) As Any Ptr  Export  
	If w =0 Then w = WindowClientWidth(window_)
	If h =0 Then h = WindowClientHeight(window_)
	Window9RectPos.left = x
	Window9RectPos.top = y
	GdipCreateFromHWND(window_,@GpGraphics2)
	GdipCreateBitmapFromScan0(w,h, 0, PixelFormat32bppARGB, NULL, @hbitmapMaindrawGP)
	GdipGetImageGraphicsContext(hbitmapMaindrawGP,@GpGraphics)
	If ColorFlag=1 Then
		GdipGraphicsClear(GpGraphics,Color_)
	EndIf
	FlagDrawOnWindow = 1
	Return GpGraphics
End Function

Function ImageStartDrawA(ByRef bitmapGP As Any Ptr,ByVal ColorFlag As Integer=0, ByVal Color_ As Integer=&hFFFFFFFF) As Any Ptr  Export
	If bitmapGP=0 Then
		Print "Error bitmapGP"
		Return 0
	EndIf
	Dim As long w,h
	GdipGetImageWidth(bitmapGP,@w)
	GdipGetImageHeight(bitmapGP,@h)
	GdipGetImageGraphicsContext(bitmapGP,@GpGraphics)
	If ColorFlag=1 Then
		GdipGraphicsClear(GpGraphics,Color_)
	EndIf
	hbitmapMaindrawGP=bitmapGP
	Return GpGraphics
End function

Sub StopDrawA() Export
	If FlagDrawOnWindow = 1 Then
		FlagDrawOnWindow = 0
		GdipDrawImage(GpGraphics2,hbitmapMaindrawGP,Window9RectPos.left,Window9RectPos.top)
		GdipDeleteGraphics(GpGraphics2)
		GdipDisposeImage(hbitmapMaindrawGP)
	EndIf
	GdipDeleteGraphics(GpGraphics)
End Sub

Sub LineDrawA(ByVal x As single,ByVal y As single,ByVal x1 As single,ByVal y1 As single,ByVal widthPen As Single=1,ByVal colorPen As Integer=&hff000000,byval brushPen as Any Ptr=0)Export
	Dim pen As GpPen Ptr
	If brushPen<>0 Then
		GdipCreatePen2(brushPen,widthPen,Unitpixel,@pen)
	Else
		GdipCreatePen1(ColorPen,widthPen,Unitpixel,@pen)
	EndIf
	GdipDrawLine(GpGraphics,pen , x, y, x1, y1)
	GdipDeletePen(pen)
End Sub
Sub BoxDrawA(ByVal x As single,ByVal y As single,ByVal width_ As single,ByVal height_ As single,ByVal ColorPen As integer=&hff000000,ByVal flagcolorBK As Integer=1,ByVal ColorBk As integer=&hff000000,byval brushPen as Any Ptr=0,byval brushBk as Any Ptr=0,ByVal widthPen As Single=1)Export
	Dim As GpPen Ptr pen
	If brushPen<>0 Then
		GdipCreatePen2(brushPen,widthPen,Unitpixel,@pen)
	Else
		GdipCreatePen1(ColorPen,widthPen,Unitpixel,@pen)
	EndIf
	GdipDrawRectangle(GpGraphics,pen,x,y,width_,height_)
	GdipDeletePen(pen)
	If flagcolorBK=1 Then
		Dim As PVOID brush
		GdipCreateSolidFill(ColorBk,@brush)
		GdipFillRectangle(GpGraphics,brush,x,y,width_,height_)
		GdipDeleteBrush(brush)
	ElseIf flagcolorBK=2 Then
		GdipFillRectangle(GpGraphics,brushBk,x,y,width_,height_)
	EndIf
End Sub
Sub CircleDrawA(ByVal x As single,ByVal y As single,ByVal Radius As single,ByVal ColorPen As integer=&hff000000,ByVal flagcolorBK As Integer=1,ByVal ColorBk As integer=&hff000000,ByVal brushPen as Any Ptr=0,byval brushBk as Any Ptr=0,ByVal widthPen As Single=1)Export
	Dim As GpPen Ptr pen
	If brushPen<>0 Then
		GdipCreatePen2(brushPen,widthPen,Unitpixel,@pen)
	Else
		GdipCreatePen1(ColorPen,widthPen,Unitpixel,@pen)
	EndIf
	GdipDrawEllipse(GpGraphics,pen,x-Radius,y-Radius,Radius*2,Radius*2)
	GdipDeletePen(pen)
	If flagcolorBK=1 Then
		Dim As PVOID brush
		GdipCreateSolidFill(ColorBk,@brush)
		GdipFillEllipse(GpGraphics,brush,x-Radius,y-Radius,Radius*2,Radius*2)
		GdipDeleteBrush(brush)
	ElseIf flagcolorBK=2 Then
		GdipFillEllipse(GpGraphics,brushBk,x-Radius,y-Radius,Radius*2,Radius*2)
	EndIf
End Sub
Sub RoundDrawA(ByVal x As single,ByVal y As single,ByVal width_ As single,ByVal height_ As single,ByVal ColorPen As integer=&hff000000,ByVal flagcolorBK As Integer=1,ByVal ColorBk As integer=&hff000000,ByVal brushPen as Any Ptr=0,byval brushBk as Any Ptr=0,ByVal widthPen As Single=1)Export
	Dim As GpPen Ptr pen
	If brushPen<>0 Then
		GdipCreatePen2(brushPen,widthPen,Unitpixel,@pen)
	Else
		GdipCreatePen1(ColorPen,widthPen,Unitpixel,@pen)
	EndIf
	GdipDrawEllipse(GpGraphics,pen,x,y,width_,height_)
	GdipDeletePen(pen)
	If flagcolorBK=1 Then
		Dim As PVOID brush
		GdipCreateSolidFill(ColorBk,@brush)
		GdipFillEllipse(GpGraphics,brush,x,y,width_,height_)
		GdipDeleteBrush(brush)
	ElseIf flagcolorBK=2 Then
		GdipFillEllipse(GpGraphics,brushBk,x,y,width_,height_)
	EndIf
End Sub
Sub BezierDrawA(ByVal x0 As single,ByVal y0 As single,ByVal x1 As single,ByVal y1 As single,ByVal x2 As single,ByVal y2 As single,ByVal x3 As single,ByVal y3 As single,ByVal ColorPen As integer=&hff000000,ByVal brushPen as Any Ptr=0,ByVal widthPen As Single=1)Export
	Dim As GpPen Ptr pen
	If brushPen<>0 Then
		GdipCreatePen2(brushPen,widthPen,Unitpixel,@pen)
	Else
		GdipCreatePen1(ColorPen,widthPen,Unitpixel,@pen)
	EndIf
	GdipDrawBezier (GpGraphics,pen, x0, y0, x1, y1, x2, y2, x3, y3)
	GdipDeletePen(pen)
End Sub
Sub ArcDrawA(ByVal x As single,ByVal y As single,ByVal width_ As single,ByVal height_ As single,ByVal startAngle As Single,ByVal sweepAngle As Single,ByVal ColorPen As integer=&hff000000,ByVal brushPen as Any Ptr=0,ByVal widthPen As Single=1)Export
	Dim As GpPen Ptr pen
	If brushPen<>0 Then
		GdipCreatePen2(brushPen,widthPen,Unitpixel,@pen)
	Else
		GdipCreatePen1(ColorPen,widthPen,Unitpixel,@pen)
	EndIf
	GdipDrawArc (GpGraphics,pen, x, y, width_, height_, startAngle,sweepAngle)
	GdipDeletePen(pen)
End Sub
Sub PieDrawA(ByVal x As single,ByVal y As single,ByVal width_ As single,ByVal height_ As single,ByVal startAngle As Single,ByVal sweepAngle As Single,ByVal ColorPen As integer=&hff000000,ByVal flagcolorBK As Integer=1,ByVal ColorBk As integer=&hff000000,ByVal brushPen as Any Ptr=0,byval brushBk as Any Ptr=0,ByVal widthPen As Single=1)Export
	Dim As GpPen Ptr pen
	If brushPen<>0 Then
		GdipCreatePen2(brushPen,widthPen,Unitpixel,@pen)
	Else
		GdipCreatePen1(ColorPen,widthPen,Unitpixel,@pen)
	EndIf
	GdipDrawPie (GpGraphics,pen, x, y, width_, height_, startAngle,sweepAngle)
	GdipDeletePen(pen)
	If flagcolorBK=1 Then
		Dim As PVOID brush
		GdipCreateSolidFill(ColorBk,@brush)
		GdipFillPie (GpGraphics,brush, x, y, width_, height_, startAngle,sweepAngle)
		GdipDeleteBrush(brush)
	ElseIf flagcolorBK=2 Then
		GdipFillPie (GpGraphics,brushBk, x, y, width_, height_, startAngle,sweepAngle)
	EndIf
End Sub
Sub PolygonDrawA(ByVal Points As Any ptr,ByVal countPoints As Integer,ByVal ColorPen As integer=&hff000000,ByVal flagcolorBK As Integer=1,ByVal ColorBk As integer=&hff000000,ByVal brushPen as Any Ptr=0,byval brushBk as Any Ptr=0,ByVal widthPen As Single=1,ByVal fillmode As Integer=0)Export
	If Points=0 Then
		Print "Error Points ptr"
		Exit Sub
	EndIf
	Dim As GpPen Ptr pen
	If brushPen<>0 Then
		GdipCreatePen2(brushPen,widthPen,Unitpixel,@pen)
	Else
		GdipCreatePen1(ColorPen,widthPen,Unitpixel,@pen)
	EndIf
	GdipDrawPolygon (GpGraphics,pen,Points,countPoints)
	GdipDeletePen(pen)
	If flagcolorBK=1 Then
		Dim As PVOID brush
		GdipCreateSolidFill(ColorBk,@brush)
		GdipFillPolygon (GpGraphics,brush,Points,countPoints,fillmode)
		GdipDeleteBrush(brush)
	ElseIf flagcolorBK=2 Then
		GdipFillPolygon (GpGraphics,brushBk,Points,countPoints,fillmode)
	EndIf
End Sub
Sub CurveDrawA(ByVal Points As Any ptr,ByVal countPoints As Integer,ByVal RoundPoints As Single=0.5,ByVal ColorPen As integer=&hff000000,ByVal flagcolorBK As Integer=1,ByVal ColorBk As integer=&hff000000,ByVal brushPen as Any Ptr=0,byval brushBk as Any Ptr=0,ByVal widthPen As Single=1,ByVal Closed As Integer=0,ByVal fillmode As Integer=0)Export
	Dim As GpPen Ptr pen
	If brushPen<>0 Then
		GdipCreatePen2(brushPen,widthPen,Unitpixel,@pen)
	Else
		GdipCreatePen1(ColorPen,widthPen,Unitpixel,@pen)
	EndIf
	If Closed=1 Then
		GdipDrawClosedCurve2(GpGraphics,pen,Points,countPoints,RoundPoints)
	Else
		GdipDrawCurve2(GpGraphics,pen,Points,countPoints,RoundPoints)
	EndIf
	GdipDeletePen(pen)
	If flagcolorBK=1 And Closed=1 Then
		Dim As PVOID brush
		GdipCreateSolidFill(ColorBk,@brush)
		GdipFillClosedCurve2(GpGraphics,brush,Points,countPoints,RoundPoints,fillmode)
		GdipDeleteBrush(brush)
	ElseIf flagcolorBK=2 And Closed=1 Then
		GdipFillClosedCurve2(GpGraphics,brushBk,Points,countPoints,RoundPoints,fillmode)
	EndIf
End Sub
Sub SetPixA(ByVal X As single,ByVal Y As Single,ByVal Color_ As integer=&hff000000)Export
	GdipBitmapSetPixel(hbitmapMaindrawGP,x,y,Cast(ARGB,Color_))
End Sub
function GetPixA(ByVal X As single,ByVal Y As Single) As Integer Export
	Dim As ulong Color_
	GdipBitmapGetPixel(hbitmapMaindrawGP,x,y,@Color_)
	Return Color_
End Function
Function CreateFontDrawA(ByVal name_ As USTRING="Arial",ByVal size As Integer=10,ByVal style As Integer=0,ByVal Unit As Integer=6) As Any Ptr Export
	Dim GpFontFamily_01 AS GpFontFamily Ptr
	Dim GpFont_01 AS GpFont Ptr
	#ifdef UNICODE
		GdipCreateFontFamilyFromName(*name_, NULL, @GpFontFamily_01)
	#else	
		GdipCreateFontFamilyFromName(WSTR(name_), NULL, @GpFontFamily_01)
	#EndIf	
	GdipCreateFont(GpFontFamily_01,size , style ,Unit, @GpFont_01)
	GdipDeleteFontFamily(GpFontFamily_01)
	Return GpFont_01
End Function
Sub FreeFontDrawA(ByVal GpFont As Any Ptr) Export
	GdipDeleteFont(GpFont)
End Sub
Sub FreeBrushA(ByVal Brush As Any Ptr) Export
	GdipDeleteBrush(brush)
End Sub
Sub TextDrawA(ByRef text As USTRING,ByVal x As Integer, ByVal y As Integer, ByVal GpFont As Any Ptr=0, ByVal color_ As Integer=&hFFFFFFFF,ByVal brush As Any Ptr=0,ByVal mode As Integer=0) Export
	DIM RECTF_01 AS RECTF
	RECTF_01.X = x
	RECTF_01.Y = y
	#ifdef UNICODE
		Dim As USTRING wbuf = *text
	#else	
		Var blen = (Len(text)*2)+2
		Dim As WString Ptr wbuf
		wbuf  = Allocate( blen )
		if wbuf = 0 then exit sub
		MultiByteToWideChar(CP_ACP, 0, StrPtr(text), -1, wbuf, blen)  	  	
	#EndIf	
	Dim Gpbrush_01 AS Gpbrush Ptr
	If brush=0 Then
		GdipCreateSolidFill(color_, Cast(GpSolidFill Ptr Ptr,@Gpbrush_01))
	Else
		Gpbrush_01=brush
	EndIf
	If mode<>0 Then GdipSetTextRenderingHint(GpGraphics,mode)
	If GpFont=0 Then
		Dim GpFontFamily_01 AS GpFontFamily Ptr
		Dim GpFont_01 AS Any Ptr
		GdipCreateFontFamilyFromName(WSTR("Arial"), NULL, @GpFontFamily_01)
		GdipCreateFont(GpFontFamily_01,12 , 0, 6, @GpFont_01)
		GdipDeleteFontFamily(GpFontFamily_01)
		GdipDrawString(GpGraphics,*wbuf, Len(text), GpFont_01, @RECTF_01, NULL, Gpbrush_01)
		GdipDeleteFont(GpFont_01)
	Else
		GdipDrawString(GpGraphics,*wbuf,Len(text) , GpFont, @RECTF_01, NULL, Gpbrush_01)
	EndIf
	If brush=0 Then
		GdipDeleteBrush(Gpbrush_01)
	EndIf
	#ifndef UNICODE
		DeAllocate(wbuf)
	#EndIf
End Sub
Sub ImageDrawA(ByVal GpImage As Any Ptr,ByVal x As single, ByVal y As single,ByVal Width_ As Single=0, ByVal Height_ As Single=0) Export
	If Width_=0 Or Height_=0 Then
		GdipDrawImage(GpGraphics,GpImage,x,y)
	Else
		GdipDrawImageRect(GpGraphics,GpImage,x,y,width_,height_)
	EndIf
End Sub
Sub ModeDrawA(ByVal mode As Integer)Export
	GdipSetSmoothingMode(GpGraphics,mode)
End Sub
Function CreateBrushA(ByVal x0 As Single=0,ByVal y0 As Single=0,ByVal x1 As Single=0,ByVal y1 As Single=0, ByVal color1 As Integer=&hFF00FF00, ByVal color2 As Integer=&hFF0000FF,ByVal GpImage As Any ptr=0,ByVal WrapMode As Integer=3 ) As Any Ptr Export
	Dim brush As PVOID
	If GpImage<>0 Then
		GdipCreateTexture(GpImage,WrapMode,@brush)
	Else
		Dim As PointF  P,P1
		P.x=x0:P.y=y0
		P1.x=x1:P1.y=y1
		GdipCreateLineBrush (Cast(GpPointF Ptr,@P), Cast(GpPointF Ptr,@P1),color1,color2,WrapMode, @brush)
	EndIf
	Return brush
End Function

Type Int___Pt2D
	As Integer x, y
End Type

Sub FillRectDrawA(byval x As Integer, byval y As Integer, byval newcol As integer) export
	Dim As long Top,temp,oldcol,ncol=newcol , wImg,hImg
	
	GdipGetImageWidth(hbitmapMaindrawGP,@wImg)
	GdipGetImageHeight(hbitmapMaindrawGP,@hImg)
	GdipBitmapGetPixel(hbitmapMaindrawGP,x,y,@oldcol)   
	If oldcol = newcol Then Exit Sub
	ReDim As Int___Pt2D Stack(3)
	Stack(0) = Type(x, y)
	Do Until Top = -1
		
		if Stack(Top).x > wImg-1 orelse Stack(Top).x < 0 orelse Stack(Top).y > hImg-1 orelse Stack(Top).y < 0 then
			
			Top -= 1
			
			continue do
			
		EndIf
		
		GdipBitmapGetPixel(hbitmapMaindrawGP,Stack(Top).x, Stack(Top).y,@temp)
		If oldcol = temp Then
			GdipBitmapSetPixel(hbitmapMaindrawGP,Stack(Top).x, Stack(Top).y,Cast(ARGB,ncol))            
			If Top + 3 > UBound(Stack) Then ReDim Preserve Stack(UBound(Stack)*2)
			Stack(Top+1) = Type(Stack(Top).x-1, Stack(Top).y)
			Stack(Top+2) = Type(Stack(Top).x, Stack(Top).y+1)
			Stack(Top+3) = Type(Stack(Top).x, Stack(Top).y-1)
			Stack(Top).x += 1
			Top += 4
		End If
		Top -= 1
	Loop
End Sub
