So here is a new version with the Sprite Sheet creation - these goto the Local appdata area to start with until i add a editbox style thingy to store to where you want. - but thats for another day..
The sprite animation now works with PlaySprite.
I just need to add a template AppGameKit code using loads of WriteLines to a file, this will provide a AppGameKit starting template to load that sprite sheet and play it this will occur when Save it.
Im going to now fine tune it all, correcting any boundary errors i may get and just make it more robust
Catch up soon
// 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]
createpalleteandmouse()
setupspritedata()
addbuttons(1,50,730,60,"New",255,255,0)
addbuttons(2,150,730,60,"Open",255,255,0)
addbuttons(3,250,730,60,"Save",255,255,0)
addbuttons(4,350,730,60,"Apply",255,255,0)
addbuttons(5,450,730,60,"Prev",255,255,0)
addbuttons(6,550,730,60,"Next",255,255,0)
addbuttons(7,650,730,60,"Copy",255,255,0)
addbuttons(8,750,730,60,"Play",255,255,0)
addbuttons(9,850,730,60,"Clear",255,255,0)
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()
SetSpriteColor(dataforsprite[x,y],selectcolorred,selectcolorgreen,selectcolorblue,255)
endif
if GetRawMouseRightState()
SetSpriteColor(dataforsprite[x,y],0,0,0,255)
endif
endif
next
next
// 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
// 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
//
if GetRawKeyPressed(27) then end
Render2DBack()
Sync()
loop
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)
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
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()
// how many are there
// maxsprites
// what size is each one
// spritesizex
// spritesizey
// 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
There is also an Open to do, where can select a sprite sheet and it imports that into it.
Ooooh what happens if those arent 32x32..
Lets leave that for a another day too - head about to explode