-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfont.lua
59 lines (47 loc) · 1.23 KB
/
font.lua
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
local G = love.graphics
Font = Object:New { img = G.newImage("media/font.png") }
function Font:init()
self.batch = G.newSpriteBatch(self.img, 100, "stream")
self.img:setFilter("nearest", "nearest")
self.scale = 4
self.quads = {}
local w = self.img:getWidth()
local h = self.img:getHeight()
local cw = w / 8
local ch = h / 16
for i = 0, 127 do
local c = string.char(i)
local x = (i % 8) * cw
local y = math.floor(i / 8) * ch
self.quads[c] = G.newQuad(x, y, cw, ch, w, h)
end
self.char_width = 6
self.char_height = ch
end
function Font:printCentered(text, x, y, s)
local dx = (s or self.scale) * self.char_width
self:print(text, x - #text * dx * 0.5, y, s)
end
function Font:print(text, x, y, s)
self.batch:clear()
self:_print(text, x, y, s)
G.draw(self.batch)
end
function Font:_print(text, x, y, s)
r, g, b, a = G.getColor()
self.batch:setColor(r/4, g/4, b/4, a)
self:print_(text, x, y+4, s)
self.batch:setColor(r, g, b, a)
self:print_(text, x, y, s)
end
function Font:print_(text, x, y, s)
s = s or self.scale
local dx = s * self.char_width
local quads = self.quads
local batch = self.batch
for c in text:gmatch(".") do
local q = quads[c]
if q then self.batch:add(q, x, y, 0, s) end
x = x + dx
end
end