Skip to content
Open
8 changes: 7 additions & 1 deletion lsp_def/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -888,4 +888,10 @@ function SMODS.copy_card(card, args) end
---@param card Card|table Card to add
---@param args {set: string?, area: CardArea|table?, playing_card: integer?}?
---@return Card|table
function SMODS.add_to_deck(card, args) end
function SMODS.add_to_deck(card, args) end

-- Util function to render one card to a `.png` file, saved to `love.filesystem.getSaveDirectory()`
---@param card Card|table Card to save as an image
---@param scale number? Scale to render the card at (default = G.SETTINGS.GRAPHICS.texture_scaling)
---@param filename string? Name of the file (default = [center.key])
function SMODS.card_to_image(card, scale, filename) end
62 changes: 62 additions & 0 deletions src/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4227,4 +4227,66 @@ function SMODS.add_to_deck(card, args)
local area = args.area or G.jokers
area:emplace(card)
return card
end

-- Hook for the below Util function
local sprite_draw_from_ref = Sprite.draw_from
function Sprite:draw_from(...)
local old_filter_min, old_filter_mag
if self.atlas and SMODS.texture_filter_override then
old_filter_min, old_filter_mag = self.atlas.image:getFilter()
self.atlas.image:setFilter(SMODS.texture_filter_override, SMODS.texture_filter_override)
end
local ret = sprite_draw_from_ref(self, ...)
if self.atlas and SMODS.texture_filter_override then
self.atlas.image:setFilter(old_filter_min, old_filter_mag)
end
return ret
end

-- Hook for the below Util function
local sprite_draw_self_ref = Sprite.draw_self
function Sprite:draw_self(...)
local old_filter_min, old_filter_mag
if self.atlas and SMODS.texture_filter_override then
old_filter_min, old_filter_mag = self.atlas.image:getFilter()
self.atlas.image:setFilter(SMODS.texture_filter_override, SMODS.texture_filter_override)
end
local ret = sprite_draw_self_ref(self, ...)
if self.atlas and SMODS.texture_filter_override then
self.atlas.image:setFilter(old_filter_min, old_filter_mag)
end
return ret
end

-- Util function to render one card to a .png file (usually saved to the mods folder's parent directory)
function SMODS.card_to_image(card, scale, filename)
if not type(card) == "table" then return end
local key = ((card.config or {}).center or {}).key or "card_to_image"
scale = scale or G.SETTINGS.GRAPHICS.texture_scaling
filename = (filename or key == "j_joker" and "jimbo" or key) .. ".png"

local canvas = love.graphics.newCanvas(71 * scale, 95 * scale, {type = '2d', readable = true})
canvas:setFilter('nearest', 'nearest')

local old_t = SMODS.shallow_copy(card.T)
local old_shadow = card.no_shadow
local old_rm = G.SETTINGS.reduced_motion
card.T.r = 0
local old_scale = card.T.scale
card.T.scale = scale / G.TILE_H * G.window_prev.orig_scale * G.window_prev.orig_scale/G.TILESCALE * 1.5 * 763/768 -- Don't ask me why I had to multiply by 1.5 * 763/768 here, I do not know,,, (this may have been brute-tinkered) (well the times 763/768 is to remove an extra pixel in height for scales == 2.0 -> 16.0 (at least))
card:hard_set_T(card.T.w/2*(card.T.scale-1), card.T.h/2*(card.T.scale-1))
card.no_shadow = true
G.SETTINGS.reduced_motion = true
SMODS.texture_filter_override = "nearest"
canvas:renderTo(card.draw, card)
SMODS.texture_filter_override = nil
G.SETTINGS.reduced_motion = old_rm
card.no_shadow = old_shadow
card.T.scale = old_scale
card:hard_set_T(old_t.x, old_t.y, old_t.w, old_t.h)

local image_data = canvas:newImageData()
image_data:encode("png", filename)
print("SMODS : Saved card image to "..love.filesystem.getSaveDirectory().."/"..filename)
end