Author Topic: Multi-File-Index program  (Read 3564 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Multi-File-Index program
« on: March 15, 2020, 12:37:00 pm »
For those of you who have downloaded and played any of my games over the past couple years now, You may have noticed I use a MFI file for the games resources, Graphics, Music, Sounds, ect.

Well I've cleaned it up a bit and commented it for ease of use. Version 1.0 allows for 255 files stored. Even my most ambitious game to-date, Dragon Warrior, only has 86 files stored. An idea for Version 2.0 will be to allow 32767 files (honestly though if you have that many resources then you need to rethink things!).

This Code Creates the MFI file:
Code: QB64: [Select]
  1. 'Multi-File-Index Maker Ver. 1.0
  2. 'Release Date: 3/15/2020
  3. 'UniKorn ProDucKions
  4. 'Cobalt (Aka. David)
  5.  
  6. 'FileList.TXT format:
  7. ' line  1: name of MFI file to be made(or what ever extention you want)
  8. ' line  2: number of files
  9. ' line 3+: files to add
  10.  
  11. '--------Setup---------
  12. SCREEN 0: CLS: _DELAY .15
  13. _TITLE "Multi-File-Index Creater V1.0 @2019 UniKorn ProDucKions"
  14. '----------------------
  15.  
  16. IF _FILEEXISTS("filelist.txt") THEN 'check for the file list to use
  17.  'File Compactor
  18.  DIM Size(255) AS LONG, F$(255)
  19.  F1 = FREEFILE
  20.  OPEN "filelist.txt" FOR BINARY AS #F1
  21.  LINE INPUT #F1, MFI_File$
  22.  
  23.  F2 = FREEFILE
  24.  OPEN MFI_File$ FOR BINARY AS #F2
  25.  
  26.  LINE INPUT #F1, files$ 'Number of image files
  27.  c~%% = VAL(files$) ' change to numeric value
  28.  F3 = FREEFILE
  29.  
  30.  '-------Get files Sizes------
  31.  FOR i~%% = 1 TO c~%%
  32.   LINE INPUT #F1, F$(i~%%)
  33.   OPEN F$(i~%%) FOR BINARY AS #F3
  34.   Size(i~%%) = LOF(F3)
  35.   CLOSE #F3
  36.  NEXT i~%%
  37.  '----------------------------
  38.  
  39.  FOffset& = c~%% * 8 + 1 'number of files*(LONG+LONG)+ _BYTE(number of files max-255)
  40.  
  41. '-------Put the File Data into the MFI File-------
  42.  PUT #F2, , c~%%
  43.  FOR i~%% = 1 TO c~%%
  44.   PUT #F2, , FOffset&
  45.   PUT #F2, , Size(i~%%)
  46.   FOffset& = FOffset& + Size(i~%%)
  47. '-------------------------------------------------
  48.  
  49. '--------Add files to the MFI file--------
  50.  FOR i~%% = 1 TO c~%%
  51.   PRINT F$(i~%%); TAB(60); Size(i~%%): TS& = TS& + Size(i~%%)'debuging and informative prints: this can be commented out for speed
  52.   OPEN F$(i~%%) FOR BINARY AS #F3
  53.   dat$ = SPACE$(Size(i~%%))
  54.   GET #F3, , dat$
  55.   PUT #F2, , dat$
  56.   CLOSE #F3
  57.  NEXT i~%%
  58. '-----------------------------------------
  59.  
  60.  PRINT TAB(58); "-----------"
  61.  PRINT "Total bytes output:"; TAB(60); TS&'total bytes added to MFI file
  62.  
  63. ELSE 'No file list found to process, in form user to make one.
  64.  PRINT "FileList.TXT missing. Please make FileList.TXT with the following layout:"
  65.  PRINT "(And make sure FileList.TXT is in correct directory.)"
  66.  PRINT "FileName.MFI"
  67.  PRINT "## (total number of files, Maximum 255 as of V1.0)"
  68.  PRINT "File #1 to include"
  69.  PRINT "File #2 to include"
  70.  PRINT "..."
  71.  PRINT "File #N to include"
  72.  

This Code Loads the files Stored in the MFI file:
Example call:
 MFI_Loader  "DragonWV1.MFI"

Code: QB64: [Select]
  1. SUB MFI_Loader (FN$)
  2.  'FN$: Name of the MFI file.
  3.  DIM MFI_Size(255) AS LONG, MFI_FOffset(255) AS LONG
  4.  MFI = FREEFILE 'file number for MFI file to load from, this value must be passed to SUBs.
  5.  '               This avoids issues with user already having the file number open
  6.  OPEN FN$ FOR BINARY AS #MFI
  7.  GET #MFI, , c~%% 'retrieve number of files
  8.  FOR I~%% = 1 TO c~%%
  9.   GET #MFI, , MFI_FOffset(I~%%)
  10.   GET #MFI, , MFI_Size(I~%%)
  11.   MFI_FOffset&(I~%%) = MFI_FOffset&(I~%%) + 1
  12.  NEXT I~%%
  13.  
  14.  REM Loading Example Lines***************************************************************
  15.  '-----------Load the files where they go here----------
  16.  ' Layer(2) = LoadGFX(MFI_FOffset(4), MFI_Size(4), MFI) '_LOADIMAGE("YahtzeeSplash.BMP", 32)
  17.  '
  18.  ' FFX& = LoadFFX(MFI_FOffset(8), MFI_Size(8), 24, MFI) '_LOADFONT("OBGB.ttf", 24, "MONOSPACE")
  19.  '
  20.  ' BGM(1) = LoadSFX(MFI_FOffset(16), MFI_Size(16), MFI) 'Splash\Title Screen Music
  21.  '
  22.  ' SFX(1) = LoadSFX(MFI_FOffset(13), MFI_Size(13), MFI) 'rolling dice'
  23.  '
  24.  '--------------------------------------------------------
  25.  REM *************************************************************************************
  26.  CLOSE #MFI
  27.  IF _FILEEXISTS("MFI_temp.dat") THEN KILL "MFI_temp.dat"
  28.  
  29.  
  30.  
  31. FUNCTION LoadGFX& (MFI_Foff&, MFI_Size&, MFI)
  32.  IF _FILEEXISTS("MFI_temp.dat") THEN KILL "MFI_temp.dat"
  33.  MFIData = FREEFILE
  34.  OPEN "MFI_temp.dat" FOR BINARY AS #MFIData
  35.  dat$ = SPACE$(MFI_Size&)
  36.  GET #MFI, MFI_Foff&, dat$
  37.  PUT #MFIData, , dat$
  38.  CLOSE #MFIData
  39.  LoadGFX& = _LOADIMAGE("MFI_temp.dat", 32)
  40.  
  41. FUNCTION LoadFFX& (MFI_Foff&, MFI_Size&, MFI_Fize%%, MFI)
  42.  IF _FILEEXISTS("MFI_temp.dat") THEN KILL "MFI_temp.dat"
  43.  MFIData = FREEFILE
  44.  OPEN "MFI_temp.dat" FOR BINARY AS #MFIData
  45.  dat$ = SPACE$(MFI_Size&)
  46.  GET #MFI, MFI_Foff&, dat$
  47.  PUT #MFIData, , dat$
  48.  CLOSE #MFIData
  49.  LoadFFX& = _LOADFONT("MFI_temp.dat", MFI_Fize%%, "monospace")
  50.  
  51. FUNCTION LoadSFX& (MFI_Foff&, MFI_Size&, MFI)
  52.  IF _FILEEXISTS("MFI_temp.dat") THEN KILL "MFI_temp.dat"
  53.  MFIData = FREEFILE
  54.  OPEN "MFI_temp.dat" FOR BINARY AS #MFIData
  55.  dat$ = SPACE$(MFI_Size&)
  56.  GET #MFI, MFI_Foff&, dat$
  57.  PUT #MFIData, , dat$
  58.  CLOSE #MFIData
  59.  LoadSFX& = _SNDOPEN("MFI_temp.dat")
  60.  
  61. SUB LoadData (MFI_Foff&, MFI_Size&, MFI)
  62.  IF _FILEEXISTS("MFI_temp.dat") THEN KILL "MFI_temp.dat"
  63.  MFIData = FREEFILE
  64.  OPEN "MFI_temp.dat" FOR BINARY AS #MFIData
  65.  dat$ = SPACE$(MFI_Size&)
  66.  GET #MFI, MFI_Foff&, dat$
  67.  PUT #MFIData, , dat$
  68.  CLOSE #MFIData
  69.  
  70.  F1 = FREEFILE
  71.  OPEN "MFI_temp.dat" FOR BINARY AS #F1
  72.  'Load stored data here
  73.  CLOSE #F1
  74.  
* Example.MFI (Filesize: 315.1 KB, Downloads: 94)
IMG_5975.JPG
* IMG_5975.JPG (Filesize: 315.09 KB, Dimensions: 1024x683, Views: 54)
« Last Edit: February 21, 2022, 11:34:16 am by Cobalt »
Granted after becoming radioactive I only have a half-life!