Interpreter for Creative Coding
Lu5 is a free and open-source Lua interpreter for Creative Coding.
diff --git a/docs/index.html b/docs/index.html index 230c256a..d5e7ee5f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1 +1 @@ -
Lu5 is a free and open-source Lua interpreter for Creative Coding.
Lu5 is a free and open-source Lua interpreter for Creative Coding.
orbitControl();
Helpful utility for controlling the camera around the origin.
Enables quick camera controls without much of an implementation.
function setup()
+ createWindow(800, 800, GL3D);
+end
+
+function draw()
+ background(51);
+
+ -- Allows us to control the camera
+ orbitControl();
+
+ noFill();
+ stroke(80, 255, 50);
+ strokeWeight(4);
+
+ box(60);
+end
class(name);
Create a class.
if a class implements a print
method, it can be used in the print
function
name
The class name local Player = class('Player');
+lu5 | classes classes
v0.1.6class(name);
Create a class.
if a class implements a print
method, it can be used in the print
function
Arguments
name
The class namelocal Player = class('Player');
- function Player:init(x, y)
- self.x = x;
- self.y = y;
- return self;
- end
+function Player:init(x, y)
+ self.x = x;
+ self.y = y;
+ return self;
+end
- function Player:draw()
- circle(self.x, self.y, 16);
- end
+function Player:draw()
+ circle(self.x, self.y, 16);
+end
- -- Create 2 players and draw them
- local p1 = Player:new(100, 200);
- local p2 = Player:new(300, 200);
+-- Create 2 players and draw them
+local p1 = Player:new(100, 200);
+local p2 = Player:new(300, 200);
- function setup()
- createWindow(400, 400);
- end
-
- function draw()
- background(51);
+function setup()
+ createWindow(400, 400);
+end
- p1:draw();
- p2:draw();
- end
\ No newline at end of file
+function draw()
+ background(51);
+
+ p1:draw();
+ p2:draw();
+end
loadImage(path);
Load a png or jpeg image
path
The local image pathimg
The image reference function setup()
- createWindow(600, 600);
- img = loadImage('/path/to/mypic.png');
- end
+lu5 | image image
v0.1.6loadImage(path);
Load a png or jpeg image
Arguments
path
The local image pathReturns
Image
The image instancefunction setup()
+ createWindow(600, 600);
+ img = loadImage('/path/to/mypic.png');
+end
- function draw()
- background(51);
+function draw()
+ background(51);
- image(img, 0, 0);
- end
image(image, x, y, [w], [h]);
Draw an image to the screen
Arguments
image
The image referencex
The x position of the imagey
The y position of the image[w]
The width of the image, if not specified, using source width[h]
The height of the image, if not specified, using source height
\ No newline at end of file
+ image(img, 0, 0);
+end
image(img, x, y, [w], [h]);
Draw an image to the screen
img
The Image instancex
The x position of the imagey
The y position of the image[w]
The width of the image, if not specified, using source width[h]
The height of the image, if not specified, using source heightImage.crop(x, y, w, h);
Crop an Image into an other Image
x
The x position of the imagey
The y position of the imagew
The width of the new cropped imageh
The height of the new cropped imageImage
the cropped imageprint(value);
Printing utility, can take any lua type.
value
The value to print to stdout print(42);
+lu5 | io io
v0.1.6print(value);
Printing utility, can take any lua type.
Arguments
value
The value to print to stdoutprint(42);
- print(1, 2, 3);
-
- print('Hello world');
+print(1, 2, 3);
- print({ 3, 8, 1 });
loadText(file_path);
Read a file as a string
Arguments
file_path
The path of the file file = loadText("./your-file.txt");
- print(file)
loadStrings(file_path);
Read a file as an array of strings split by line
Arguments
file_path
The path of the file file = loadStrings("./your-file.txt");
- print(file)
\ No newline at end of file
+print('Hello world');
+
+print({ 3, 8, 1 });
loadText(file_path);
Read a file as a string
file_path
The path of the filefile = loadText("./your-file.txt");
+print(file)
loadStrings(file_path);
Read a file as an array of strings split by line
file_path
The path of the filefile = loadStrings("./your-file.txt");
+print(file)
keyIsDown(k);
Check if a keyboard key is pressed, returns true
if key is down and returns false
if it's not
k
The keyboard key to check as a key global, or a stringboolean
Whether or not the key is down -- Use a string
- if (keyIsDown('a')) then
- -- key 'a' is down
- end
+lu5 | keyboard keyboard
v0.1.6keyIsDown(k);
Check if a keyboard key is pressed, returns true
if key is down and returns false
if it's not
Arguments
k
The keyboard key to check as a key global, or a stringReturns
boolean
Whether or not the key is down-- Use a string
+if (keyIsDown('a')) then
+ -- key 'a' is down
+end
- -- use a global
- if (keyIsDown(LEFT_ARROW)) then
- -- left arrow is down
- end
keyPressed(key, keyCode);
EventCalled whenever a key is pressed.
Arguments
key
The pressed key as a string, is nil if glfw cannot find a name for the keykeyCode
The pressed key as a key code function setup()
- createWindow(600, 600);
- end
-
- function draw()
- background(51);
- end
-
- function keyPressed(key, keyCode)
- print(key, keyCode)
- end
keyReleased(key, keyCode);
EventCalled whenever a key is released.
Arguments
key
The released key as a string, is nil if glfw cannot find a name for the keykeyCode
The released key as a key code
keyHeld(key, keyCode);
EventCalled whenever a key is held down.
Arguments
key
The held key as a string, is nil if glfw cannot find a name for the keykeyCode
The held key as a key code
UP_ARROW
GlobalNo description
DOWN_ARROW
GlobalNo description
LEFT_ARROW
GlobalNo description
RIGHT_ARROW
GlobalNo description
KEY_ENTER
GlobalNo description
KEY_BACKSPACE
GlobalNo description
KEY_A
GlobalNo description
KEY_B
GlobalNo description
KEY_C
GlobalNo description
KEY_D
GlobalNo description
KEY_E
GlobalNo description
KEY_F
GlobalNo description
KEY_G
GlobalNo description
KEY_H
GlobalNo description
KEY_I
GlobalNo description
KEY_J
GlobalNo description
KEY_K
GlobalNo description
KEY_L
GlobalNo description
KEY_M
GlobalNo description
KEY_N
GlobalNo description
KEY_O
GlobalNo description
KEY_P
GlobalNo description
KEY_Q
GlobalNo description
KEY_R
GlobalNo description
KEY_S
GlobalNo description
KEY_T
GlobalNo description
KEY_U
GlobalNo description
KEY_V
GlobalNo description
KEY_W
GlobalNo description
KEY_X
GlobalNo description
KEY_Y
GlobalNo description
KEY_Z
GlobalNo description
\ No newline at end of file
+-- use a global
+if (keyIsDown(LEFT_ARROW)) then
+ -- left arrow is down
+end
keyPressed(key, keyCode);
EventCalled whenever a key is pressed.
key
The pressed key as a string, is nil if glfw cannot find a name for the keykeyCode
The pressed key as a key codefunction setup()
+ createWindow(600, 600);
+end
+
+function draw()
+ background(51);
+end
+
+function keyPressed(key, keyCode)
+ print(key, keyCode)
+end
keyReleased(key, keyCode);
EventCalled whenever a key is released.
key
The released key as a string, is nil if glfw cannot find a name for the keykeyCode
The released key as a key codekeyHeld(key, keyCode);
EventCalled whenever a key is held down.
key
The held key as a string, is nil if glfw cannot find a name for the keykeyCode
The held key as a key codeUP_ARROW
ConstantNo description
DOWN_ARROW
ConstantNo description
LEFT_ARROW
ConstantNo description
RIGHT_ARROW
ConstantNo description
KEY_ENTER
ConstantNo description
KEY_BACKSPACE
ConstantNo description
KEY_A
ConstantNo description
KEY_B
ConstantNo description
KEY_C
ConstantNo description
KEY_D
ConstantNo description
KEY_E
ConstantNo description
KEY_F
ConstantNo description
KEY_G
ConstantNo description
KEY_H
ConstantNo description
KEY_I
ConstantNo description
KEY_J
ConstantNo description
KEY_K
ConstantNo description
KEY_L
ConstantNo description
KEY_M
ConstantNo description
KEY_N
ConstantNo description
KEY_O
ConstantNo description
KEY_P
ConstantNo description
KEY_Q
ConstantNo description
KEY_R
ConstantNo description
KEY_S
ConstantNo description
KEY_T
ConstantNo description
KEY_U
ConstantNo description
KEY_V
ConstantNo description
KEY_W
ConstantNo description
KEY_X
ConstantNo description
KEY_Y
ConstantNo description
KEY_Z
ConstantNo description
randomSeed(seed);
Set a random seed
seed
The seed valuenumber
The random valuerandom(min, max);
Get a random number
min
The minimum of the rangemax
The maximum of the rangenumber
The random value random() -- random number between 0 and 1
- random(3) -- random number between 0 and 3
- random(3, 12) -- random number between 3 and 12
+lu5 | math math
v0.1.6randomSeed(seed);
Set a random seed
Arguments
seed
The seed valueReturns
number
The random value
random(min, max);
Get a random number
Arguments
min
The minimum of the rangemax
The maximum of the rangeReturns
number
The random valuerandom() -- random number between 0 and 1
+random(3) -- random number between 0 and 3
+random(3, 12) -- random number between 3 and 12
- random({ 'A', 'B', 'C' }) -- random element in table
round(x);
Round a number to the nearest integer
Arguments
x
The value to roundReturns
int
The rounded value
floor(x);
floor is a function that takes in a number, and returns the greatest integer less or equal to x
see here
Arguments
x
The value to floorReturns
int
greatest integer less than or equals to x
ceil(x);
ceil is a function that takes in a number, and returns the smallest integer greater or equal to x
see here
Arguments
x
The value to floorReturns
int
The floored value
min(a, b);
Return the smallest value.
Can also take any number of arguments, or a table.
Arguments
a
The first valueb
The second valueReturns
number
The smallest value min(6, 7); -- returns 6
+random({ 'A', 'B', 'C' }) -- random element in table
round(x);
Round a number to the nearest integer
Arguments
x
The value to roundReturns
int
The rounded value
floor(x);
floor is a function that takes in a number, and returns the greatest integer less or equal to x
see here
Arguments
x
The value to floorReturns
int
greatest integer less than or equals to x
ceil(x);
ceil is a function that takes in a number, and returns the smallest integer greater or equal to x
see here
Arguments
x
The value to floorReturns
int
The floored value
min(a, b);
Return the smallest value.
Can also take any number of arguments, or a table.
Arguments
a
The first valueb
The second valueReturns
number
The smallest valuemin(6, 7); -- returns 6
- min(6, 7, 4); -- returns 4
+min(6, 7, 4); -- returns 4
- min({ 6, 7, 4 }); -- returns 4
max(a, b);
Return the largest value.
Can also take any number of arguments, or a table.
Arguments
a
The first valueb
The second valueReturns
number
The largest value max(2, 5); -- returns 5
+min({ 6, 7, 4 }); -- returns 4
max(a, b);
Return the largest value.
Can also take any number of arguments, or a table.
Arguments
a
The first valueb
The second valueReturns
number
The largest valuemax(2, 5); -- returns 5
- max(2, 5, 7); -- returns 7
+max(2, 5, 7); -- returns 7
- max({ 2, 5, 7 }); -- returns 7
abs(x);
Returns the absolute value of x which is its distance from 0
see here
Arguments
x
The input valueReturns
number
The absolote value of the input
map(x, s1, e1, s2, e2);
Maps x from an original range to a target range
Arguments
x
The value to maps1
The start of the initial rangee1
The end of the initial ranges2
The start of the target rangee2
The end of the target rangeReturns
number
The mapped value
dist(x1, y1, x2, y1);
Calculates the distance between 2 points in 2D space
Arguments
x1
The x coordinate of the first pointy1
The y coordinate of the first pointx2
The x coordinate of the second pointy1
The y coordinate of the second pointReturns
number
The distance between the points
constrain(x, min, max);
Limits x to a minimum and maximum value
Arguments
x
The input valuemin
The minimum valuemax
The maximum valueReturns
number
The constrained value
sin(x);
Sine function (Trigonometry)
Arguments
x
The input valueReturns
number
The output value
cos(x);
Cosine function (Trigonometry)
Arguments
x
The input valueReturns
number
The output value
tan(x);
Tangent function (Trigonometry)
Arguments
x
The input valueReturns
number
The output value
PI
GlobalNo description
TWO_PI
GlobalNo description
HALF_PI
GlobalNo description
QUARTER_PI
GlobalNo description
\ No newline at end of file
+max({ 2, 5, 7 }); -- returns 7
abs(x);
Returns the absolute value of x
see here
x
The input valuenumber
The absolote value of the inputmap(x, s1, e1, s2, e2);
Maps x from an original range to a target range
x
The value to maps1
The start of the initial rangee1
The end of the initial ranges2
The start of the target rangee2
The end of the target rangenumber
The mapped valuedist(x1, y1, x2, y1);
Calculates the distance between 2 points in 2D space
x1
The x coordinate of the first pointy1
The y coordinate of the first pointx2
The x coordinate of the second pointy1
The y coordinate of the second pointnumber
The distance between the pointsconstrain(x, min, max);
Limits x to a minimum and maximum value
x
The input valuemin
The minimum valuemax
The maximum valuenumber
The constrained valuesin(x);
Sine function (Trigonometry)
x
The input valuenumber
The output valuecos(x);
Cosine function (Trigonometry)
x
The input valuenumber
The output valuetan(x);
Tangent function (Trigonometry)
x
The input valuenumber
The output valuePI
ConstantNo description
TWO_PI
ConstantNo description
HALF_PI
ConstantNo description
QUARTER_PI
ConstantNo description
model(model);
Draw a 3D model
model
The 3D modelloadModel(model);
Load a 3D model
model
The 3D modelmouseX
GlobalThe x mouse coordinate
createWindow(400, 400);
-
- function draw()
- background(51);
-
- line(mouseX, 0, mouseX, height);
- end
mouseY
GlobalThe y mouse coordinate
createWindow(400, 400);
-
- function draw()
- background(51);
-
- line(0, mouseY, width, mouseY);
- end
mouseIsPressed
GlobalIs true
when the mouse is pressed, is false
when it's not
mousePressed(button);
EventCalled when a mouse button is pressed.
button
The pressed mouse button
-In the following example, the circle's size increases when the user clicks the mouse.
size = 0;
-
- function setup()
- createWindow(400, 400);
- end
-
- function draw()
- background(51);
-
- circle(mouseX, mouseY, size);
- end
-
- -- Increase circle size when mouse pressed
- function mousePressed()
- size = size + 1;
- end
mouseReleased(button);
EventCalled when a mouse button is released.
button
The released mouse button
-In the following example, the circle's size increases when the user clicks the mouse.
size = 32;
-
- function setup()
- createWindow(400, 400);
- end
-
- function draw()
- background(51);
-
- circle(mouseX, mouseY, size);
- end
-
- -- Set larger circle size
- function mousePressed()
- size = 64;
- end
-
- -- Reset size
- function mouseReleased()
- size = 32;
- end
mouseY
GlobalThe y mouse coordinate
createWindow(400, 400);
+
+function draw()
+ background(51);
+
+ line(0, mouseY, width, mouseY);
+end
mouseX
GlobalThe x mouse coordinate
createWindow(400, 400);
+
+function draw()
+ background(51);
+
+ line(mouseX, 0, mouseX, height);
+end
pmouseY
GlobalNo description
pmouseX
GlobalNo description
mouseIsPressed
GlobalIs true
when the mouse is pressed, is false
when it's not
mousePressed(button);
EventCalled when a mouse button is pressed.
button
The pressed mouse button
+In the following example, the circle's size increases when the user clicks the mouse.
size = 0;
+
+function setup()
+ createWindow(400, 400);
+end
+
+function draw()
+ background(51);
+
+ circle(mouseX, mouseY, size);
+end
+
+-- Increase circle size when mouse pressed
+function mousePressed()
+ size = size + 1;
+end
mouseReleased(button);
EventCalled when a mouse button is released.
button
The released mouse button
+In the following example, the circle's size increases when the user clicks the mouse.
size = 32;
+
+function setup()
+ createWindow(400, 400);
+end
+
+function draw()
+ background(51);
+
+ circle(mouseX, mouseY, size);
+end
+
+-- Set larger circle size
+function mousePressed()
+ size = 64;
+end
+
+-- Reset size
+function mouseReleased()
+ size = size + 6;
+end
mouseWheel(button);
EventCalled when a mouse wheel is used
button
The pressed mouse button
+In the following example, the circle's size changes when the user scrolls
size = 0;
+
+function setup()
+ createWindow(400, 400);
+end
+
+function draw()
+ background(51);
+
+ circle(mouseX, mouseY, size);
+end
+
+-- Change circle size when mouse is scrolled
+function mouseWheel(delta)
+ size = size + delta;
+end
background(r, g, b, a);
Clear a background with a color
r
The red byteg
The green byteb
The blue bytea
The alpha bytefill(r, g, b, [a]);
Set the fill color for shapes
r
The red byteg
The green byteb
The blue byte[a]
The alpha byteCan also accept a hexadecimal or color name as string
fill(255, 150, 40);
- square(200, 200, 64);
strokeWeight(weight);
No description
weight
The line width in pixels strokeWeight(8);
- line(200, 200, mouseX, mouseY);
noFill();
Disable fill
stroke(r, g, b, a);
Set the stroke color for shapes
r
The red byteg
The green byteb
The blue bytea
The alpha byteOnly implemented for line
and circle
noStroke();
Disable stroke
Only implemented for line
and circle
push();
Save the style settings
pop();
Restore the style settings
background(r, g, b, a);
Clear a background with a color
r
The red byteg
The green byteb
The blue bytea
The alpha bytefill(r, g, b, [a]);
Set the fill color for shapes
r
The red byteg
The green byteb
The blue byte[a]
The alpha byteCan also accept a hexadecimal or color name as string
fill(255, 150, 40);
+square(200, 200, 64);
strokeWeight(weight);
No description
weight
The line width in pixelstrokeWeight(8);
+ine(200, 200, mouseX, mouseY);
noFill();
Disable fill
stroke(r, g, b, a);
Set the stroke color for shapes
r
The red byteg
The green byteb
The blue bytea
The alpha byteOnly implemented for line
and circle
noStroke();
Disable stroke
Only implemented for line
and circle
push();
Save the style settings
pop();
Restore the style settings
circle(x, y, d);
Draw a circle to the opengl context
x
The x position of the circley
The y position of the circled
The diameter of the circlerect(x, y, w, h);
Draw a rectangle to the opengl context
x
The x position of the reactangley
The y position of the reactanglew
The widthh
The heightsquare(x, y, s);
Draw a square to the opengl context
x
The x position of the squarey
The y position of the squares
The size of the squareline(x1, y1, x2, y2);
Draw a line to the opengl context
x1
The x position of the first pointy1
The y position of the first pointx2
The x position of the second pointy2
The y position of the second pointquad(x1, y1, x2, y2, x3, y3, x4, y4);
Draw a quad on the screen
x1
The x position of the first pointy1
The y position of the first pointx2
The x position of the second pointy2
The y position of the second pointx3
The x position of the third pointy3
The y position of the third pointx4
The x position of the fourth pointy4
The y position of the fourth pointpoint();
Draw a point on the screen
arc();
Draw an arc on the screen
ellipse();
Draw an ellipse on the screen
triangle();
Draw a triangle on the screen
beginShape(mode);
Begin adding vertices to a custom shape
mode
The opengl shape mode LINES
, POINTS
, QUADS
, TRIANGLES
, TRIANGLE_FAN
The following would draw a 100 by 100 square at position 100, 100 -The following would draw a 100 by 100 square at position 100, 100
beginShape(QUADS);
- vertex(100, 100);
- vertex(100, 200);
- vertex(200, 200);
- vertex(200, 100);
- endShape();
vertex(x, y);
beginShape
must be called prior
Adding a vertex to a custom shape
x
The x positiony
The y positionendShape();
beginShape
must be called prior
Close the custom shape
circle(x, y, d);
Draw a circle to the opengl context
x
The x position of the circley
The y position of the circled
The diameter of the circlerect(x, y, w, h);
Draw a rectangle to the opengl context
x
The x position of the reactangley
The y position of the reactanglew
The widthh
The heightsquare(x, y, s);
Draw a square to the opengl context
x
The x position of the squarey
The y position of the squares
The size of the squareline(x1, y1, x2, y2);
Draw a line to the opengl context
x1
The x position of the first pointy1
The y position of the first pointx2
The x position of the second pointy2
The y position of the second pointquad(x1, y1, x2, y2, x3, y3, x4, y4);
Draw a quad on the screen
x1
The x position of the first pointy1
The y position of the first pointx2
The x position of the second pointy2
The y position of the second pointx3
The x position of the third pointy3
The y position of the third pointx4
The x position of the fourth pointy4
The y position of the fourth pointpoint();
Draw a point on the screen
arc();
Draw an arc on the screen
ellipse();
Draw an ellipse on the screen
triangle();
Draw a triangle on the screen
beginShape(mode);
Begin adding vertices to a custom shape
mode
The opengl shape mode LINES
, POINTS
, QUADS
, TRIANGLES
, TRIANGLE_FAN
The following would draw a 100 by 100 square at position 100, 100 +The following would draw a 100 by 100 square at position 100, 100
beginShape(QUADS);
+ vertex(100, 100);
+ vertex(100, 200);
+ vertex(200, 200);
+ vertex(200, 100);
+endShape();
vertex(x, y);
beginShape
must be called prior
Adding a vertex to a custom shape
x
The x positiony
The y positionendShape();
beginShape
must be called prior
Close the custom shape
plane(w, h);
Draw a plane
w
The width dimensionh
The height dimensionbox(w, [h], [d]);
Draw a 3D box
w
The width dimension[h]
The height dimension[d]
The depth dimensionsphere(w, [detailX], [detailY]);
Draw a 3D Sphere
w
The width dimension[detailX]
The detail in the X axis[detailY]
the detailt in the Y axisdebugMode();
Helpful 3D Graphics
rotateX(angle);
Rotate geometry around the X axis
angle
The angle of rotationrotateY(angle);
Rotate geometry around the Y axis
angle
The angle of rotationrotateZ(angle);
Rotate geometry around the Z axis
angle
The angle of rotationrotate(angle);
Rotate geometry in 2D
angle
The angle of rotationscale(x, y, [z]);
scale geometry
currently only used for 3D
x
The x scalingy
The y scaling[z]
The z scalingtranslate(x, y, [z]);
translate geometry
currently only used for 3D
x
The x translationy
The y translation[z]
The z translationloadFont(path);
Load a font
path
The font path on the local systemfont
The image reference function setup()
- createWindow(600, 600);
- font = loadFont('/path/to/myfont.ttf');
- end
+lu5 | typography typography
v0.1.6loadFont(path);
Load a font
Arguments
path
The font path on the local systemReturns
font
The image referencefunction setup()
+ createWindow(600, 600);
+ font = loadFont('/path/to/myfont.ttf');
+end
- function draw()
- background(51);
+function draw()
+ background(51);
- textFont(font);
- text('Hello from lu5!', 30, 50);
- end
textSize(size);
Set the font size
Arguments
size
The size of the font in pixelsIf this is called using fonts, make sure to call textSize
after calling textFont
textSize(56);
- text('Big Text', 50, 50);
textFont(font);
Set the font family
Arguments
font
The font value returned when using loadFont
textFont(myfont);
- text('Hello world!', 100, 200);
text(str, x, y);
Draw text on the screen
Arguments
str
String to renderx
The x position of the start of the texty
The y position of the top of the textFonts are not yet implemented
text('Hello lu5!', 40, 60);
\ No newline at end of file
+ textFont(font);
+ text('Hello from lu5!', 30, 50);
+end
textSize(size);
Set the font size
size
The size of the font in pixelsIf this is called using fonts, make sure to call textSize
after calling textFont
textSize(56);
+text('Big Text', 50, 50);
textFont(font);
Set the font family
font
The font value returned when using loadFont
textFont(myfont);
+text('Hello world!', 100, 200);
text(str, x, y);
Draw text on the screen
str
String to renderx
The x position of the start of the texty
The y position of the top of the textFonts are not yet implemented
text('Hello lu5!', 40, 60);
createVector(x, y);
Create a vector instance
x
The first component of the vectory
The second component of the vectorVector
The created vector local a = createVector(3, 5);
- local b = createVector(1, 3);
-
- local c = a:add(b);
-
- print(c);
Vector.print();
Since a Vector
implements a print
method, it can be used in the print
function
local a = createVector(3, 5);
-
- -- With print
- print(a);
-
- -- With class method
- Vector.print(a);
-
- -- with instance
- a:print();
Vector.mult(a, b);
No description
a
The first vectorb
The second vectorVector
The added vector local a = createVector(3, 5);
- local b = createVector(1, 3);
-
- print(a:mult(b));
- print(Vector.mult(a, b));
Vector.add(a, b);
No description
a
The first vectorb
The second vectorVector
The multiplied vector local a = createVector(1, 4);
- local b = createVector(8, 2);
-
- print(a:add(b));
- print(Vector.add(a, b));
createVector(x, y);
Create a vector instance
x
The first component of the vectory
The second component of the vectorVector
The created vectorlocal point = createVector(80, 125);
+
+print(point);
Vector.print();
Since a Vector
implements a print
method, it can be used in the print
function
local a = createVector(3, 5);
+
+-- With print
+print(a);
+
+-- With class method
+Vector.print(a);
+
+-- with instance
+a:print();
Vector.dist(a, b);
No description
a
The first pointb
The second pointnumber
The distance between the two pointslocal a = createVector(300, 500);
+local b = createVector(200, 250);
+
+local distance = a:dist(b);
+print(distance);
Vector.dot(a, b);
No description
a
The first vectorb
The second vectornumber
The dot productlocal a = createVector(3, 5);
+local b = createVector(1, 3);
+
+-- From prototype
+print(a:dot(b)); -- 18.0
+
+-- With static method
+print(Vector.dot(a, b)); -- 18.0
+
+-- With concat syntax
+print(a .. b); -- 18.0
Vector.add(a, b);
No description
a
The first vectorb
The second vectorVector
The added vectorlocal a = createVector(4, 2);
+local b = createVector(2, 1);
+
+-- Non-mutable
+print(Vector.add(a, b)); -- { 6.0, 3.0 }
+print(a + b); -- { 6.0, 3.0 }
+print(a); -- { 4.0, 2.0 }
+
+-- Mutable
+print(a:add(b)); -- { 6.0, 3.0 }
+print(a) -- { 6.0, 3.0 }
Vector.sub(a, b);
No description
a
The first vectorb
The second vectorVector
The subtracted vectorlocal a = createVector(3, 5);
+local b = createVector(2, 3);
+
+-- Non-mutable
+print(Vector.sub(a, b)); -- { 1.0, 2.0 }
+print(a - b); -- { 1.0, 2.0 }
+print(a); -- { 3.0, 5.0 }
+
+-- Mutable
+print(a:sub(b)); -- { 1.0, 2.0 }
+print(a); -- { 1.0, 2.0 }
Vector.mult(a, b);
No description
a
The first vectorb
The second vectorVector
The multiplied vectorlocal a = createVector(3, 5);
+local b = createVector(2, 3);
+
+-- Non-mutable
+print(Vector.mult(a, b)); -- { 6.0, 15.0 }
+print(a * b); -- { 6.0, 15.0 }
+print(a); -- { 3.0, 5.0 }
+
+-- Mutable
+print(a:mult(b)); -- { 3.0, 15.0 }
+print(a); -- { 3.0, 15.0 }
Vector.div(a, b);
No description
a
The first vectorb
The second vectorVector
The divided vectorlocal a = createVector(4, 2);
+local b = createVector(2, 1);
+
+-- Non-mutable
+print(Vector.div(a, b)); -- { 2.0, 2.0 }
+print(a / b); -- { 2.0, 2.0 }
+print(a); -- { 4.0, 2.0 }
+
+-- Mutable
+print(a:div(b)); -- { 2.0, 2.0 }
+print(a) -- { 2.0, 2.0 }
Vector.idiv(a, b);
No description
a
The first vectorb
The second vectorVector
The divided vectorlocal a = createVector(7, 8);
+local b = createVector(2, 3);
+
+-- Non-mutable
+print(Vector.idiv(a, b)) -- { 3.0, 2.0 }
+print(a // b); -- { 3.0, 2.0 }
+print(a); -- { 7.0, 8.0 }
+
+-- Mutable
+print(a:idiv(b)); -- { 3.0, 2.0 }
+print(a) -- { 3.0, 2.0 }
Vector.copy();
No description
Vector
The divided vectorlocal a = createVector(300, 450, 400);
+
+local copy_1 = a:copy();
+
+local copy_2 = Vector.copy(a);
createWindow(w, h);
Create a GLFW window.
w
Window widthh
Window heightIt is also possible to create a window in the global scope without defining a setup function.
function setup()
- -- Create the window here
- createWindow(600, 600);
- end
-
- function draw()
- -- draw things
- end
frameRate(fps);
Set the frame rate
fps
The frame rate to setIf frame rate is called without an argument, it will return frame per seconds
x = 0;
-
- function setup()
- createWindow(400, 400);
- frameRate(24);
- end
-
- function draw()
- background(51);
- text('fps: ' .. frameRate(), 20, 10);
-
- circle(x, 200, 32);
- x = x + 1;
- end
deltaTime
GlobalElapsed time since the last draw call in seconds
x = 0;
- vx = 128;
-
- function setup()
- createWindow(400, 400);
- frameRate(24); -- try with 60
- end
-
- function draw()
- background(51);
-
- circle(x, height/2, 32);
-
- -- Get the same velocity with different framerates
- x = x + vx deltaTime;
- end
width
GlobalThe window's width in pixels. If no window was created, this value is nil
createWindow(800, 600);
-
- print(width);
- -- 800
height
GlobalThe window's height in pixels. If no window was created, this value is nil
createWindow(800, 600);
-
- print(height);
- -- 600
windowResized();
EventCalled when the window is resized
function setup()
- createWindow(500, 500);
- end
-
- function draw()
- background(51)
- end
-
- function windowResized()
- print('Resized!')
- end
createWindow(w, h);
Create a GLFW window.
w
Window widthh
Window heightIt is also possible to create a window in the global scope without defining a setup function.
function setup()
+ -- Create the window here
+ createWindow(600, 600);
+end
+
+function draw()
+ -- draw things
+end
frameRate(fps);
Set the frame rate
fps
The frame rate to setIf frame rate is called without an argument, it will return frame per seconds
x = 0;
+
+function setup()
+ createWindow(400, 400);
+ frameRate(24);
+end
+
+function draw()
+ background(51);
+ text('fps: ' .. frameRate(), 20, 10);
+
+ circle(x, 200, 32);
+ x = x + 1;
+end
deltaTime
GlobalElapsed time since the last draw call in seconds
x = 0;
+vx = 128;
+
+function setup()
+ createWindow(400, 400);
+ frameRate(24); -- try with 60
+end
+
+function draw()
+ background(51);
+
+ circle(x, height/2, 32);
+
+ -- Get the same velocity with different framerates
+ x = x + vx * deltaTime;
+end
width
GlobalThe window's width in pixels. If no window was created, this value is nil
createWindow(800, 600);
+
+print(width);
+-- 800
height
GlobalThe window's height in pixels. If no window was created, this value is nil
createWindow(800, 600);
+
+print(height);
+-- 600
windowResized();
EventCalled when the window is resized
function setup()
+ createWindow(500, 500);
+end
+
+function draw()
+ background(51)
+end
+
+function windowResized()
+ print('Resized!')
+end
orbitControl();
Helpful utility for controlling the camera around the origin.
Enables quick camera controls without much of an implementation.
function setup()
+ createWindow(800, 800, GL3D);
+end
+
+function draw()
+ background(51);
+
+ -- Allows us to control the camera
+ orbitControl();
+
+ noFill();
+ stroke(80, 255, 50);
+ strokeWeight(4);
+
+ box(60);
+end
class(name);
Create a class.
if a class implements a print
method, it can be used in the print
function
name
The class namelocal Player = class('Player');
+
+function Player:init(x, y)
+ self.x = x;
+ self.y = y;
+ return self;
+end
+
+function Player:draw()
+ circle(self.x, self.y, 16);
+end
+
+
+-- Create 2 players and draw them
+local p1 = Player:new(100, 200);
+local p2 = Player:new(300, 200);
+
+function setup()
+ createWindow(400, 400);
+end
+
+function draw()
+ background(51);
+
+ p1:draw();
+ p2:draw();
+end
loadImage(path);
Load a png or jpeg image
path
The local image pathImage
The image instancefunction setup()
+ createWindow(600, 600);
+ img = loadImage('/path/to/mypic.png');
+end
+
+function draw()
+ background(51);
+
+ image(img, 0, 0);
+end
image(img, x, y, [w], [h]);
Draw an image to the screen
img
The Image instancex
The x position of the imagey
The y position of the image[w]
The width of the image, if not specified, using source width[h]
The height of the image, if not specified, using source heightImage.crop(x, y, w, h);
Crop an Image into an other Image
x
The x position of the imagey
The y position of the imagew
The width of the new cropped imageh
The height of the new cropped imageImage
the cropped imageprint(value);
Printing utility, can take any lua type.
value
The value to print to stdoutprint(42);
+
+print(1, 2, 3);
+
+print('Hello world');
+
+print({ 3, 8, 1 });
loadText(file_path);
Read a file as a string
file_path
The path of the filefile = loadText("./your-file.txt");
+print(file)
loadStrings(file_path);
Read a file as an array of strings split by line
file_path
The path of the filefile = loadStrings("./your-file.txt");
+print(file)
keyIsDown(k);
Check if a keyboard key is pressed, returns true
if key is down and returns false
if it's not
k
The keyboard key to check as a key global, or a stringboolean
Whether or not the key is down-- Use a string
+if (keyIsDown('a')) then
+ -- key 'a' is down
+end
+
+-- use a global
+if (keyIsDown(LEFT_ARROW)) then
+ -- left arrow is down
+end
keyPressed(key, keyCode);
EventCalled whenever a key is pressed.
key
The pressed key as a string, is nil if glfw cannot find a name for the keykeyCode
The pressed key as a key codefunction setup()
+ createWindow(600, 600);
+end
+
+function draw()
+ background(51);
+end
+
+function keyPressed(key, keyCode)
+ print(key, keyCode)
+end
keyReleased(key, keyCode);
EventCalled whenever a key is released.
key
The released key as a string, is nil if glfw cannot find a name for the keykeyCode
The released key as a key codekeyHeld(key, keyCode);
EventCalled whenever a key is held down.
key
The held key as a string, is nil if glfw cannot find a name for the keykeyCode
The held key as a key codeUP_ARROW
ConstantNo description
DOWN_ARROW
ConstantNo description
LEFT_ARROW
ConstantNo description
RIGHT_ARROW
ConstantNo description
KEY_ENTER
ConstantNo description
KEY_BACKSPACE
ConstantNo description
KEY_A
ConstantNo description
KEY_B
ConstantNo description
KEY_C
ConstantNo description
KEY_D
ConstantNo description
KEY_E
ConstantNo description
KEY_F
ConstantNo description
KEY_G
ConstantNo description
KEY_H
ConstantNo description
KEY_I
ConstantNo description
KEY_J
ConstantNo description
KEY_K
ConstantNo description
KEY_L
ConstantNo description
KEY_M
ConstantNo description
KEY_N
ConstantNo description
KEY_O
ConstantNo description
KEY_P
ConstantNo description
KEY_Q
ConstantNo description
KEY_R
ConstantNo description
KEY_S
ConstantNo description
KEY_T
ConstantNo description
KEY_U
ConstantNo description
KEY_V
ConstantNo description
KEY_W
ConstantNo description
KEY_X
ConstantNo description
KEY_Y
ConstantNo description
KEY_Z
ConstantNo description
randomSeed(seed);
Set a random seed
seed
The seed valuenumber
The random valuerandom(min, max);
Get a random number
min
The minimum of the rangemax
The maximum of the rangenumber
The random valuerandom() -- random number between 0 and 1
+random(3) -- random number between 0 and 3
+random(3, 12) -- random number between 3 and 12
+
+random({ 'A', 'B', 'C' }) -- random element in table
round(x);
Round a number to the nearest integer
x
The value to roundint
The rounded valuefloor(x);
floor is a function that takes in a number, and returns the greatest integer less or equal to x
see here
x
The value to floorint
greatest integer less than or equals to xceil(x);
ceil is a function that takes in a number, and returns the smallest integer greater or equal to x
see here
x
The value to floorint
The floored valuemin(a, b);
Return the smallest value.
Can also take any number of arguments, or a table.
a
The first valueb
The second valuenumber
The smallest valuemin(6, 7); -- returns 6
+
+min(6, 7, 4); -- returns 4
+
+min({ 6, 7, 4 }); -- returns 4
max(a, b);
Return the largest value.
Can also take any number of arguments, or a table.
a
The first valueb
The second valuenumber
The largest valuemax(2, 5); -- returns 5
+
+max(2, 5, 7); -- returns 7
+
+max({ 2, 5, 7 }); -- returns 7
abs(x);
Returns the absolute value of x
see here
x
The input valuenumber
The absolote value of the inputmap(x, s1, e1, s2, e2);
Maps x from an original range to a target range
x
The value to maps1
The start of the initial rangee1
The end of the initial ranges2
The start of the target rangee2
The end of the target rangenumber
The mapped valuedist(x1, y1, x2, y1);
Calculates the distance between 2 points in 2D space
x1
The x coordinate of the first pointy1
The y coordinate of the first pointx2
The x coordinate of the second pointy1
The y coordinate of the second pointnumber
The distance between the pointsconstrain(x, min, max);
Limits x to a minimum and maximum value
x
The input valuemin
The minimum valuemax
The maximum valuenumber
The constrained valuesin(x);
Sine function (Trigonometry)
x
The input valuenumber
The output valuecos(x);
Cosine function (Trigonometry)
x
The input valuenumber
The output valuetan(x);
Tangent function (Trigonometry)
x
The input valuenumber
The output valuePI
ConstantNo description
TWO_PI
ConstantNo description
HALF_PI
ConstantNo description
QUARTER_PI
ConstantNo description
model(model);
Draw a 3D model
model
The 3D modelloadModel(model);
Load a 3D model
model
The 3D modelmouseY
GlobalThe y mouse coordinate
createWindow(400, 400);
+
+function draw()
+ background(51);
+
+ line(0, mouseY, width, mouseY);
+end
mouseX
GlobalThe x mouse coordinate
createWindow(400, 400);
+
+function draw()
+ background(51);
+
+ line(mouseX, 0, mouseX, height);
+end
pmouseY
GlobalNo description
pmouseX
GlobalNo description
mouseIsPressed
GlobalIs true
when the mouse is pressed, is false
when it's not
mousePressed(button);
EventCalled when a mouse button is pressed.
button
The pressed mouse button
+In the following example, the circle's size increases when the user clicks the mouse.
size = 0;
+
+function setup()
+ createWindow(400, 400);
+end
+
+function draw()
+ background(51);
+
+ circle(mouseX, mouseY, size);
+end
+
+-- Increase circle size when mouse pressed
+function mousePressed()
+ size = size + 1;
+end
mouseReleased(button);
EventCalled when a mouse button is released.
button
The released mouse button
+In the following example, the circle's size increases when the user clicks the mouse.
size = 32;
+
+function setup()
+ createWindow(400, 400);
+end
+
+function draw()
+ background(51);
+
+ circle(mouseX, mouseY, size);
+end
+
+-- Set larger circle size
+function mousePressed()
+ size = 64;
+end
+
+-- Reset size
+function mouseReleased()
+ size = size + 6;
+end
mouseWheel(button);
EventCalled when a mouse wheel is used
button
The pressed mouse button
+In the following example, the circle's size changes when the user scrolls
size = 0;
+
+function setup()
+ createWindow(400, 400);
+end
+
+function draw()
+ background(51);
+
+ circle(mouseX, mouseY, size);
+end
+
+-- Change circle size when mouse is scrolled
+function mouseWheel(delta)
+ size = size + delta;
+end
background(r, g, b, a);
Clear a background with a color
r
The red byteg
The green byteb
The blue bytea
The alpha bytefill(r, g, b, [a]);
Set the fill color for shapes
r
The red byteg
The green byteb
The blue byte[a]
The alpha byteCan also accept a hexadecimal or color name as string
fill(255, 150, 40);
+square(200, 200, 64);
strokeWeight(weight);
No description
weight
The line width in pixelstrokeWeight(8);
+ine(200, 200, mouseX, mouseY);
noFill();
Disable fill
stroke(r, g, b, a);
Set the stroke color for shapes
r
The red byteg
The green byteb
The blue bytea
The alpha byteOnly implemented for line
and circle
noStroke();
Disable stroke
Only implemented for line
and circle
push();
Save the style settings
pop();
Restore the style settings
circle(x, y, d);
Draw a circle to the opengl context
x
The x position of the circley
The y position of the circled
The diameter of the circlerect(x, y, w, h);
Draw a rectangle to the opengl context
x
The x position of the reactangley
The y position of the reactanglew
The widthh
The heightsquare(x, y, s);
Draw a square to the opengl context
x
The x position of the squarey
The y position of the squares
The size of the squareline(x1, y1, x2, y2);
Draw a line to the opengl context
x1
The x position of the first pointy1
The y position of the first pointx2
The x position of the second pointy2
The y position of the second pointquad(x1, y1, x2, y2, x3, y3, x4, y4);
Draw a quad on the screen
x1
The x position of the first pointy1
The y position of the first pointx2
The x position of the second pointy2
The y position of the second pointx3
The x position of the third pointy3
The y position of the third pointx4
The x position of the fourth pointy4
The y position of the fourth pointpoint();
Draw a point on the screen
arc();
Draw an arc on the screen
ellipse();
Draw an ellipse on the screen
triangle();
Draw a triangle on the screen
beginShape(mode);
Begin adding vertices to a custom shape
mode
The opengl shape mode LINES
, POINTS
, QUADS
, TRIANGLES
, TRIANGLE_FAN
The following would draw a 100 by 100 square at position 100, 100 +The following would draw a 100 by 100 square at position 100, 100
beginShape(QUADS);
+ vertex(100, 100);
+ vertex(100, 200);
+ vertex(200, 200);
+ vertex(200, 100);
+endShape();
vertex(x, y);
beginShape
must be called prior
Adding a vertex to a custom shape
x
The x positiony
The y positionendShape();
beginShape
must be called prior
Close the custom shape
plane(w, h);
Draw a plane
w
The width dimensionh
The height dimensionbox(w, [h], [d]);
Draw a 3D box
w
The width dimension[h]
The height dimension[d]
The depth dimensionsphere(w, [detailX], [detailY]);
Draw a 3D Sphere
w
The width dimension[detailX]
The detail in the X axis[detailY]
the detailt in the Y axisdebugMode();
Helpful 3D Graphics
rotateX(angle);
Rotate geometry around the X axis
angle
The angle of rotationrotateY(angle);
Rotate geometry around the Y axis
angle
The angle of rotationrotateZ(angle);
Rotate geometry around the Z axis
angle
The angle of rotationrotate(angle);
Rotate geometry in 2D
angle
The angle of rotationscale(x, y, [z]);
scale geometry
currently only used for 3D
x
The x scalingy
The y scaling[z]
The z scalingtranslate(x, y, [z]);
translate geometry
currently only used for 3D
x
The x translationy
The y translation[z]
The z translationloadFont(path);
Load a font
path
The font path on the local systemfont
The image referencefunction setup()
+ createWindow(600, 600);
+ font = loadFont('/path/to/myfont.ttf');
+end
+
+function draw()
+ background(51);
+
+ textFont(font);
+ text('Hello from lu5!', 30, 50);
+end
textSize(size);
Set the font size
size
The size of the font in pixelsIf this is called using fonts, make sure to call textSize
after calling textFont
textSize(56);
+text('Big Text', 50, 50);
textFont(font);
Set the font family
font
The font value returned when using loadFont
textFont(myfont);
+text('Hello world!', 100, 200);
text(str, x, y);
Draw text on the screen
str
String to renderx
The x position of the start of the texty
The y position of the top of the textFonts are not yet implemented
text('Hello lu5!', 40, 60);
createVector(x, y);
Create a vector instance
x
The first component of the vectory
The second component of the vectorVector
The created vectorlocal point = createVector(80, 125);
+
+print(point);
Vector.print();
Since a Vector
implements a print
method, it can be used in the print
function
local a = createVector(3, 5);
+
+-- With print
+print(a);
+
+-- With class method
+Vector.print(a);
+
+-- with instance
+a:print();
Vector.dist(a, b);
No description
a
The first pointb
The second pointnumber
The distance between the two pointslocal a = createVector(300, 500);
+local b = createVector(200, 250);
+
+local distance = a:dist(b);
+print(distance);
Vector.dot(a, b);
No description
a
The first vectorb
The second vectornumber
The dot productlocal a = createVector(3, 5);
+local b = createVector(1, 3);
+
+-- From prototype
+print(a:dot(b)); -- 18.0
+
+-- With static method
+print(Vector.dot(a, b)); -- 18.0
+
+-- With concat syntax
+print(a .. b); -- 18.0
Vector.add(a, b);
No description
a
The first vectorb
The second vectorVector
The added vectorlocal a = createVector(4, 2);
+local b = createVector(2, 1);
+
+-- Non-mutable
+print(Vector.add(a, b)); -- { 6.0, 3.0 }
+print(a + b); -- { 6.0, 3.0 }
+print(a); -- { 4.0, 2.0 }
+
+-- Mutable
+print(a:add(b)); -- { 6.0, 3.0 }
+print(a) -- { 6.0, 3.0 }
Vector.sub(a, b);
No description
a
The first vectorb
The second vectorVector
The subtracted vectorlocal a = createVector(3, 5);
+local b = createVector(2, 3);
+
+-- Non-mutable
+print(Vector.sub(a, b)); -- { 1.0, 2.0 }
+print(a - b); -- { 1.0, 2.0 }
+print(a); -- { 3.0, 5.0 }
+
+-- Mutable
+print(a:sub(b)); -- { 1.0, 2.0 }
+print(a); -- { 1.0, 2.0 }
Vector.mult(a, b);
No description
a
The first vectorb
The second vectorVector
The multiplied vectorlocal a = createVector(3, 5);
+local b = createVector(2, 3);
+
+-- Non-mutable
+print(Vector.mult(a, b)); -- { 6.0, 15.0 }
+print(a * b); -- { 6.0, 15.0 }
+print(a); -- { 3.0, 5.0 }
+
+-- Mutable
+print(a:mult(b)); -- { 3.0, 15.0 }
+print(a); -- { 3.0, 15.0 }
Vector.div(a, b);
No description
a
The first vectorb
The second vectorVector
The divided vectorlocal a = createVector(4, 2);
+local b = createVector(2, 1);
+
+-- Non-mutable
+print(Vector.div(a, b)); -- { 2.0, 2.0 }
+print(a / b); -- { 2.0, 2.0 }
+print(a); -- { 4.0, 2.0 }
+
+-- Mutable
+print(a:div(b)); -- { 2.0, 2.0 }
+print(a) -- { 2.0, 2.0 }
Vector.idiv(a, b);
No description
a
The first vectorb
The second vectorVector
The divided vectorlocal a = createVector(7, 8);
+local b = createVector(2, 3);
+
+-- Non-mutable
+print(Vector.idiv(a, b)) -- { 3.0, 2.0 }
+print(a // b); -- { 3.0, 2.0 }
+print(a); -- { 7.0, 8.0 }
+
+-- Mutable
+print(a:idiv(b)); -- { 3.0, 2.0 }
+print(a) -- { 3.0, 2.0 }
Vector.copy();
No description
Vector
The divided vectorlocal a = createVector(300, 450, 400);
+
+local copy_1 = a:copy();
+
+local copy_2 = Vector.copy(a);
createWindow(w, h);
Create a GLFW window.
w
Window widthh
Window heightIt is also possible to create a window in the global scope without defining a setup function.
function setup()
+ -- Create the window here
+ createWindow(600, 600);
+end
+
+function draw()
+ -- draw things
+end
frameRate(fps);
Set the frame rate
fps
The frame rate to setIf frame rate is called without an argument, it will return frame per seconds
x = 0;
+
+function setup()
+ createWindow(400, 400);
+ frameRate(24);
+end
+
+function draw()
+ background(51);
+ text('fps: ' .. frameRate(), 20, 10);
+
+ circle(x, 200, 32);
+ x = x + 1;
+end
deltaTime
GlobalElapsed time since the last draw call in seconds
x = 0;
+vx = 128;
+
+function setup()
+ createWindow(400, 400);
+ frameRate(24); -- try with 60
+end
+
+function draw()
+ background(51);
+
+ circle(x, height/2, 32);
+
+ -- Get the same velocity with different framerates
+ x = x + vx * deltaTime;
+end
width
GlobalThe window's width in pixels. If no window was created, this value is nil
createWindow(800, 600);
+
+print(width);
+-- 800
height
GlobalThe window's height in pixels. If no window was created, this value is nil
createWindow(800, 600);
+
+print(height);
+-- 600
windowResized();
EventCalled when the window is resized
function setup()
+ createWindow(500, 500);
+end
+
+function draw()
+ background(51)
+end
+
+function windowResized()
+ print('Resized!')
+end