From 7604a238249bf12320329faff963695508b232c3 Mon Sep 17 00:00:00 2001 From: mars Date: Thu, 20 Apr 2023 21:19:17 -0400 Subject: [PATCH] Load Atlas and Font images from paths --- atlas.lua | 6 ++++-- font.lua | 8 ++++---- main.lua | 6 ++---- units.lua | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/atlas.lua b/atlas.lua index 2a0ce2a..8db110b 100644 --- a/atlas.lua +++ b/atlas.lua @@ -6,12 +6,14 @@ local Object = require "lib/classic" local Atlas = Object:extend() ---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 sh integer The height of each sprite. ---@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. -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. self.image = image diff --git a/font.lua b/font.lua index 014fd72..91b0ffe 100644 --- a/font.lua +++ b/font.lua @@ -5,12 +5,12 @@ local Atlas = require "atlas" local Font = Atlas:extend() ---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 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) +function Font:new(image_path, sw, sh, characters) + Font.super.new(self, image_path, sw, sh) ---A map of characters to sprites. self.characters = {} @@ -32,7 +32,7 @@ function Font:draw(text, x, y) local sprite = self.characters[c] if sprite then local x = (i - 1) * self.sw + x - Font.super.draw(self, sprite, x, y) + self.super.draw(self, sprite, x, y) end end end diff --git a/main.lua b/main.lua index dfd1976..f50f400 100644 --- a/main.lua +++ b/main.lua @@ -17,8 +17,6 @@ local pushOpts = { push:setupScreen(gameWidth, gameHeight, windowWidth, windowHeight, pushOpts) function love.load() - 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 @@ -28,9 +26,9 @@ function love.load() "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, { tl = 26, diff --git a/units.lua b/units.lua index ef8d9bc..b545b42 100644 --- a/units.lua +++ b/units.lua @@ -11,8 +11,8 @@ local function rowState(atlas, row, len, on_finish, next) return spannedState(atlas, start, len, on_finish, next) end -local leafRanger = Atlas(love.graphics.newImage("assets/leaf-ranger.png"), 288, 128, 144, 128) -local bringerOfDeath = Atlas(love.graphics.newImage("assets/bringer-of-death.png"), 140, 93, 105, 93) +local leafRanger = Atlas("assets/leaf-ranger.png", 288, 128, 144, 128) +local bringerOfDeath = Atlas("assets/bringer-of-death.png", 140, 93, 105, 93) return { {