1515
1616int sdl_display_init_bottom (uint16_t height , uint16_t width , uint16_t zoom_pct ,
1717 bool use_accelerator , void * * window , void * * renderer , void * * mutex ,
18- void * * texture , void * * read_texture )
18+ void * * texture , void * * read_texture , void * * background_texture ,
19+ uint32_t transparency_grid_color1 , uint32_t transparency_grid_color2 ,
20+ uint16_t transparency_grid_cell_size )
1921{
2022 * window = SDL_CreateWindow ("Zephyr Display" , SDL_WINDOWPOS_UNDEFINED ,
2123 SDL_WINDOWPOS_UNDEFINED , width * zoom_pct / 100 ,
@@ -51,6 +53,7 @@ int sdl_display_init_bottom(uint16_t height, uint16_t width, uint16_t zoom_pct,
5153 nsi_print_warning ("Failed to create SDL texture: %s" , SDL_GetError ());
5254 return -1 ;
5355 }
56+ SDL_SetTextureBlendMode (* texture , SDL_BLENDMODE_BLEND );
5457
5558 * read_texture = SDL_CreateTexture (* renderer , SDL_PIXELFORMAT_ARGB8888 ,
5659 SDL_TEXTUREACCESS_TARGET , width , height );
@@ -59,16 +62,50 @@ int sdl_display_init_bottom(uint16_t height, uint16_t width, uint16_t zoom_pct,
5962 return -1 ;
6063 }
6164
65+ * background_texture = SDL_CreateTexture (* renderer , SDL_PIXELFORMAT_ARGB8888 ,
66+ SDL_TEXTUREACCESS_STREAMING , width , height );
67+ if (* background_texture == NULL ) {
68+ nsi_print_warning ("Failed to create SDL texture: %s" , SDL_GetError ());
69+ return -1 ;
70+ }
71+
72+ void * background_data ;
73+ int background_pitch ;
74+ int err ;
75+
76+ err = SDL_LockTexture (* background_texture , NULL , & background_data , & background_pitch );
77+ if (err != 0 ) {
78+ nsi_print_warning ("Failed to lock background texture: %d" , err );
79+ return -1 ;
80+ }
81+ for (int y = 0 ; y < height ; y ++ ) {
82+ uint32_t * row = (uint32_t * )((uint8_t * )background_data + background_pitch * y );
83+
84+ for (int x = 0 ; x < width ; x ++ ) {
85+ bool x_cell_even = ((x / transparency_grid_cell_size ) % 2 ) == 0 ;
86+ bool y_cell_even = ((y / transparency_grid_cell_size ) % 2 ) == 0 ;
87+
88+ if (x_cell_even == y_cell_even ) {
89+ row [x ] = transparency_grid_color1 | 0xff000000 ;
90+ } else {
91+ row [x ] = transparency_grid_color2 | 0xff000000 ;
92+ }
93+ }
94+ }
95+ SDL_UnlockTexture (* background_texture );
96+
6297 SDL_SetRenderDrawColor (* renderer , 0 , 0 , 0 , 0xFF );
6398 SDL_RenderClear (* renderer );
99+ SDL_RenderCopy (* renderer , * background_texture , NULL , NULL );
64100 SDL_RenderPresent (* renderer );
65101
66102 return 0 ;
67103}
68104
69105void sdl_display_write_bottom (const uint16_t height , const uint16_t width , const uint16_t x ,
70106 const uint16_t y , void * renderer , void * mutex , void * texture ,
71- uint8_t * buf , bool display_on , bool frame_incomplete )
107+ void * background_texture , uint8_t * buf , bool display_on ,
108+ bool frame_incomplete )
72109{
73110 SDL_Rect rect ;
74111 int err ;
@@ -88,6 +125,7 @@ void sdl_display_write_bottom(const uint16_t height, const uint16_t width, const
88125
89126 if (display_on && !frame_incomplete ) {
90127 SDL_RenderClear (renderer );
128+ SDL_RenderCopy (renderer , background_texture , NULL , NULL );
91129 SDL_RenderCopy (renderer , texture , NULL , NULL );
92130 SDL_RenderPresent (renderer );
93131 }
@@ -127,9 +165,10 @@ int sdl_display_read_bottom(const uint16_t height, const uint16_t width,
127165 return err ;
128166}
129167
130- void sdl_display_blanking_off_bottom (void * renderer , void * texture )
168+ void sdl_display_blanking_off_bottom (void * renderer , void * texture , void * background_texture )
131169{
132170 SDL_RenderClear (renderer );
171+ SDL_RenderCopy (renderer , background_texture , NULL , NULL );
133172 SDL_RenderCopy (renderer , texture , NULL , NULL );
134173 SDL_RenderPresent (renderer );
135174}
@@ -141,8 +180,13 @@ void sdl_display_blanking_on_bottom(void *renderer)
141180}
142181
143182void sdl_display_cleanup_bottom (void * * window , void * * renderer , void * * mutex , void * * texture ,
144- void * * read_texture )
183+ void * * read_texture , void * * background_texture )
145184{
185+ if (* background_texture != NULL ) {
186+ SDL_DestroyTexture (* background_texture );
187+ * background_texture = NULL ;
188+ }
189+
146190 if (* read_texture != NULL ) {
147191 SDL_DestroyTexture (* read_texture );
148192 * read_texture = NULL ;
0 commit comments