Just a few things to get out of the way:
I know other map editor exist
I know you can use Tiled
My coding isn't beautiful (but it's commented)
I posted this a while back, but I decided to revisit it and try to get it to work. For some reason I can't get the saving and loading correct. Please can someone help me with my code. Thank you.
SetWindowTitle( "Map Creator")
//====== Get Device Resolution ======//
thisWidth = getDeviceWidth()
thisHeight = getDeviceHeight()
thisMidX = thisWidth //* 0.5
thisMidY = thisHeight//* 0.5
// Size of area to be magnified ( initially 1/3 of display )
//ScissorW = thisWidth / 3.0
//ScissorH = thisHeight / 3.0
// Zoom Magnification
//myZoom# = 3.0
//====== Device Resolution ======//
setVirtualResolution( thisWidth , thisHeight )
SetWindowSize(thisWidth,thisHeight,0,1)
//SetVirtualResolution( 1280, 960 )
SetWindowAllowResize( 1 )
SetAntialiasMode( 0 )
SetScissor(0,0,0,0)
SetClearColor( 93, 93, 93 )
//====== Map Varibles ======//
type Tile
spriteID
image
endtype
Global MapSize = 100
dim Map [MapSize,MapSize] as Tile
mapx# = 0
mapy# = 0
image = 0
button = 0
global grid = 64 // Size of Grid Tiles
//====== Load GUI images ======//
DrawImage = LoadImage ("paint.png")
ImgNewButton = LoadImage ("new.png")
ImgSaveButton = LoadImage ("save.png")
ImgEditButton = LoadImage ("edit.png")
ImgObjectButton = LoadImage ("object.png")
ImgTileButton = LoadImage ("tile.png")
ImgUpLayerButton = LoadImage ("uplayer.png")
ImgDownLayerButton = LoadImage ("downlayer.png")
//====== Placement of Icons ======//
x#=150
y#=5
//====== Spacing of Icons ======//
spacing# = 64
//====== Create Buttons ======//
newButton = createsprite(ImgNewButton)
SetSpritePosition (newButton,x#,y#)
SetSpriteDepth ( newButton,0 )
x# = x# + spacing#
saveButton = createsprite(ImgSaveButton)
SetSpritePosition (saveButton,x#,y#)
SetSpriteDepth ( saveButton ,0 )
x# = x# + spacing#
editButton = createsprite(ImgEditButton)
SetSpritePosition (editButton,x#,y#)
SetSpriteDepth ( editButton ,0 )
x# = x# + spacing#
objectButton = createsprite(ImgObjectButton)
SetSpritePosition (objectButton,x#,y#)
SetSpriteDepth ( objectButton ,0 )
x# = x# + spacing#
tileButton = createsprite(ImgTileButton)
SetSpritePosition (tileButton,x#,y#)
SetSpriteDepth ( tileButton ,0 )
x# = x# + spacing#
upLayerButton = createsprite(ImgUpLayerButton)
SetSpritePosition (upLayerButton,x#,y#)
SetSpriteDepth ( upLayerButton ,0 )
x# = x# + spacing#
downLayerButton = createsprite(ImgDownLayerButton)
SetSpritePosition (downLayerButton,x#,y#)
SetSpriteDepth ( downLayerButton ,0 )
x# = x# + spacing#
AddVirtualJoystick(1,x#+24,y#+24,48)
//====== Draw GUI ======//
DefaultMap()
FixSpriteToScreen(newButton,1)
FixSpriteToScreen(saveButton,1)
FixSpriteToScreen(editButton,1)
FixSpriteToScreen(objectButton,1)
FixSpriteToScreen(tileButton,1)
FixSpriteToScreen(upLayerButton,1)
FixSpriteToScreen(downLayerButton ,1)
global CanEditMap = 1
//======****** Main Software Loop ******======//
do
//====== Main Map varibles ======//
ScreenScrollspeed = 32
jScreenScrollspeed = 15
ScreenBounds = 64
ScreenX = GetViewOffsetx()
ScreenY = GetViewOffsetY()
MouseX = ScreenToWorldX(GetPointerX())
MouseY = ScreenToWorldY(GetPointerY())
joystickX# = GetVirtualJoystickX ( 1 )
joystickY# = GetVirtualJoystickY ( 1 )
mx = (floor(MouseX/grid))
my = (floor(MouseY/grid))
LeftScreenScroll = GetScreenBoundsLeft() + 10
RightScreenScroll = GetScreenBoundsRight() - 10
TopScreenScroll = GetScreenBoundsTop() + 10
BottomScreenScroll = GetScreenBoundsBottom() - 10
//print ("Mouse X")
//print(MouseX)
//print ("Mouse Y")
//print(MouseY)
//====== Check the current Tile ======//
CurrentSprite = GetSpriteHit(MouseX,MouseY)
print (CurrentSprite)
if CurrentSprite = 100001
CanEditMap = 0
elseif CurrentSprite
CanEditMap = 1
endif
if mx>0 and my>0 and mx<MapSize and my<MapSize
//print (map[mx,my].image)
endif
//====== Joystick ======//
SetViewOffset ( ScreenX +joystickX#*jScreenScrollspeed ,ScreenY+ joystickY#*jScreenScrollspeed)
//====== If cursor is moves to the end of screen, scroll screen ======//
if GetPointerX() < LeftScreenScroll
SetViewOffset (ScreenX - 10,ScreenY)
elseif GetPointerX() > RightScreenScroll
SetViewOffset (ScreenX + 10,ScreenY)
endif
if GetPointerY() < TopScreenScroll
SetViewOffset (ScreenX,ScreenY - 10)
elseif GetPointerY() > BottomScreenScroll
SetViewOffset (ScreenX,ScreenY+ 10)
endif
// Esc Key //
if GetRawKeyPressed(27)
Exit
endif
if GetRawKeyPressed(32)
SetViewOffset ( 0,0)
endif
//====== Zoom map ======//
if GetRawMouseMiddleState() = 1
DragX = ScreenToWorldX(floor(GetPointerx())-GetViewOffsetx()+ScreenScrollSpeed)
DragY = ScreenToWorldy(floor(GetPointery())-GetViewOffsetY()+ScreenScrollSpeed)
SetViewOffset (DragX,DragY)
endif
//====== New map icon button test ======//
if GetPointerPressed() = 1
MouseHit = GetSpriteHit (MouseX,MouseY)
if MouseHit = 100001
DefaultMap()
endif
endif
//====== Save map icon button test ======//
if GetPointerPressed() = 1
MouseHit = GetSpriteHit (MouseX,MouseY)
if MouseHit = 100002
// SaveMap()
endif
endif
//====== Load map icon button test ======//
if GetPointerPressed() = 1
MouseHit = GetSpriteHit (MouseX,MouseY)
if MouseHit = 100003
// LoadMap()
endif
endif
//====== Draws the map tile when left key is pressed ======//
if CanEditMap = 1
if GetPointerState() = 1
hit = GetSpriteHit(MouseX,MouseY)
if mx=>0 and my=>0 and mx=<MapSize and my=<MapSize //Checks if the User is in bounds when they click
hit=map[mx,my].spriteID //Where sprite it clicked
DeleteSprite(map[mx,my].spriteID) //Get rid of whats under sprite
DrawTile = CreateSprite (DrawImage) //Create new Tile
SetSpritePosition(DrawTile,mx*grid,my*grid) //Draw New Tile within the Grid
map[mx,my].image=1 //The map gets told which Tile to store in memory
endif
endif
//====== Deletes the Sprite when someone uses right mouse button ======//
if GetRawMouseRightState() = 1
hit = GetSpriteHit(MouseX,MouseY)
Deleteimage(hit)
if mx=>0 and my=>0 and mx=<MapSize and my=<MapSize //Checks if the User is in bounds when they click
hit=map[mx,my].spriteID
DeleteSprite(hit)
//DeleteSprite(map[mx,my].spriteID)
EraseMapTileImage =LoadImage ("image.png")
EraseTile = CreateSprite (EraseMapTileImage)
SetSpriteSize(EraseTile,64,64)
SetSpritePosition(EraseTile,mx*grid,my*grid)
SetSpriteDepth ( 0,0 )
map[mx,my].image=0
endif
endif
endif
sync()
loop
//====== Default map or New map ======//
function DefaultMap()
print ("New Map")
BlankMapTileImage = LoadImage ("image.png")
count = 0
for x = 0 to MapSize
for y = 0 to MapSize
if GetSpriteExists(map[x,y].spriteID)=1 //Checks if there is already a map and deltes it
DeleteSprite(map[x,y].spriteID)
endif
count = count + 1
CreateSprite(0+count,BlankMapTileImage) //Creates the Blank Tiles
//SetSpriteSize(0+count,63,63)
SetSpritePosition(0+count,x*grid,y*grid) //Posistions the Blank Tiles
//setspritecolor(0+count,195,195,195,100)
map[x,y].image=0+count //Puts the Tiles in Map Memory as Blank
next y
next x
endfunction
//function SaveMap()
//file= openToWrite("map.dat")
//write integer file, mapWidth
//write integer file, mapHeight
//for y = 0 to mapHeight-1
// for x = 0 to mapWidth-1
// write integer file, map[x,y].id
// write integer file, map[x,y].att1
// write integer file, map[x,y].att2
// next x
//next y
//closeFile file
//endfunction
//function SaveMap()
// MapFile = OpenToWrite ("Map.dat",0)
//save the map tile images
// count = 0
//for x = 1 to MapSize
// for y = 1 to MapSize
//count = count / grid
// TileImage = map[x,y].spriteID
//if GetSpriteExists(TileImage) >= 0
// imagex = GetSpriteX (TileImage)*64
// imagey = getspritey (TileImage)*64
// WriteInteger(MapFile,imagex)
// WriteInteger(MapFile,imagey)
// WriteInteger(MapFile,TileImage)
//endif
// next y
//next x
// CloseFile(MapFile)
//endfunction
//function LoadMap()
//file= openToRead("map.dat")
//mapWidth = readInteger(file)
//mapHeight = readInteger(file)
//map as MyUDT[mapWidth, mapHeight]
//for y = 0 to mapHeight-1
// for x = 0 to mapWidth-1
// id = readIntege( file)
// att1 = readInteger(file)
// att2 = readInteger(file)
// var as MyUDT
// var.id = id
// var.att1 = att1
// var.att2 = att2
// map[x,y] = var
// next x
//next y
//closeFile file
//endfunction
//function SaveMap()
// MapFile = OpenToWrite ("Map.dat",0)
//save the map tile images
// for i = 1 to 25
// for j = 1 to 25
// WriteInteger(MapFile, map[i,j] )
// next j
// next i
// CloseFile(MapFile)
//endfunction
//function LoadMap()
// MapFile = OpenToRead ("Map.dat")
// Read in the map tile images into our array
// for i = 1 to 25
// for j = 1 to 25
// map[i,j] = ReadInteger(MapFile)
// next j
// next i
// CloseFile(MapFile)
//endfunction