Here's something I was working on for the Lua Player. Since the lua player currently only has 3 functions for 2d graphics - points, lines, and rectangles (you can do more with the 3d gu, but that takes a bit more work to figure out), I decided to make a small graphics library for drawing polygons. These functions take some arguments and use them to calculate the coordinates for the various points on a polygon, then use the lua player's drawline function to draw them out. polylib.lua is the library itself, polytest.lua is a small test program I made to show off each of the functions in the lib. I know I'll have to change the graphics and input functions over to something compatible with FB, but that shouldn't be a problem as long as I can somehow get FB to use lua's tables and metatables.
function list:
draw_poly(x, y, radius, rotation, sides, sides_to_draw, closed, color)
draws a regular polygon (i.e. all sides are the same length)
x,y - the center point of the poly
radius - distance from the center point to each corner of the poly
rotation - angle in degrees in which to rotate the poly around it's center
sides - number of sides the poly has
sides_to_draw - number of sides that will actually be drawn
closed - if true and sides_to_draw is less than sides then it will draw a line connecting the end points
color - color to draw the poly
function draw_tri(x, y, side1, side2, angle, rotation, color)
draws a triangle rotated around the start point with two sides of the specified length at the given angle
draw_para(x,y, side1, side2, angle, rotation, color)
draws a parallelogram rotated around the start point, side1 and side2 are the lengths of each pair of sides, angle is the angle of the two sides that meet at the start point
draw_free(x, y, ptable, scale, color)
this one lets you draw free-form polys
ptable is basically a table with a list of coordinates relative to x,y. This example table has the coords to draw an outline of an x:
polytable = {
{x = 1, y = 0},
{x = 0, y = 1},
{x = 1, y = 2},
{x = 0, y = 3},
{x = 1, y = 4},
{x = 2, y = 3},
{x = 3, y = 4},
{x = 4, y = 3},
{x = 3, y = 2},
{x = 4, y = 1},
{x = 3, y = 0},
{x = 2, y = 1},
{x = 1, y = 0},
}
"scale" will let you change the size the poly is drawn at, set it to 1 for no change
-- advanced functions
polymorph.new(poly_table, frames, delay)
creates a new polymorph object
poly_table is a table that contains 2 of the same type of tables used for draw_free. Although the 2 poly tables don't have to be the exact same size, the smaller table must have at least half the number of points as the larger table.
frames is the number of animation frames you want it to take to morph one poly into the other,
delay is the delay between frames.
polymorph:start()
starts the animation timer
polymorph:set_auto(auto_type, wait)
sets up the automatic switching, auto_type is a string value:
"reverse" -sets it to auto-reverse the animation
"reset" -sets it to auto-reset to the first frame of the animation
"stop" -stops the animation timer
wait is a number value, it sets how long to wait before starting the next animation loop. If left out it will continue to use the previous wait value (default is 0)
polymorph:draw(x, y, scale, color)
draws the polymorph animation
draw_adv(x, y, ptable, scale, color)
this is an advanced version of the draw_free function, by adding a few things to the poly table you can change the way it gets drawn:
poly1 = {
{color = green, x = 1, y = 0},
{x = 0, y = 1},
{x = 1, y = 2},
{x = 2, y = 1},
{x = 1, y = 0},
{command = "break"},
{color = red, x = 1, y = 3},
{x = 0, y = 4},
{x = 1, y = 5},
{x = 2, y = 4},
{x = 1, y = 3},
{command = "break"},
{color = blue, x = 4, y = 0},
{x = 3, y = 1},
{x = 4, y = 2},
{x = 5, y = 1},
{x = 4, y = 0},
{command = "break"},
{color = white, x = 4, y = 3},
{x = 3, y = 4},
{x = 4, y = 5},
{x = 5, y = 4},
{x = 4, y = 3},
}
putting color = newcolor in any index will change the current drawing color. Although you can still put in a color when calling the function, it will be over-ridden by any color codes in the poly table
putting in command = "break" will stop the function from drawing a line between the points before and after the command