Home

Awesome

Brady

Brady is a camera library for LOVE that features parallax scrolling and aspect ratios.

Minimal Example

Below is a minimal example of using the module. In the repository, you will find a more advanced example/demo here

local Camera = require 'Camera'

function love.load()
	-- Create a camera with a 'width' of 400 x 300 on screen at ( 32, 32 ).
	-- Because the camera has the flags 'resizable' and 'maintainAspectRatio', the default scaling will take over
	-- So everything drawn will be scaled up to maximize space within the window, with a 32 pixel buffer from each edge.
	cam = Camera( 400, 300, { x = 32, y = 32, resizable = true, maintainAspectRatio = true } )
end

function love.update( dt )
	cam:update() -- Needed to appropriately resize the camera
end

function love.draw()
	cam:push()
		-- By default, translation is half camera width, half camera height
		-- So this draws a rectangle at the center of the screen.
		love.graphics.rectangle( 'fill', -32, -32, 64, 64 )
	cam:pop()
end

Demo

Within the repo is a demo to show off some of the capabilities. To run, follow the instructions found here. In the demo, the left box shows a close-up view, the right box shows an overview. Use the mouse to create squares using either box. With the mousewheel you can zoom in and out and you can move around using the wasd keys and rotate with q and e. To reset the view, press o (for origin). To clear the squares, press c, and to reset everything, press r.

Gotchas

Functions

Camera.newCamera

Creates a new camera object.

Synopsis: cam = Camera.newCamera( w, h, flags ) or cam = Camera( w, h, flags )

cam:push

Prepare the draw area. Pseudonym for layer:push.

Synopsis: cam:push( layer )

cam:pop

Used to stop the camera scaling, etc. Pseudonym for layer:pop.

Synopsis: cam:pop( layer )

cam:getWorldCoordinates

Returns the position within the "world," i.e. what the camera is seeing. Translates mouse coordinates to in-game coordinates.

Synopsis: worldX, worldY = cam:getWorldCoordinates( screenX, screenY )

cam:getScreenCoordinates

Translates in-game coordinates to the screen. The opposite of cam:getWorldCoordinates.

Synopsis: screenX, screenY = cam:getWorldCoordinates( worldX, worldY )

cam:getMouseWorldCoordinates

Short for cam:getScreenCoordinates( love.mouse.getPosition() ). See cam:getScreenCoordinates for more.

Synopsis: screenX, screenY = cam:getMouseWorldCoordinates( layer )

cam:setViewportPosition

Set the x and y position of the camera's upper-left corner on-screen. Wrapper function for cam.x, cam.y = -- etc.

Synopsis: cam:setViewportPosition( x, y )

cam:getViewportPosition

Returns the upper-left x and y position of the camera.

Synopsis: x, y = cam:getViewportPosition()

cam:setOffset

Sets the camera's offset, which, by default, is w / 2 and h / 2.

Synopsis: cam:setOffset( x, y )

cam:setTranslation

Gets the camera's offset.

Synopsis: x, y = cam:getOffset()

cam:getTranslation

cam:increaseTranslation

Get/set/increase the translation.

cam:translate

Synonymous with cam:increaseTranslation

cam:setRotation

cam:getRotation

cam:increaseRotation

Get/set/increase the rotation.

cam:rotate

Synonymous with cam:increaseRotation

cam:setScale

cam:getScale

cam:increaseScale

Get/set/increase the scale.

cam:scaleBy

Increase the scale by a factor.

Synopsis: cam:scaleBy( factor )

cam:increaseScaleToPoint

Increment the scale, while zooming in to a point

Synopsis: cam:increaseScaleToPoint( ds, wx, wy )

cam:scaleToPoint

Increase the scale by a factor, while zooming to a point

Synopsis: cam:scaleToPoint( s, wx, wy )

cam:getLayer

Get the specified layer.

Synopsis: layer = cam:getLayer( target )

cam:addLayer

Create a new layer for the camera.

Synopsis: layer = cam:addLayer( name, scale, flags )

layer:push

Prepare for the layer's translations.

Synopsis: layer:push()

layer:pop

Ends the layer's translations. By default, it's only [love.graphics.pop].

Synopsis: layer:pop()

License

This is under the MIT license, which can be found at the top of camera.lua