Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // *
- // * @package Aika j2d
- // * @copyright (c) Dwar, 2010
- // * @link ProGamerCity.net
- // * @author Dwar
- // * @license Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
- // * @desc Threads
- // *
- unit thMain;
- interface
- uses
- Classes, Windows,SysUtils,Forms, ShellApi;
- type
- TThreadMain = class(TThread)
- private
- FAct : short;
- { Private declarations }
- protected
- procedure Execute; override;
- procedure Sync;
- public
- property Act : Short read FAct write FAct;
- end;
- //threads var
- var FCount : integer; //files count
- CCount : integer; //current progress
- error : boolean; //if we have errors
- StatusStr : string; //string for status message
- status : short; //status code
- CurFile : string; //current file
- implementation
- uses dMainForm, func, StrUtils;
- procedure TThreadMain.Execute;
- var
- i,j : integer;
- inFile, outFile : TFileStream;
- buff : array of byte;
- fNAme : string;
- //header data
- jHeader : array [0..3] of AnsiChar;
- jHeight : dword;
- jWidth : dword;
- fHeader : string;
- fHeaderDXT : array [0..107] of byte;
- fHeaderDXT1 : array [0..$B] of byte;
- bufTmp : array of byte;
- fSize : dword;
- destDir : string;
- begin
- while not Terminated do
- begin
- FCount := 0;
- CCount := 0;
- // dxt5 header
- fHeader := '000004000000000009000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000040000004458543500000000000000000000000000000000000000000810400000000000000000000000000000000000';
- //converting string into array
- for I := 0 to 103 do
- begin
- fHeaderDXT[i] := StrToInt('$'+copy(fHeader,i*2 + 1,2));
- end;
- fHeader := '444453207C00000007100A00';
- for I := 0 to $B do
- begin
- fHeaderDXT1[i] := StrToInt('$'+copy(fHeader,i*2 + 1,2));
- end;
- CCount := 0;
- //set file count for displaying in progress status on main form
- FCount := MainForm.FileList.Count;
- //process all files from our list
- for I := 0 to MainForm.FileList.Count - 1 do
- begin
- status := 0; //
- error := false; //no error
- fName := MainForm.FileList.Strings[i]; //get file name from list
- CurFile := ExtractFileNameEx(fNAme, true); //get file name with extension
- inFile := TFileStream.Create(fNAme, fmShareDenyNone); //create new file stream
- //create directory for converted files
- destDir := IncludeTrailingBackslash(IncludeTrailingBackslash(ExtractFilePath(fNAme))+ 'Converted');
- if not DirectoryExists(destDir) then
- {$I+}MkDir(destDir);{$I-}
- //stream for output file
- outFile := TFileStream.Create(destDir + ExtractFileNameEx(CurFile,false)+'.dds', fmCreate);
- //perform sync with main form
- Synchronize(Sync);
- //get dword from opened file
- inFile.read(jHeader, 4);
- //check header: JT31, JT33, JT35
- //set some header data for output file
- case AnsiIndexStr(string(jHeader),['JT31','JT33','JT35']) of
- 0 : // dxt1
- begin
- fHeaderDXT[2] := 4;
- fHeaderDXT[67] := $31;
- end;
- 1 : // dxt3
- begin
- fHeaderDXT[67] := $33;
- end;
- 2 : // dxt5
- begin
- fHeaderDXT[67] := $35;
- end;
- end;
- // write header 1
- outFile.Position := 0;
- outFile.WriteBuffer(fHeaderDXT1[0],length(fHeaderDXT1));
- // r-w dds size 8b
- setLength(bufTmp, 8);
- inFile.ReadBuffer (jWidth,4);
- inFile.ReadBuffer (jHeight,4);
- outFile.WriteBuffer (jHeight,4);
- outFile.WriteBuffer (jWidth,4);
- fSize := jHeight*jWidth*2;
- // write header 2
- outFile.WriteBuffer (fHeaderDXT[0],length(fHeaderDXT));
- outFile.CopyFrom(inFile, inFile.Size - $c);
- if (outFile.Size - 128) < fSize then
- begin
- setLength(bufTmp, fSize-(outFile.Size - 128));
- outFile.WriteBuffer(bufTmp[0], length(bufTmp));
- end;
- inFile.Free;
- outFile.Free;
- Synchronize(Sync);
- end; // for I := 0 to MainForm.FileList.Count
- status := 2;
- Synchronize(Sync);
- //wait another operation
- suspend;
- end;
- end;
- procedure TThreadMain.Sync;
- var StrSig : PWideChar;
- begin
- with MainForm do
- begin
- if error then
- begin
- LblStatus.Caption := StatusStr;
- DragAcceptFiles( Handle, True );
- end
- else
- case status of
- 0:
- begin
- ProgressStatus.TotalParts := FCount;
- ProgressStatus.PartsComplete := CCount;
- LblStatus.Caption := CurFile; //wrong file
- end;
- 2:
- begin
- LblStatus.Caption := 'Complete';
- ProgressStatus.PartsComplete := 0;
- fileList.Clear;
- DragAcceptFiles( Handle, True );
- end;
- end;
- end;
- end;
- end.
Add Comment
Please, Sign In to add comment