Load Atlas and Font images from paths
This commit is contained in:
parent
886ad221a0
commit
7604a23824
|
@ -6,12 +6,14 @@ local Object = require "lib/classic"
|
||||||
local Atlas = Object:extend()
|
local Atlas = Object:extend()
|
||||||
|
|
||||||
---Creates a new atlas.
|
---Creates a new atlas.
|
||||||
---@param image love.Image The source image for the atlas.
|
---@param image_path string The path to the source image for the atlas.
|
||||||
---@param sw integer The width of each sprite.
|
---@param sw integer The width of each sprite.
|
||||||
---@param sh integer The height of each sprite.
|
---@param sh integer The height of each sprite.
|
||||||
---@param ax integer? The anchor X coordinate of each sprite. Defaults to 0.
|
---@param ax integer? The anchor X coordinate of each sprite. Defaults to 0.
|
||||||
---@param ay integer? The anchor Y coordinate of each sprite. Defaults to 0.
|
---@param ay integer? The anchor Y coordinate of each sprite. Defaults to 0.
|
||||||
function Atlas:new(image, sw, sh, ax, ay)
|
function Atlas:new(image_path, sw, sh, ax, ay)
|
||||||
|
local image = love.graphics.newImage(image_path)
|
||||||
|
|
||||||
---The source image of the atlas.
|
---The source image of the atlas.
|
||||||
self.image = image
|
self.image = image
|
||||||
|
|
||||||
|
|
8
font.lua
8
font.lua
|
@ -5,12 +5,12 @@ local Atlas = require "atlas"
|
||||||
local Font = Atlas:extend()
|
local Font = Atlas:extend()
|
||||||
|
|
||||||
---Creates a new font.
|
---Creates a new font.
|
||||||
---@param image love.Image The source image for the atlas.
|
---@param image_path string The path to the source image for the atlas.
|
||||||
---@param sw integer The width of each sprite.
|
---@param sw integer The width of each sprite.
|
||||||
---@param sh integer The height of each sprite.
|
---@param sh integer The height of each sprite.
|
||||||
---@param characters string[] A list of characters with indices corresponding to each sprite.
|
---@param characters string[] A list of characters with indices corresponding to each sprite.
|
||||||
function Font:new(image, sw, sh, characters)
|
function Font:new(image_path, sw, sh, characters)
|
||||||
Font.super.new(self, image, sw, sh)
|
Font.super.new(self, image_path, sw, sh)
|
||||||
|
|
||||||
---A map of characters to sprites.
|
---A map of characters to sprites.
|
||||||
self.characters = {}
|
self.characters = {}
|
||||||
|
@ -32,7 +32,7 @@ function Font:draw(text, x, y)
|
||||||
local sprite = self.characters[c]
|
local sprite = self.characters[c]
|
||||||
if sprite then
|
if sprite then
|
||||||
local x = (i - 1) * self.sw + x
|
local x = (i - 1) * self.sw + x
|
||||||
Font.super.draw(self, sprite, x, y)
|
self.super.draw(self, sprite, x, y)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
6
main.lua
6
main.lua
|
@ -17,8 +17,6 @@ local pushOpts = {
|
||||||
push:setupScreen(gameWidth, gameHeight, windowWidth, windowHeight, pushOpts)
|
push:setupScreen(gameWidth, gameHeight, windowWidth, windowHeight, pushOpts)
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
local blockyFont = love.graphics.newImage("assets/small-blocky-font.png")
|
|
||||||
|
|
||||||
local blockFontChars = {
|
local blockFontChars = {
|
||||||
" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "×", "+", ",", "-", ".", "/", --first line
|
" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "×", "+", ",", "-", ".", "/", --first line
|
||||||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", --second line
|
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", --second line
|
||||||
|
@ -28,9 +26,9 @@ function love.load()
|
||||||
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "{", "|", "}", "~", --fourth line
|
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "{", "|", "}", "~", --fourth line
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockyFont = Font(blockyFont, 8, 8, blockFontChars)
|
BlockyFont = Font("assets/small-blocky-font.png", 8, 8, blockFontChars)
|
||||||
|
|
||||||
local uiAtlas = Atlas(love.graphics.newImage("assets/flat-ui.png"), 32, 32)
|
local uiAtlas = Atlas("assets/flat-ui.png", 32, 32)
|
||||||
|
|
||||||
Window = NineSlice(uiAtlas, {
|
Window = NineSlice(uiAtlas, {
|
||||||
tl = 26,
|
tl = 26,
|
||||||
|
|
|
@ -11,8 +11,8 @@ local function rowState(atlas, row, len, on_finish, next)
|
||||||
return spannedState(atlas, start, len, on_finish, next)
|
return spannedState(atlas, start, len, on_finish, next)
|
||||||
end
|
end
|
||||||
|
|
||||||
local leafRanger = Atlas(love.graphics.newImage("assets/leaf-ranger.png"), 288, 128, 144, 128)
|
local leafRanger = Atlas("assets/leaf-ranger.png", 288, 128, 144, 128)
|
||||||
local bringerOfDeath = Atlas(love.graphics.newImage("assets/bringer-of-death.png"), 140, 93, 105, 93)
|
local bringerOfDeath = Atlas("assets/bringer-of-death.png", 140, 93, 105, 93)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue