I don't know exactly why I did it, but I did it : a DLL to make, display and animate easily and fast 2D boards.
I think it is time for a first release and to hear what people think about it and what improvements I should make.
Here are the main features :
- very fast (speed informations at the bottom of the message)
- create large boards easily
- the boards can be far bigger than the screen (in the attached example, it is 320x240 tiles big, the tiles are 32x32 pixels images)
- save and load the boards
- the boards can have from 1 to thousands of layers (1 to 10 are better for the memory
)
- each layer of each tile can be animated with almost any quantity of frames you want
- each layer of each tile can also have a flag value between 0 and 255 for your own use
- there is a "shape system" which allows you to get informations about the tiles under any object of your game (more informations in the command list)
- the 33 almost-best commands of the known universe (well... okay, their names are ugly, but they're working rather correctly - I've had no crash for at least half a dozen tests).
- the c++ source code is so terrible that I can now understand only about its half. When I try to modify something, I feel like poking a dead dog, that's amusing
First, here's an example that shows how easy using my DLL can be. It cannot be run for it is only an example : the medias do not exist and the board that is loaded either.
This example loads a saved board and then you can move a "player" on it, with, of course, collision tests. If the board contains animations, they also will be played.
`display and funny things
set display mode 800, 600, 32, 1
randomize timer()
sync on
sync rate 0
sync
`loading the tiles
load image "tilesgrass1.png", 1, 1
(...)
load image "tilestree3.png", 16, 1
(...)
`Initialization of what we need to use the board system. More informations in
`the function at the end of the source.
initBoard()
`loading the file... It should be really fast.
load board "myboard.dat"
`initialisation of the player
type t_player
posx as integer
posy as integer
shape as integer
endtype
player as t_player
player.posx = 36
player.posy = 42
`let's init the collision system for the player...
player.shape = new board shape()
add shape point player.shape, 0, 0
add shape point player.shape, 0, 12
add shape point player.shape, 12, 12
add shape point player.shape, 12, 0
`main loop
do
`moving the screen dude
if upkey()=1
if shape contains(player.shape, player.posx, player.posy-5, 0, 1)=0
dec player.posy, 5
endif
endif
`same system with the other directions
if downkey()=1
if shape contains(player.shape, player.posx, player.posy+5, 0, 1)=0
inc player.posy, 5
endif
endif
if leftkey()=1
if shape contains(player.shape, player.posx-5, player.posy, 0, 1)=0
dec player.posx, 5
endif
endif
if rightkey()=1
if shape contains(player.shape, player.posx+5, player.posy, 0, 1)=0
inc player.posx, 5
endif
endif
center board camera player.posx+5, player.posy+5
`let's draw the board
draw board 0
`and here's the "player"
x = get screen coord x(player.posx)
y = get screen coord y(player.posy)
box x, y, x+12, y+12
sync
loop
end
function initBoard()
a as integer = 0
repeat
inc a
until image exist(a)=0
get image a, 0, 0, 2, 2
paste image a, 0, 0
delete image a
init board
endfunction
As you can see, it is rather easy.
So, what commands does the DLL contains ? There are currenly 33 of them :
1 INIT BOARD
No parameter
Inits the board data.
2 NEW BOARD
new board Board width, board height, tile width, tile height, grid width, grid height, levels quantity
Makes a new board.
Board width and board height : quantity of tiles wanted.
Tile width, tile height : size of the images used as tiles
Grid width, grid height : quantity of tiles displayed in the screen (you can see it as some kind of
2D camera field of view).
Levels quantity : how many layers of tiles and values you want to have"
3 SET BOARD TILE
set board tile Tile x, tile y, layer, image, value
Set the image and the value of specified layer of the specified tile. The value must be
between 0 and 255, any greater value will be turned into a modulo 256.
4 DRAW BOARD
draw board flag
Draws the board - or I should say the visible area of the board. If the flag is set to zero,
the whole board will be drawn. If it is set to another value, then only the tiles which need
to be drawn will be.
5 POSITION BOARD CAMERA
position camera Position x, position y
Position the "2D camera" at the specified coordinates.
6 BOARD CAMERA POSITION X()
x = board camera position x()
Gives the current position X of the camera.
7 BOARD CAMERA POSITION Y()
y = board camera position y()
Gives the current position Y of the camera.
8 SAVE BOARD
save board Filename
Saves the board data which was created with the new board command and all the tiles. The fils
may be heavy if there are too many layers (almost 4Mb for a 320x240 tiles board with 9 layers
for each tile !)
9 LOAD BOARD
load board Filename
Loads a board previously saved.
10 CENTER BOARD CAMERA
center board camera Position X, position Y
Centers the camera on these coordinates. If the coordinates are near the border of the
board, the camera won't be exactly centered on the coordinates, it'll be moved so that
it does not shows an area that is outside the board.
11 GET BOARD VALUE
get board value(Tile X, tile Y, layer)
Returns the value of the specified layer of the specified tile.
12 GET BOARD TILE X()
x = get board tile x(X coordinate)
Returns the X value of the tile containing the _absolute_ X coordinate.
13 GET BOARD TILE Y()
y = get board tile y(Y coordinate)
Returns the Y value of the tile containing the _absolute_ Y coordinate.
14 GET SCREEN COORD X()
x = get screen coord x(X coordinate)
Returns the X screen coordinate of the absolute X coordinate.
15 GET SCREEN COORD Y()
y = get screen coord y(Y coordinate)
Same as previous one, but with Y.
16 SET BOARD IMAGE
set board image Tile x, tile y, layer, image
Sets the image of the specified layer of the specified tile. If you want to remove
the image, set its number to 0.
17 SET BOARD VALUE
set board value Tile x, tile y, layer, value
Sets the value of the specified layer of the specified tile. The value must be between
0 and 255, any greater value will be turned into a modulo 256.
18 GET BOARD IMAGE()
img = get board image(Tile X, tile Y, layer)
Returns the number of the image of the specified layer of the specified tile.
19 PICK BOARD TILE X()
x = pick board tile x(Screen X coordinate)
Returns the X number of the tiles which are under the specified column of the screen.
20 PICK BOARD TILE Y()
y = pick board tile(Screen Y coordinate)
Same as previous function, but works with Y.
21 CORNER SUM()
res = corner sum(Start x, start y, width, height, layer)
This is my first attempt to make an automatic collision system. This function makes some kind
of rectangle and then counts the sum of the values of the tiles which are under the corners of
the rectangle. This is not very useful since I made the shape commands.
22 NEW BOARD SHAPE()
id = new board shape()
Okay, so what is a shape ? You can see it as a "clouder of points" that you can move to test
the values of the board for each point. When you create a new shape, an ID is given. If you
use later the wrong ID or a shape without points, then the result will always be 0.
23 ADD SHAPE POINT
add shape point Shape id, Offset X, offset Y
Adds a point to the shape. The offset X and Y are the relative coordinates of the point. The
coordinates are calculated from the center of the shape.
24 POSITION SHAPE
position shape Shape ID, Position x, position y
Positions a shape at the specified coordinates. I must agree that it is not a very useful command.
25 SHAPE CURRENT SUM()
res = shape current sum(Shape ID, layer)
Returns the sum of the values of the tiles which are under the points of the shape. If one tile
is under two points, its value will be counted twice.
26 SHAPE SUM()
res = shape sum(Shape ID, position X, position Y, layer)
Positions the shape at the (X,Y) coordinate, calculate the sum of the values of the tiles which
are under the points of the shape and then send the shape back from where it came.
27 SHAPE CONTAINS()
res = shape contains(Shape ID, position X, position Y, layer, value)
Position the shape at the (x,y) coordinates, returns 1 if the value is in any of the layers under
any of the points and, before exiting the function, puts the shape back where it was.
28 SHAPE CONTAINS NOW()
res = shape contains now(Shape ID, layer, value)
Same as previous one, but does not move the shape.
29 REMOVE POINTS
remove points Shape ID
Remove all the points of the shape.
30 NEW TILE ANIM
new board anim Tile x, tile y, layer, frames quantity
Creates data for an animation on the specified layer of the specified tile. The frames
quantity is how many frames will the animation contain.
31 SET TILE ANIM
set board anim Tile X, tile Y, layer, frame number, frame length, image number
Sets a frame of the animation of the specified layer of the specified tile. The frame
number starts at 0, so if you have 5 frames, their numbers will be 0,1,2,3 and 4.
The length, in milliseconds, is how long the image will be displayed.
32 REMOVE TILE ANIMATION
remove tile animation Tile X, tile Y, layer
Removes the animation of the tiles and frees a few allocated bytes.
33 RANDOM TILE ANIMATION
random tile animation Tile X, tile Y, layer
Select a random frame of the animation of the selected tile.
(I wrote them here in the same order as they are in the stringtable.)
I attached an example to this message. The example contains :
- the DLL (move it in the "compilerplugins-user" directory if you want to compile)
- a DBP project as example. It makes a board with an animated river and an ugly bridge the player can cross. All the tiles of the river have the same "animation time and speed", remove the comments of the two lines "random tile animation" to randomize the animations. The river will still be ugly, but a bit less.
- the tiles which are needed (I made them myself, that is why they are terribly ugly)
- an executable of the project, for the people who are too lazy to compile it If you want to run it, scan it for virus. I did it before uploading and found none, but one should always be careful with executables
When I run this program on my two years old laptop, the board is displayed in about 22 milliseconds without optimisations.
With the optimisation flag to 1, it is displayed in 22 milliseconds too, when the camera is moving, but in 1 to 3 milliseconds when it is not. When that second time was calculated, the animated river was displayed across the screen.
I also looked how fast is the "load board" command. The 320x240=76800 tiles, with two layers and 1200 animated tiles was loaded in 132 milliseconds.
Hmm... no screenshots, I'm ashamed of my ugly tiles and they wouldn't really show what the DLL does, they'd just show tiles
I hope it may be useful to some people. If you use this DLL in a game you make, write a link to this page in the readme file. Any comment or question will be welcome
The sleeper must awaken !