Post by 8XenoTrogdor8 on Dec 12, 2004 16:08:08 GMT -5
There are many uses for tilemaps saved in txt files, one example would be the "Easy 3D Enviornments" program. But it can be a pain to type all the numbers into a tilemap, especially if your creating a fairly large one, like, say, 50X50. Thats a lot of places to mess up, and just one wrong number can cause all sorts of havoc.
So, I decided to make the following program, which provides a relatively easy interface for creating tilemaps. It gives you a visual representation of the tilemap as you modify it, and lets you change the tiles using the mouse! Very handy!
you will need to create a new text document called 'testmap.txt' in Basic4gl's 'files' folder for this program to write the tilemap into.
Currently, it writes tilemaps which coorespond to the 'Easy 3D Enviornments' demo program, but it can be easily modified to create virtually any kind of tilemap.
BTW, its a little easier to use this in windowed mode, because you can see the standard mouse arrow along with the cursor sprite.
Later all y'all!
So, I decided to make the following program, which provides a relatively easy interface for creating tilemaps. It gives you a visual representation of the tilemap as you modify it, and lets you change the tiles using the mouse! Very handy!
you will need to create a new text document called 'testmap.txt' in Basic4gl's 'files' folder for this program to write the tilemap into.
Currently, it writes tilemaps which coorespond to the 'Easy 3D Enviornments' demo program, but it can be easily modified to create virtually any kind of tilemap.
'From the Labs of XenoTrogdor,
'The Easy Tilemap creating program!
'V 1.1
dim Tex(8), TileSprite(10), Cursor, Sprite
dim Tile, File, XSize, YSize, SprAreaScale
dim X, Y, I
' Load the same textures as in the Easy 3D program.
' The default texture for the entire map is Tex (0).
Tex (0) = LoadTexture ("textures\00008.jpg") ' Green thingy.
Tex (1) = LoadTexture ("textures\00001.jpg") ' Rocks.
Tex (2) = LoadTexture ("textures\00004.jpg") ' Marble.
Tex (3) = LoadTexture ("textures\00005.jpg") ' Moss?
Tex (4) = LoadTexture ("textures\00009.jpg") ' Cracked tiles.
Tex (5) = LoadTexture ("textures\001.jpg") ' Water.
Tex (6) = LoadTexture ("textures\floor01.jpg") ' Tiles.
Tex (7) = LoadTexture ("textures\pavement.jpg")' What could it be?
Tex (8) = LoadTexture ("textures\wall01.jpg") ' Boards.
Cursor = LoadTexture ("data\star.bmp") ' The default for the cursor is a star.
' The "main menu".
Cls
' The "startup screen"
Printr " Tilemap creation program!"
Printr
Printr " 1 = Create a new map."
Printr " 2 = Load a map."
Printr
Print " Enter selection then press return : "
' put entered value into a variable.
I = Val (Input$ ())
if I = 1 then goto NewMap endif ' If input is 1 then build a new map.
if I = 2 then goto LoadMap endif ' If input is 2 then load a map.
NewMap:
Cls
Print "Type X size (from 0) : "
XSize = Val (Input$ ())
Print "Type Y size (from 0) : "
YSize = Val (Input$ ())
' Allocate a variable array to store the tilemap in.
dim Map (XSize)(YSize)
dim SpriteMap (XSize)(YSize)
goto Start
Loadmap:
' Open file to read tilemap from.
File = OpenFileRead ("files\testmap.txt")
' Read the first two lines in the file (map X size and map Y size)
XSize = Val (ReadLine (File))
YSize = Val (ReadLine (File))
' Allocate a variable array to store the tilemap in.
dim Map (XSize)(YSize)
dim SpriteMap (XSize)(YSize)
' Read the tilemap from the text into the variable.
for Y = 0 to YSize
for X = 0 to XSize
Map (X)(Y) = Val (ReadText (File, TRUE))
next
next
CloseFile (File)
Goto Start
Start:
Cls
' Write some graphically astounding text.
Color (255, 255, 255)
Printr " Passable-"
Printr
Printr "N"
Printr "o"
Printr "n"
Printr "P"
Printr "a"
Printr "s"
Printr "s"
Printr "a"
Printr "b"
Printr "l"
Printr "e"
' Set up selectable tiles (tiles going down).
for I = 0 to 8
TileSprite (I) = NewSprite (Tex (I))
SprSetPos (51, 34*I + 85)
SprSetZOrder (1)
next
' Set up selectable tiles (Tiles going across).
for I = 9 to 10
TileSprite (I) = NewSprite (Tex(I - 4))
SprSetPos (85 + (I - 9)*34, 51)
SprSetZOrder (1)
SprSetScale (.8)
next
' Give the cursor a sprite
Sprite = NewSprite (Cursor)
' Set the sprite area depending on X and Y size.
if XSize > YSize then
SprAreaScale = XSize * 34 + 204
else
SprAreaScale = YSize * 34 + 204
endif
' Make sure screen is at least 420 units high
if SprAreaScale < 420 then
SprAreaScale = 420
endif
ResizeSpriteArea (SprAreaScale*1.25, SprAreaScale) ' Screen width is 25% larger that height.
TextMode (TEXT_BUFFERED)
While True
' Draw the tilemap and any changes made to it.
for X = 0 to XSize
for Y = 0 to YSize
DeleteSprite (SpriteMap(X)(Y))
' If the tile is unpassable then make it the normal size.
if Map (X)(Y) >= 0 then
SpriteMap (X)(Y) = NewSprite (Tex(Map (X)(Y)))
SprSetPos (X*34 + 153, Y*34 + 153)
SprSetZOrder (1)
' If the tile is passable, make it slightly smaller.
else
SpriteMap (X)(Y) = NewSprite (Tex(Abs(Map (X)(Y)) + 4))
SprSetPos (X*34 + 153, Y*34 + 153)
SprSetZOrder (1)
SprSetScale (.8)
endif
next
next
' Write the file if asked to.
if ScanKeyDown (VK_RETURN) then
gosub WriteFile
endif
' Bind the cursor sprite for movement and tile placement.
BindSprite (Sprite)
' TILE PLACEMENT/SELECTION--
' Either pick up a tile or drop one into the tilemap.
if Mouse_Button (0) then ' If left mouse button pressed.
' Pick up a tile from row going down.
if SprX ()= 51 then
Tile = (SprY () - 85)/34
' Change the cursor sprite to selected tile.
DeleteSprite (Sprite)
Sprite = NewSprite (Tex (Tile))
endif
' Pick up a tile from row going across.
if SprY () = 51 then
Tile = (SprX () - 51)/(-34)
' Change the cursor sprite to selected tile.
DeleteSprite (Sprite)
Sprite = NewSprite (Tex(Abs(Tile) + 4))
endif
' Modify tilemap based on location and current sprite of cursor.
if (SprX () > 119 and SprY () > 119) and (SprX () <= 153 + 34 * XSize and SprY() <= 153 + 34 * YSize) then
Map ((SprX () - 153)/34)((SprY () - 153)/34) = Tile
endif
endif
' CURSOR MOVEMENT--
' Find a whole number that represents the mouse pixel position / 34.
X = Mouse_X ()* (SprAreaScale*1.25)/34
Y = Mouse_Y ()* SprAreaScale/34
' Multiplying the whole number mouse position by 34
' ensures that the sprite position will be a multiple of 34.
SprSetPos (Vec2 (X*34 + 17, Y*34 + 17))
' Make the cursor unique so it can be seen easier.
SprSetAngle (SprAngle ()+ 1)
SprSetScale (.5)
DrawText ()
Wend
WriteFile:
' Change the textmode so text can be drawn without using DrawText ().
TextMode (TEXT_SIMPLE)
Locate 0, 13
Print "Saving File, Please Wait..." ' Verify saving of file.
file = OpenFileWrite ("files/testmap.txt")
WriteLine (file, XSize) ' The first line is the X size of tilemap.
WriteLine (file, YSize) ' The second line is the Y size of tilemap.
for Y = 0 to YSize
for X = 0 to XSize
' Compensate for the fact that a - will take up a space.
' This doesn't have an affect on how the tilemap will load,
' but does make tilemaps easier to read from text documents.
if Map(X)(Y) >= 0 then
' If the value contains no -, then put a space before it and after it.
WriteString (file," " + Map (X)(Y) + " ")
else
' If the value contains a -, then only put a space afterwards.
WriteString (file, Map (X)(Y) + " ")
endif
next
WriteLine (file,"") ' Return at every next Y value.
next
CloseFile (file)
' Confirm the file has been saved.
Locate 0, 13
ClearLine ()
Printr "File Saved"
Sleep (50)
Locate 0, 13
ClearLine ()
'Change the textmode back.
TextMode (TEXT_BUFFERED)
return
BTW, its a little easier to use this in windowed mode, because you can see the standard mouse arrow along with the cursor sprite.
Later all y'all!