├── game.project
├── debug-draw
└── debug-draw.lua
└── README.md
/game.project:
--------------------------------------------------------------------------------
1 | [library]
2 | include_dirs = debug-draw
3 |
--------------------------------------------------------------------------------
/debug-draw/debug-draw.lua:
--------------------------------------------------------------------------------
1 |
2 | -- ************************************************************************************************
3 | -- Copyright 2019 Ross Grams
4 | --
5 | -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 | -- and associated documentation files (the "Software"), to deal in the Software without
7 | -- restriction, including without limitation the rights to use, copy, modify, merge, publish,
8 | -- distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
9 | -- Software is furnished to do so, subject to the following conditions:
10 | --
11 | -- The above copyright notice and this permission notice shall be included in all copies or
12 | -- substantial portions of the Software.
13 | --
14 | -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
15 | -- BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 | -- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 | -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 | -- ************************************************************************************************
20 |
21 | -- Debug-Draw - For Defold.
22 | -- A set of convenience functions to draw lines, shapes, and text with the
23 | -- "draw_line" and "draw_text" render messages.
24 |
25 | local M = {}
26 |
27 | M.COLORS = {
28 | white = vmath.vector4(1),
29 | black = vmath.vector4(0, 0, 0, 1),
30 | red = vmath.vector4(1, 0, 0, 1),
31 | cyan = vmath.vector4(0, 1, 1, 1),
32 | yellow = vmath.vector4(1, 1, 0, 1),
33 | orange = vmath.vector4(1, 0.5, 0, 1),
34 | green = vmath.vector4(0, 1, 0, 1),
35 | blue = vmath.vector4(0, 0.5, 1, 1),
36 | pink = vmath.vector4(1, 0.5, 1, 1),
37 | magenta = vmath.vector4(1, 0, 1, 1),
38 | }
39 | M.default_color = M.COLORS.yellow -- Can be a vector4 or a name.
40 | M.default_circle_segments = 16
41 |
42 | local TWO_PI = math.pi * 2
43 | local V1, V2 = vmath.vector3(), vmath.vector3()
44 | local MSGDATA = { start_point = V1, end_point = V2 }
45 | local TEXTMSGDATA = {}
46 | local cos, sin = math.cos, math.sin
47 |
48 | local function rotate_xy(x, y, a)
49 | local c = cos(a); local s = sin(a)
50 | return c * x - s * y, s * x + c * y
51 | end
52 |
53 | function M.ray(v1, v2, color)
54 | color = color or M.default_color
55 | if M.COLORS[color] then color = M.COLORS[color] end
56 | V1.x = v1.x; V1.y = v1.y; V1.z = v1.z
57 | V2.x = v2.x; V2.y = v2.y; V2.z = v2.z
58 | MSGDATA.color = color
59 | msg.post("@render:", "draw_line", MSGDATA)
60 | end
61 |
62 | function M.line(x1, y1, x2, y2, color)
63 | color = color or M.default_color
64 | if M.COLORS[color] then color = M.COLORS[color] end
65 | V1.x = x1; V1.y = y1; V1.z = 0
66 | V2.x = x2; V2.y = y2; V2.z = 0
67 | MSGDATA.color = color
68 | msg.post("@render:", "draw_line", MSGDATA)
69 | end
70 |
71 | function M.rect(lt, rt, top, bot, color)
72 | color = color or M.default_color
73 | if M.COLORS[color] then color = M.COLORS[color] end
74 | M.line(lt, top, rt, top, color)
75 | M.line(rt, top, rt, bot, color)
76 | M.line(rt, bot, lt, bot, color)
77 | M.line(lt, bot, lt, top, color)
78 | end
79 |
80 | function M.box(cx, cy, w, h, color, rot)
81 | h = h or w
82 | color = color or M.default_color
83 | if M.COLORS[color] then color = M.COLORS[color] end
84 | local w2, h2 = w/2, h/2
85 | local tlx, tly, trx, try, brx, bry, blx, bly
86 | if rot and rot ~= 0 then -- Rotate corner offsets then add center pos.
87 | tlx, tly = rotate_xy(-w2, h2, rot)
88 | trx, try = rotate_xy(w2, h2, rot)
89 | brx, bry = rotate_xy(w2, -h2, rot)
90 | blx, bly = rotate_xy(-w2, -h2, rot)
91 | tlx, tly, trx, try = tlx + cx, tly + cy, trx + cx, try + cy
92 | brx, bry, blx, bly = brx + cx, bry + cy, blx + cx, bly + cy
93 | else
94 | tlx, tly = cx - w2, cy + h2
95 | trx, try = cx + w2, cy + h2
96 | brx, bry = cx + w2, cy - h2
97 | blx, bly = cx - w2, cy - h2
98 | end
99 | M.line(tlx, tly, trx, try, color)
100 | M.line(trx, try, brx, bry, color)
101 | M.line(brx, bry, blx, bly, color)
102 | M.line(blx, bly, tlx, tly, color)
103 | end
104 |
105 | function M.cross(cx, cy, radiusX, radiusY, color, rot)
106 | radiusY = radiusY or radiusX
107 | color = color or M.default_color
108 | if M.COLORS[color] then color = M.COLORS[color] end
109 |
110 | local x1x, x1y, x2x, x2y = -radiusX, 0, radiusX, 0 -- X line (horizontal), points 1 and 2.
111 | local y1x, y1y, y2x, y2y = 0, -radiusY, 0, radiusY -- Y line (vertical), points 1 and 2
112 | if rot and rot ~= 0 then -- Rotate point offsets if needed.
113 | x1x, x1y = rotate_xy(x1x, x1y, rot)
114 | x2x, x2y = rotate_xy(x2x, x2y, rot)
115 | y1x, y1y = rotate_xy(y1x, y1y, rot)
116 | y2x, y2y = rotate_xy(y2x, y2y, rot)
117 | end
118 | -- Add center pos to point positions.
119 | x1x, x1y, x2x, x2y = x1x + cx, x1y + cy, x2x + cx, x2y + cy
120 | y1x, y1y, y2x, y2y = y1x + cx, y1y + cy, y2x + cx, y2y + cy
121 |
122 | M.line(x1x, x1y, x2x, x2y, color)
123 | M.line(y1x, y1y, y2x, y2y, color)
124 | end
125 |
126 | function M.circle(cx, cy, radius, color, segments, baseAngle)
127 | segments = segments or M.default_circle_segments
128 | if segments <= 1 then return end
129 | color = color or M.default_color
130 | if M.COLORS[color] then color = M.COLORS[color] end
131 | baseAngle = baseAngle or 0
132 | local a = TWO_PI / segments
133 | local x1, y1 = cx + cos(baseAngle) * radius, cy + sin(baseAngle) * radius
134 | for i=1,segments do
135 | local a2 = baseAngle + i * a
136 | local x2, y2 = cx + cos(a2) * radius, cy + sin(a2) * radius
137 | M.line(x1, y1, x2, y2, color)
138 | x1, y1 = x2, y2
139 | if segments <= 2 then break end -- Don't bother drawing both if it's only 2 with no gap.
140 | end
141 | end
142 |
143 | function M.text(text, x, y, color)
144 | color = color or M.default_color
145 | if M.COLORS[color] then color = M.COLORS[color] end
146 | V1.x, V1.y = x, y
147 | TEXTMSGDATA.text, TEXTMSGDATA.position, TEXTMSGDATA.color = text, V1, color
148 | msg.post("@render:", "draw_debug_text", TEXTMSGDATA)
149 | end
150 |
151 | return M
152 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Debug-Draw
2 | A set of convenience functions to draw lines, shapes, and text with the "draw_line" and "draw_debug_text" render messages. This is not intended to be used for final game graphics. Most of the functions only draw on a 2D plane at z=0, though the `ray` function can draw 3D lines.
3 |
4 | __Dependency Link:__
5 | >https://github.com/rgrams/debug-draw/archive/master.zip
6 |
7 | *Don't know what to do with this link? Read the manual: https://www.defold.com/manuals/libraries/*
8 |
9 | To use the module in your scripts, you must require it, like so:
10 | > `local debugdraw = require "debug-draw.debug-draw"`
11 |
12 | ## Settings:
13 |
14 | ### debugdraw.COLORS
15 | table
16 |
17 | A table of colors that you can use for drawing lines and shapes. You can modify this at runtime if you want.
18 |
19 | The list of built-in colors:
20 | * white
21 | * black
22 | * red
23 | * cyan
24 | * yellow
25 | * orange
26 | * green
27 | * blue
28 | * pink
29 | * magenta
30 |
31 | ### debugdraw.default_color
32 | vector4 | string
33 |
34 | The default color that will be used if you don't specify one. The default value is __*yellow*__. You can set it to a new vector4 or a string name from the `COLORS` list.
35 |
36 | ### debugdraw.default_circle_segments
37 | number
38 |
39 | The default number of segments for drawing circles used if you don't specify it. The default value is __*16*__.
40 |
41 | ## Functions:
42 | > NOTE: Unless you mess with your render script, all coordinates are in world space.
43 |
44 | ### debugdraw.ray(v1, v2, [color])
45 | Draw a line from `v1` to `v2`. The line can be 3D.
46 |
47 | *PARAMETERS*
48 | * __v1__ number - Line start point.
49 | * __v2__ number - Line end point.
50 | * __color__ nil | string | vector4 - Line color. Can be a vector4 or a string name from the COLORS list.
51 | * *Optional - `debugdraw.default_color` (yellow) by default.*
52 |
53 | ### debugdraw.line(x1, y1, x2, y2, [color])
54 | Draw a 2D line from (x1, y1) to (x2, y2).
55 |
56 | *PARAMETERS*
57 | * __x1, y1__ number - Line start point X and Y.
58 | * __x2, y2__ number - Line end point X and Y.
59 | * __color__ nil | string | vector4 - Line color. Can be a vector4 or a string name from the COLORS list.
60 | * *Optional - `debugdraw.default_color` (yellow) by default.*
61 |
62 | ### debugdraw.rect(lt, rt, top, bot, [color])
63 | Draw a rectangle from it's left, right, top, and bottom coordinates.
64 |
65 | *PARAMETERS*
66 | * __lt, rt, top, bot__ number - Left, right, top, and bottom coordinates of the rectangle.
67 | * __color__ nil | string | vector4 - Line color. Can be a vector4 or a string
68 | name from the COLORS list.
69 | * *Optional - `debugdraw.default_color` (yellow) by default.*
70 |
71 | ### debugdraw.box(cx, cy, w, h, [color], [rot])
72 | Draw a rectangle from a center point, width, and height, with optional rotation.
73 |
74 | *PARAMETERS*
75 | * __cx, cy__ number - X and Y of the center of the box.
76 | * __w, h__ number - Width and height of the box.
77 | * __color__ nil | string | vector4 - Line color. Can be a vector4 or a string name from the COLORS list.
78 | * *Optional - `debugdraw.default_color` (yellow) by default.*
79 | * __rot__ nil | number - Rotation angle (in radians) of the box.
80 | * *Optional - `0` by default.*
81 |
82 | ### debugdraw.cross(cx, cy, radiusX, radiusY, [color], [rot])
83 | Draw a cross or X from a center point and two radii, with optional rotation.
84 |
85 | *PARAMETERS*
86 | * __cx, cy__ number - X and Y of the center of the cross.
87 | * __radiusX__ number - The radius (half of the length) of the horizontal (before rotation) line of the cross.
88 | * __radiusY__ number - The radius (half of the length) of the vertical (before rotation) line of the cross.
89 | * __color__ nil | string | vector4 - Line color. Can be a vector4 or a string name from the COLORS list.
90 | * *Optional - `debugdraw.default_color` (yellow) by default.*
91 | * __rot__ nil | number - Rotation angle (in radians) of the cross.
92 | * *Optional - `0` by default.*
93 |
94 | ### debugdraw.circle(cx, cy, radius, [color], [segments], [baseAngle])
95 | Draw a circle from a center point and a radius, with optional segment count and base angle. If you reduce the number of segments, you can use this to draw equilateral triangles, squares, pentagons, hexagons, heptagons, etc., or even single lines. If you do, you may want to supply a `baseAngle` to orient your shape the way you want.
96 |
97 | *PARAMETERS*
98 | * __cx, cy__ number - X and Y of the center of the circle.
99 | * __radius__ number - The radius of the circle.
100 | * __color__ nil | string | vector4 - Line color. Can be a vector4 or a string name from the COLORS list.
101 | * *Optional - `debugdraw.default_color` (yellow) by default.*
102 | * __segments__ nil | number - The number of line segments to draw the circle/shape with. If you use a non-integer number you'll get a space at the end of your circle instead of a closing line. Setting this to `2` will draw one line across the center of the would-be circle, rotated at `baseAngle`. Setting it to less than 2 will draw a line across only part of the circle, and setting it to 1 or less will draw nothing.
103 | * *Optional - `debugdraw.default_circle_segments` (16) by default.*
104 | * __baseAngle__ nil | number - The angle in radians of the first vertex of the circle. Measured counter-clockwise from the positive X-axis. This only really matters if you're using a very low number of segments.
105 | * *Optional - `0` by default.*
106 |
107 | ### debugdraw.text(text, x, y, [color])
108 | Draws some text. Unlike the other functions, this one's X and Y are in *screen space* (starting from (0,0) at the bottom left of the screen).
109 |
110 | *PARAMETERS*
111 | * __text__ string - The text to draw.
112 | * __x, y__ number - X and Y of the top left corner of the text's bounding box, in *screen space*.
113 | * __color__ nil | string | vector4 - Line color. Can be a vector4 or a string name from the COLORS list.
114 | * *Optional - `debugdraw.default_color` (yellow) by default.*
115 |
--------------------------------------------------------------------------------