4. Line drawing toolkit
This toolkit is a very simple and lightweight implementation of the bresenham line drawing algorithm. It allows you to follow straight paths on your map very easily.
Initializing the line
First, you have to initialize the toolkit with your starting and ending coordinates.
static void TCODLine::init (int xFrom, int yFrom, int xTo, int yTo)
void TCOD_line_init (int xFrom, int yFrom, int xTo, int yTo)
line_init (xFrom, yFrom, xTo, yTo)
static void TCODLine::init(int xFrom, int yFrom, int xTo, int yTo)
tcod.line.init(xFrom,yFrom, xTo,yTo)
Parameter | Description |
---|---|
xFrom,yFrom | Coordinates of the line's starting point. |
xTo,yTo | Coordinates of the line's ending point. |
Walking the line
You can then step through each cell with this function. It returns true when you reach the line's ending point.
static bool TCODLine::step (int * xCur, int * yCur)
bool TCOD_line_step (int * xCur, int * yCur)
line_step ()
static bool TCODLine::step(ref int xCur, ref int yCur)
tcod.line.step(x,y)
Parameter | Description |
---|---|
xCur,yCur | the coordinates of the next cell on the line are stored here when the function returns |
Example:
// Going from point 5,8 to point 13,4
int x = 5, y = 8;
TCODLine::init(x,y,13,4);
do {
// update cell x,y
} while (!TCODLine::step(&x,&y));
int x = 5, y = 8;
TCOD_line_init(x,y,13,4);
do {
// update cell x,y
} while (!TCOD_line_step(&x,&y));
libtcod.line_init(5,8,13,4)
# update cell 5,8
x,y=libtcod.line_step()
while (not x is None) :
# update cell x,y
x,y=libtcod.line_step()
x=5
y=8
tcod.line.init(x,y,13,4)
repeat
-- update cell x,y
lineEnd,x,y = tcod.line.step(x,y)
until lineEnd
Callback-based function
The function returns false if the line has been interrupted by the callback (it returned false before the last point).
class TCODLIB_API TCODLineListener {
virtual bool putPoint (int x, int y) = 0;
};
static bool TCODLine::line (int xFrom, int yFrom, int xTo, int yTo, TCODLineListener * listener)
typedef bool (*TCOD_line_listener_t) (int x, int y);
bool TCOD_line(int xFrom, int yFrom, int xTo, int yTo, TCOD_line_listener_t listener)
def line_listener(x,y) : # ...
line(xFrom, yFrom, xTo, yTo, listener)
static bool line(int xFrom, int yFrom, int xTo, int yTo, TCODLineListener listener)
Parameter | Description |
---|---|
xFrom,yFrom | Coordinates of the line's starting point. |
xTo,yTo | Coordinates of the line's ending point. |
listener | Callback called for each line's point. The function stops if the callback returns false. |
Example:
// Going from point 5,8 to point 13,4
class MyLineListener : public TCODLineListener {
public:
bool putPoint (int x,int y) {
printf ("%d %d\n",x,y);
return true;
}
};
MyLineListener myListener;
TCODLine::line(5,8,13,4,&myListener);
bool my_listener(int x,int y) {
printf ("%d %d\n",x,y);
return true;
}
TCOD_line_line(5,8,13,4,my_listener);
def my_listener(x,y):
print x,y
return True
libtcod.line_line(5,8,13,4,my_listener)