From 20b9f0d4f1914dd2cf144d466ed88fd495578710 Mon Sep 17 00:00:00 2001 From: ionut Date: Fri, 1 May 2026 15:28:12 +0300 Subject: [PATCH] bugfixes and improvements credit to blendi for fixing the depth mapper --- TODO.md | 1 - src/machine.lua | 2 +- src/main.c | 1 + src/neonucleus.c | 24 +++++++++++++++++------- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/TODO.md b/TODO.md index b96331d..c5e5102 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,5 @@ # For MVP functionality -- fix coloring issue in UlOS 2 beta 5 (and in OpenOS `dmesg`, it appears color-mapper is wrong) - make `computer` component use callbacks - make a lot of stuff which likely will need to be indirect actually be indirect (modem comms, computer funcs) - make beeps, power, etc. be callbacks on one shared local state (computer environment) diff --git a/src/machine.lua b/src/machine.lua index 791828e..381f4ab 100644 --- a/src/machine.lua +++ b/src/machine.lua @@ -72,7 +72,7 @@ local syncedMethodStats local function realInvoke(address, method, ...) local t = {pcall(cinvoke, address, method, ...)} - if not _SYNCED then + if not _SYNCED and not os.getenv("NN_FAST") then if computer.energy() <= 0 then sysyield() end -- out of power if computer.isOverused() then sysyield() end -- overused if computer.isIdle() then sysyield() end -- machine idle diff --git a/src/main.c b/src/main.c index 3b0b8fd..3004b05 100644 --- a/src/main.c +++ b/src/main.c @@ -496,6 +496,7 @@ int main(int argc, char **argv) { nn_mountComponent(c, testFlash, 5, false); int ltx = 0, lty = 0; double scrollBuf = 0; + SetTargetFPS(60); while(true) { if(WindowShouldClose()) break; diff --git a/src/neonucleus.c b/src/neonucleus.c index 76d01cd..3420e18 100644 --- a/src/neonucleus.c +++ b/src/neonucleus.c @@ -2909,9 +2909,9 @@ void nn_initPalettes() { } static void nn_splitColor(int color, double *r, double *g, double *b) { - int _r = (color >> 16) & 0xFF; - int _g = (color >> 8) & 0xFF; - int _b = (color >> 0) & 0xFF; + unsigned int _r = (color >> 16) & 0xFF; + unsigned int _g = (color >> 8) & 0xFF; + unsigned int _b = (color >> 0) & 0xFF; *r = (double)_r / 255; *g = (double)_g / 255; @@ -2925,10 +2925,20 @@ double nn_colorLuminance(int color) { return r * 0.2126 + g * 0.7152 + b * 0.0722; } +// Credit to Blendi for writing this, the old algorithm based off luminance gave bad results static double nn_colorDistance(int a, int b) { - double n = nn_colorLuminance(a) - nn_colorLuminance(b); - if(n < 0) n = -n; - return n; + // double n = nn_colorLuminance(a) - nn_colorLuminance(b); + // if(n < 0) n = -n; + + double ar,ag,ab; + double br,bg,bb; + + nn_splitColor(a, &ar, &ag, &ab); + nn_splitColor(b, &br, &bg, &bb); + + double dr = ar-br, dg = ag-bg, db = ab-bb; + + return ((dr < 0) ? -dr : dr) + ((dg < 0) ? -dg : dg) + ((db < 0) ? -db : db); } int nn_mapColor(int color, int *palette, size_t len) { @@ -2938,7 +2948,7 @@ int nn_mapColor(int color, int *palette, size_t len) { for(size_t i = 0; i < len; i++) { int entry = palette[i]; double dist = nn_colorDistance(color, entry); - if(dist < bestDist) { + if(dist <= bestDist) { bestDist = dist; bestColor = entry; }