Skip to content

Commit ff351d2

Browse files
committed
feat: use native math funcs, non-ffi fallback
1 parent 5516075 commit ff351d2

File tree

2 files changed

+25
-41
lines changed

2 files changed

+25
-41
lines changed

characters.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ end
520520
---@param imagedata boolean?
521521
local function _splitimagenative(img, color, exclude, imagedata)
522522
local output = img:clone()
523+
523524
output:mapPixel(function(_, _, r, g, b, a)
524525
local place = false
525526
if exclude then
@@ -537,13 +538,14 @@ local function _splitimagenative(img, color, exclude, imagedata)
537538
end
538539
if place then
539540
if exclude then
540-
return r, g, b, a
541+
return nil -- leaves value (r,g,b,a) unchanged
541542
else
542543
return 255, 255, 255, a
543544
end
544545
end
545546
return 0, 0, 0, 0
546547
end)
548+
547549
if imagedata then
548550
return output
549551
else
@@ -558,7 +560,7 @@ end
558560
function splitimage(img, color, exclude, imagedata) --split singe image into colorable images
559561
if false then --useShader then
560562
return _splitimageshaders(img, color, exclude, imagedata)
561-
elseif true then
563+
elseif FFIAVAILABLE then
562564
return _splitimageffi(img, color, exclude, imagedata)
563565
else
564566
return _splitimagenative(img, color, exclude, imagedata)

main.lua

+21-39
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,13 @@
5555
if love._version_major ~= 11 then error("You have an outdated version of Love2d! Get 11.5 and retry.") end
5656

5757
----- COLOR MIGRATION helpers -----
58-
function clamp(x, lower, upper)
59-
return math.min(math.max(x, lower), upper)
60-
end
61-
62-
local function convertFromByte(x)
63-
return clamp(math.floor(x + 0.5) / 255, 0, 1)
64-
end
65-
66-
local colorCache = {}
67-
for i = 0, 255 do colorCache[i] = convertFromByte(i) end
68-
69-
local function convertFromCachedByte(x)
70-
x = clamp(x, 0, 255)
71-
local cached = colorCache[x]
72-
return (cached ~= nil and cached) or convertFromByte(x) -- fallback for decimals
73-
end
74-
75-
local function convertFromCachedTable(table)
76-
for i, v in ipairs(table) do
77-
table[i] = v ~= nil and convertFromCachedByte(v) or nil
78-
end
79-
return table
80-
end
58+
FFIAVAILABLE = pcall(function () require("ffi") end)
8159

8260
local function convertText(text)
8361
if type(text) == "table" then
8462
for i, v in ipairs(text) do
8563
if type(v) == "table" then
86-
text[i] = unpack(convertFromCachedTable(v))
64+
text[i] = {love.math.colorFromBytes(unpack(v))}
8765
end
8866
end
8967
end
@@ -93,31 +71,31 @@ end
9371
----- COLOR MIGRATION for real -----
9472

9573
local defaultSetColor = love.graphics.setColor
96-
function love.graphics.setColor(r, g, b, a, ...)
97-
if type(r) == "table" then r, g, b, a = r[1], r[2], r[3], r[4] end
98-
return defaultSetColor(convertFromCachedTable({r, g, b, a}), ...)
74+
function love.graphics.setColor(r, g, b, a)
75+
if type(r) == "table" then r, g, b, a = unpack(r) end
76+
return defaultSetColor(love.math.colorFromBytes(r, g, b, a))
9977
end
10078

10179
local defaultGetColor = love.graphics.getColor
102-
function love.graphics.getColor(...)
103-
return love.math.colorToBytes(defaultGetColor(...))
80+
function love.graphics.getColor()
81+
return love.math.colorToBytes(defaultGetColor())
10482
end
10583

10684
local defaultSetBackgroundColor = love.graphics.setBackgroundColor
107-
function love.graphics.setBackgroundColor(r, g, b, a, ...)
108-
if type(r) == "table" then r, g, b, a = r[1], r[2], r[3], r[4] end
109-
return defaultSetBackgroundColor(convertFromCachedTable({r, g, b, a}), ...)
85+
function love.graphics.setBackgroundColor(r, g, b, a)
86+
if type(r) == "table" then r, g, b, a = unpack(r) end
87+
return defaultSetBackgroundColor(love.math.colorFromBytes(r, g, b, a))
11088
end
11189

11290
local defaultGetBackgroundColor = love.graphics.getBackgroundColor
113-
function love.graphics.getBackgroundColor(...)
114-
return love.math.colorToBytes(defaultGetBackgroundColor(...))
91+
function love.graphics.getBackgroundColor()
92+
return love.math.colorToBytes(defaultGetBackgroundColor())
11593
end
11694

11795
local defaultClear = love.graphics.clear
11896
function love.graphics.clear(r, g, b, a, ...)
11997
if r ~= nil and g ~= nil and b ~= nil then
120-
r, g, b, a = unpack(convertFromCachedTable({r, g, b, a}))
98+
r, g, b, a = love.math.colorFromBytes(r, g, b, a)
12199
end
122100
return defaultClear(r, g, b, a, ...)
123101
end
@@ -163,19 +141,19 @@ local SpriteBatch = debug.getregistry().SpriteBatch
163141

164142
local defaultSpriteBatchSetColor = SpriteBatch.setColor
165143
function SpriteBatch:setColor(...)
166-
return defaultSpriteBatchSetColor(self, unpack(convertFromCachedTable({...})))
144+
return defaultSpriteBatchSetColor(self, love.math.colorFromBytes(...))
167145
end
168146

169147
local defaultSpriteBatchGetColor = SpriteBatch.getColor
170148
function SpriteBatch:getColor(...)
171149
return love.math.colorToBytes(defaultSpriteBatchGetColor(self, ...))
172150
end
173151

174-
local ImageData = debug.getregistry().ImageData
152+
local ImageData = debug.getregistry().ImageData -- TODO: use FFI by default? not sure if fetching the pointer constantly will actually be efficient
175153

176154
local defaultImageDataSetPixel = ImageData.setPixel
177155
function ImageData:setPixel(x, y, ...)
178-
return defaultImageDataSetPixel(self, x, y, unpack(convertFromCachedTable({...})))
156+
return defaultImageDataSetPixel(self, x, y, love.math.colorFromBytes(...))
179157
end
180158

181159
local defaultImageDataGetPixel = ImageData.getPixel
@@ -185,7 +163,11 @@ end
185163

186164
local defaultImageDataMapPixel = ImageData.mapPixel
187165
function ImageData:mapPixel(pixelFunction, ...)
188-
return defaultImageDataMapPixel(self, function(x, y, r, g, b, a) return unpack(convertFromCachedTable({pixelFunction(x, y, love.math.colorToBytes(r, g, b, a))})) end, ...)
166+
return defaultImageDataMapPixel(self, function(x, y, r, g, b, a)
167+
local nr, ng, nb, na = pixelFunction(x, y, love.math.colorToBytes(r, g, b, a))
168+
if nr == nil then return r, g, b, a end
169+
return love.math.colorFromBytes(nr, ng, nb, na)
170+
end, ...)
189171
end
190172

191173
-- TODO: ParticleSystem, linear/gamma functions, points, sendColor, newMesh, [gs]etVertex

0 commit comments

Comments
 (0)