Browse code

Delay loading images if over fps deadline

Dario Rodriguez authored on 16/03/2025 12:04:54
Showing 1 changed files
... ...
@@ -25,6 +25,7 @@
25 25
  *      20250311 Fix bug because of stray CloseWindow() call.
26 26
  *               Add show using all window with right button.
27 27
  *      20250316 Add android target support.
28
+ *               Delay loading images if over fps deadline.
28 29
  *
29 30
  * Author: Dario Rodriguez dario@darionomono.com
30 31
  * (c) Dario Rodriguez 2025
... ...
@@ -38,6 +39,7 @@
38 39
 #include <sys/types.h>
39 40
 #include <dirent.h>
40 41
 #include <sys/stat.h>
42
+#include <sys/time.h>
41 43
 
42 44
 #include "raylib.h"
43 45
 #include "roboto_regular.c"
... ...
@@ -46,6 +48,8 @@
46 48
 
47 49
 #define SIMANDROID
48 50
 
51
+#define TARGETFPS 30
52
+
49 53
 #if defined(ANDROID) || defined(SIMANDROID)
50 54
 #define ROOTDIR "/sdcard/"
51 55
 #define DEFAULTWIDTH 2400
... ...
@@ -229,7 +233,7 @@ menubar_t *im_menubar_init(char *menus, font_t *font);
229 233
 void im_menubar_free(menubar_t *menubar);
230 234
 
231 235
 int im_menubar_mouse(menubar_t *menubar, Vector2 mousepos, int lmbpressed, int lmbreleased, int lmbdown, int *click_avail, char **sel_menu, char **sel_submenu);
232
-int im_menubar_draw(menubar_t *menubar, int windowwidth, int windowheight);
236
+int im_menubar_draw(menubar_t *menubar, int windowwidth, int windowheight, int *needs_nextredraw);
233 237
 
234 238
 body_t *im_body_init(int x, int y, font_t *font, font_t *fontbig, font_t *fonthuge, int leftsize, char *rootdir, int windowwidth, int windowheight);
235 239
 void im_body_free(body_t *body);
... ...
@@ -237,7 +241,7 @@ void im_body_free(body_t *body);
237 241
 int im_body_add(body_t *body,char *dir);
238 242
 
239 243
 int im_body_mouse(body_t *body, Vector2 mousepos, Vector2 wheel, int lmbpressed, int lmbreleased, int lmbdown, int *click_avail);
240
-int im_body_draw(body_t *body, Vector2 mousepos, int lmbdown, int rmbdown, int windowwidth, int windowheight);
244
+int im_body_draw(body_t *body, Vector2 mousepos, int lmbdown, int rmbdown, int windowwidth, int windowheight, int *needs_nextredraw);
241 245
 
242 246
 int listing_get(listing_t *listing, char *pathprefix, char *path, int flag_sort);
243 247
 void listing_freedata(listing_t *listing);
... ...
@@ -258,28 +262,47 @@ int menudata_pos2option(menudata_t *menudata, Vector2 pos);
258 262
 int *getcodepoints(int *sizecodepoints);
259 263
 int is_imagefilename(char *filename);
260 264
 Image imutil_loadimage(const char *filename);
265
+void imutil_fpsreset(void);
266
+int imutil_fpsleft(void);
261 267
 
262 268
 int
263 269
 main(int argc, char *argv[])
264 270
 {
265 271
         im_t *im;
266
-        Vector2 mousepos,wheel;
272
+        Vector2 mousepos,wheel,oldmousepos;
267 273
         int flag_ignorelmb;
268
-        int lmbpressed,lmbreleased,lmbdown,rmbdown;
274
+        int lmbpressed,lmbreleased,lmbdown,rmbdown,oldlmbdown,oldrmbdown;
269 275
         int click_avail;
276
+        int has_mousechanges;
277
+        int needs_nextredraw;
270 278
         char *sel_menu,*sel_submenu;
271 279
         if((im=im_init("Fichero\nAjustes\nSalir\n\nEditar\nNuevo directorio\n\nAyuda\nInformación sobre el programa\n\n",ROOTDIR))==NULL) {
272 280
                 return(1);
273 281
         }
274 282
         flag_ignorelmb=0;
283
+        mousepos=(Vector2) {.x=0,.y=0};
284
+        lmbdown=rmbdown=-1;
285
+        needs_nextredraw=1;
275 286
         while(!WindowShouldClose()) {
287
+                imutil_fpsreset();
288
+                oldmousepos=mousepos;
276 289
                 mousepos=GetMousePosition();
277 290
                 wheel=GetMouseWheelMoveV();
278 291
                 lmbpressed=IsMouseButtonPressed(0);
279 292
                 lmbreleased=IsMouseButtonReleased(0);
293
+                oldlmbdown=lmbdown;
280 294
                 lmbdown=IsMouseButtonDown(0);
295
+                oldrmbdown=rmbdown;
281 296
                 rmbdown=IsMouseButtonDown(1);
282 297
                 click_avail=1;
298
+                has_mousechanges=(lmbdown!=oldlmbdown || rmbdown!=oldrmbdown || mousepos.x!=oldmousepos.x || mousepos.y!=oldmousepos.y || wheel.x!=0 || wheel.y!=0)?1:0;
299
+                needs_nextredraw=(has_mousechanges==0 && needs_nextredraw==0)?0:1;
300
+                if(needs_nextredraw==0) {
301
+                        BeginDrawing();
302
+                        EndDrawing();
303
+                        continue;
304
+                }
305
+                needs_nextredraw=0;
283 306
                 /* process clicks on menus */
284 307
                 if(click_avail) {
285 308
                         sel_menu=sel_submenu=NULL;
... ...
@@ -292,13 +315,11 @@ fprintf(stderr,"SELECTED: \"%s\"->\"%s\"\n",sel_menu,sel_submenu);
292 315
                 }
293 316
                 if(click_avail)
294 317
                         im_body_mouse(im->body, mousepos, wheel, lmbpressed, lmbreleased, lmbdown, &click_avail);
295
-
296
-
297 318
                 /* draw screen contents */
298 319
                 BeginDrawing();
299 320
                 ClearBackground(RAYWHITE);
300
-                im_body_draw(im->body,mousepos,lmbdown,rmbdown,im->w,im->h);
301
-                im_menubar_draw(im->menubar,im->w,im->h);
321
+                im_body_draw(im->body,mousepos,lmbdown,rmbdown,im->w,im->h,&needs_nextredraw);
322
+                im_menubar_draw(im->menubar,im->w,im->h,&needs_nextredraw);
302 323
 #if 0
303 324
 {
304 325
  int i,j;
... ...
@@ -592,7 +613,7 @@ im_menubar_mouse(menubar_t *menubar, Vector2 mousepos, int lmbpressed, int lmbre
592 613
 }
593 614
 
594 615
 int
595
-im_menubar_draw(menubar_t *menubar, int windowwidth, int windowheight)
616
+im_menubar_draw(menubar_t *menubar, int windowwidth, int windowheight, int *needs_nextredraw)
596 617
 {
597 618
         int i,j,k,x;
598 619
         menudata_t *menudata;
... ...
@@ -803,7 +824,7 @@ im_body_mouse(body_t *body, Vector2 mousepos, Vector2 wheel, int lmbpressed, int
803 824
 }
804 825
 
805 826
 int
806
-im_body_draw(body_t *body, Vector2 mousepos, int lmbdown, int rmbdown, int windowwidth, int windowheight)
827
+im_body_draw(body_t *body, Vector2 mousepos, int lmbdown, int rmbdown, int windowwidth, int windowheight, int *needs_nextredraw)
807 828
 {
808 829
         int i,k,margin,righty;
809 830
         int lastx,lasty;
... ...
@@ -949,7 +970,9 @@ DrawRectangle(UNROLLXYWH(body->backxywh),((Color){ 0,255,0,255 })); /* hit zone
949 970
                                 /* show image */
950 971
                                 has_imagedrawn=0;
951 972
                                 if(is_imagefilename(elem->name+1)) {
952
-                                        if(thumb->has_texture==0 && thumb->has_failedload==0) {
973
+                                        if(thumb->has_texture==0 && thumb->has_failedload==0 && imutil_fpsleft()==0) {
974
+                                                *needs_nextredraw=1;
975
+                                        } else if(thumb->has_texture==0 && thumb->has_failedload==0) {
953 976
                                                 Image im;
954 977
                                                 char fullpath[2048];
955 978
                                                 snprintf(fullpath,sizeof(fullpath),"%s/%s/%s",body->rootdir,dirdata->dirname,elem->name+1);
... ...
@@ -1584,3 +1607,35 @@ imutil_loadimage(const char *filename)
1584 1607
 }
1585 1608
 #endif
1586 1609
 
1610
+static void
1611
+intimutil_fpsdata(struct timeval **deadline)
1612
+{
1613
+        static struct timeval mydeadline;
1614
+        *deadline=&(mydeadline);
1615
+        return;
1616
+}
1617
+
1618
+void
1619
+imutil_fpsreset(void)
1620
+{
1621
+        struct timeval *deadline;
1622
+        long deadlineincr;
1623
+        intimutil_fpsdata(&deadline);
1624
+        gettimeofday(deadline,NULL);
1625
+        deadlineincr=1000000L/TARGETFPS;
1626
+        deadline->tv_usec+=deadlineincr;
1627
+        deadline->tv_sec+=(deadline->tv_usec)/1000000L;
1628
+        deadline->tv_usec%=1000000L;
1629
+}
1630
+
1631
+int
1632
+imutil_fpsleft(void)
1633
+{
1634
+        struct timeval *deadline,now;
1635
+        intimutil_fpsdata(&deadline);
1636
+        gettimeofday(&now,NULL);
1637
+        if(deadline->tv_sec<now.tv_sec || (deadline->tv_sec==now.tv_sec && deadline->tv_usec<now.tv_usec))
1638
+                return(0);
1639
+        return(1);
1640
+}
1641
+