summaryrefslogtreecommitdiffhomepage
path: root/not/Layer.lua
blob: 04465999f46931b2eb8aed466c7a87ce01124e9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
--- A little bit more than just a Canvas.
Layer = require "not.Object":extends()

function Layer:new (width, height)
	self.canvas = love.graphics.newCanvas(width, height)
	self.scale = false
	self.ratio = false
end

function Layer:delete ()
	self.canvas = nil
end

--- Sets this layer as current canvas for drawing with love.graphics functions.
-- @return old canvas used by love
function Layer:setAsCanvas ()
	local c = love.graphics.getCanvas()
	love.graphics.setCanvas(self.canvas)
	return c
end

function Layer:renderTo (func, ...)
	local c = self:setAsCanvas()
	func(...)
	love.graphics.setCanvas(c)
end

function Layer:renderToWith (camera, func, ...)
	camera:push()
	camera:scale(self.scale)
	camera:translate(self.ratio)
	self:renderTo(func, ...)
	camera:pop()
end

function Layer:clear ()
	self:renderTo(love.graphics.clear)
end

function Layer:draw ()
	local scale = 1
	if self.scale then
		scale = getScale() / self.scale
	end
	love.graphics.setColor(255, 255, 255, 255)
	love.graphics.draw(self.canvas, 0, 0, 0, scale, scale)
end

return Layer