From cb08c5cbb7e3679fa90d40d784d223fa197aeb5a Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Sun, 10 Oct 2021 14:52:55 +0200 Subject: [PATCH] Fix dirty pixels problem on window's right side The last column of pixel on the window's right side isn't correctly drawn and pixels appear dirty and more noticeably when the a NagView message was previously shown, a stripe of red pixels remains on the right. We use now a more souding roundig scheme. Now the rectangles to clip or to draw are passed around as Lua numbers without any rounding. In turns, when the rect coordinates are passed to the renderer we ensure the border of the rect are correctly snapped to the pixel's grid. It works by computing the coordinates of the edges, round them to integers and then compute the rect's width based on the rounded coordinates values. --- data/core/rootview.lua | 2 +- data/core/view.lua | 2 +- src/api/renderer.c | 27 +++++++++++++++++---------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/data/core/rootview.lua b/data/core/rootview.lua index 0d219474..fa200aec 100644 --- a/data/core/rootview.lua +++ b/data/core/rootview.lua @@ -602,7 +602,7 @@ function Node:draw() self:draw_tabs() end local pos, size = self.active_view.position, self.active_view.size - core.push_clip_rect(pos.x, pos.y, size.x + pos.x % 1, size.y + pos.y % 1) + core.push_clip_rect(pos.x, pos.y, pos.x + size.x, pos.y + size.y) self.active_view:draw() core.pop_clip_rect() else diff --git a/data/core/view.lua b/data/core/view.lua index d1374ee4..5b4b3228 100644 --- a/data/core/view.lua +++ b/data/core/view.lua @@ -140,7 +140,7 @@ end function View:draw_background(color) local x, y = self.position.x, self.position.y local w, h = self.size.x, self.size.y - renderer.draw_rect(x, y, w + x % 1, h + y % 1, color) + renderer.draw_rect(x, y, w, h, color) end diff --git a/src/api/renderer.c b/src/api/renderer.c index 8dc13ada..ec3bc3ba 100644 --- a/src/api/renderer.c +++ b/src/api/renderer.c @@ -49,23 +49,30 @@ static int f_end_frame(lua_State *L) { } +static RenRect rect_to_grid(lua_Number x, lua_Number y, lua_Number w, lua_Number h) { + int x1 = (int) (x + 0.5), y1 = (int) (y + 0.5); + int x2 = (int) (x + w + 0.5), y2 = (int) (y + h + 0.5); + return (RenRect) {x1, y1, x2 - x1, y2 - y1}; +} + + static int f_set_clip_rect(lua_State *L) { - RenRect rect; - rect.x = luaL_checknumber(L, 1); - rect.y = luaL_checknumber(L, 2); - rect.width = luaL_checknumber(L, 3); - rect.height = luaL_checknumber(L, 4); + lua_Number x = luaL_checknumber(L, 1); + lua_Number y = luaL_checknumber(L, 2); + lua_Number w = luaL_checknumber(L, 3); + lua_Number h = luaL_checknumber(L, 4); + RenRect rect = rect_to_grid(x, y, w, h); rencache_set_clip_rect(rect); return 0; } static int f_draw_rect(lua_State *L) { - RenRect rect; - rect.x = luaL_checknumber(L, 1); - rect.y = luaL_checknumber(L, 2); - rect.width = luaL_checknumber(L, 3); - rect.height = luaL_checknumber(L, 4); + lua_Number x = luaL_checknumber(L, 1); + lua_Number y = luaL_checknumber(L, 2); + lua_Number w = luaL_checknumber(L, 3); + lua_Number h = luaL_checknumber(L, 4); + RenRect rect = rect_to_grid(x, y, w, h); RenColor color = checkcolor(L, 5, 255); rencache_draw_rect(rect, color); return 0;