Ok cool
The Line function is done - I hope...
// Project: spritemodellor
// Created: 2018-12-13
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
// set window properties
SetWindowTitle( "spritemodellor" )
SetWindowSize( screenwidth, screenheight, fullscreen )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( screenwidth, screenheight ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( screenrate, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
//SetErrorMode(1)
global background_memblock, red, green, blue, palette,mouse, maxframes=0
global initialsizex=32
global initialsizey=32
global buttons as integer[10]
type _animationframes
spritesizex
spritesizey
spr
image
memblock
endtype
global sprites as _animationframes[32]
dim dataforsprite [initialsizex, initialsizey]
dim tmpcolordata [initialsizex, initialsizey] // used when drawing lines etc
title = CreateText("AGK - Sprite Modeller and Animation System")
SetTextSize(title,24)
SetTextAngle(title,270)
createpalleteandmouse()
setupspritedata()
addbuttons(1,50,730,60,"New",255,255,0)
addbuttons(2,110,730,60,"Open",255,255,0)
addbuttons(3,170,730,60,"Save",255,255,0)
addbuttons(4,230,730,60,"Apply",255,255,0)
addbuttons(5,290,730,60,"Prev",255,255,0)
addbuttons(6,350,730,60,"Next",255,255,0)
addbuttons(7,410,730,60,"Copy",255,255,0)
addbuttons(8,470,730,60,"Play",255,255,0)
addbuttons(9,530,730,60,"Clear",255,255,0)
addbuttons(17,590,730,60,"Undo",255,255,0)
addbuttons(11,650,730,60,"Rotate",0,255,0)
addbuttons(12,710,730,60,"Merge",0,255,0)
addbuttons(13,770,730,60,"Circle",0,255,0)
addbuttons(14,830,730,60,"Line",0,255,0)
addbuttons(15,890,730,60,"Shift",0,255,0)
addbuttons(10,950,730,60,"Fill",0,255,0)
addbuttons(16,1010,730,60,"Add Limb",0,0,255)
addbuttons(18,1010,730,60,"Sqaure",0,0,255)
animationframestep#=timer()
currentsprite=0 : maxframes=1
animationframe=0
player=-1
do
SetSpritePosition(mouse, getpointerx(), getpointery())
SetSpritePosition(palette,650,200)
// color selector
if GetSpriteCollision(mouse,palette)
getpixelcolor(getpointerx(),getpointery())
if GetRawMouseLeftState()
selectcolor = MakeColor(activecolorred,activecolorgreen,activecolorblue)
selectcolorred = red
selectcolorgreen = green
selectcolorblue = blue
endif
activecolorred = red
activecolorgreen=green
activecolorblue = blue
colorselected = MakeColor(activecolorred,activecolorgreen,activecolorblue)
endif
DrawBox(700,610,1000,640,colorselected,colorselected,colorselected,colorselected,1)
DrawBox(700,650,1000,680,selectcolor,selectcolor,selectcolor,selectcolor,1)
// draw large grids for srite
originx=50
originy=90
size=18
spritenumber=0
for x=0 to sprites[spritenumber].spritesizex
for y=0 to sprites[spritenumber].spritesizey
ax=originx + (x * size)
ay=originy + (y * size)
DrawBox( ax ,ay , ax+size,ay+size,MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),0)
SetSpritePosition(dataforsprite[x,y], ax, ay)
SetSpriteSize(dataforsprite[x,y],size,size)
if GetSpriteCollision(mouse,dataforsprite[x,y])
if GetRawMouseLeftState()
// if getpointerx()>ax and getpointery()>ay
// perform the functions
// Flood Fill
if fill=1
// what is the colour at this location
currentcolorred = GetSpriteColorRed(dataforsprite[x,y])
currentcolorgreen = GetSpriteColorGreen(dataforsprite[x,y])
currentcolorblue = GetSpriteColorBlue(dataforsprite[x,y])
floodfill (x,y,selectcolorred,selectcolorgreen,selectcolorblue,currentcolorred,currentcolorgreen,currentcolorblue)
// turn it off now we finihed
fill=0
SetVirtualButtonColor(10,0,255,0)
else
// Draw a line
if line=1
oldx#=getpointerx()
oldy#=getpointery()
line=2
endif
if line=2
distancex# = getpointerx() - oldx#
distancey# = getpointery() - oldy#
if abs(distancex#)>=abs(distancey#)
stp# = abs(distancex#)
else
stp# = abs(distancey#)
endif
distancex# = distancex#/stp#
distancey# = distancey#/stp#
xx#=oldx#
yy#=oldy#
col = MakeColor(selectcolorred,selectcolorgreen,selectcolorblue)
// clear the tmp color data before we use it
for xx=0 to sprites[spritenumber].spritesizex
for yy=0 to sprites[spritenumber].spritesizey
tmpcolordata[xx,yy]=0
next
next
for i=1 to stp#
drawBox((xx#),(yy#),((xx#)+size),((yy#)+size),col,col,col,col,1)
posx=floor(xx#-50)/size
posy=floor(yy#-90)/size
tmpcolordata[posx,posy]=col
inc xx#,distancex#
inc yy#,distancey#
next
else
// Draw a circle
if circle=1
oldx=getpointerx()
oldy=getpointery()
circle=2
endif
if circle=2
distancex = getpointerx() - oldx
distancey = getpointery() - oldy
col = MakeColor(selectcolorred,selectcolorgreen,selectcolorblue)
DrawEllipse(oldx+i,oldy+i,size+distancex,size+distancey,col,col,0)
else
line=0:circle=0:fill=0
SetSpriteColor(dataforsprite[x,y],selectcolorred,selectcolorgreen,selectcolorblue,255)
endif
endif
endif
endif
// endif
// if line is selected and just released it, then reset it so can create another line
if GetRawMouseLeftReleased() and line=2
line=1
// grab the data from the tmpcolordata and set the sprite with that info
for xx=0 to sprites[spritenumber].spritesizex
for yy=0 to sprites[spritenumber].spritesizey
if tmpcolordata[xx,yy]<>0
SetSpriteColor(dataforsprite[xx,yy],GetColorRed(tmpcolordata[xx,yy]),GetColorGreen(tmpcolordata[xx,yy]),GetColorBlue(tmpcolordata[xx,yy]),255)
tmpcolordata[xx,yy]=0
endif
next
next
endif
if GetRawMouseLeftReleased() and circle=2 then circle=1
if GetRawMouseRightState()
SetSpriteColor(dataforsprite[x,y],0,0,0,255)
endif
endif
next
next
print(line)
// draw the smaller version of the sprite and the animation frames
originx=800
originy=100
spritenumber=0
for x=0 to sprites[spritenumber].spritesizex
for y=0 to sprites[spritenumber].spritesizey
cr = GetSpriteColorRed(dataforsprite[x,y])
cg = GetSpriteColorGreen(dataforsprite[x,y])
cb = GetSpriteColorBlue(dataforsprite[x,y])
c = MakeColor(cr,cg,cb)
ax =originx + x
ay = originy + y
DrawBox(ax ,ay, ax+1,ay+1,c,c,c,c,1)
next
next
// draw the sprites animation steps along the top
originx=50
originy=10
spritenumber=0
for y=0 to 25
for x=0 to 25
if spritenumber<sprites.length
ax= originx+(x * (sprites[spritenumber].spritesizex))
ay= originy + (y * (sprites[spritenumber].spritesizex))
SetSpritePosition(sprites[spritenumber].spr,ax,ay)
// draw box around the currentsprite, so we know which one we working with
if spritenumber=currentsprite then DrawBox(ax,ay,ax+sprites[spritenumber].spritesizex,ay+sprites[spritenumber].spritesizey,MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),0)
endif
inc spritenumber
next
next
// new sheet
if GetVirtualButtonPressed(1)=1
for spritenumber=0 to sprites.length
DeleteSprite(sprites[spritenumber].spr)
DeleteMemblock(sprites[spritenumber].memblock)
DeleteImage(sprites[spritenumber].image)
next
setupspritedata()
currentsprite=0
endif
// open sheet
if GetVirtualButtonPressed(2)=1
openspritesheet("test.png",32,32)
endif
// save sheet
if GetVirtualButtonPressed(3)=1
savespritesheet()
endif
// apply
if GetVirtualButtonPressed(4)=1
applyimage(currentsprite)
endif
// Goto previous frame
if GetVirtualButtonPressed(5)=1
dec currentsprite
endif
// GO TO NEXT FRAME
if GetVirtualButtonPressed(6)=1
applyimage(currentsprite)
inc maxframes
inc currentsprite
endif
// COPY
if GetVirtualButtonPressed(7)=1
inc maxframes
inc currentsprite
applyimage(currentsprite)
endif
if GetVirtualButtonPressed(8)=1
player=-player
if player=1
DeleteSprite(animation)
animation=CreateSprite(0)
for a=1 to maxframes-1
AddSpriteAnimationFrame(animation,sprites[a].image)
next
PlaySprite(animation)
else
stopsprite(animation)
endif
SetSpritePosition(animation,700,100)
endif
if GetVirtualButtonPressed(9) = 1
spritenumber=0
for x=0 to sprites[spritenumber].spritesizex
for y=0 to sprites[spritenumber].spritesizey
SetSpriteColor(dataforsprite[x,y],0,0,0,255)
next
next
endif
// Fill
if GetVirtualButtonPressed(10)=1
if fill=0
fill=1
SetVirtualButtonColor(10,0,100,0)
SetVirtualButtonColor(13,0,255,0) : circle=0
else
fill=0
SetVirtualButtonColor(10,0,255,0)
endif
endif
// Circle
if GetVirtualButtonPressed(13)=1
if circle=0
circle=1
SetVirtualButtonColor(10,0,255,0)
SetVirtualButtonColor(13,0,100,0)
else
circle=0
SetVirtualButtonColor(13,0,255,0)
endif
endif
// Line
if GetVirtualButtonPressed(14)=1
if line=0
line=1
SetVirtualButtonColor(10,0,255,0)
SetVirtualButtonColor(13,0,255,0)
SetVirtualButtonColor(14,0,100,0)
else
line=0
SetVirtualButtonColor(14,0,255,0)
endif
endif
//
if GetRawKeyPressed(27) then end
SetTextPosition(title,10,screenheight/2 + GetTextTotalWidth(title)/2)
print(ScreenFPS())
Render2DBack()
Sync()
loop
function floodfill ( x,y, nred,ngreen,nblue, ored,ogreen,oblue)
if x<0 or x>32 or y<0 or y>32
exitfunction
endif
cred = GetSpriteColorRed(dataforsprite[x,y])
cgreen = GetSpriteColorGreen(dataforsprite[x,y])
cblue = GetSpriteColorBlue(dataforsprite[x,y])
if cred = ored and cgreen = ogreen and cblue = oblue
SetSpriteColor(dataforsprite[x,y],nred,ngreen,nblue,255)
floodfill (x+1,y,nred,ngreen,nblue,ored,ogreen,oblue)
floodfill (x,y+1,nred,ngreen,nblue,ored,ogreen,oblue)
floodfill (x-1,y,nred,ngreen,nblue,ored,ogreen,oblue)
floodfill (x,y-1,nred,ngreen,nblue,ored,ogreen,oblue)
endif
endfunction
function setupspritedata()
for spritenumber=0 to sprites.length
for x=0 to initialsizex
for y=0 to initialsizey
sprites[spritenumber].spr = CreateSprite(0)
sprites[spritenumber].spritesizex = initialsizex
sprites[spritenumber].spritesizey = initialsizey
setspritecolor(sprites[spritenumber].spr,0,0,0,255)
SetSpriteSize(sprites[spritenumber].spr,initialsizex,initialsizey)
SetSpriteColor(sprites[spritenumber].spr,255,255,255,255)
next
next
next
for x=0 to initialsizex
for y=0 to initialsizey
dataforsprite[x,y] = CreateSprite(0)
SetSpriteSize(dataforsprite[x,y],initialsizex,initialsizey)
SetSpriteColor(dataforsprite[x,y],0,0,0,255)
inc spritenumber
next
next
for spritenumber=0 to sprites.length
applyimage(spritenumber)
next
endfunction
function createpalleteandmouse()
//create palette
swap()
DrawBox(0,0,200,100,MakeColor(0,0,0),MakeColor(255,0,0),MakeColor(255,255,255),MakeColor(0,255,255),1)
DrawBox(0,100,200,200,MakeColor(255,255,255),MakeColor(0,255,255),MakeColor(0,0,255),MakeColor(0,255,255),1)
DrawBox(0,200,200,300,MakeColor(0,0,255),MakeColor(0,255,255),MakeColor(255,255,0),MakeColor(255,0,255),1)
DrawBox(0,300,200,400,MakeColor(255,255,0),MakeColor(255,0,255),MakeColor(255,255,255),MakeColor(0,0,0),1)
DrawBox(200,0,400,100,MakeColor(255,0,0),MakeColor(0,255,0),MakeColor(0,255,255),MakeColor(255,255,255),1)
DrawBox(200,100,400,200,MakeColor(0,255,255),MakeColor(255,255,255),MakeColor(0,255,255),MakeColor(255,255,255),1)
DrawBox(200,200,400,300,MakeColor(0,255,255),MakeColor(255,255,255),MakeColor(255,0,255),MakeColor(0,255,0),1)
DrawBox(200,300,400,400,MakeColor(255,0,255),MakeColor(0,255,0),MakeColor(0,0,0),MakeColor(0,0,0),1)
render()
palette = CreateSprite(GetImage(0,0,400,400))
background_memblock = CreateMemblockFromImage(palette)
mouse= CreateSprite(getimage(0,0,1,1))
SetSpriteSize(mouse,1,1)
endfunction
function applyimage(currentsprite)
sprites[currentsprite].image = getimage(0,0,sprites[currentsprite].spritesizex+1,sprites[currentsprite].spritesizey+1)
sprites[currentsprite].memblock = CreateMemblockFromImage (sprites[currentsprite].image)
background_imgwidth = GetMemblockInt(sprites[currentsprite].memblock, 0)
background_imgheight = GetMemblockInt(sprites[currentsprite].memblock, 4)
for xx=0 to sprites[currentsprite].spritesizex
for yy=0 to sprites[currentsprite].spritesizey
offset = 12 + (((Yy * background_imgwidth) + Xx) * 4)
cr = GetSpriteColorRed(dataforsprite[xx,yy])
cg = GetSpriteColorGreen(dataforsprite[xx,yy])
cb = GetSpriteColorBlue(dataforsprite[xx,yy])
SetMemblockByte(sprites[currentsprite].memblock, offset, cr)
SetMemblockByte(sprites[currentsprite].memblock, offset+1, cg)
setMemblockByte(sprites[currentsprite].memblock, offset+2, cb)
setMemblockByte(sprites[currentsprite].memblock, offset+3, 254) // 254 is air, can move in it
next
next
// deleteimage (img)
sprites[currentsprite].image = CreateImageFromMemblock(sprites[currentsprite].memblock)
//DeleteMemblock(background_memblock)
SetSpriteImage(sprites[currentsprite].spr,sprites[currentsprite].image)
endfunction
// Get a pixel color - which will provide what the color is on the loaded image it is at any
// given location
// a lemming can move on a completely blank area
function getpixelcolor( X, Y)
if (x>getspritex(palette) and y>GetSpriteY(palette) and x<getspritex(palette)+GetSpriteWidth(palette) and y<GetSpriteY(palette)+GetSpriteHeight(palette))
offset = 12 + ((((Y-getspritey(palette)) * GetSpriteWidth(palette)) + (X-getspritex(palette))) * 4)
if offset>0
red=GetMemblockByte(background_memblock, offset)
green=GetMemblockByte(background_memblock, offset+1)
blue=GetMemblockByte(background_memblock, offset+2)
endif
endif
endfunction
function addbuttons(id,x,y,size,name$,colorred,colorgreen,colorblue)
AddVirtualButton(id,x,y,size)
SetVirtualButtonAlpha(id,255)
SetVirtualButtonColor(id,colorred,colorgreen,colorblue)
SetVirtualButtonText(id,name$)
//GetVirtualButtonExists
//GetVirtualButtonPressed
//GetVirtualButtonReleased
//GetVirtualButtonState
//SetButtonScreenPosition
//SetVirtualButtonActive
//SetVirtualButtonImageDown
//SetVirtualButtonImageUp
//SetVirtualButtonPosition
// SetVirtualButtonSize(
endfunction
function savespritesheet()
// lets build an image based on the size
sync()
spritenumber=0
for y=0 to 16
for x=0 to 16
if spritenumber<maxframes
SetSpritePosition(sprites[spritenumber].spr,x*32,y*32)
endif
inc spritenumber
next
next
sync()
render()
img = getimage(0,0,maxframes * 32,32)
SaveImage(img,"test.png")
endfunction
function openspritesheet(sheet$, sizex,sizey)
/* img = CreateSprite(LoadImage(sheet$))
sprites[currentsprite].image = getimage(0,0,sprites[currentsprite].spritesizex+1,sprites[currentsprite].spritesizey+1)
sprites[currentsprite].memblock = CreateMemblockFromImage (sprites[currentsprite].image)
background_imgwidth = GetMemblockInt(sprites[currentsprite].memblock, 0)
background_imgheight = GetMemblockInt(sprites[currentsprite].memblock, 4)
for xx=0 to sprites[currentsprite].spritesizex
for yy=0 to sprites[currentsprite].spritesizey
offset = 12 + (((Yy * background_imgwidth) + Xx) * 4)
cr = GetSpriteColorRed(dataforsprite[xx,yy])
cg = GetSpriteColorGreen(dataforsprite[xx,yy])
cb = GetSpriteColorBlue(dataforsprite[xx,yy])
SetMemblockByte(sprites[currentsprite].memblock, offset, cr)
SetMemblockByte(sprites[currentsprite].memblock, offset+1, cg)
setMemblockByte(sprites[currentsprite].memblock, offset+2, cb)
setMemblockByte(sprites[currentsprite].memblock, offset+3, 254) // 254 is air, can move in it
next
next
*/
endfunction
Shall work on tidying up the GUI a bit, like make a much better palette and work on doing a circle and a square..