Mega Code Archive

 
Categories / Delphi / Files
 

Build one Resfile from files located in a Directory

Title: Build one Resfile from files located in a Directory Question: you may have a picture or sound-collection with n-files (bmp, wav etc.) you want to pack in one resourcefile without manually add them in the resource-workshop. Answer: { We want to get an output like this in a *.res format: BMP1 BITMAP "shiftshaft.bmp" BMP2 BITMAP "max2uml.bmp" BMP3 BITMAP "maxbox3.bmp" ..... 1. We put all the files in a directory 2. We start the scriptResourceFile() procedure gets all the files like *.bmp or *.wav in a *.rc format 3. Activate the resource-compiler to produce the res-file 4. Bind the res-file in an exe or a dll } procedure TStatForm.scriptresourceFile2(restype: string); var f: textfile; ResFile: ShortString; resstr: string; s: array[0..2048] of Char; i, filecount: Byte; myResList: TStringList; begin myresList:= TStringList.Create; filecount:= getfilelist(myResList); if filecount totalPictures then filecount:= totalPictures; for i:= 0 to filecount - 1 do begin resstr:= Format('%s%d %s %s%s%s', ['bmp', i, restype, '"', myReslist.Strings[i], '"']); StrCat(s, PChar(resstr)); StrCat(s, #13#10); end; ResFile:= 'membmp.rc'; AssignFile(f, ResFile); Rewrite(f); Write(f, s); closefile(f); myResList.Free; compileResfile(ResFile); end; procedure TStatForm.btnGenClick(Sender: TObject); begin scriptResourceFile2('Bitmap'); end; function TStatForm.getFileList(aList: TStringList): Integer; var DOSerr: Integer; fsrch: TsearchRec; begin Result:= 0; doserr:= FindFirst('*.bmp', faAnyFile, fsrch); if (DOSerr = 0) then begin while (DOSerr = 0) do begin aList.Add(fsrch.Name); if (fsrch.attr and faDirectory) = 0 then Inc(Result); DOSerr:= findnext(fsrch); end; findClose(fsrch); end; end; procedure TStatForm.compileResfile(vfile: string); var i, iCE: Integer; begin {$IFDEF MSWINDOWS} iCE:= shellapi.shellExecute(0, nil, PChar('BRCC32.exe'), PChar(vfile), nil, 0); i:= 0; repeat Inc(i); sleep(600); Application.ProcessMessages; until i = 10; if iCE {$ENDIF} end; 4. Then you link the file in an exe {$IFDEF MEMSOUND} {$R memsound.RES} {$ELSE} {$R mempics.RES} {$ENDIF MEMSOUND} A memory game project with this range of technic is available (108kb): http://www.softwareschule.ch/download/memory_source.zip there is also a report how to build a memory game with resources, but sorry only in german: http://www.softwareschule.ch/download/ressourcen_total.pdf