Added a Dissolve feature
// Project: AGK-Paint
// Created: 2017-02-25
// show all errors
SetErrorMode(2)
global width=1024
global height=768
// set window properties
SetWindowTitle( "AGK-Paint" )
SetWindowSize( width, height, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
global mem,imgwidth, imgheight, image, img, mouse
global x1, y1, x2, y2 // Used to remeber coordintes if need be
global frames as integer [100] // Used for Animation Frames
global currentframe = 1 // Used so you can make changes to the current frame
global selection as integer // 0 = free hand, 1=line, 2=circle
global maxframes = 1
global filename$ = "test.png" // This is the variable that stores the file - so we can change the string
// for when we save the images _1 _2 _3 _4 etc
global zoom, startx, starty
// Deal with the spectrum variables
global spectrum as integer // The color spectrum
global rainbowmem as integer // Used to pick a colour from the spectrum
global spectrummemory as integer
global colorselected, colorselectedred, colorselectedgreen, colorselectedblue as integer
global gradient as integer
global gradientsprite as integer
// Blending variables
global colorred, colorgreen, colorblue, newframe
// Create a pin point mouse so can do collistion detection as its a sprite to sprite collision
img=getimage(0,0,1,1)
mouse = createsprite(img)
SetRawMousePosition(width/2,height/2)
// Build and create the spectrum
rainbowmem = CreateRainBow(200,200)
spectrum = CreateSprite(rainbowmem)
spectrummemory=CreateMemblockFromImage(spectrum)
// Build a gradient
buildgradient(makecolor(255,255,255))
img = loadimage(filename$)
mem=CreateMemblockFromImage(img)
imgwidth = GetMemblockInt(mem, 0)
imgheight = GetMemblockInt(mem, 4)
image=CreateSprite(img)
AddSpriteAnimationFrame( image, GetSpriteImageID( image ))
Frames[maxframes] = CloneSprite(image)
Zoom = 10
do
SetSpritePosition(mouse, getpointerx(), getpointery())
displaythumbnail()
displayeditor ()
edit( selection)
// updatethumbnail( image )
drawspectrum()
drawtoolbox()
checkcollisions()
checkkeypresses()
print(GetRawLastKey())
Render2Dback ()
Sync()
loop
function drawtoolbox()
col = MakeColor(255,255,255)
toolselected = MakeColor(255,0,0)
x=1 : y=200 :oldy=y : size=50
for xx = 1 to 2
for yy= 1 to 6
drawbox (x + (xx * size), y + (yy * size ), x + (xx * size) + size, y + (yy * size) + size, col, col, col, col, 1)
if getpointerx() > x + (xx * size) and getpointery()>y + (yy * size ) and getpointerx() < x + (xx * size) + size and getpointery() < y + (yy * size) + size
for a=1 to 4
drawbox (x + (xx * size)+a, y + (yy * size )+a, x + (xx * size) + size-a, y + (yy * size) + size-a, toolselected, toolselected, toolselected, toolselected, 0)
next
endif
inc y, size / 2
next
inc x, size / 2
y=oldy
next
x=width-200 : y=200 :oldy=y : size=50
for xx = 1 to 2
for yy= 1 to 6
drawbox (x + (xx * size), y + (yy * size ), x + (xx * size) + size, y + (yy * size) + size, col, col, col, col, 1)
if getpointerx() > x + (xx * size) and getpointery()>y + (yy * size ) and getpointerx() < x + (xx * size) + size and getpointery() < y + (yy * size) + size
for a=1 to 4
drawbox (x + (xx * size)+a, y + (yy * size )+a, x + (xx * size) + size-a, y + (yy * size) + size-a, toolselected, toolselected, toolselected, toolselected, 0)
next
endif
inc y, size / 2
next
inc x, size / 2
y=oldy
next
endfunction
function checkcollisions()
// Check collisions of the Frames area
endfunction
function saveframes()
for a=1 to maxframes
partfile$ = mid(filename$, 1, len(filename$) - 4)
lastpart$ = mid(filename$, len(filename$)-3, len(filename$))
newfile$ = partfile$ + "_" + str(a) + lastpart$
SaveImage( GetSpriteImageID(frames[a]), newfile$)
next
endfunction
function drawspectrum()
// SetSpriteOffset(spectrum,0,0)
// SetSpritePositionByOffset(spectrum, 0, 0)
// SetSpriteAngle(spectrum,-90)
SetSpriteOffset(spectrum, 0,0)
wherex=getpointerx()
wherey=getpointery()
SetSpritePosition(gradientsprite, width-179,0)
// if (wherex> getspritex(spectrum) and wherex<getspritex(spectrum)+GetSpriteWidth(spectrum) and wherey>getspritey(spectrum) and wherey<getspritey(spectrum)+GetSpriteHeight(spectrum))
if GetSpriteCollision(mouse, spectrum) = 1
if GetRawMouseLeftPressed()=1
col=getpixelcolor(spectrum, rainbowmem, wherex, wherey, 2)
colorselected=col
buildgradient(col)
endif
DrawEllipse(width,0,200,200,colorselected<3,colorselected,1)
endif
DrawBox(50,height-80,90,height-20,colorselected,colorselected,colorselected,colorselected,1)
endfunction
function buildgradient(col)
DrawEllipse(width,0,200,200,col<3,col,1)
gradient = getimage(width-200,0, width, 200)
gradientsprite = CreateSprite(gradient)
endfunction
function checkkeypresses( )
if GetRawKeyPressed(48) = 1 then selection=0 // Free
if GetRawKeyPressed(49) = 1 then selection=1 // Line
if GetRawKeyPressed(50) = 1 then selection=2 // Air Brush
if GetRawKeyPressed(51) = 1 then selection=3 // Paint Brush
if GetRawKeyPressed(65) = 1 then createblankimage() // A = Add a frame
if GetRawKeyPressed(67) = 1 then copyframe( ) // C = Copy a frame
if GetRawKeyPressed(80) = 1 then frameplay() // P = Play Sprite Frames
if GetRawKeyPressed(83) = 1 then SaveFrames() // Save
if GetRawKeyPressed(38) = 1 then inc zoom
if GetRawKeyPressed(40) = 1 then dec zoom
if GetRawKeyPressed(68) = 1 then dissolve() // D = Dissolve
if zoom<1 then zoom=1
if zoom>10 then zoom=10
endfunction
function frameplay()
frametime#=timer()
CFrame = 1
anim = createsprite(0)
frametime#=timer()
repeat
for a=1 to maxframes
SetSpritePosition(frames[a], -100,-100)
next
if timer()-frametime# < .5
SetSpritePosition(frames[CFrame], width/2, height/2)
//AddSpriteAnimationFrame(anim,frames[a])
inc cframe
if cframe>maxframes then cframe=1
frametime#=timer()
sync()
endif
until GetRawKeyPressed(27)=1
/*SetSpritePosition(anim, width/2, height/2)
PlaySprite(anim,20)
repeat
sync()
until GetRawKeyPressed(27)=1
*/
// DeleteSprite(anim)
endfunction
function edit( mode)
outline=MakeColor(0,0,0)
x=startx
y=starty
for xx = 1 to imgwidth-1
for yy = 1 to imgheight-1
if GetRawMouseLeftState() = 1
x1 = xx * zoom
y1 = yy * zoom
if getpointerx() > x and getpointery() > y and getpointerx() < x+ zoom and getpointery() < y + zoom
select mode
case 0 : // free hand
setpixelcolor(frames[currentframe], xx, yy, colorselectedred,colorselectedgreen,colorselectedblue,255)
endcase
case 1 :
drawline(x1, y1, getpointerx(), getpointery(), makecolor(255,255,255), makecolor(255,255,255))
endcase
case 2 : // air brush
setpixelcolor(frames[currentframe],xx-1, yy, colorselectedred,colorselectedgreen,colorselectedblue,255)
setpixelcolor(frames[currentframe], xx-1, yy+1, colorselectedred,colorselectedgreen,colorselectedblue,255)
setpixelcolor(frames[currentframe], xx, yy+1, colorselectedred,colorselectedgreen,colorselectedblue,255)
setpixelcolor(frames[currentframe], xx+1, yy+1, colorselectedred,colorselectedgreen,colorselectedblue,255)
setpixelcolor(frames[currentframe], xx+1, yy, colorselectedred,colorselectedgreen,colorselectedblue,255)
setpixelcolor(frames[currentframe], xx+1, yy-1, colorselectedred,colorselectedgreen,colorselectedblue,255)
setpixelcolor(frames[currentframe], xx-1, yy, colorselectedred,colorselectedgreen,colorselectedblue,255)
setpixelcolor(frames[currentframe], xx-1, yy-1, colorselectedred,colorselectedgreen,colorselectedblue,255)
setpixelcolor(frames[currentframe], xx, yy, colorselectedred,colorselectedgreen,colorselectedblue,255)
endcase
case 3: // Paint brush
for a=1 to 4
setpixelcolor(frames[currentframe], xx-a, yy, colorselectedred,colorselectedgreen,colorselectedblue,255)
setpixelcolor(frames[currentframe], xx+a, yy, colorselectedred,colorselectedgreen,colorselectedblue,255)
next
endcase
case default
endcase
endselect
endif
endif
inc y, zoom
next
inc x, zoom
y=starty
next
endfunction
function copyframe( )
inc maxframes
inc currentframe
newimg=CloneSprite(frames[currentframe-1])
//AddSpriteAnimationFrame( frames[currentframe], GetSpriteImageID(frames[currentframe-1]))
Frames[maxframes]=newimg
mem=CreateMemblockFromImage(GetSpriteImageID(Frames[CurrentFrame]))
endfunction
function createblankimage()
startx = ((width -(imgwidth * zoom) /2) - (imgwidth * zoom)/2) / 2
starty = ((height-(imgheight * zoom) /2) - (imgheight * zoom) /2) /2
outline=MakeColor(0,0,0)
x=startx
y=starty
c1 = 40
c2 = 190
stepx=-imgwidth
for xx = 1 to imgwidth-1
for yy = 1 to imgheight-1
setpixelcolor(frames[currentframe], xx, yy, col,col,col,0)
setpixelcolor(frames[currentframe], xx, yy, col,col,col,1)
DrawBox(x, y, x + zoom, y + zoom, col, col, col, col, 1)
inc stepy
col=c1
if stepy>imgheight/(imgheight/2)
col=c2
stepy=-imgheight/(imgheight/2)
endif
inc y, zoom
next
inc x, zoom
y=starty
next
endfunction
function displaythumbnail( )
start=50
framewidth=imgWidth
white=MakeColor(255,255,255)
for a=1 to maxframes //10 Frames
DrawBox(start + (a * framewidth) , height-70 , start + (a * framewidth) + framewidth , height-70+framewidth, white,white,white,white,0)
SetSpritePosition( Frames[a], start + (a * framewidth) , height-70 )
if getpointerx() > start + (a * framewidth) and getpointery() > height-70 and getpointerx() < start + (a * framewidth) + framewidth and getpointery() < height-70+framewidth
print (" Frame Selected " + str(a))
if GetPointerPressed()
currentframe=a
mem=CreateMemblockFromImage(GetSpriteImageID(frames[a]))
endif
endif
next
endfunction
function displayeditor ( )
outline=MakeColor(0,0,0)
startx = ((width -(imgwidth * zoom) /2) - (imgwidth * zoom)/2) / 2
starty = ((height-(imgheight * zoom) /2) - (imgheight * zoom) /2) /2
x=startx
y=starty
for xx = 1 to imgwidth-1
for yy = 1 to imgheight-1
c=0
c=getpixelcolor(frames[currentframe],mem, xx, yy,1)
// DrawBox(x, y, x + units, y + units, outline, outline, outline, outline, 0)
DrawBox(x, y, x + zoom, y + zoom, c, c, c, c, 1)
inc y, zoom
next
inc x, zoom
y=starty
next
endfunction
function setpixelcolor(img, wherex, wherey, color_r, color_g, color_b, color_a)
offset=12 + ((((wherey) * GetSpriteWidth(img)) + wherex) *4)
if color_a = 1 // 1 = True for Transparency, used to create a blank worksheet
setMemblockByte(mem,offset+3, 0)
else
if color_a<>256
setMemblockByte(mem,offset, color_r)
setMemblockByte(mem,offset+1, color_g)
setMemblockByte(mem,offset+2, color_b)
setMemblockByte(mem,offset+3, 255)
img = CreateImageFromMemblock( mem )
SetSpriteImage(Frames[currentframe], img)
else
setMemblockByte(newframe,offset, color_r)
setMemblockByte(newframe,offset+1, color_g)
setMemblockByte(newframe,offset+2, color_b)
setMemblockByte(newframe,offset+3, 255)
img = CreateImageFromMemblock( newframe )
SetSpriteImage(Frames[3], img)
endif
endif
endfunction
function getpixelcolor(img, mem, wherex, wherey, mode)
// offset=12 + ((((wherey-getspritey(img)) * GetSpriteWidth(img)) + wherex) *4)
offset=12 + ((((wherey) * GetSpriteWidth(img)) + wherex) *4)
select mode
case 1 : // Colour from the pictures
r=GetMemblockByte(mem,offset)
g=GetMemblockByte(mem,offset+1)
b=GetMemblockByte(mem,offset+2)
a=GetMemblockByte(mem,offset+3)
colorred = r
colorgreen=g
colorblue =b
endcase
case 2 : //Colour from the rainbow
r=GetMemblockByte(spectrummemory,offset)
g=GetMemblockByte(spectrummemory,offset+1)
b=GetMemblockByte(spectrummemory,offset+2)
a=GetMemblockByte(spectrummemory,offset+3)
colorselectedred = r
colorselectedgreen = g
colorselectedblue = b
endcase
endselect
color=makecolor(r,g,b)
endfunction color
Function CreateRainBow(w,h)
// create the rainbow
clearscreen()
render()
local r,g,b as float
r = 255
g = 0
b = 0
phase = 0
a as float
a = (255*6)
a = a/h
c = makecolor(r,g,b)
For i = 0 to h
DrawEllipse(0,0,h-i,h-i,c,c,1)
// Drawbox(i, 0, i+1, h, c,c,c,c,1)
if phase = 0
b = b + a
if b >= 255
b=255
phase = 1
endif
elseif phase = 1
r = r - a
if r<=0
r=0
phase = 2
endif
elseif phase = 2
g = g + a
if g>=255
g=255
phase = 3
endif
elseif phase = 3
// dec b
b = b -a
if b<= 0
b=0
phase = 4
endif
elseif phase = 4
// inc r
r = r + a
if r>=255
r=255
phase =5
endif
elseif phase = 5
// dec g
g = g -a
if g<= 0
g=0
//exit
endif
endif
c = makecolor(r,g,b)
next
swap()
img=getimage(0,0,w,h)
EndFunction img
function dissolve ()
outline=MakeColor(0,0,0)
mem=CreateMemblockFromImage(GetSpriteImageID(Frames[1]))
lastframe = CreateMemblockFromImage(GetSpriteImageID(frames[2]))
for step# = 0 to 1 step 0.05
x=startx
y=starty
inc currentframe : inc maxframes
frames[currentframe] = CloneSprite(frames[currentframe-1])
newframe = CreateMemblockFromImage(GetSpriteImageID(frames[currentframe]))
for xx = 1 to imgwidth-1
for yy = 1 to imgheight-1
getpixelcolor(frames[1],mem, xx, yy,1)
color1red = colorred : color1green=colorgreen : color1blue = colorblue
getpixelcolor(frames[2], lastframe, xx, yy,1)
color2red = colorred : color2green=colorgreen : color2blue = colorblue
color3red = color1red + ( color2red - color1red ) * step#
color3green = color1green + ( color2green - color1green ) * step#
color3blue = color1blue + ( color2blue - color1blue ) * step#
setpixelcolor(frames[currentframe], xx, yy, color3red,color3green, color3blue,256)
c=getpixelcolor(frames[currentframe],newframe, xx, yy,1)
DrawBox(x, y, x + zoom, y + zoom, c,c,c, 255, 1)
inc y, zoom
next
inc x, zoom
y=starty
next
sync()
// sleep(1000)
img = CreateImageFromMemblock(newframe)
frames[currentframe] = CreateSprite(img)
next
endfunction
To use:-
Press "C" to create a copy of the first image, so you have now two in the Frame section
Draw anything you like in both of them - make sure they are diferent
Press "D" to Dissolve from one to the other
See the resulting frames in the frames bar
Press "S" to save the image frames in your local APPDATA AppGameKit area
Please let me know what you all think
Damo