Skip to content

Commit 0ad74de

Browse files
authored
gg: make draw_rect_empty/5 draw more exact borders, independent of the device, and fitting the draw_rect_filled/5 shapes (#24024)
1 parent a199a6e commit 0ad74de

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

vlib/gg/draw.c.v

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,29 @@ pub fn (ctx &Context) draw_rect_empty(x f32, y f32, w f32, h f32, c gx.Color) {
190190
sgl.load_pipeline(ctx.pipeline.alpha)
191191
}
192192
sgl.c4b(c.r, c.g, c.b, c.a)
193-
194-
sgl.begin_line_strip()
195-
sgl.v2f(x * ctx.scale, y * ctx.scale)
196-
sgl.v2f((x + w) * ctx.scale, y * ctx.scale)
197-
sgl.v2f((x + w) * ctx.scale, (y + h) * ctx.scale)
198-
sgl.v2f(x * ctx.scale, (y + h) * ctx.scale)
199-
sgl.v2f(x * ctx.scale, (y - 1) * ctx.scale)
193+
// The small offsets here, are to make sure, that the start and end points will be
194+
// inside pixels, and not on their borders. That in turn, makes it much more likely
195+
// that different OpenGL implementations will render them identically, for example
196+
// Mesa, with `LIBGL_ALWAYS_SOFTWARE=1` renders the same as HD4000.
197+
mut toffset := f32(0.1)
198+
mut boffset := f32(-0.1)
199+
tleft_x := toffset + x * ctx.scale
200+
tleft_y := toffset + y * ctx.scale
201+
bright_x := boffset + (x + w) * ctx.scale
202+
bright_y := boffset + (y + h) * ctx.scale
203+
sgl.begin_lines() // more predictable, compared to sgl.begin_line_strip, at the price of more vertexes send
204+
// top:
205+
sgl.v2f(tleft_x, tleft_y)
206+
sgl.v2f(bright_x, tleft_y)
207+
// left:
208+
sgl.v2f(tleft_x, tleft_y)
209+
sgl.v2f(tleft_x, bright_y)
210+
// right:
211+
sgl.v2f(bright_x, tleft_y)
212+
sgl.v2f(bright_x, bright_y)
213+
// bottom:
214+
sgl.v2f(tleft_x, bright_y)
215+
sgl.v2f(bright_x, bright_y)
200216
sgl.end()
201217
}
202218

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module main
2+
3+
import gg
4+
import gx
5+
6+
gg.start(
7+
bg_color: gx.white
8+
window_title: 'Rectangles'
9+
width: 250
10+
height: 100
11+
frame_fn: fn (ctx &gg.Context) {
12+
ctx.begin()
13+
ctx.draw_rect_filled(10, 10, 10, 10, gx.blue)
14+
ctx.draw_rect_empty(30, 10, 10, 10, gx.red)
15+
ctx.draw_rect_filled(50, 10, 30, 10, gx.green)
16+
ctx.draw_rect_empty(100, 10, 30, 10, gx.black)
17+
18+
ctx.draw_rect_empty(10, 50, 10, 10, gx.blue)
19+
ctx.draw_rect_filled(30, 50, 10, 10, gx.red)
20+
ctx.draw_rect_empty(50, 50, 30, 10, gx.green)
21+
ctx.draw_rect_filled(100, 50, 30, 10, gx.black)
22+
ctx.end()
23+
}
24+
)

0 commit comments

Comments
 (0)