Hello, it's me again.
With a new function library...
With the functions I wrote it's easily possible to use tilebased maps for 2D-Games, where each tile can have individual values, defining if it's e.g. possible to walk over it, build something on it, or how much a character is slowed down when he walks over it.
The functions make the tile-map-management very convenient and easy, you don't really have to care about anything related to the tilemap, apart from defining the tiles and templates in the beginning of your program.
At first a screenshot of the example-program below:
There you can see three tiles (grass, earth, sand) and some terrain-information in the edge.
By the way, the functions do NOT support texture-blending for smoother transitions.. I thought about it and discussed the topic with a friend for some hours but we didn't find a good solution.
Here a list of all functions and some information:
Quote: "
Functions included:
tbm_Tile(x,y) - Not a function, but an array, storing the template a certain tile belongs to
tbm_Start() - Has to be called before any other function, sets up elemental variables and arrays
tbm_SetMapSize(TilesX,TilesY) - Defines the used amount of tiles. The tiles will go from 0 to TilesX/Y
tbm_AddTileTemplate(...) - Adds a tile-template with specified parameters
tbm_SetTemplateFloat(Template,FloatID,Value#) - Defined the FloatID'th Float-Value of a template (info below)
tbm_SetTemplateInteger()
tbm_SetTemplateString()
tbm_SetTile(x,y,template) - Defines a certain tile's template, can also be made by manipulating the tbm_Tile-Array
tbm_SetTileMap(template) - Changes the whole map to a certain tile-template
tbm_SetTileBox(x1,y1,x2,y2,template) - Changes a box of tiles of the map
tbm_SetTileRandom(template,random_min,random_range) - if for any tile (rnd(random_range) < random_min) is true it becomes the template
tbm_DrawMap(ScrollX,ScrollY) - Draws the visible part of the map to the screen depending on two scroll-values
tbm_GetWalkable(x,y) - Returns 1 if a unit can walk over the tile, otherwise 0
tbm_GetBuildable()
tbm_GetSpeedFactor()
tbm_GetTileFloat(x,y,FloatID) - Returns the specific Tile's FloadID'th float-value (info below)
tbm_GetTileInteger()
tbm_GetTileString()
tbm_GetTile(x,y) - Computes the Tile the absolut point(x|y) is on (independent of scroll-values from DrawMap) and saves them to tbm_ActX|tbm_ActY
tbm_LoadSprite(file$) - Loads an external image, creates a sprite of it and prepares it for tile-usage
tbm_FreeSprite() - Returns the first free sprite-ID
tbm_FreeImage()
Template-Floats/Integers/Strings:
Each tile-template can have several additional information. They can be float, integer or string.
Each template gets a reserved amount of values of each type, depending on the only 3 used constants
tbm_cFloats/Integers/Strings. If one of these constants is 1, each template will have one personal
variable of this type in a separate array.
You can specify the values with tbm_SetTemplateFloat/Integer/String(Template,Offset,Value) and get
the values of a specific tile with tbm_GetTileFloat/Integer/String(x,y,Offset).
"
The functions:
remstart
Help-Info
Functions included:
tbm_Tile(x,y) - Not a function, but an array, storing the template a certain tile belongs to
tbm_Start() - Has to be called before any other function, sets up elemental variables and arrays
tbm_SetMapSize(TilesX,TilesY) - Defines the used amount of tiles. The tiles will go from 0 to TilesX/Y
tbm_AddTileTemplate(...) - Adds a tile-template with specified parameters
tbm_SetTemplateFloat(Template,FloatID,Value#) - Defined the FloatID'th Float-Value of a template (info below)
tbm_SetTemplateInteger()
tbm_SetTemplateString()
tbm_SetTile(x,y,template) - Defines a certain tile's template, can also be made by manipulating the tbm_Tile-Array
tbm_SetTileMap(template) - Changes the whole map to a certain tile-template
tbm_SetTileBox(x1,y1,x2,y2,template) - Changes a box of tiles of the map
tbm_SetTileRandom(template,random_min,random_range) - if for any tile (rnd(random_range) < random_min) is true it becomes the template
tbm_DrawMap(ScrollX,ScrollY) - Draws the visible part of the map to the screen depending on two scroll-values
tbm_GetWalkable(x,y) - Returns 1 if a unit can walk over the tile, otherwise 0
tbm_GetBuildable()
tbm_GetSpeedFactor()
tbm_GetTileFloat(x,y,FloatID) - Returns the specific Tile's FloadID'th float-value (info below)
tbm_GetTileInteger()
tbm_GetTileString()
tbm_GetTile(x,y) - Computes the Tile the absolut point(x|y) is on (independent of scroll-values from DrawMap) and saves them to tbm_ActX|tbm_ActY
tbm_LoadSprite(file$) - Loads an external image, creates a sprite of it and prepares it for tile-usage
tbm_FreeSprite() - Returns the first free sprite-ID
tbm_FreeImage()
Template-Floats/Integers/Strings:
Each tile-template can have several additional information. They can be float, integer or string.
Each template gets a reserved amount of values of each type, depending on the only 3 used constants
tbm_cFloats/Integers/Strings. If one of these constants is 1, each template will have one personal
variable of this type in a separate array.
You can specify the values with tbm_SetTemplateFloat/Integer/String(Template,Offset,Value) and get
the values of a specific tile with tbm_GetTileFloat/Integer/String(x,y,Offset).
remend
rem This constants define how much additional info-parameters each tile-template has, change them to any value you need
#constant tbm_c_Floats = 0
#constant tbm_c_Integers = 0
#constant tbm_c_Strings = 1
function tbm_Start()
rem Template-Array
global tbm_TemplateCount as integer = 0
dim tbm_Template(0) as tbm_TemplateType
rem Template-Info-Arrays (additional parameters)
dim tbm_TFloat(0) as float
dim tbm_TInteger(0) as integer
dim tbm_TString(0) as string
rem Tile-Array
global tbm_TilesX as integer = 0
global tbm_TilesY as integer = 0
global tbm_SizeX as integer = 0
global tbm_SizeY as integer = 0
dim tbm_Tile(0,0)
rem Store-Variables
global tbm_ActX as integer = 0
global tbm_ActY as integer = 0
endfunction
type tbm_TemplateType
SpriteID as integer
Walkable as boolean
Buildable as boolean
SpeedFactor as float
endtype
function tbm_SetMapSize(SegmentsX,SegmentsY)
tbm_TilesX = SegmentsX
tbm_TilesY = SegmentsY
dim tbm_Tile(tbm_TilesX,tbm_TilesY)
endfunction
function tbm_SetTileSize(SizeX,SizeY)
tbm_SizeX = SizeX
tbm_SizeY = SizeY
endfunction
function tbm_AddTileTemplate(SpriteID,Walkable,Buildable,SpeedFactor#)
rem Template-Array
inc tbm_TemplateCount
array insert at bottom tbm_Template()
ID = tbm_TemplateCount
rem Terrain-Info-Parameters
tbm_Template(ID).SpriteID = SPriteID
tbm_Template(ID).Walkable = Walkable
tbm_Template(ID).Buildable = Buildable
tbm_Template(ID).SpeedFactor = SpeedFactor#
rem Additional information-parameters
array insert at bottom tbm_TFloat(), tbm_c_Floats
array insert at bottom tbm_TInteger(), tbm_c_Integers
array insert at bottom tbm_TString(), tbm_c_Strings
endfunction ID
function tbm_SetTemplateFloat(ID,FloatID,F#)
pos = FloatID + (ID-1)*tbm_c_Floats
tbm_TFloat(pos) = F#
endfunction
function tbm_SetTemplateInteger(ID,IntegerID,I)
pos = IntegerID + (ID-1)*tbm_c_Integers
tbm_TInteger(pos) = I
endfunction
function tbm_SetTemplateString(ID,StringID,S$)
pos = StringID + (ID-1)*tbm_c_Strings
tbm_TString(pos) = S$
endfunction
function tbm_SetTile(X,Y,Template)
tbm_Tile(X,Y) = Template
endfunction
function tbm_SetTileMap(Template)
for x = 0 to tbm_TilesX
for y = 0 to tbm_TilesY
tbm_Tile(x,y) = Template
next y
next x
endfunction
function tbm_SetTileBox(x1,y1,x2,y2,Template)
for x = x1 to x2
for y = y1 to y2
tbm_Tile(x,y) = Template
next y
next x
endfunction
function tbm_SetTileRandom(Template,Min,Random)
for x = 0 to tbm_TilesX
for y = 0 to tbm_TilesY
r = rnd(Random)
if r < Min
tbm_Tile(x,y) = Template
endif
next y
next x
endfunction
function tbm_DrawMap(ScrollX,ScrollY)
rem Get FOV
x1 = ScrollX/tbm_SizeX
y1 = ScrollY/tbm_SizeY
x2 = x1 + screen width()/tbm_SizeX + 1
y2 = y1 + screen height()/tbm_SizeY + 1
rem Maximum
if x1 < 0 then x1 = 0
if x2 > tbm_TilesX then x2 = tbm_TilesX
if y1 < 0 then y1 = 0
if y2 > tbm_TilesY then y2 = tbm_TilesY
rem Draw-Loop
for x = x1 to x2
for y = y1 to y2
s = tbm_Template(tbm_Tile(x,y)).SpriteID
xp = x*tbm_SizeX - ScrollX
yp = y*tbm_SizeY - ScrollY
paste sprite s, xp,yp
next y
next x
endfunction
function tbm_GetWalkable(x,y)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
w = tbm_Template(tbm_Tile(x,y)).Walkable
endfunction w
function tbm_GetBuildable(x,y)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
b = tbm_Template(tbm_Tile(x,y)).Buildable
endfunction b
function tbm_GetSpeedFactor(x,y)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
f# = tbm_Template(tbm_Tile(x,y)).SpeedFactor
endfunction f#
function tbm_GetTileFloat(x,y,FloatID)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
pos = FloatID + (tbm_Tile(x,y)-1)*tbm_c_Floats
f# = tbm_TFloat(pos)
endfunction f#
function tbm_GetTileInteger(x,y,IntegerID)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
pos = IntegerID + (tbm_Tile(x,y)-1)*tbm_c_Integers
i = tbm_TInteger(pos)
endfunction i
function tbm_GetTileString(x,y,StringID)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
pos = StringID + (tbm_Tile(x,y)-1)*tbm_c_Strings
s$ = tbm_TString(pos)
endfunction s$
function tbm_GetTile(x,y)
tbm_ActX = x/tbm_SizeX
tbm_ActY = y/tbm_SizeY
endfunction
function tbm_LoadSprite(file$)
ID = tbm_FreeSprite()
Img = tbm_FreeImage()
load image file$, img, 1
sprite ID, 99999, 0, Img
hide sprite ID
`offset sprite ID, sprite width(ID)*0.5, sprite height(ID)*0.5
set sprite ID, 0,1
endfunction ID
function tbm_FreeSprite()
ID = 0
repeat
inc ID
until sprite exist(ID)=0
endfunction ID
function tbm_FreeImage()
ID = 0
repeat
inc ID
until image exist(ID)=0
endfunction ID
And an example with functions (executable in this form):
rem Display-Setup
set display mode 800,600,32
sync on
sync rate 0
rem Setup the TileBasedMaps-Functions
tbm_Start()
rem The map gets 31x31 (0..30) segments
tbm_SetMapSize(30,30)
rem Tiles are 64*64 pixels big
tbm_SetTileSize(64,64)
rem Create textures
lock pixels
for x = 0 to 64
for y = 0 to 64
rem Grass
g = 100 + rnd(120)
b = 50 + rnd(60) : if b > g then b = g
r = rnd(30)
dot x,y,rgb(r,g,b)
rem Earth
r = 40 + rnd(50)
g = rnd(r)-rnd(20) : if g < 0 then g = 0
b = rnd(g)
dot x+65,y,rgb(r,g,b)
rem Sand
r = 180 + rnd(70)
g = 160 + rnd(r-160)
b = g*0.2+rnd(g*0.2)
dot x+130,y,rgb(r,g,b)
next y
next x
unlock pixels
rem Get images
grass = 1
earth = 2
sand = 3
get image grass, 1,1, 65,65, 1
get image earth, 65,1,129,65, 1
get image sand, 130,1,194,65, 1
rem Create sprites of images
sprite grass, 0,0, grass
sprite earth, 0,0, earth
sprite sand, 0,0, sand
hide sprite grass
hide sprite earth
hide sprite sand
set sprite grass,0,1
set sprite earth,0,1
set sprite sand, 0,1
rem Add templates
tGrass = tbm_AddTileTemplate(grass,1,1,1.0)
tEarth = tbm_AddTileTemplate(earth,1,1,0.8)
tSand = tbm_AddTileTemplate(sand, 1,1,0.5)
rem Assign template-strings
tbm_SetTemplateString(tGrass,1,"Grass")
tbm_SetTemplateString(tEarth,1,"Earth")
tbm_SetTemplateString(tSand, 1,"Sand" )
rem Define tiles
rem 1. Whole map = grass
tbm_SetTileMap(tGrass)
rem 2. fields (10|10) to (20|20) become earth
tbm_SetTileBox(10,10,20,20,tEarth)
rem 3. Random fields become sand ("rnd(5)<1" is the condition, so each ~6th field is sand afterwards)
tbm_SetTileRandom(tSand,1,5)
rem Camera/Scroll-Values
camx = 0
camy = 0
rem Player-Position
px as float = 500.0
py as float = 500.0
do
rem Clear screen
cls
rem Get tile the player stands on
tbm_GetTile(px,py)
rem Get speed-factor of this tile
f# = tbm_GetSpeedFactor(tbm_ActX,tbm_ActY)
rem Control player depending on speed-factor
inc px, (rightkey()-leftkey())*f#
inc py, (downkey()-upkey())*f#
rem Fix Cam to player
camx = px - 400
camy = py - 300
rem Draw Map to screen
tbm_DrawMap(camx,camy)
rem Draw Player
circle px-camx, py-camy, 10
rem Print information to screen
set cursor 0,0
print "FPS: ", screen fps()
print "Tile: ", tbm_ActX, "|", tbm_ActY
print "Factor: ", f#
print "Ground-Type: ", tbm_GetTileString(tbm_ActX,tbm_ActY,1)
rem Update screen
sync
loop
remstart
Help-Info
Functions included:
tbm_Tile(x,y) - Not a function, but an array, storing the template a certain tile belongs to
tbm_Start() - Has to be called before any other function, sets up elemental variables and arrays
tbm_SetMapSize(TilesX,TilesY) - Defines the used amount of tiles. The tiles will go from 0 to TilesX/Y
tbm_AddTileTemplate(...) - Adds a tile-template with specified parameters
tbm_SetTemplateFloat(Template,FloatID,Value#) - Defined the FloatID'th Float-Value of a template (info below)
tbm_SetTemplateInteger()
tbm_SetTemplateString()
tbm_SetTile(x,y,template) - Defines a certain tile's template, can also be made by manipulating the tbm_Tile-Array
tbm_SetTileMap(template) - Changes the whole map to a certain tile-template
tbm_SetTileBox(x1,y1,x2,y2,template) - Changes a box of tiles of the map
tbm_SetTileRandom(template,random_min,random_range) - if for any tile (rnd(random_range) < random_min) is true it becomes the template
tbm_DrawMap(ScrollX,ScrollY) - Draws the visible part of the map to the screen depending on two scroll-values
tbm_GetWalkable(x,y) - Returns 1 if a unit can walk over the tile, otherwise 0
tbm_GetBuildable()
tbm_GetSpeedFactor()
tbm_GetTileFloat(x,y,FloatID) - Returns the specific Tile's FloadID'th float-value (info below)
tbm_GetTileInteger()
tbm_GetTileString()
tbm_GetTile(x,y) - Computes the Tile the absolut point(x|y) is on (independent of scroll-values from DrawMap) and saves them to tbm_ActX|tbm_ActY
tbm_LoadSprite(file$) - Loads an external image, creates a sprite of it and prepares it for tile-usage
tbm_FreeSprite() - Returns the first free sprite-ID
tbm_FreeImage()
Template-Floats/Integers/Strings:
Each tile-template can have several additional information. They can be float, integer or string.
Each template gets a reserved amount of values of each type, depending on the only 3 used constants
tbm_cFloats/Integers/Strings. If one of these constants is 1, each template will have one personal
variable of this type in a separate array.
You can specify the values with tbm_SetTemplateFloat/Integer/String(Template,Offset,Value) and get
the values of a specific tile with tbm_GetTileFloat/Integer/String(x,y,Offset).
remend
rem This constants define how much additional info-parameters each tile-template has, change them to any value you need
#constant tbm_c_Floats = 0
#constant tbm_c_Integers = 0
#constant tbm_c_Strings = 1
function tbm_Start()
rem Template-Array
global tbm_TemplateCount as integer = 0
dim tbm_Template(0) as tbm_TemplateType
rem Template-Info-Arrays (additional parameters)
dim tbm_TFloat(0) as float
dim tbm_TInteger(0) as integer
dim tbm_TString(0) as string
rem Tile-Array
global tbm_TilesX as integer = 0
global tbm_TilesY as integer = 0
global tbm_SizeX as integer = 0
global tbm_SizeY as integer = 0
dim tbm_Tile(0,0)
rem Store-Variables
global tbm_ActX as integer = 0
global tbm_ActY as integer = 0
endfunction
type tbm_TemplateType
SpriteID as integer
Walkable as boolean
Buildable as boolean
SpeedFactor as float
endtype
function tbm_SetMapSize(SegmentsX,SegmentsY)
tbm_TilesX = SegmentsX
tbm_TilesY = SegmentsY
dim tbm_Tile(tbm_TilesX,tbm_TilesY)
endfunction
function tbm_SetTileSize(SizeX,SizeY)
tbm_SizeX = SizeX
tbm_SizeY = SizeY
endfunction
function tbm_AddTileTemplate(SpriteID,Walkable,Buildable,SpeedFactor#)
rem Template-Array
inc tbm_TemplateCount
array insert at bottom tbm_Template()
ID = tbm_TemplateCount
rem Terrain-Info-Parameters
tbm_Template(ID).SpriteID = SPriteID
tbm_Template(ID).Walkable = Walkable
tbm_Template(ID).Buildable = Buildable
tbm_Template(ID).SpeedFactor = SpeedFactor#
rem Additional information-parameters
array insert at bottom tbm_TFloat(), tbm_c_Floats
array insert at bottom tbm_TInteger(), tbm_c_Integers
array insert at bottom tbm_TString(), tbm_c_Strings
endfunction ID
function tbm_SetTemplateFloat(ID,FloatID,F#)
pos = FloatID + (ID-1)*tbm_c_Floats
tbm_TFloat(pos) = F#
endfunction
function tbm_SetTemplateInteger(ID,IntegerID,I)
pos = IntegerID + (ID-1)*tbm_c_Integers
tbm_TInteger(pos) = I
endfunction
function tbm_SetTemplateString(ID,StringID,S$)
pos = StringID + (ID-1)*tbm_c_Strings
tbm_TString(pos) = S$
endfunction
function tbm_SetTile(X,Y,Template)
tbm_Tile(X,Y) = Template
endfunction
function tbm_SetTileMap(Template)
for x = 0 to tbm_TilesX
for y = 0 to tbm_TilesY
tbm_Tile(x,y) = Template
next y
next x
endfunction
function tbm_SetTileBox(x1,y1,x2,y2,Template)
for x = x1 to x2
for y = y1 to y2
tbm_Tile(x,y) = Template
next y
next x
endfunction
function tbm_SetTileRandom(Template,Min,Random)
for x = 0 to tbm_TilesX
for y = 0 to tbm_TilesY
r = rnd(Random)
if r < Min
tbm_Tile(x,y) = Template
endif
next y
next x
endfunction
function tbm_DrawMap(ScrollX,ScrollY)
rem Get FOV
x1 = ScrollX/tbm_SizeX
y1 = ScrollY/tbm_SizeY
x2 = x1 + screen width()/tbm_SizeX + 1
y2 = y1 + screen height()/tbm_SizeY + 1
rem Maximum
if x1 < 0 then x1 = 0
if x2 > tbm_TilesX then x2 = tbm_TilesX
if y1 < 0 then y1 = 0
if y2 > tbm_TilesY then y2 = tbm_TilesY
rem Draw-Loop
for x = x1 to x2
for y = y1 to y2
s = tbm_Template(tbm_Tile(x,y)).SpriteID
xp = x*tbm_SizeX - ScrollX
yp = y*tbm_SizeY - ScrollY
paste sprite s, xp,yp
next y
next x
endfunction
function tbm_GetWalkable(x,y)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
w = tbm_Template(tbm_Tile(x,y)).Walkable
endfunction w
function tbm_GetBuildable(x,y)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
b = tbm_Template(tbm_Tile(x,y)).Buildable
endfunction b
function tbm_GetSpeedFactor(x,y)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
f# = tbm_Template(tbm_Tile(x,y)).SpeedFactor
endfunction f#
function tbm_GetTileFloat(x,y,FloatID)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
pos = FloatID + (tbm_Tile(x,y)-1)*tbm_c_Floats
f# = tbm_TFloat(pos)
endfunction f#
function tbm_GetTileInteger(x,y,IntegerID)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
pos = IntegerID + (tbm_Tile(x,y)-1)*tbm_c_Integers
i = tbm_TInteger(pos)
endfunction i
function tbm_GetTileString(x,y,StringID)
if x < 0 then x = 0 else if x > tbm_TilesX then x = tbm_TilesX
if y < 0 then y = 0 else if y > tbm_TilesY then y = tbm_TilesY
pos = StringID + (tbm_Tile(x,y)-1)*tbm_c_Strings
s$ = tbm_TString(pos)
endfunction s$
function tbm_GetTile(x,y)
tbm_ActX = x/tbm_SizeX
tbm_ActY = y/tbm_SizeY
endfunction
function tbm_LoadSprite(file$)
ID = tbm_FreeSprite()
Img = tbm_FreeImage()
load image file$, img, 1
sprite ID, 99999, 0, Img
hide sprite ID
`offset sprite ID, sprite width(ID)*0.5, sprite height(ID)*0.5
set sprite ID, 0,1
endfunction ID
function tbm_FreeSprite()
ID = 0
repeat
inc ID
until sprite exist(ID)=0
endfunction ID
function tbm_FreeImage()
ID = 0
repeat
inc ID
until image exist(ID)=0
endfunction ID
I hope anybody needs such functions or finds use for them.
Criticism and suggestions appreciated.
Have fun and see ya.