-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiledbackground.tl
62 lines (52 loc) · 1.76 KB
/
tiledbackground.tl
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
50
51
52
53
54
55
56
57
58
59
60
61
require "globals"
require "love"
local gr = love.graphics
global type TiledBackround = record
new: function(): TiledBackround
prepareDrawing: function(TiledBackround)
draw: function(TiledBackround, alpha: number)
resize: function(TiledBackround, number, number)
img: love.graphics.Texture
canvas: love.graphics.Canvas
end
local TiledBackround_mt: metatable<TiledBackround> = {
__index = TiledBackround
}
--TiledBackround.__index = TiledBackround
function TiledBackround.new(): TiledBackround
local self = setmetatable({} as TiledBackround, TiledBackround_mt)
self.img = gr.newImage(SCENEPREFIX .. "gfx/tile1.png") as love.graphics.Texture
self:prepareDrawing()
return self
end
function TiledBackround:prepareDrawing()
local w, h = gr.getDimensions()
self.canvas = gr.newCanvas(w, h)
gr.clear(0, 0, 0, 0)
gr.setColor{1, 1, 1}
gr.setCanvas(self.canvas)
local cam = require "camera".new()
cam:zoom(0.12)
local maxi, maxj = math.ceil(h / (self.img:getHeight() * cam.scale)), math.ceil(w / (self.img:getWidth() * cam.scale))
local k = 1 / cam.scale
cam:lookAt((w / 2) * k, (h / 2) * k)
cam:attach()
for i = 0, maxi do
for j = 0, maxj do
gr.draw(self.img as love.graphics.Drawable, j * self.img:getWidth(), i * self.img:getHeight())
end
end
cam:detach()
gr.setCanvas()
--self.canvas:newImageData():encode("png", "tiledback.png")
end
function TiledBackround:draw(alpha: number)
gr.clear(0, 0, 0, 0)
gr.setColor(1, 1, 1, alpha or 1)
gr.draw(self.canvas as love.graphics.Drawable, 0, 0)
end
function TiledBackround:resize(_: number, _: number)
self:prepareDrawing()
end
--return TiledBackround
global tiledback: TiledBackround = TiledBackround.new()