Add font.lua
This commit is contained in:
parent
75f626bf50
commit
8619098d43
|
@ -5,7 +5,7 @@ local Object = require "lib/classic"
|
|||
local Atlas = Object:extend()
|
||||
|
||||
---Creates a new atlas.
|
||||
---@param image love.Image
|
||||
---@param image love.Image The source image for the atlas.
|
||||
---@param sw integer The width of each sprite.
|
||||
---@param sh integer The height of each sprite.
|
||||
function Atlas:new(image, sw, sh)
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
local Atlas = require "atlas"
|
||||
|
||||
---A set of character sprites that can be drawn as strings.
|
||||
local Font = Atlas:extend()
|
||||
|
||||
---Creates a new font.
|
||||
---@param image love.Image The source image for the atlas.
|
||||
---@param sw integer The width of each sprite.
|
||||
---@param sh integer The height of each sprite.
|
||||
---@param characters string[] A list of characters with indices corresponding to each sprite.
|
||||
function Font:new(image, sw, sh, characters)
|
||||
Font.super.new(self, image, sw, sh)
|
||||
|
||||
---A map of characters to sprites.
|
||||
self.characters = {}
|
||||
|
||||
for sprite, ch in ipairs(characters) do
|
||||
if ch then
|
||||
self.characters[ch] = sprite
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Draws a string with this font.
|
||||
function Font:draw(text, x, y)
|
||||
local x = x or 0
|
||||
local y = y or 0
|
||||
|
||||
for i = 1, #text do
|
||||
local c = text:sub(i, i)
|
||||
local sprite = self.characters[c]
|
||||
if sprite then
|
||||
local x = (i - 1) * self.sw + x
|
||||
love.graphics.draw(self.meshes[sprite], x, y)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Font
|
25
main.lua
25
main.lua
|
@ -1,9 +1,10 @@
|
|||
local Atlas = require "atlas"
|
||||
local Font = require "font"
|
||||
local push = require "lib/push"
|
||||
|
||||
local gameWidth, gameHeight = 320, 200
|
||||
local windowWidth, windowHeight = love.window.getDesktopDimensions()
|
||||
windowWidth, windowHeight = windowWidth*.7, windowHeight*.7 --make the window a bit smaller than the screen itself
|
||||
windowWidth, windowHeight = windowWidth * .7, windowHeight * .7 --make the window a bit smaller than the screen itself
|
||||
local pushOpts = {
|
||||
fullscreen = false,
|
||||
resizable = true,
|
||||
|
@ -79,21 +80,25 @@ end
|
|||
function love.load()
|
||||
Menu = MultiMenu({ "Fight", "Use", "Pkmn", "Flee" })
|
||||
|
||||
local font = love.graphics.newImage("assets/small-blocky-font.png")
|
||||
Font = Atlas(font, 8, 8)
|
||||
local blockyFont = love.graphics.newImage("assets/small-blocky-font.png")
|
||||
|
||||
local blockFontChars = {
|
||||
" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "×", "+", ",", "-", ".", "/", --first line
|
||||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", --second line
|
||||
"@", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", --third line
|
||||
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "[", "\\", "]", "^", "_", --fourth line
|
||||
"@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", --third line
|
||||
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "{", "|", "}", "~", --fourth line
|
||||
}
|
||||
|
||||
BlockyFont = Font(blockyFont, 8, 8, blockFontChars)
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
push:start()
|
||||
|
||||
Menu:draw()
|
||||
|
||||
for sprite = 1,#Font.meshes do
|
||||
local width = 12
|
||||
local row = math.floor(sprite / width)
|
||||
local col = sprite - row * width
|
||||
love.graphics.draw(Font.meshes[sprite], col * 10, row * 10)
|
||||
end
|
||||
BlockyFont:draw("Hello, world!")
|
||||
|
||||
push:finish()
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue