A 2d graphics rendering library for LĂ–VR
g2d = require("lovr-g2d").init()
-- https://github.com/Papaew/lovr-g2d/blob/main/resources/lovr-ico.png
local image = lovr.graphics.newTexture("/lovr-ico.png")
function lovr.draw()
g2d.set()
g2d.print("Hello, LĂ–VR!", 10,10)
g2d.draw(image, 14,30)
g2d.unset()
end
Function | Description |
---|---|
g2d.init() | Initializes the module with selected settings |
g2d.set() | The entry point into drawing 2D graphics |
g2d.unset() | Exit point from drawing 2D graphics |
g2d.push() | Copies and pushes the current rendering settings onto the stack |
g2d.pop() | Pops the current rendering settings from the stack |
g2d.reset() | Resets the current graphics settings |
Function | Description |
---|---|
g2d.draw() | Draws a texture on the screen |
g2d.circle() | Draws a circle |
g2d.line() | Draws lines between points |
g2d.points() | Draws one or more points |
g2d.print() | Draws text on screen |
g2d.rectangle() | Draws a rectangle |
Function | Description |
---|---|
g2d.getColor() | Gets the current color |
g2d.getFont() | Gets the current Font object |
g2d.getLineWidth() | Gets the current line width |
g2d.getPointSize() | Gets the point size |
g2d.getShader() | Gets the current Shader |
g2d.isWireframe() | Gets whether wireframe mode is used when drawing |
g2d.setColor( r,g,b,a ) | Sets the color used for drawing |
g2d.setFont( font ) | Set an already-loaded Font as the current font |
g2d.setlineWidth( width ) | Sets the line width |
g2d.setPointSize( size ) | Sets the point size |
g2d.setShader( shader ) | Routes drawing operations through a shader |
g2d.setWireframe( enabled ) | Sets whether wireframe lines will be used when drawing |
- Stacks and coordinate system transform functions
- origin, translate, rotate, scale
- Quad, spritebatches and image autobatching most likely based on this
- Shape rendering functions
- arc, ellipse, polygon
- line join styles, anti-aliasing for lines
- stencil and blend modes support(?)
g2d = require("lovr-g2d").init( maxTriangles, stackSize, zNear, zFar, useVeraSans )
number
maxTriangles (5000)
number
stackSize (64)
number
zNear (-1)
number
zFar (1)
boolean
useVeraSans
table
g2d
Can be called several times per frame, but must be closed with g2d.unset()
g2d:set()
-- draw 2d stuff
None.
Nothing.
Functions from lovr.graphics called between g2d.set() and g2d.unset() may not work correctly
Must be called after g2d.set() for correct rendering
-- draw 2d stuff
g2d:unset()
None.
Nothing.
g2d:push()
None.
Nothing.
-- set the current color to red
g2d.setColor(1,0,0)
g2d.push() -- save current graphics state so any changes can be restored
-- draw something else with a different color
g2d.setColor(1,0,0)
g2d.circle("fill", 100,100, 30)
g2d.pop() -- restore the saved graphics state
-- draw a rectangle with restored red color
g2d.rectangle("fill", 64,32, 128,96)
The function saves the following types of states:
- current color
- active font
- active shader
- wireframe mode
- line width
- point size
Returns the current graphics state to what it was before the last preceding g2d.push()
g2d:pop()
None.
Nothing.
g2d.reset()
None.
Nothing.
This function, like any other function that change graphics state, only affects the current stack
Draws a Texture on the screen with optional rotation
g2d.draw( texture, x,y, angle )
Texture
texture
number
x
number
y
number
angle
Nothing.
function lovr.load()
hamster = lovr.graphics.newTexture("hamster.png")
end
function lovr.draw()
g2d.set()
g2d.draw(hamster, 100, 100)
g2d.unset()
end
g2d.circle( mode, x,y, radius, segments )
DrawMode
mode
number
x
number
y
number
radius
number
segments
Nothing.
g2d.line( x1, y1, x2, y2, ... )
number
x1
number
y1
number
x2
number
y2
number
...
You can continue passing point positions
Nothing.
function lovr.load()
sometable = {
100, 100,
200, 200,
300, 100,
400, 200,
}
end
function lovr.draw()
g2d.set()
g2d.line(200,50, 400,50, 500,300, 100,300, 200,50)
g2d.line(sometable) -- Also table of point positions can be passed
g2d.unset()
end
g2d.points( x, y, ... )
number
x
number
y
number
...
You can continue passing point positions
Nothing.
function lovr.load()
sometable = {
100, 100,
200, 200,
300, 100,
400, 200,
}
end
function lovr.draw()
g2d.set()
g2d.points(200,50, 400,50, 500,300, 100,300, 200,50)
g2d.points(sometable) -- Also table of point positions can be passed
g2d.unset()
end
g2d.print( text, x,y, r, halign, valign )
string
text
number
x
number
y
number
angle
HorizontalAlign
halign
VerticalAlign
valign
Nothing.
g2d.print( mode, x,y, width,height )
DrawMode
mode
number
x
number
y
number
width
number
height
Nothing.
g2d.setColor( r,g,b,a )
number
r
number
g
number
b
number
a
Nothing.
Sets the active font used to render text with g2d.print
g2d.setFont( font )
Font
font
Nothing.
Sets the width of lines rendered using g2d.line
g2d.setlineWidth( width )
number
width
Nothing.
g2d.setPointSize( size )
number
size
Nothing.
g2d.setShader( shader )
Shader
shader
Nothing.
-- smol rainbow effect shader
shader = g.newShader([[
// vertex shader
vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
return projection * transform * vertex;
} ]], [[
// fragment shader
uniform float time;
vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
vec3 col = 0.5 + 0.5*cos(time+uv.xyx+vec3(0,2,4));
return vec4(col, 1.0) * lovrVertexColor * texture(image, uv);
} ]])
local time = 0
function lovr.update(dt)
time = time + dt
shader:send("time", time)
end
function lovr.draw()
g2d.set()
g2d.setShader(shader)
g2d.circle('fill', 100,100, 50)
g2d.setShader()
g2d.unset()
end
g2d.setWireframe( enabled )
boolean
enabled
Nothing.
g2d.getColor()
None.
number
r
number
g
number
b
number
a
g2d.getFont()
None.
Font
font
g2d.getLineWidth()
None.
number
width
g2d.getPointSize()
None.
number
size
g2d.getShader()
None.
Shader
shader
g2d.isWireframe()
None.
boolean
state
"fill"
Draw filled shape"line"
Draw outlined shape
"top"
Align the top of the text to the origin"middle"
Vertically center the text"bottom"
Align the bottom of the text to the origin
"left"
Left aligned lines of text"center"
Centered aligned lines of text"right"
Right aligned lines of text