Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[=[
- Example font image: https://vk.com/doc75175715_480892724
- Charset for this image:
- local charset = table.concat{
- [[ ☺☻♥♦♣♠•◘○◙♂♀♪♫☼]],
- [[►◄↕‼¶§▬↨↑↓→←∟↔▲▼]],
- [[ !"#$%&'()*+,-./]],
- [[0123456789:;<=>?]],
- [[@ABCDEFGHIJKLMNO]],
- [[PQRSTUVWXYZ[\]^_]],
- [[`abcdefghijklmno]],
- [[pqrstuvwxyz{|}~⌂]],
- [[ÇüéâäàåçêëèïîìÄÅ]],
- [[ÉæÆôöòûùÿÖÜ¢£¥₧ƒ]],
- [[áíóúñѪº¿⌐¬½¼¡«»]],
- [[░▒▓│┤╡╢╖╕╣║╗╝╜╛┐]],
- [[└┴┬├─┼╞╟╚╔╩╦╠═╬╧]],
- [[╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀]],
- [[αßΓπΣσµτΦΘΩδ∞φε∩]],
- [[≡±≥≤⌠⌡÷≈°∙·√ⁿ²■⯑]]
- }
- example for this img:
- local d = display:new('image.png', 16, 16, charset)
- ]=]
- local utf8 = require'utf8'
- local display = {}
- display._white = {1, 1, 1, 1}
- display.__index = display
- function display:new(image, w, h, sym, def)
- local gr = love.graphics
- self = setmetatable({}, self)
- self.map = {}
- self.image = gr.newImage(image)
- self.layers = {}
- self.layer = nil -- corrent layer
- self.image:setFilter('nearest')
- local iw, ih = self.image:getDimensions()
- self.w = iw/w
- self.h = ih/h
- local symw = iw/self.w
- local i = 0
- for p, c in utf8.codes(sym) do
- local char = utf8.char(c)
- local x, y = i%symw, math.floor(i/symw)
- x, y = x * self.w, y * self.h
- local quad = gr.newQuad(x, y, self.w, self.h, iw, ih)
- self.map[char] = quad
- self.map[i] = quad
- i = i + 1
- end
- self._s = assert(self:get(def or ' '))
- self._b = assert(self:get(def or ' '))
- self._scolor = self._white
- self._bcolor = self._white
- self:setLayer(1)
- return self
- end
- function display:get(sym)
- return self.map[sym]
- end
- function display:getImage()
- return self.image
- end
- function display:setLayer(i)
- local l = self.layers[i]
- if not l then
- l = {}
- l.batch = love.graphics.newSpriteBatch(self.image)
- l.used = {}
- self.layers[i] = l
- end
- self.layer = l
- end
- function display:setColor(color, ...)
- if type(color) == 'table' then
- self._scolor = color
- return
- end
- if type(color) == 'number' then
- self._scolor = {color, ...}
- return
- end
- self._scolor = self._white
- end
- function display:setBackground(sym)
- self._b = assert(self:get(sym))
- end
- function display:setBackgroundColor(color, ...)
- if type(color) == 'table' then
- print('SET BCOLOR')
- self._bcolor = color
- return
- end
- if type(color) == 'number' then
- self._bcolor = {color, ...}
- return
- end
- self._bcolor = self._white
- end
- function display:draw(...)
- for i, v in pairs(self.layers) do
- love.graphics.draw(v.batch, ...)
- end
- end
- display.__call = display.get
- function display:_batch_get(x, y)
- local layer = self.layer
- if not layer.used[y] then
- layer.used[y] = {}
- end
- if not layer.used[y][x] then
- -- background, tile
- layer.batch:setColor(self._bcolor)
- local sym = layer.batch:add(self._b, x * self.w, y * self.h)
- layer.batch:setColor(self._scolor)
- local bcgr = layer.batch:add(self._s, x * self.w, y * self.h)
- layer.used[y][x] = {sym, bcgr}
- end
- return layer.used[y][x]
- end
- function display:_batch_set(quad, x, y, backgr, color)
- local layer = self.layer
- local tile = self:_batch_get(x, y)
- local index = backgr and tile[1] or tile[2]
- local color = color or backgr and self._bcolor or self._scolor
- layer.batch:setColor(color)
- layer.batch:set(index, quad, x * self.w, y * self.h)
- end
- function display:_clear_area(x, y, w, h, color)
- local layer = self.layer
- for i = x, x + w - 1 do
- for j = y, y + h - 1 do
- local tile = self:_batch_get(i, j)
- layer.batch:setColor(color or self._scolor)
- layer.batch:set(tile[1], self._b, i * self.w, j * self.h)
- layer.batch:setColor(color or self._bcolor)
- layer.batch:set(tile[2], self._s, i * self.w, j * self.h)
- end
- end
- end
- function display:print(str, px, py)
- assert(px, 'arg#1 x pos expected')
- assert(py, 'arg#2 y pos expected')
- local x, y = 0, 0
- local bit = 4
- for p, c in utf8.codes(str) do
- c = utf8.char(c)
- if c == '\n' then
- x = 0
- y = y + 1
- else
- local cx = (x + px)
- local cy = (y + py)
- local q = self.map[c] or self.map[#self.map - 1]
- self:_batch_set(q, cx, cy)
- x = x + 1
- end
- end
- end
- function display:clearAll()
- for i, v in self.layers do
- v.batch:clear()
- v.used = {}
- end
- end
- function display:clearLayer(i)
- local l = self.layers[i] or self.layer
- if l then
- l.batch:clear()
- l.used = {}
- end
- end
- return display
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement