1616
1717typedef struct {
1818 harp_window_t * win ;
19+ uint32_t * backbuf ; /* off-screen render target */
1920 font_face_t * font ;
2021 int font_size ;
2122 struct nk_user_font nk_font ;
@@ -63,7 +64,7 @@ static void nk_harp_fill_rect(nk_harp_t *nh, int x, int y, int w, int h, uint32_
6364 if (x1 >= x2 || y1 >= y2 ) return ;
6465 uint8_t a = (col >> 24 ) & 0xFF ;
6566 for (int row = y1 ; row < y2 ; row ++ ) {
66- uint32_t * line = nh -> win -> buf + row * nh -> win -> w + x1 ;
67+ uint32_t * line = nh -> backbuf + row * nh -> win -> w + x1 ;
6768 if (a == 255 ) {
6869 for (int i = 0 ; i < x2 - x1 ; i ++ ) line [i ] = col ;
6970 } else {
@@ -160,7 +161,7 @@ static void nk_harp_draw_text(nk_harp_t *nh,
160161 int copy = len < 511 ? len : 511 ;
161162 memcpy (tmp , text , copy );
162163 tmp [copy ] = '\0' ;
163- font_draw (nh -> font , nh -> win -> buf , nh -> win -> w , nh -> win -> h ,
164+ font_draw (nh -> font , nh -> backbuf , nh -> win -> w , nh -> win -> h ,
164165 x , y + sz , sz ,
165166 fg | 0xFF000000 , bg ,
166167 tmp );
@@ -179,6 +180,9 @@ static inline uint32_t nk_color_to_u32(struct nk_color c)
179180
180181static void nk_harp_render (nk_harp_t * nh )
181182{
183+ /* clear backbuf once — full frame drawn before anything hits win->buf */
184+ memset (nh -> backbuf , 0 , (size_t )nh -> win -> w * nh -> win -> h * sizeof (uint32_t ));
185+
182186 const struct nk_command * cmd ;
183187 nk_foreach (cmd , & nh -> ctx ) {
184188 switch (cmd -> type ) {
@@ -256,6 +260,9 @@ static void nk_harp_render(nk_harp_t *nh)
256260 }
257261 }
258262 nk_clear (& nh -> ctx );
263+
264+ /* blit the completed backbuffer to the window buffer in one shot */
265+ memcpy (nh -> win -> buf , nh -> backbuf , (size_t )nh -> win -> w * nh -> win -> h * sizeof (uint32_t ));
259266}
260267
261268static nk_harp_t * nk_harp_init (const char * title , int x , int y , int w , int h ,
@@ -267,6 +274,9 @@ static nk_harp_t *nk_harp_init(const char *title, int x, int y, int w, int h,
267274 nh -> win = harp_open (title , x , y , w , h );
268275 if (!nh -> win ) { free (nh ); return NULL ; }
269276
277+ nh -> backbuf = (uint32_t * )calloc ((size_t )w * h , sizeof (uint32_t ));
278+ if (!nh -> backbuf ) { harp_close (nh -> win ); free (nh ); return NULL ; }
279+
270280 nh -> font = font_load (font_path );
271281 if (!nh -> font ) { harp_close (nh -> win ); free (nh ); return NULL ; }
272282
@@ -370,6 +380,7 @@ static void nk_harp_free(nk_harp_t *nh)
370380 nk_free (& nh -> ctx );
371381 font_free (nh -> font );
372382 harp_close (nh -> win );
383+ free (nh -> backbuf );
373384 free (nh );
374385 g_nh = NULL ;
375- }
386+ }
0 commit comments