Browse code

Basic image loading

Dario Rodriguez authored on 01/03/2025 22:04:42
Showing 1 changed files
... ...
@@ -14,7 +14,8 @@
14 14
  *      20250225 Draw image placeholders
15 15
  *      20250226 Fix rightside positions
16 16
  *      20250228 Navigate directories
17
- *      20250301 Aestetic fixes for leftside.
17
+ *      20250301 Aesthetic fixes for leftside.
18
+ *               Basic image loading.
18 19
  *
19 20
  * Author: Dario Rodriguez dario@darionomono.com
20 21
  * (c) Dario Rodriguez 2025
... ...
@@ -70,6 +71,22 @@
70 71
 #define UNROLLXYWH(xywh) (xywh).x,(xywh).y,(xywh).w,(xywh).h
71 72
 #endif
72 73
 
74
+#ifndef __linux__
75
+/* the old raylib used in the windows build lacks this function */
76
+bool IsImageValid(Image image)
77
+{
78
+    bool result = false;
79
+
80
+    if ((image.data != NULL) &&     // Validate pixel data available
81
+        (image.width > 0) &&        // Validate image width
82
+        (image.height > 0) &&       // Validate image height
83
+        (image.format > 0) &&       // Validate image format
84
+        (image.mipmaps > 0)) result = true; // Validate image mipmaps (at least 1 for basic mipmap level)
85
+
86
+    return result;
87
+}
88
+#endif
89
+
73 90
 typedef struct xywh_t {
74 91
         int x;
75 92
         int y;
... ...
@@ -102,7 +119,11 @@ typedef struct menubar_t {
102 119
 
103 120
 typedef struct listingdata_t {
104 121
         xywh_t leftxywh;
122
+        Texture2D lefttexture;
123
+        int has_lefttexture;
105 124
         xywh_t xywh;
125
+        Texture2D texture;
126
+        int has_texture;
106 127
         char *name; /* first byte in the name is the type, next is the name, i.e. a directory is "dhome" and a file is "f.bashrc" (this is done for easier sorting) */
107 128
 } listingdata_t;
108 129
 
... ...
@@ -179,6 +200,7 @@ char *imutil_strduplen(char *str, int len);
179 200
 int is_imutil_insidexywh(Vector2 pos, xywh_t *xywh);
180 201
 int menudata_pos2option(menudata_t *menudata, Vector2 pos);
181 202
 int *getcodepoints(int *sizecodepoints);
203
+int is_imagefilename(char *filename);
182 204
 
183 205
 int
184 206
 main(int argc, char *argv[])
... ...
@@ -580,6 +602,7 @@ body_t *
580 602
 im_body_init(int x, int y, font_t *font, font_t *fontbig, font_t *fonthuge, int leftsize, char *rootdir)
581 603
 {
582 604
         body_t *body;
605
+        static char sep[]={SEP};
583 606
         if(font==NULL || fontbig==NULL || fonthuge==NULL || rootdir==NULL)
584 607
                 return(NULL); /* sanity check failed */
585 608
         if((body=calloc(1,sizeof(body_t)))==NULL
... ...
@@ -593,6 +616,8 @@ im_body_init(int x, int y, font_t *font, font_t *fontbig, font_t *fonthuge, int
593 616
         body->ptrfont=font;
594 617
         body->ptrfontbig=fontbig;
595 618
         body->ptrfonthuge=fonthuge;
619
+        if(body->rootdir[0]!='\0' && strcmp(body->rootdir,SEP)!=0 && body->rootdir[strlen(body->rootdir)-1]==sep[0])
620
+                body->rootdir[strlen(body->rootdir)-1]='\0'; /* rootdir doesn't need the final '/' */
596 621
         return(body);
597 622
 }
598 623
 
... ...
@@ -865,8 +890,12 @@ DrawRectangle(UNROLLXYWH(body->backxywh),((Color){ 0,255,0,255 })); /* hit zone
865 890
                                         char shortname[1024];
866 891
                                         xywh_t *pos;
867 892
                                         int l;
893
+                                        Texture2D *te;
894
+                                        int *has_texture;
868 895
                                         font_t *myfont=(is_leftside)?fonthuge:font;
869 896
                                         pos=is_leftside?&(elem->leftxywh):&(elem->xywh);
897
+                                        te=is_leftside?&(elem->lefttexture):&(elem->texture);
898
+                                        has_texture=is_leftside?&(elem->has_lefttexture):&(elem->has_texture);
870 899
                                         if((ptr=strchr(elem->name+1,'.'))!=NULL) {
871 900
                                                 m2=MeasureTextEx(myfont->font,ptr,myfont->height,0);
872 901
                                                 DrawTextEx(myfont->font,ptr,(Vector2){x+margin+(sidelen-m2.x)/2,y+(font->height)/2+margin+(sidelen-myfont->height)/2},myfont->height,0,(Color){ 0,0,0,96 });
... ...
@@ -878,6 +907,24 @@ DrawRectangle(UNROLLXYWH(body->backxywh),((Color){ 0,255,0,255 })); /* hit zone
878 907
                                         shortname[l]='\0';
879 908
                                         DrawRectangle(pos->x,pos->y,pos->w,font->height+font->height/2,((Color){0,0,0,64}));
880 909
                                         DrawTextEx(font->font,shortname,(Vector2){pos->x+font->height/4,pos->y+font->height/4},font->height,0,(Color){ 192,192,192,255 });
910
+                                        /* show image */
911
+                                        if(is_imagefilename(elem->name+1)) {
912
+                                                if(*has_texture==0) {
913
+                                                        Image im;
914
+                                                        char fullpath[2048];
915
+                                                        snprintf(fullpath,sizeof(fullpath),"%s/%s/%s",body->rootdir,dirdata->dirname,elem->name+1);
916
+                                                        fullpath[sizeof(fullpath)-1]='\0';
917
+                                                        im=LoadImage(fullpath);
918
+                                                        if(IsImageValid(im)) {
919
+                                                                ImageResize(&im,sidelen,sidelen);
920
+                                                                *te=LoadTextureFromImage(im);
921
+                                                                UnloadImage(im);
922
+                                                                *has_texture=1;
923
+                                                        }
924
+                                                }
925
+                                                if(*has_texture!=0)
926
+                                                        DrawTexture(*te,pos->x,pos->y,WHITE);
927
+                                        }
881 928
                                 }
882 929
                                 x+=margin*2+sidelen;
883 930
                         }
... ...
@@ -1048,15 +1095,30 @@ strptrcmp(void *a,void *b)
1048 1095
 int
1049 1096
 listing_get(listing_t *listing, char *pathprefix, char *parampath, int flag_sort)
1050 1097
 {
1051
-        int l;
1098
+        int i,l;
1052 1099
         DIR *d;
1053 1100
         struct dirent *de;
1054 1101
         unsigned char dtype;
1055 1102
         char path[1024];
1103
+        listingdata_t *ld;
1056 1104
         if(listing==NULL)
1057 1105
                 return(-1); /* sanity check failed */
1106
+        /* free old textures */
1107
+        for(i=0;i<listing->usedelems;i++) {
1108
+                ld=listing->elems+i;
1109
+                if(ld->has_lefttexture) {
1110
+                        UnloadTexture(ld->lefttexture);
1111
+                        ld->has_lefttexture=0;
1112
+                }
1113
+                if(ld->has_texture) {
1114
+                        UnloadTexture(ld->texture);
1115
+                        ld->has_texture=0;
1116
+                }
1117
+        }
1118
+        /* reset struct */
1058 1119
         listing->usedelems=listing->usedbuf=0;
1059 1120
         memset(listing->elems,0,sizeof(listingdata_t)*listing->sizeelems);
1121
+        /* fill listing */
1060 1122
         if(pathprefix==NULL && parampath==NULL)
1061 1123
                 return(-1); /* nothing to fill */
1062 1124
         snprintf(path,sizeof(path),"%s%s%s",(pathprefix!=NULL)?pathprefix:"",SEP,(parampath!=NULL)?parampath:"");
... ...
@@ -1146,10 +1208,50 @@ listing_freedata(listing_t *listing)
1146 1208
 {
1147 1209
         if(listing==NULL)
1148 1210
                 return; /* nothing to do */
1149
-        if(listing->elems!=NULL)
1211
+        if(listing->elems!=NULL) {
1212
+                int i;
1213
+                listingdata_t *ld;
1214
+                for(i=0;i<listing->usedelems;i++) {
1215
+                        ld=listing->elems+i;
1216
+                        if(ld->has_lefttexture) {
1217
+                                UnloadTexture(ld->lefttexture);
1218
+                                ld->has_lefttexture=0;
1219
+                        }
1220
+                        if(ld->has_texture) {
1221
+                                UnloadTexture(ld->texture);
1222
+                                ld->has_texture=0;
1223
+                        }
1224
+                }
1150 1225
                 free(listing->elems),listing->elems=NULL,listing->sizeelems=listing->usedelems=0;
1226
+        }
1151 1227
         if(listing->buf!=NULL)
1152 1228
                 free(listing->buf),listing->buf=NULL,listing->sizebuf=listing->usedbuf=0;
1153 1229
         return;
1154 1230
 }
1155 1231
 
1232
+int
1233
+is_imagefilename(char *filename)
1234
+{
1235
+        char *ptr;
1236
+        int i;
1237
+        char *knownext[]={".jpg",".jpeg",".JPG",".JPEG",".Jpg",".Jpeg"
1238
+                         ,".png",".PNG",".Png"
1239
+                         ,".tga",".TGA",".Tga"
1240
+                         ,".bmp",".BMP",".Bmp"
1241
+                         ,".psd",".PSD",".Psd"
1242
+                         ,".gif",".GIF",".Gif"
1243
+                         ,".hdr",".HDR",".Hdr"
1244
+                         ,".pic",".PIC",".Pic"
1245
+                         ,".pnm",".PNM",".Pnm"
1246
+        };
1247
+        if(filename==NULL)
1248
+                return(0); /* sanity check failed */
1249
+        if((ptr=strrchr(filename,'.'))==NULL)
1250
+                return(0); /* no extension found */
1251
+        for(i=0;i<(sizeof(knownext)/sizeof(knownext[0]));i++) {
1252
+                if(strcmp(ptr,knownext[i])==0)
1253
+                        return(1); /* it has a known ext */
1254
+        }
1255
+        return(0); /* not in the knownext list */
1256
+}
1257
+