@@ -226,6 +226,7 @@ ensure_background_images_generation(bool release_texture) {
226226 if (global_state .background_images .generation == OPT (background_images ).generation ) return ;
227227 free_global_background_images (release_texture );
228228 global_state .background_images .generation = OPT (background_images ).generation ;
229+ global_state .background_images .entries_attempted = 0 ;
229230 if (OPT (background_images ).count ) {
230231 global_state .background_images .images = calloc (
231232 OPT (background_images ).count , sizeof (global_state .background_images .images [0 ]));
@@ -238,12 +239,11 @@ static unsigned bg_image_id_counter = 0;
238239static BackgroundImage *
239240global_background_image (size_t idx ) {
240241 ensure_background_images_generation (true);
241- while (global_state .background_images .count <= idx && OPT (background_images ).count ) {
242- RAII_ALLOC (char , path , OPT (background_images ).paths [0 ]); // transfer ownership
243- remove_i_from_array (OPT (background_images .paths ), 0 , OPT (background_images ).count );
242+ while (global_state .background_images .count <= idx && global_state .background_images .entries_attempted < OPT (background_images ).count ) {
243+ size_t j = global_state .background_images .entries_attempted ++ ;
244244 BackgroundImage * img = calloc (1 , sizeof (BackgroundImage ));
245245 if (!img ) fatal ("Out of memory" );
246- if (image_path_to_bitmap (path , & img -> bitmap , & img -> width , & img -> height , & img -> mmap_size )) {
246+ if (image_path_to_bitmap (OPT ( background_images ). paths [ j ] , & img -> bitmap , & img -> width , & img -> height , & img -> mmap_size )) {
247247 if (send_bgimage_to_gpu (OPT (background_image_layout ), img )) {
248248 img -> refcnt ++ ;
249249 img -> id = ++ bg_image_id_counter ;
@@ -273,7 +273,7 @@ increment_bg_image_idx(size_t idx, int delta) {
273273 }
274274 if ((unsigned )abs (delta ) <= idx ) return idx + delta ;
275275 // wrap to last image, which means we need to load all
276- global_background_image ( global_state . background_images .count + OPT (background_images ).count + 1 );
276+ if ( OPT ( background_images ) .count > 0 ) global_background_image ( OPT (background_images ).count - 1 );
277277 return global_state .background_images .count ? global_state .background_images .count - 1 : 0 ;
278278}
279279
@@ -1417,6 +1417,12 @@ pyset_background_image(PyObject *self UNUSED, PyObject *args, PyObject *kw) {
14171417 }
14181418 global_state .background_images .images [0 ] = bgimage ;
14191419 bgimage -> refcnt ++ ;
1420+ if (OPT (background_images ).count > 0 ) {
1421+ free (OPT (background_images ).paths [0 ]);
1422+ char * new_path = strdup (path );
1423+ if (!new_path ) fatal ("Out of memory" );
1424+ OPT (background_images ).paths [0 ] = new_path ;
1425+ }
14201426 } else free_global_background_images (true);
14211427 OPT (background_image_layout ) = layout ;
14221428 if (pylinear && pylinear != Py_None ) convert_from_python_background_image_linear (pylinear , & global_state .opts );
0 commit comments