Yeah, thanks for the headsup, will do that.
Here's an updated example, this one lets you destroy with the lmb, and draw with the rmb - scroll with arrow keys, and space to zoom in or out. Seems very fast to me, faster than I thought AppGameKit would be with memblocks. Probably decent on Android as well, although I couldn't test it properly without a mouse and keyboard.
Anyway, new example code:
rem
rem AGK 2D destructable memblock terrain
rem
rem Landscape App
SetDisplayAspect( 1024.0/768.0 )
SetVirtualResolution(1024,768)
SetViewZoomMode( 0 )
gosub init
scrollx=0
scrolly=0
zoom#=1.0
do
Print("FPS:"+str(screenfps()))
print(str(getrawlastkey()))
mx=getrawmousex()/zoom#
my=getrawmousey()/zoom#
mb=getrawmouseleftstate()+(getrawmouserightstate()*2)
if mb=1
ex=mx+scrollx
ey=my+scrolly
er=8
terr_explosion(ex,ey,er)
terr_update()
//sleep(10) //Don't know why it needs this - probably to allow time to update something internal
endif
if mb=2
ex=mx+scrollx
ey=my+scrolly
er=8
terr_draw(ex,ey,er)
terr_update()
//sleep(10) //Don't know why it needs this - probably to allow time to update something internal
endif
if getrawkeystate(32)=1
if zoom#>1.0
zoom#=1.0
scrollx=0
scrolly=0
else
scrollx=(mx+scrollx)-(512/zoom#)
scrolly=(my+scrolly)-(384/zoom#)
zoom#=4.0
endif
while getrawkeystate(32)=1
sync()
endwhile
endif
if getrawkeystate(38)=1 then dec scrolly
if getrawkeystate(40)=1 then inc scrolly
if getrawkeystate(37)=1 then dec scrollx
if getrawkeystate(39)=1 then inc scrollx
if scrollx<0 then scrollx=0
if scrollx>1024-(640/zoom#) then scrollx=1024-(640/zoom#)
if scrolly<0 then scrolly=0
if scrolly>1024-(480/zoom#) then scrolly=1024-(480/zoom#)
//update particles
particle_update()
setviewoffset(scrollx,scrolly)
setviewzoom(zoom#)
Sync()
loop
end
init:
dim terr_ID[8,8]
dim terr_Update[8,8]
loadimage(100,"backdrop.png")
loadimage(101,"terrain.png")
creatememblockfromimage(101,101)
ID=1
for y=0 to 7
for x=0 to 7
terr_ID[x,y]=ID
terr_Update[x,y]=1
mem_surgeon(101,ID,x*128,y*128,128,128)
createimagefrommemblock(ID,ID)
createsprite(ID,ID)
setspriteoffset(ID,0,0)
setspritedepth(ID,5)
setspritepositionbyoffset(ID,x*128,y*128)
inc ID
next x
next y
createsprite(100,100)
setspritesize(100,2048,2048)
setspritepositionbyoffset(100,320,240)
setspritedepth(100,100)
//particles
type _particle
x as float
y as float
xs as float
ys as float
life as float
endtype
global part_count=512
dim part[part_count] as _particle
global part_cur colr colg colb cola
return
function terr_explosion(x,y,r)
for yy=y-r to y+r
for xx=x-r to x+r
dx#=xx-x
dy#=yy-y
d#=sqrt((dx#*dx#)+(dy#*dy#))
if d#<=r
terr_killpixel(xx,yy)
endif
next xx
next yy
endfunction
function terr_draw(x,y,r)
for yy=y-r to y+r
for xx=x-r to x+r
dx#=xx-x
dy#=yy-y
d#=sqrt((dx#*dx#)+(dy#*dy#))
if d#<=r
terr_resetpixel(xx,yy)
endif
next xx
next yy
endfunction
function terr_killpixel(x,y)
tx=x/128
ty=y/128
if tx<0 or ty<0 or tx>7 or ty>7 then exitfunction
ID=terr_ID[tx,ty]
ox=tx*128
oy=ty*128
mem_pixel_alpha(ID,x-ox,y-oy,0)
if cola>0 then particle_spawn(x,y,colr,colg,colb)
terr_Update[tx,ty]=1
endfunction
function terr_resetpixel(x,y)
tx=x/128
ty=y/128
if tx<0 or ty<0 or tx>7 or ty>7 then exitfunction
ID=terr_ID[tx,ty]
ox=tx*128
oy=ty*128
mem_pixel_alpha(ID,x-ox,y-oy,255)
terr_Update[tx,ty]=1
endfunction
function terr_checkalpha(x,y)
tx=x/128
ty=y/128
if tx<0 or ty<0 or tx>7 or ty>7 then exitfunction 0
ID=terr_ID[tx,ty]
ox=tx*128
oy=ty*128
a=get_mem_pixel_alpha(ID,x-ox,y-oy)
endfunction a
function terr_update()
for y=0 to 7
for x=0 to 7
if terr_Update[x,y]=1
ID=terr_ID[x,y]
terr_Update[x,y]=0
createimagefrommemblock(ID,ID)
setimageminfilter(ID,0)
setimagemagfilter(ID,0)
endif
next x
next y
endfunction
function mem_pixel_alpha(ID,xx,yy,v)
if xx<0 or xx>127 or yy<0 or yy>127 then exitfunction
pos=12+(((yy*128)+xx)*4)
//store current pixel colour
colr=getmemblockbyte(ID,pos+0)
colg=getmemblockbyte(ID,pos+1)
colb=getmemblockbyte(ID,pos+2)
cola=getmemblockbyte(ID,pos+3)
//set alpha
setmemblockbyte(ID,pos+3,v) //A
endfunction
function get_mem_pixel_alpha(ID,xx,yy)
if xx<0 or xx>127 or yy<0 or yy>127 then exitfunction 0
pos=12+(((yy*128)+xx)*4)
a=getmemblockbyte(ID,pos+3) //A
endfunction a
function mem_surgeon(src,dst,x,y,w,h)
if getmemblockexists(src)=0
message("Memblock exists?")
exitfunction
endif
if getmemblockexists(dst)=0
sz=12+(h*w*4)
creatememblock(dst,sz)
setmemblockint(dst,0,w)
setmemblockint(dst,4,h)
setmemblockint(dst,8,32)
endif
for dx=0 to w-1
for dy=0 to h-1
sx=x+dx
sy=y+dy
spos=12+(((sy*1024)+sx)*4)
dpos=12+(((dy*128)+dx)*4)
for c=0 to 3
v=getmemblockbyte(src,spos+c)
setmemblockbyte(dst,dpos+c,v)
next c
next dx
next dy
endfunction
function particle_spawn(x,y,r,g,b)
inc part_cur
if part_cur>part_count then part_cur=0
part[part_cur].x=x
part[part_cur].y=y
part[part_cur].xs=(sin(random(0,360))*0.1)
part[part_cur].ys=(sin(random(0,360))*0.1)
part[part_cur].life=1.0
spr=part_cur+1000
if getspriteexists(spr)=0
createsprite(spr,0)
setspritesize(spr,1,1)
setspritephysicsoff(spr)
setspritedepth(spr,4)
endif
setspritepositionbyoffset(spr,x,y)
setspritecolor(spr,r,g,b,255)
setspritevisible(spr,1)
endfunction
function particle_update()
for p=0 to part_count
spr=p+1000
if part[p].life>0.0 and getspriteexists(spr)=1
lx#=part[p].x
ly#=part[p].y
part[p].ys=part[p].ys+0.02
if part[p].ys>1.0 then part[p].ys=1.0
part[p].x=part[p].x+part[p].xs
part[p].y=part[p].y+part[p].ys
if terr_checkalpha(part[p].x,ly#)>0
part[p].x=lx#
part[p].xs=part[p].xs*-0.7
endif
if terr_checkalpha(lx#,part[p].y)>0
part[p].y=ly#
part[p].ys=part[p].ys*-0.7
endif
part[p].life=part[p].life-0.001
if round((part[p].xs+part[p].ys)*10)=0 then part[p].life=part[p].life-0.01
if part[p].life<=0.0
part[p].life=0.0
setspritevisible(spr,0)
else
setspritepositionbyoffset(spr,part[p].x,part[p].y)
setspritecoloralpha(spr,part[p].life*255)
endif
endif
next p
endfunction
I am the one who knocks...