aboutsummaryrefslogtreecommitdiff
path: root/src/player.zig
diff options
context:
space:
mode:
authortslil clingman <>2021-09-22 23:05:38 -0400
committertslil clingman <>2021-09-22 23:05:38 -0400
commitd932cb0a1fd0235c8f298dd7f3acbf2864338903 (patch)
tree2c560fae565955fffd11a6e8fd28c6a1077bad0f /src/player.zig
parent9d25e1d69dede7d79ca9115a4ce4e65af49b8ddc (diff)
Switch to more accurate version
Pre-emptive micro-optimisation is bad. There aren't that many sprites pixels usually anyway.
Diffstat (limited to 'src/player.zig')
-rw-r--r--src/player.zig32
1 files changed, 18 insertions, 14 deletions
diff --git a/src/player.zig b/src/player.zig
index e69b3ce..74a1f10 100644
--- a/src/player.zig
+++ b/src/player.zig
@@ -42,26 +42,30 @@ fn fasterColourBlend(onto: Colour, from: Colour) Colour {
const na: u16 = af + @divTrunc(of * (255 - af), 255);
if (na == 0) return Colour.Black;
- const rf: u16 = from.r;
- const ro: u16 = onto.r;
- const gf: u16 = from.g;
- const go: u16 = onto.g;
- const bf: u16 = from.b;
- const bo: u16 = onto.b;
+ const rf: i32 = from.r;
+ const ro: i32 = onto.r;
+ const gf: i32 = from.g;
+ const go: i32 = onto.g;
+ const bf: i32 = from.b;
+ const bo: i32 = onto.b;
- // These computations are expensive, but more accurate
+ // The most accurate i've found is
+ const nr = @divTrunc(ro * na + (rf - ro) * af, na);
+ const ng = @divTrunc(go * na + (gf - go) * af, na);
+ const nb = @divTrunc(bo * na + (bf - bo) * af, na);
+
+ // Note: there are other versions which don't require using intermediate
+ // i32 division, and instead work on plain u16.
+
+ // These computations are more expensive and less accurate
// const nr = @divTrunc(af * rf, na) + ro - @divTrunc(af * ro, na);
// const ng = @divTrunc(af * gf, na) + go - @divTrunc(af * go, na);
// const nb = @divTrunc(af * bf, na) + bo - @divTrunc(af * bo, na);
// These computations are incorrect, but fast
- const nr = (af * rf + (255 - af) * ro) / 255;
- const ng = (af * gf + (255 - af) * go) / 255;
- const nb = (af * bf + (255 - af) * bo) / 255;
-
- // The most accurate i've found is @divTrunc(ro * na + (rf - ro) * af, na);
- // but this requires using signed integers, for which presumably division is
- // slower still
+ // const nr = (af * rf + (255 - af) * ro) / 255;
+ // const ng = (af * gf + (255 - af) * go) / 255;
+ // const nb = (af * bf + (255 - af) * bo) / 255;
return Colour{
.a = @intCast(u8, na),