@@ -184,6 +184,8 @@ pub fn (ctx &Context) draw_convex_poly(points []f32, c Color) {
184184// draw_rect_empty draws the outline of a rectangle.
185185// `x`,`y` is the top-left corner of the rectangle.
186186// `w` is the width, `h` is the height and `c` is the color of the outline.
187+ // Note: it is much more efficient to draw lots of empty rectangles one after the other,
188+ // without filled rectangles between them, than to draw a mix.
187189pub fn (ctx &Context) draw_rect_empty (x f32 , y f32 , w f32 , h f32 , c Color) {
188190 if c.a != 255 {
189191 sgl.load_pipeline (ctx.pipeline.alpha)
@@ -215,9 +217,48 @@ pub fn (ctx &Context) draw_rect_empty(x f32, y f32, w f32, h f32, c Color) {
215217 sgl.end ()
216218}
217219
220+ // draw_rect_empty_no_context draws the outline of a rectangle, but without saving/restoring the context.
221+ // It is intended to be used in loops, where you do manually: `sgl.begin_lines()` *before* the loop,
222+ // then draw many rectangles, then call manually `sgl.end()` *after* the loop.
223+ // `x`,`y` is the top-left corner of the rectangle.
224+ // `w` is the width, `h` is the height and `c` is the color of the outline.
225+ // Note: it is much more efficient to draw lots of empty rectangles one after the other,
226+ // without filled rectangles between them, than to draw a mix.
227+ pub fn (ctx &Context) draw_rect_empty_no_context (x f32 , y f32 , w f32 , h f32 , c Color) {
228+ // The small offsets here, are to make sure, that the start and end points will be
229+ // inside pixels, and not on their borders. That in turn, makes it much more likely
230+ // that different OpenGL implementations will render them identically, for example
231+ // Mesa, with `LIBGL_ALWAYS_SOFTWARE=1` renders the same as HD4000.
232+ mut toffset := f32 (0.1 )
233+ mut boffset := f32 (- 0.1 )
234+ tleft_x := toffset + x * ctx.scale
235+ tleft_y := toffset + y * ctx.scale
236+ bright_x := boffset + (x + w) * ctx.scale
237+ bright_y := boffset + (y + h) * ctx.scale
238+ // Note: the following line is deliberately commented, compare to draw_rect_empty/5;
239+ // sgl.begin_lines() // more predictable, compared to sgl.begin_line_strip, at the price of more vertexes send
240+ sgl.c4b (c.r, c.g, c.b, c.a)
241+ // top:
242+ sgl.v2f (tleft_x, tleft_y)
243+ sgl.v2f (bright_x, tleft_y)
244+ // left:
245+ sgl.v2f (tleft_x, tleft_y)
246+ sgl.v2f (tleft_x, bright_y)
247+ // right:
248+ sgl.v2f (bright_x, tleft_y)
249+ sgl.v2f (bright_x, bright_y)
250+ // bottom:
251+ sgl.v2f (tleft_x, bright_y)
252+ sgl.v2f (bright_x, bright_y)
253+ // Note: the following line is deliberately commented, compare to draw_rect_empty/5;
254+ // sgl.end()
255+ }
256+
218257// draw_rect_filled draws a filled rectangle.
219258// `x`,`y` is the top-left corner of the rectangle.
220259// `w` is the width, `h` is the height and `c` is the color of the fill.
260+ // Note: it is much more efficient to draw lots of filled rectangles one after the other,
261+ // without empty rectangles between them, than to draw a mix.
221262pub fn (ctx &Context) draw_rect_filled (x f32 , y f32 , w f32 , h f32 , c Color) {
222263 $if macos {
223264 if ctx.native_rendering {
@@ -239,6 +280,25 @@ pub fn (ctx &Context) draw_rect_filled(x f32, y f32, w f32, h f32, c Color) {
239280 sgl.end ()
240281}
241282
283+ // draw_rect_filled_no_context draws a filled rectangle, but without saving/restoring the context.
284+ // It is intended to be used in loops, where you do manually: `sgl.begin_quads()` *before* the loop,
285+ // then draw many rectangles, then call manually `sgl.end()` *after* the loop.
286+ // `x`,`y` is the top-left corner of the rectangle.
287+ // `w` is the width, `h` is the height and `c` is the color of the fill.
288+ // Note: it is much more efficient to draw lots of filled rectangles one after the other,
289+ // without empty rectangles between them, than to draw a mix.
290+ pub fn (ctx &Context) draw_rect_filled_no_context (x f32 , y f32 , w f32 , h f32 , c Color) {
291+ // Note: the following line is deliberately commented, compare to draw_rect_empty/5;
292+ // sgl.begin_quads()
293+ sgl.c4b (c.r, c.g, c.b, c.a)
294+ sgl.v2f (x * ctx.scale, y * ctx.scale)
295+ sgl.v2f ((x + w) * ctx.scale, y * ctx.scale)
296+ sgl.v2f ((x + w) * ctx.scale, (y + h) * ctx.scale)
297+ sgl.v2f (x * ctx.scale, (y + h) * ctx.scale)
298+ // Note: the following line is deliberately commented, compare to draw_rect_filled/5;
299+ // sgl.end()
300+ }
301+
242302pub enum PaintStyle {
243303 fill
244304 stroke
0 commit comments