Browse code

Change ordering of background image loading to last added

Dario Rodriguez authored on 22/09/2025 17:20:32
Showing 3 changed files
... ...
@@ -24,6 +24,8 @@
24 24
 #include "rayui.h"
25 25
 #include "bg.h"
26 26
 
27
+#define UNLOADTIMEOUTSECONDS 10
28
+
27 29
 static int mypipe(int fds[2]);
28 30
 static int mypiperead(int fd, char *buf, int count);
29 31
 static int mypipewrite(int fd, char *buf, int count);
... ...
@@ -153,6 +155,7 @@ bg_add(bg_t *bg, char *path)
153 155
                 if(bgload->lended_to_thread && strcmp(path,bgload->path)==0) {
154 156
                         bgload->is_todo=1;
155 157
                         bgload->has_mark=1;
158
+                        gettimeofday(&(bgload->tv_lastadded),NULL); // Breaks premise of "don't touch until thread finished", but this is rather innocuous
156 159
                         dummy=0;
157 160
                         mypipewrite(bg->pipe[WR],&dummy,1);
158 161
                         return(0); /* already on list */
... ...
@@ -165,6 +168,7 @@ bg_add(bg_t *bg, char *path)
165 168
                         bgload->path[sizeof(bgload->path)-1]='\0';
166 169
                         bgload->is_todo=1;
167 170
                         bgload->has_mark=1;
171
+                        gettimeofday(&(bgload->tv_lastadded),NULL); // Breaks premise of "don't touch until thread finished", but this is rather innocuous
168 172
                         dummy=0;
169 173
                         bgload->lended_to_thread=1;
170 174
                         mypipewrite(bg->pipe[WR],&dummy,1);
... ...
@@ -179,11 +183,13 @@ bg_freeunmarked(bg_t *bg)
179 183
 {
180 184
         int i;
181 185
         bgload_t *bgload;
186
+        time_t now;
182 187
         if(bg==NULL)
183 188
                 return(-1);
189
+        now=time(NULL);
184 190
         for(i=0,bgload=bg->bgload;i<bg->sizebgload;i++,bgload++) {
185 191
                 if(bgload->lended_to_thread && bgload->thread_finished && bgload->has_mark==0) {
186
-                        if(bgload->has_data) {
192
+                        if(bgload->has_data && (bgload->lastused<(now-UNLOADTIMEOUTSECONDS) || bgload->lastused>(now+UNLOADTIMEOUTSECONDS))) {
187 193
 #if 1
188 194
 fprintf(stderr,"bg: Unloading: \"%s\"\n",bgload->path);
189 195
 #endif
... ...
@@ -196,6 +202,8 @@ fprintf(stderr,"bg: Cancelling: \"%s\"\n",bgload->path);
196 202
 }
197 203
 #endif
198 204
                         memset(bgload,0,sizeof(bgload_t));
205
+                } else if(bgload->lended_to_thread && bgload->thread_finished && bgload->has_mark!=0 && bgload->has_data) {
206
+                        bgload->lastused=now;
199 207
                 }
200 208
         }
201 209
         return(0);
... ...
@@ -208,7 +216,7 @@ bg_thread(void *parambg)
208 216
         char dummy;
209 217
         bg=(bg_t *)parambg;
210 218
         int i;
211
-        bgload_t *bgload;
219
+        bgload_t *bgload,*candidate;
212 220
         while(1) {
213 221
                 mypiperead(bg->pipe[RD],&dummy,1);
214 222
 #if 1
... ...
@@ -216,7 +224,7 @@ fprintf(stderr,"Thread received byte: %i\n",(int)((unsigned char)dummy));
216 224
 #endif
217 225
                 if(dummy!=0)
218 226
                         break; /* was told to exit */
219
-                for(i=0,bgload=bg->bgload;i<bg->sizebgload;i++,bgload++) {
227
+                for(candidate=NULL,i=0,bgload=bg->bgload;i<bg->sizebgload;i++,bgload++) {
220 228
                         if(bgload->lended_to_thread==0)
221 229
                                 continue;
222 230
                         if(bgload->is_todo==0) {
... ...
@@ -224,14 +232,24 @@ fprintf(stderr,"Thread received byte: %i\n",(int)((unsigned char)dummy));
224 232
                                 continue;
225 233
                         }
226 234
                         if(bgload->has_data==0 && bgload->has_failedload==0) {
227
-                                bgload->image=global_loadimage(bgload->path);
228
-                                if(IsImageValid(bgload->image))
229
-                                        bgload->has_data=1;
230
-                                else
231
-                                        bgload->has_failedload=1;
232
-                                bgload->thread_finished=1;
235
+                                if(candidate==NULL
236
+                                  || bgload->tv_lastadded.tv_sec>candidate->tv_lastadded.tv_sec
237
+                                  || (bgload->tv_lastadded.tv_sec==candidate->tv_lastadded.tv_sec && bgload->tv_lastadded.tv_usec>candidate->tv_lastadded.tv_usec)
238
+                                ) {
239
+                                        candidate=bgload;
240
+                                }
233 241
                         }
234 242
                 }
243
+                if(candidate!=NULL) {
244
+                        bgload=candidate;
245
+                        bgload->image=global_loadimage(bgload->path);
246
+                        if(IsImageValid(bgload->image))
247
+                                bgload->has_data=1;
248
+                        else
249
+                                bgload->has_failedload=1;
250
+                        bgload->lastused=time(NULL);
251
+                        bgload->thread_finished=1;
252
+                }
235 253
         }
236 254
         pthread_exit(NULL);
237 255
         return(NULL);
... ...
@@ -17,6 +17,8 @@
17 17
 #define BG_H
18 18
 
19 19
 #include <pthread.h>
20
+#include <time.h>
21
+#include <sys/time.h>
20 22
 #include "raylib.h"
21 23
 
22 24
 typedef struct bgload_t {
... ...
@@ -30,6 +32,8 @@ typedef struct bgload_t {
30 32
         Image image; // to use only from owner
31 33
         int has_data; // to use only from owner
32 34
         int has_failedload; // to use only from owner
35
+        time_t lastused; // to don't purge until some time has passed after load
36
+        struct timeval tv_lastadded; // to be able to load the "most recently added bgload" first
33 37
 } bgload_t;
34 38
 
35 39
 typedef struct bg_t {
... ...
@@ -30,7 +30,8 @@
30 30
 
31 31
 #define DEFAULTWIDTH 1280
32 32
 #define DEFAULTHEIGHT 768
33
-#define LEFTIMAGESIDELEN 326
33
+//#define LEFTIMAGESIDELEN 326
34
+#define LEFTIMAGESIDELEN 64
34 35
 #define SCROLLTHRESHOLD (LEFTIMAGESIDELEN/3)
35 36
 #define WHEELSTEP LEFTIMAGESIDELEN
36 37
 #define SEP '/'