|
| 1 | +/* |
| 2 | +* libtcod 1.5.1 |
| 3 | +* Copyright (c) 2008,2009,2010,2012 Jice & Mingos |
| 4 | +* All rights reserved. |
| 5 | +* |
| 6 | +* Redistribution and use in source and binary forms, with or without |
| 7 | +* modification, are permitted provided that the following conditions are met: |
| 8 | +* * Redistributions of source code must retain the above copyright |
| 9 | +* notice, this list of conditions and the following disclaimer. |
| 10 | +* * Redistributions in binary form must reproduce the above copyright |
| 11 | +* notice, this list of conditions and the following disclaimer in the |
| 12 | +* documentation and/or other materials provided with the distribution. |
| 13 | +* * The name of Jice or Mingos may not be used to endorse or promote products |
| 14 | +* derived from this software without specific prior written permission. |
| 15 | +* |
| 16 | +* THIS SOFTWARE IS PROVIDED BY JICE AND MINGOS ``AS IS'' AND ANY |
| 17 | +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | +* DISCLAIMED. IN NO EVENT SHALL JICE OR MINGOS BE LIABLE FOR ANY |
| 20 | +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +*/ |
| 27 | + |
| 28 | +#ifndef _TCOD_BRESENHAM_HPP |
| 29 | +#define _TCOD_BRESENHAM_HPP |
| 30 | +class TCODLIB_API TCODLineListener { |
| 31 | +public : |
| 32 | + virtual bool putPoint(int x,int y) = 0; |
| 33 | + virtual ~TCODLineListener() {} |
| 34 | +}; |
| 35 | + |
| 36 | +class TCODLIB_API TCODLine { |
| 37 | +public : |
| 38 | + /** |
| 39 | + @PageName line |
| 40 | + @PageCategory Base toolkits |
| 41 | + @PageTitle Line drawing toolkit |
| 42 | + @PageDesc 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. |
| 43 | + @FuncTitle Initializing the line |
| 44 | + @FuncDesc First, you have to initialize the toolkit with your starting and ending coordinates. |
| 45 | + @Cpp static void TCODLine::init (int xFrom, int yFrom, int xTo, int yTo) |
| 46 | + @C void TCOD_line_init (int xFrom, int yFrom, int xTo, int yTo) |
| 47 | + @Py line_init (xFrom, yFrom, xTo, yTo) |
| 48 | + @C# static void TCODLine::init(int xFrom, int yFrom, int xTo, int yTo) |
| 49 | + @Lua tcod.line.init(xFrom,yFrom, xTo,yTo) |
| 50 | + @Param xFrom,yFrom Coordinates of the line's starting point. |
| 51 | + @Param xTo,yTo Coordinates of the line's ending point. |
| 52 | + */ |
| 53 | + static void init(int xFrom, int yFrom, int xTo, int yTo); |
| 54 | + |
| 55 | + /** |
| 56 | + @PageName line |
| 57 | + @FuncTitle Walking the line |
| 58 | + @FuncDesc You can then step through each cell with this function. It returns true when you reach the line's ending point. |
| 59 | + @Cpp static bool TCODLine::step (int * xCur, int * yCur) |
| 60 | + @C bool TCOD_line_step (int * xCur, int * yCur) |
| 61 | + @Py line_step () # returns x,y or None,None if finished |
| 62 | + @C# static bool TCODLine::step(ref int xCur, ref int yCur) |
| 63 | + @Lua tcod.line.step(x,y) -- returns lineEnd,x,y |
| 64 | + @Param xCur,yCur the coordinates of the next cell on the line are stored here when the function returns |
| 65 | + @CppEx |
| 66 | + // Going from point 5,8 to point 13,4 |
| 67 | + int x = 5, y = 8; |
| 68 | + TCODLine::init(x,y,13,4); |
| 69 | + do { |
| 70 | + // update cell x,y |
| 71 | + } while (!TCODLine::step(&x,&y)); |
| 72 | + @CEx |
| 73 | + int x = 5, y = 8; |
| 74 | + TCOD_line_init(x,y,13,4); |
| 75 | + do { |
| 76 | + // update cell x,y |
| 77 | + } while (!TCOD_line_step(&x,&y)); |
| 78 | + @PyEx |
| 79 | + libtcod.line_init(5,8,13,4) |
| 80 | + # update cell 5,8 |
| 81 | + x,y=libtcod.line_step() |
| 82 | + while (not x is None) : |
| 83 | + # update cell x,y |
| 84 | + x,y=libtcod.line_step() |
| 85 | + @LuaEx |
| 86 | + x=5 |
| 87 | + y=8 |
| 88 | + tcod.line.init(x,y,13,4) |
| 89 | + repeat |
| 90 | + -- update cell x,y |
| 91 | + lineEnd,x,y = tcod.line.step(x,y) |
| 92 | + until lineEnd |
| 93 | + */ |
| 94 | + static bool step(int *xCur, int *yCur); |
| 95 | + |
| 96 | + /** |
| 97 | + @PageName line |
| 98 | + @FuncTitle Callback-based function |
| 99 | + @FuncDesc The function returns false if the line has been interrupted by the callback (it returned false before the last point). |
| 100 | + @Cpp |
| 101 | + class TCODLIB_API TCODLineListener { |
| 102 | + virtual bool putPoint (int x, int y) = 0; |
| 103 | + }; |
| 104 | + static bool TCODLine::line (int xFrom, int yFrom, int xTo, int yTo, TCODLineListener * listener) |
| 105 | + @C |
| 106 | + typedef bool (*TCOD_line_listener_t) (int x, int y); |
| 107 | + bool TCOD_line(int xFrom, int yFrom, int xTo, int yTo, TCOD_line_listener_t listener) |
| 108 | + @Py |
| 109 | + def line_listener(x,y) : # ... |
| 110 | + line(xFrom, yFrom, xTo, yTo, listener) |
| 111 | + @C# static bool line(int xFrom, int yFrom, int xTo, int yTo, TCODLineListener listener) |
| 112 | + @Param xFrom,yFrom Coordinates of the line's starting point. |
| 113 | + @Param xTo,yTo Coordinates of the line's ending point. |
| 114 | + @Param listener Callback called for each line's point. The function stops if the callback returns false. |
| 115 | + @CppEx // Going from point 5,8 to point 13,4 |
| 116 | +class MyLineListener : public TCODLineListener { |
| 117 | + public: |
| 118 | + bool putPoint (int x,int y) { |
| 119 | + printf ("%d %d\n",x,y); |
| 120 | + return true; |
| 121 | + } |
| 122 | +}; |
| 123 | +MyLineListener myListener; |
| 124 | +TCODLine::line(5,8,13,4,&myListener); |
| 125 | + @CEx bool my_listener(int x,int y) { |
| 126 | + printf ("%d %d\n",x,y); |
| 127 | + return true; |
| 128 | +} |
| 129 | +TCOD_line_line(5,8,13,4,my_listener); |
| 130 | + @PyEx def my_listener(x,y): |
| 131 | + print x,y |
| 132 | + return True |
| 133 | +libtcod.line_line(5,8,13,4,my_listener) |
| 134 | + */ |
| 135 | + static bool line(int xFrom, int yFrom, int xTo, int yTo, TCODLineListener *listener); |
| 136 | +}; |
| 137 | + |
| 138 | +#endif |
0 commit comments