Im currently making a tilemap editor as well.
What i did is define a tilemap size by nr of tiles in x and y, then create blank sprites with 1 pixel spacing in between.
Each sprite ID is stored in an [i,j] array, as well as the tile id according to the atlas i imported.
You can then use "getspriteid()" according to the mouse pointer to get the sprite id you want to modify.
global paint=0
global mode=0
once = 1
function game ()
if once=1
once=0
buildmap()
endif
if GetRawKeyPressed(81) then mode=1
print(GetSpriteImageID(selection[1,1]))
print(GetSpriteImageID(selection[1,1]))
if GetRawKeyPressed(39) then shiftright()
if GetRawKeyPressed(37) then shiftleft()
if GetRawKeyPressed(87) then mapup()
if GetRawKeyPressed(65) then mapleft()
if GetRawKeyPressed(83) then mapdown()
if GetRawKeyPressed(68) then mapright()
if GetPointerX()>(screenX-6*17)
if GetPointerPressed()
paint=GetSpriteImageID(GetSpriteHit(ScreenToWorldX(GetPointerX()),ScreenToWorldY(GetPointerY())))
endif
print("capturing")
else
if GetPointerState()=1
SetSpriteImage(GetSpriteHit(ScreenToWorldX(GetPointerX()),ScreenToWorldY(GetPointerY())),paint)
endif
endif
for j = 1 to tilemapy
for i = 1 to tilemapx
SetSpriteVisible(tilemap[i,j],1)
SetSpriteShapeBox(tilemap[i,j],16,16,-16,-16,0)
if GetSpriteX(tilemap[i,j])>ScreenToWorldX(screenX-6*17)
SetSpriteVisible(tilemap[i,j],0)
SetSpriteShapeBox(tilemap[i,j],1,1,1,1,0)
endif
if GetSpriteX(tilemap[i,j])<ScreenToWorldX(0)
SetSpriteVisible(tilemap[i,j],0)
SetSpriteShapeBox(tilemap[i,j],1,1,1,1,0)
endif
next
next
print (paint)
endfunction
function buildmap()
atlass=loadimage(atlasname$)
dim atlas[16,16]
dim name[16,16] as string
for j = 1 to 16
for i = 1 to 16
x=(j-1)*16+i
atlas[i,j]=LoadSubImage(atlass,str(x))
name[i,j]=str(x)
next
next
global dim tilemap [tilemapx,tilemapy]
for j = 1 to tilemapy
for i = 1 to tilemapx
size=33
tilemap[i,j]=CreateSprite(0)
SetSpriteDepth(tilemap[i,j],10)
SetSpritePosition(tilemap[i,j],(i-1)*size,(j-1)*size)
SetSpriteSize(tilemap[i,j],size-1,size-1)
next
next
for j = 1 to 16
for i = 1 to 16
size=33
SetSpriteImage(tilemap[i,j],(atlas[i,j]))
SetSpritePosition(tilemap[i,j],(i-1)*size,(j-1)*size)
SetSpriteSize(tilemap[i,j],size-1,size-1)
next
next
setupselection()
paint=0
endfunction
function setupselection()
global dim selection[4,16]
for j= 1 to 16
for i=1 to 4
selection[i,j]=CreateSprite(atlas[i,j])
SetSpriteSize(selection[i,j],24,24)
SetSpritePosition(selection[i,j],screenX-(5-i)*25,1+(j-1)*25)
FixSpriteToScreen(selection[i,j],1)
next
next
global dim borders[2]
borders[1]=GetSpriteImageID(selection[1,1])
borders[2]=GetSpriteImageID(tilemap[16,16])
global selectionrow=1
endfunction
function shiftleft()
if selectionrow>1
selectionrow = selectionrow-1
for j=1 to 16
is=1
for i = (selectionrow) to (selectionrow + 3)
SetSpriteImage(selection[is,j],atlas[i,j])
is=is+1
next
next
endif
endfunction
function shiftright()
if selectionrow<13
selectionrow = selectionrow+1
for j=1 to 16
is=1
for i = (selectionrow) to (selectionrow + 3)
SetSpriteImage(selection[is,j],atlas[i,j])
is=is+1
next
next
endif
endfunction
function mapup()
SetViewOffset(GetViewOffsetX(),GetViewOffsetY()+33)
endfunction
function mapdown()
SetViewOffset(GetViewOffsetX(),GetViewOffsetY()-33)
endfunction
function mapleft()
SetViewOffset(GetViewOffsetX()+33,GetViewOffsetY())
endfunction
function mapright()
SetViewOffset(GetViewOffsetX()-33,GetViewOffsetY())
endfunction
Maybe this could be a bit easier to manage?
I'll be putting the final product on the forum for anyone to use. Just having an issue with the menu atm to define all the variables
Ps, I noticed the code above isnt quite doing what it is supposed to. Currently implementing the write to file part of it