'Pixel Artist
'Created By:Ben Jimenez
'R1

'setup window
nomainwin
WindowWidth=750
WindowHeight=400
UpperLeftX=100
UpperLeftY=100

'setup array scale values
for a=0 to 9
array$(a)=str$(a+1)
next a

button #g.btn1,"Color",[selColor],ul,5,5,50,20
button #g.btn2,"Preview",[preview],ul,5,27,60,20
combobox #g.c1,array$(),[selItem],5,50,50,40
button #g.btn3,"Save",[save],ul,5,80,50,20

open "Pixel Artist" for graphics_nsb_nf as #g
#g,"trapclose [exit]"

'for api calls
hWnd=hwnd(#g)
hDC=GetDC(hWnd)

Width=32
Height=32

'setup working DC and bitmap
WorkingDC=CreateCompatibleDC(hDC)
newBitmap1=CreateCompatibleBitmap(hDC,Width,Height)'make new bmp image
oldWorkingDC=SelectObject(WorkingDC,newBitmap1)'new bmp into destDC

'setup drawing grid-------------
'Grid Size
#g,"color 50 50 50;down"
x=70
for xx=1 to Width
x=x+10
y=322
#g,"line ";x;" ";1;" ";x;" ";y
next

#g,"line 70 1 70 322"
y=1
for yy=1 to Height
y=y+10
#g,"line ";70;" ";y;" ";x;" ";y
next
#g,"line 70 1 390 1"

x=5:y=110
for r=1 to 11
#g,"line ";x;" ";y;" ";x+50;" ";y
y=y+20
next r

for c=1 to 3
#g,"line ";x;" ";110;" ";x;" ";y-20
x=x+25
next c
x=0
y=0
'---------------------------------
#g,"flush"
#g.c1,"selectindex 1"
selected$="1"

#g,"getbmp blank 399 1 750 330"
'setup border color
border=GetPixel(hDC,70,1)
#g,"when leftButtonMove [ExFloodFill2]";
#g,"when leftButtonUp [ExFloodFill]";
wait

[preview] 'preview pixel art
#g,"drawbmp blank 399 1"
scale=val(trim$(selected$))
'starting values
x=75
y=5
px=0
py=0
'get and set pixel colors
for yy=1 to Height
    for xx=1 to Width
        GetPix=GetPixel(hDC,x,y)

        r=SetPixel(WorkingDC,px,py,GetPix)
        'increase column values
        px=px+1
        x=x+10
    next xx
    'reset column values and increase row values
y=y+10
x=75
px=0
py=py+1
next yy
null=StretchBlt(hDC,400,1,Width*scale,Height*scale,WorkingDC,0,0,Width,Height,_SRCCOPY)
#g,"getbmp capture 0 1 750 322;cls;drawbmp capture 0 1"
#g,"flush"
unloadbmp "capture"

wait

[selColor]
colordialog last$,selcol$
if selcol$<>"" then
    last$=selcol$
    x=10
    y=115
    red=val(word$(selcol$,1))
    green=val(word$(selcol$,2))
    blue=val(word$(selcol$,3))
    ored=red
    ogreen=green
    oblue=blue
    selPixCol=GetLongColorVal(red,green,blue)

    for pal=1 to 20
        GetPix=GetPixel(hDC,x,y)
        ret=ExtFloodFill(hDC,x,y,selPixCol,GetPix)
        y=y+20

            if y>300 then
                y=115
                x=x+25
            end if
        green=green+10
            if green>=255 then red=red+10
            if red>=255 then blue=blue+10
        selPixCol=GetLongColorVal(red,green,blue)
    next pal
#g,"getbmp capture 0 1 750 322;cls;drawbmp capture 0 1"
#g,"flush"
unloadbmp "capture"
selPixCol=GetLongColorVal(ored,ogreen,oblue)
end if
wait

[save]
filedialog "Save Pixel Art","*.bmp",filename$
    if filename$<>"" then
        #g,"getbmp savebmp 400 1 ";Width*scale;" ";Height*scale
        bmpsave "savebmp",filename$
    end if
wait

[selItem]
#g.c1,"selection? selected$"
scale=val(trim$(selected$))
#g,"drawbmp blank 399 1"
 null=StretchBlt(hDC,400,1,Width*scale,Height*scale,WorkingDC,0,0,Width,Height,_SRCCOPY)
 #g,"getbmp capture 0 1 750 322;cls;drawbmp capture 0 1"
#g,"flush"
wait


[ExFloodFill]
if MouseX<70 and MouseY<322 then
    GetPix=GetPixel(hDC,MouseX,MouseY)
    r=GetRed(GetPix):g=GetGreen(GetPix):b=GetBlue(GetPix)
    selPixCol=GetLongColorVal(r,g,b)
end if


if MouseX<390 and MouseY<321 and MouseY>0 and MouseX>70 then
    GetPix=GetPixel(hDC,MouseX,MouseY)
    if GetPix<>border then
        ret=ExtFloodFill(hDC,MouseX,MouseY,selPixCol,GetPix)
    end if
end if
goto [preview]
wait

[ExFloodFill2]

if MouseX<390 and MouseY<321 and MouseY>0 and MouseX>70 then
    GetPix=GetPixel(hDC,MouseX,MouseY)
 if GetPix<>border then

  ret=ExtFloodFill(hDC,MouseX,MouseY,selPixCol,GetPix)
end if
end if
wait


[exit]
unloadbmp "blank"
#g,"cls"
close #g
null=SelectObject(WorkingDC,oldWorkingDC)
null=DeleteObject(newBitmap1)
null=DeleteObject(nbrush)
null=ReleaseDC(hWnd,hDC)
null=DeleteDC(WorkingDC)

end
'-----------functions-------------------------------------
Function ExtFloodFill(hDC,x,y,longfillcolor,longcolor)
  null=DeleteObject(nbrush)
  nbrush=CreateSolidBrush(longfillcolor)
  oldBrush=SelectObject(hDC,nbrush)

CallDll #gdi32,"ExtFloodFill",_
        hDC as Ulong,_
        x as long,_
        y as long,_
        longcolor as long,_ 'color to fill with new color
        1 as ulong,_ 'fill type
        ExtFoodFill as long
End Function

function GetLongColorVal(red,green,blue)
    if red<0 then red=0
    if red>255 then red=255
    if green<0 then green=0
    if green>255 then green=255
    if blue<0 then blue=0
    if blue>255 then blue=255
    GetLongColorVal=(blue*256*256)+(green*256)+red
end function

Function StretchBlt(hDC,xDest,yDest,wDest,hDest,sDC,xSrc,ySrc,wSrc,hSrc,dwRop)
calldll #gdi32,"StretchBlt",_
hDC as Ulong,_
xDest as long,_
yDest as long,_
wDest as long,_
hDest as long,_
sDC as Ulong,_
xSrc as long,_
ySrc as long,_
wSrc as long,_
hSrc as long,_
dwRop as Ulong,_
StretchBlt as Boolean
end function


Function CreateSolidBrush(bcolor)
    CallDll #gdi32,"CreateSolidBrush",_
        bcolor as long,_
        CreateSolidBrush as Ulong
End Function

Function GetDC(handle)
    CallDLL #user32, "GetDC", _
        handle As Ulong, _
        GetDC as Ulong
End Function

Function ReleaseDC(handle, hDC)
    CallDLL #user32, "ReleaseDC", _
        handle As Ulong, _
        hDC As Ulong, _
        ReleaseDC As Long
End Function

Function SelectObject(hDC, hPic)
    CallDLL #gdi32, "SelectObject", _
        hDC as Ulong, _
        hPic as Ulong, _
        SelectObject as Ulong
End Function

Function GetPixel(hdc,xpos,ypos)
    CallDll #gdi32,"GetPixel",_
    hdc as Ulong,_
    xpos as long,_
    ypos as long,_
    GetPixel as Ulong
End Function

function GetRed(color)
    blue=int(color/(256*256))
    green=int((color-blue*256*256)/256)
    GetRed=color-blue*256*256-green*256
    end function

function GetGreen(color)
    blue=int(color/(256*256))
    GetGreen=int((color-blue*256*256)/256)
    end function

function GetBlue(color)
    GetBlue=int(color/(256*256))
    end function


Function CreateCompatibleDC(hDC)
    CallDLL #gdi32, "CreateCompatibleDC", _
        hDC as Ulong, _
        CreateCompatibleDC as Ulong
End Function
Function CreateCompatibleBitmap(hDC, wMem, hMem)
    CallDLL #gdi32, "CreateCompatibleBitmap", _
        hDC as Ulong, _
        wMem as Long, _
        hMem as Long, _
        CreateCompatibleBitmap as Ulong
End Function

Function DeleteObject(objHandle)
    CallDll #gdi32,"DeleteObject",_
        objHandle as Ulong,_
        DeleteObject as long
End Function

Function DeleteDC(hDC)
    CallDLL #gdi32, "DeleteDC", _
        hDC as Ulong, _
        result as Boolean
End Function

Function SetPixel(hDC,x,y,longColor)
calldll #gdi32,"SetPixel",_
hDC as Ulong,_
x as Long,_
y as Long,_
longColor as Ulong,_
SetPixel as Ulong
end Function

