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 7474a3a4..55d23384 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.
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');
-
- 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
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');
+
+ 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 pathimg
The image reference function setup()
- createWindow(600, 600);
- img = loadImage('/path/to/mypic.png');
- end
-
- function draw()
- background(51);
-
- image(img, 0, 0);
- end
image(image, x, y, [w], [h]);
Draw an image to the screen
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 heightloadImage(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
+
+ function draw()
+ background(51);
+
+ image(img, 0, 0);
+ end
image(image, x, y, [w], [h]);
Draw an image to the screen
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 heightprint(value);
Printing utility, can take any lua type.
value
The value to print to stdout print(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 file file = 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 file file = loadStrings("./your-file.txt");
- print(file)
print(value);
Printing utility, can take any lua type.
value
The value to print to stdout print(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 file file = 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 file file = 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 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.
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
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
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 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.
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
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
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
+lu5 | math math
v0.0.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 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
- 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 value min(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.
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 value max(2, 5); -- returns 5
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 which is its distance from 0
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
GlobalNo description
TWO_PI
GlobalNo description
HALF_PI
GlobalNo description
QUARTER_PI
GlobalNo description
mouseX
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
mouseX
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
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 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
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
loadFont(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
-
- 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);
loadFont(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
+
+ 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 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 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));
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
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');
-
- 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
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');
+
+ 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 pathimg
The image reference function setup()
- createWindow(600, 600);
- img = loadImage('/path/to/mypic.png');
- end
-
- function draw()
- background(51);
-
- image(img, 0, 0);
- end
image(image, x, y, [w], [h]);
Draw an image to the screen
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 heightloadImage(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
+
+ function draw()
+ background(51);
+
+ image(img, 0, 0);
+ end
image(image, x, y, [w], [h]);
Draw an image to the screen
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 heightprint(value);
Printing utility, can take any lua type.
value
The value to print to stdout print(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 file file = 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 file file = loadStrings("./your-file.txt");
- print(file)
print(value);
Printing utility, can take any lua type.
value
The value to print to stdout print(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 file file = 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 file file = 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 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.
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
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
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 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.
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
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
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
-
- 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 value min(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 value max(2, 5); -- returns 5
-
- 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
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
GlobalNo description
TWO_PI
GlobalNo description
HALF_PI
GlobalNo description
QUARTER_PI
GlobalNo 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
+lu5 | math math
v0.0.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 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
- 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 value min(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.
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 value max(2, 5); -- returns 5
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 which is its distance from 0
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
GlobalNo description
TWO_PI
GlobalNo description
HALF_PI
GlobalNo description
QUARTER_PI
GlobalNo description
mouseX
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
mouseX
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
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 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
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
loadFont(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
-
- 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);
loadFont(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
+
+ 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 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 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));
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
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');
-
- 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 pathimg
The image reference function setup()
- createWindow(600, 600);
- img = loadImage('/path/to/mypic.png');
- end
-
- function draw()
- background(51);
-
- image(img, 0, 0);
- end
image(image, x, y, [w], [h]);
Draw an image to the screen
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 heightprint(value);
Printing utility, can take any lua type.
value
The value to print to stdout print(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 file file = 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 file file = 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 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.
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
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
mouseX
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
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
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
loadFont(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
-
- 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 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));
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