diff --git a/library/header/examples/c++/Makefile b/library/header/examples/c++/Makefile new file mode 100644 index 0000000..f99c686 --- /dev/null +++ b/library/header/examples/c++/Makefile @@ -0,0 +1,5 @@ +all: example.cpp ../../libShaderFile.h + g++ -o example example.cpp + +clean: + rm -rf *.exe \ No newline at end of file diff --git a/library/header/examples/example.cpp b/library/header/examples/c++/example.cpp similarity index 90% rename from library/header/examples/example.cpp rename to library/header/examples/c++/example.cpp index 3f2bdcd..5336997 100644 --- a/library/header/examples/example.cpp +++ b/library/header/examples/c++/example.cpp @@ -1,5 +1,5 @@ #include -#include "../libShaderFile.hpp" +#include "../../libShaderFile.hpp" int main(int argc, char **argv) { @@ -12,7 +12,7 @@ int main(int argc, char **argv) } // initialize library - lsf::Library lib("../../libShaderFile-i386-win32.dll"); + lsf::Library lib("../../../libShaderFile-i386-win32.dll"); // create and load shader file lsf::ShaderFile shaderFile(lib); diff --git a/library/header/examples/c/Makefile b/library/header/examples/c/Makefile new file mode 100644 index 0000000..03dfb6a --- /dev/null +++ b/library/header/examples/c/Makefile @@ -0,0 +1,5 @@ +all: example.c ../../libShaderFile.h + gcc -o example example.c + +clean: + rm -rf *.exe \ No newline at end of file diff --git a/library/header/examples/example.c b/library/header/examples/c/example.c similarity index 96% rename from library/header/examples/example.c rename to library/header/examples/c/example.c index 2596631..4a2d034 100644 --- a/library/header/examples/example.c +++ b/library/header/examples/c/example.c @@ -1,5 +1,5 @@ #include -#include "../libShaderFile.h" +#include "../../libShaderFile.h" int main(int argc, char **argv) { @@ -15,7 +15,7 @@ int main(int argc, char **argv) return 1; } - lsf_init("../../libShaderFile-i386-win32.dll"); + lsf_init("../../../libShaderFile-i386-win32.dll"); // create shader file sfHandle = lsf_shader_file_create(); diff --git a/library/header/examples/delphi/example.cfg b/library/header/examples/delphi/example.cfg new file mode 100644 index 0000000..ce48ecf --- /dev/null +++ b/library/header/examples/delphi/example.cfg @@ -0,0 +1,42 @@ +-$A8 +-$B- +-$C+ +-$D+ +-$E- +-$F- +-$G+ +-$H+ +-$I+ +-$J- +-$K- +-$L+ +-$M- +-$N+ +-$O+ +-$P+ +-$Q- +-$R- +-$S- +-$T- +-$U- +-$V+ +-$W- +-$X+ +-$YD +-$Z1 +-cg +-AWinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE; +-H+ +-W+ +-M +-$M16384,1048576 +-K$00400000 +-LE"c:\zusatzprogramme\delphi 7\Projects\Bpl" +-LN"c:\zusatzprogramme\delphi 7\Projects\Bpl" +-U"..\..\" +-O"..\..\" +-I"..\..\" +-R"..\..\" +-w-UNSAFE_TYPE +-w-UNSAFE_CODE +-w-UNSAFE_CAST diff --git a/library/header/examples/delphi/example.dof b/library/header/examples/delphi/example.dof new file mode 100644 index 0000000..112a24d --- /dev/null +++ b/library/header/examples/delphi/example.dof @@ -0,0 +1,141 @@ +[FileVersion] +Version=7.0 +[Compiler] +A=8 +B=0 +C=1 +D=1 +E=0 +F=0 +G=1 +H=1 +I=1 +J=0 +K=0 +L=1 +M=0 +N=1 +O=1 +P=1 +Q=0 +R=0 +S=0 +T=0 +U=0 +V=1 +W=0 +X=1 +Y=1 +Z=1 +ShowHints=1 +ShowWarnings=1 +UnitAliases=WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE; +NamespacePrefix= +SymbolDeprecated=1 +SymbolLibrary=1 +SymbolPlatform=1 +UnitLibrary=1 +UnitPlatform=1 +UnitDeprecated=1 +HResultCompat=1 +HidingMember=1 +HiddenVirtual=1 +Garbage=1 +BoundsError=1 +ZeroNilCompat=1 +StringConstTruncated=1 +ForLoopVarVarPar=1 +TypedConstVarPar=1 +AsgToTypedConst=1 +CaseLabelRange=1 +ForVariable=1 +ConstructingAbstract=1 +ComparisonFalse=1 +ComparisonTrue=1 +ComparingSignedUnsigned=1 +CombiningSignedUnsigned=1 +UnsupportedConstruct=1 +FileOpen=1 +FileOpenUnitSrc=1 +BadGlobalSymbol=1 +DuplicateConstructorDestructor=1 +InvalidDirective=1 +PackageNoLink=1 +PackageThreadVar=1 +ImplicitImport=1 +HPPEMITIgnored=1 +NoRetVal=1 +UseBeforeDef=1 +ForLoopVarUndef=1 +UnitNameMismatch=1 +NoCFGFileFound=1 +MessageDirective=1 +ImplicitVariants=1 +UnicodeToLocale=1 +LocaleToUnicode=1 +ImagebaseMultiple=1 +SuspiciousTypecast=1 +PrivatePropAccessor=1 +UnsafeType=0 +UnsafeCode=0 +UnsafeCast=0 +[Linker] +MapFile=0 +OutputObjs=0 +ConsoleApp=1 +DebugInfo=0 +RemoteSymbols=0 +MinStackSize=16384 +MaxStackSize=1048576 +ImageBase=4194304 +ExeDescription= +[Directories] +OutputDir= +UnitOutputDir= +PackageDLLOutputDir= +PackageDCPOutputDir= +SearchPath=..\..\ +Packages=rtl;vcl;vclie;xmlrtl;inet;inetdbbde;inetdbxpress;vclx;dbrtl;soaprtl;dsnap;VclSmp;dbexpress;vcldb;dbxcds;adortl;ibxpress;vclactnband;bdertl;vclshlctrls;dclOfficeXP +Conditionals= +DebugSourceDirs= +UsePackages=0 +[Parameters] +RunParams= +HostApplication= +Launcher= +UseLauncher=0 +DebugCWD= +[Version Info] +IncludeVerInfo=0 +AutoIncBuild=0 +MajorVer=1 +MinorVer=0 +Release=0 +Build=0 +Debug=0 +PreRelease=0 +Special=0 +Private=0 +DLL=0 +Locale=1031 +CodePage=1252 +[Version Info Keys] +CompanyName= +FileDescription= +FileVersion=1.0.0.0 +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion=1.0.0.0 +Comments= +[HistoryLists\hlUnitAliases] +Count=1 +Item0=WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE; +[HistoryLists\hlSearchPath] +Count=1 +Item0=..\..\ +[HistoryLists\hlUnitOutputDirectory] +Count=1 +Item0=../.. diff --git a/library/header/examples/delphi/example.dpr b/library/header/examples/delphi/example.dpr new file mode 100644 index 0000000..944ce58 --- /dev/null +++ b/library/header/examples/delphi/example.dpr @@ -0,0 +1,48 @@ +program example; + +{$APPTYPE CONSOLE} + +uses + SysUtils, + ulibShaderFile; + +var + i: Integer; + s: String; + ShaderFile: TlsfShaderFile; + Generator: TlsfGenerator; + +begin + lsf_init(ExtractFilePath(ParamStr(0)) + '..\..\..\libShaderFile-i386-win32.dll'); + try + if (ParamCount < 2) then begin + WriteLn('error: expected input file and generator/class name as parameter'); + ExitCode := 1; + exit; + end; + ShaderFile := TlsfShaderFile.Create; + try + ShaderFile.LoadFromFile(ParamStr(1)); + Generator := TlsfGenerator.Create(ShaderFile, ParamStr(2)); + try + i := 3; + while (i < ParamCount) do begin + s := ParamStr(i+1); + Generator.SetProperty(ParamStr(i), s); + inc(i, 2); + end; + WriteLn(Generator.GenerateCode); + finally + FreeAndNil(Generator); + end; + finally + FreeAndNil(ShaderFile); + end; + except + on e: Exception do begin + WriteLn('error: ' + e.Message); + ExitCode := 2; + end; + end; + lsf_finish; +end. diff --git a/library/header/examples/fpc/example.lpi b/library/header/examples/fpc/example.lpi new file mode 100644 index 0000000..37749ea --- /dev/null +++ b/library/header/examples/fpc/example.lpi @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + <UseAppBundle Value="False"/> + <ResourceType Value="res"/> + </General> + <i18n> + <EnableI18N LFM="False"/> + </i18n> + <VersionInfo> + <StringTable ProductVersion=""/> + </VersionInfo> + <BuildModes Count="1"> + <Item1 Name="Default" Default="True"/> + </BuildModes> + <PublishOptions> + <Version Value="2"/> + </PublishOptions> + <RunParams> + <local> + <FormatVersion Value="1"/> + </local> + </RunParams> + <Units Count="1"> + <Unit0> + <Filename Value="example.lpr"/> + <IsPartOfProject Value="True"/> + </Unit0> + </Units> + </ProjectOptions> + <CompilerOptions> + <Version Value="11"/> + <PathDelim Value="\"/> + <Target> + <Filename Value="example"/> + </Target> + <SearchPaths> + <IncludeFiles Value="$(ProjOutDir)"/> + <OtherUnitFiles Value="..\.."/> + <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> + </SearchPaths> + </CompilerOptions> + <Debugging> + <Exceptions Count="3"> + <Item1> + <Name Value="EAbort"/> + </Item1> + <Item2> + <Name Value="ECodetoolError"/> + </Item2> + <Item3> + <Name Value="EFOpenError"/> + </Item3> + </Exceptions> + </Debugging> +</CONFIG> diff --git a/library/header/examples/fpc/example.lpr b/library/header/examples/fpc/example.lpr new file mode 100644 index 0000000..412d37f --- /dev/null +++ b/library/header/examples/fpc/example.lpr @@ -0,0 +1,47 @@ +program example; + +uses + sysutils, variants, + ulibShaderFile; + +var + i: Integer; + s: String; + ShaderFile: TlsfShaderFile; + Generator: TlsfGenerator; + +begin + lsf_init(ExtractFilePath(ParamStr(0)) + '..\..\..\libShaderFile-x86_64-win64.dll'); + try + if (ParamCount < 2) then begin + WriteLn('error: expected input file and generator/class name as parameter'); + ExitCode := 1; + exit; + end; + ShaderFile := TlsfShaderFile.Create; + try + ShaderFile.LoadFromFile(ParamStr(1)); + Generator := TlsfGenerator.Create(ShaderFile, ParamStr(2)); + try + i := 3; + while (i < ParamCount) do begin + s := ParamStr(i+1); + Generator.SetProperty(ParamStr(i), s); + inc(i, 2); + end; + WriteLn(Generator.GenerateCode); + finally + FreeAndNil(Generator); + end; + finally + FreeAndNil(ShaderFile); + end; + except + on e: Exception do begin + WriteLn('error: ' + e.Message); + ExitCode := 2; + end; + end; + lsf_finish; +end. + diff --git a/library/header/ulibShaderFile.pas b/library/header/ulibShaderFile.pas index 8f08d4d..c081e2d 100644 --- a/library/header/ulibShaderFile.pas +++ b/library/header/ulibShaderFile.pas @@ -1,46 +1,57 @@ unit ulibShaderFile; -{$mode objfpc}{$H+} +{$IFDEF fpc} + {$mode objfpc}{$H+} +{$ENDIF} interface uses - Classes, SysUtils; - -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -const - LSF_LOGLEVEL_DEBUG = 0; - LSF_LOGLEVEL_INFO = 1; - LSF_LOGLEVEL_WARNING = 2; - LSF_LOGLEVEL_ERROR = 3; - - LSF_ERR_NONE = $00000000; - LSF_ERR_NOT_INIT = $00000001; - LSF_ERR_INVALID_HANDLE_SHADER_FILE = $00000010; - LSF_ERR_INVALID_HANDLE_SHADER_GENERATOR = $00000011; - LSF_ERR_INVALID_GENERATOR_NAME = $00000020; - LSF_ERR_INVALID_PROPERTY_INDEX = $00000021; - LSF_ERR_UNKNOWN_IDENTFIFIER = $00001000; - LSF_ERR_DUPLICATE_IDENTIFIER = $00001001; - LSF_ERR_OUT_OF_RANGE = $00001002; - LSF_ERR_INVALID_IDENTIFIER = $00001003; - LSF_ERR_INVALID_PARAMTER_COUNT = $00001004; - LSF_ERR_INVALID_PARAMTER = $00001005; - LSF_ERR_UNEXPECTED_TOKEN = $00001006; - LSF_ERR_INVALID_TOKEN = $00001007; - LSF_ERR_EXPRESSION_INTERNAL = $00001008; - LSF_ERR_EXPRESSION = $00001009; - LSF_ERR_SHADER_PART_INTERNAL = $0000100A; - LSF_ERR_SHADER_PART = $0000100B; - LSF_ERR_UNKNOWN = $FFFFFFFF; + Classes, SysUtils, Variants; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// type - TlsfErrorCode = Cardinal; - TlsfLogLevel = Cardinal; - TlsfShaderFileHandle = Pointer; - TlsfShaderGeneratorHandle = Pointer; - TlsfShaderFileLogEvent = procedure(const aLogLevel: TlsfLogLevel; const aMsg: PAnsiChar; const aUserArgs: Pointer); + {$Z4} + TlsfLogLevel = ( + llDebug = 0, + llInfo = 1, + llWarning = 2, + llError = 3 + ); + + {$Z4} + TlsfErrorCode = ( + errNone = $00000000, + errNotInit = $00000001, + errInvalidHandleShaderFile = $00000010, + errInvalidHandleShaderGenerator = $00000011, + errInvalidGeneratorName = $00000020, + errInvalidPropertyIndex = $00000021, + errInvalidPropertyName = $00000022, + errGeneratorNotAssignedToFile = $00000023, + errUnknownIdentfifier = $00001000, + errDuplicateIdentifier = $00001001, + errOutOfRange = $00001002, + errInvalidIdentifier = $00001003, + errInvalidParamterCount = $00001004, + errInvalidParamter = $00001005, + errUnexpectedToken = $00001006, + errInvalidToken = $00001007, + errExpressionInternal = $00001008, + errExpression = $00001009, + errShaderPartInternal = $0000100a, + errShaderPart = $0000100b, + errInvalidLibraryName = $00002000, + errInvalidLibraryHandle = $00002001, + errInvalidMethodName = $00002002, + errUnknown = -1 + ); + + TlsfShaderFileHandle = Pointer; + TlsfGeneratorHandle = Pointer; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TlsfShaderFileLogEvent = procedure(const aLogLevel: TlsfLogLevel; const aMsg: PAnsiChar; const aUserArgs: Pointer); stdcall; Tlsf_ShaderFile_create = function: TlsfShaderFileHandle; stdcall; Tlsf_ShaderFile_setLogCallback = function(const aHandle: TlsfShaderFileHandle; const aCallback: TlsfShaderFileLogEvent; const aUserArgs: Pointer): TlsfErrorCode; stdcall; @@ -49,19 +60,72 @@ type Tlsf_ShaderFile_getGeneratorNames = function(const aHandle: TlsfShaderFileHandle): PAnsiChar; stdcall; Tlsf_ShaderFile_destroy = function(const aHandle: TlsfShaderFileHandle): TlsfErrorCode; stdcall; - Tlsf_Generator_create = function(const aHandle: TlsfShaderFileHandle; const aName: PAnsiChar): TlsfShaderGeneratorHandle; stdcall; - Tlsf_Generator_getPropertyNames = function(const aHandle: TlsfShaderGeneratorHandle): PAnsiChar; stdcall; - Tlsf_Generator_getProperty = function(const aHandle: TlsfShaderGeneratorHandle; const aIndex: Integer): PAnsiChar; stdcall; - Tlsf_Generator_setProperty = function(const aHandle: TlsfShaderGeneratorHandle; const aIndex: Integer; const aValue: PAnsiChar): TlsfErrorCode; stdcall; - Tlsf_Generator_generateCode = function(const aHandle: TlsfShaderGeneratorHandle): PAnsiChar; stdcall; - Tlsf_Generator_destroy = function(const aHandle: TlsfShaderGeneratorHandle): TlsfErrorCode; stdcall; - - Tlsf_init = function : TlsfErrorCode; stdcall; - Tlsf_getLastErrorCode = function : TlsfErrorCode; stdcall; - Tlsf_getLastErrorMsg = function : PAnsiChar; stdcall; - Tlsf_getLastErrorTrace = function : PAnsiChar; stdcall; + Tlsf_Generator_create = function(const aHandle: TlsfShaderFileHandle; const aName: PAnsiChar): TlsfGeneratorHandle; stdcall; + Tlsf_Generator_getPropertyNames = function(const aHandle: TlsfGeneratorHandle): PAnsiChar; stdcall; + Tlsf_Generator_getProperty = function(const aHandle: TlsfGeneratorHandle; const aIndex: Integer): PAnsiChar; stdcall; + Tlsf_Generator_getPropertyByName = function(const aHandle: TlsfGeneratorHandle; const aName: PAnsiChar): PAnsiChar; stdcall; + Tlsf_Generator_setProperty = function(const aHandle: TlsfGeneratorHandle; const aIndex: Integer; const aValue: PAnsiChar): TlsfErrorCode; stdcall; + Tlsf_Generator_setPropertyByName = function(const aHandle: TlsfGeneratorHandle; const aName: PAnsiChar; const aValue: PAnsiChar): TlsfErrorCode; stdcall; + Tlsf_Generator_generateCode = function(const aHandle: TlsfGeneratorHandle): PAnsiChar; stdcall; + Tlsf_Generator_destroy = function(const aHandle: TlsfGeneratorHandle): TlsfErrorCode; stdcall; + + Tlsf_init = function: TlsfErrorCode; stdcall; + Tlsf_getLastErrorCode = function: TlsfErrorCode; stdcall; + Tlsf_getLastErrorMsg = function: PAnsiChar; stdcall; + Tlsf_getLastErrorTrace = function: PAnsiChar; stdcall; Tlsf_finish = function: TlsfErrorCode; stdcall; +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TlsfShaderFile = class(TObject) + private + fHandle: TlsfShaderFileHandle; + protected + procedure LogMsg(const aLogLevel: TlsfLogLevel; const aMsg: String); virtual; + public + property Handle: TlsfShaderFileHandle read fHandle; + + procedure LoadFromFile(const aFilename: String); + procedure SaveToFile(const aFilename: String); + + constructor Create; + destructor Destroy; override; + end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TlsfGenerator = class(TObject) + private + fName: String; + fHandle: TlsfGeneratorHandle; + fShaderFile: TlsfShaderFile; + fProperties: TStringList; + + function GetProperties: TStrings; + public + property Name: String read fName; + property ShaderFile: TlsfShaderFile read fShaderFile; + property Properties: TStrings read GetProperties; + + function GetProperty(const aName: String): Variant; overload; + function GetProperty(const aIndex: Integer): Variant; overload; + procedure SetProperty(const aName: String; const aValue: Variant); overload; + procedure SetProperty(const aIndex: Integer; const aValue: Variant); overload; + + function GenerateCode: String; + + constructor Create(const aShaderFile: TlsfShaderFile; const aName: String); + destructor Destroy; override; + end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TlsfException = class(Exception) + private + fErrorCode: TlsfErrorCode; + public + property ErrorCode: TlsfErrorCode read fErrorCode; + constructor Create(const aMsg: string; const aError: TlsfErrorCode = errNone); + end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// var lsf_ShaderFile_create: Tlsf_ShaderFile_create; lsf_ShaderFile_setLogCallback: Tlsf_ShaderFile_setLogCallback; @@ -73,7 +137,9 @@ var lsf_Generator_create: Tlsf_Generator_create; lsf_Generator_getPropertyNames: Tlsf_Generator_getPropertyNames; lsf_Generator_getProperty: Tlsf_Generator_getProperty; + lsf_Generator_getPropertyByName: Tlsf_Generator_getPropertyByName; lsf_Generator_setProperty: Tlsf_Generator_setProperty; + lsf_Generator_setPropertyByName: Tlsf_Generator_setPropertyByName; lsf_Generator_generateCode: Tlsf_Generator_generateCode; lsf_Generator_destroy: Tlsf_Generator_destroy; @@ -87,7 +153,32 @@ procedure lsf_finish; implementation uses - dynlibs; +{$IF DEFINED(WIN32) OR DEFINED(WIN64)} + windows; + +type + TLibHandle = HMODULE; + +function LibOpen(const aLibName: String; out aErrorCode: Cardinal): TLibHandle; +begin + result := LoadLibraryA(PAnsiChar(AnsiString(aLibName))); + if (result = 0) + then aErrorCode := GetLastError() + else aErrorCode := 0; +end; + +function GetAddr(const aLibHandle: TLibHandle; const aName: String): Pointer; +begin + result := GetProcAddress(aLibHandle, PAnsiChar(AnsiString(aName))); +end; + +function LibClose(const aLibHandle: TLibHandle): Boolean; +begin + result := FreeLibrary(aLibHandle); +end; +{$ELSE} + {$ERROR 'unknown operation system'} +{$IFEND} var libHandle: TLibHandle; @@ -97,17 +188,21 @@ var //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure lsf_init(const aLibName: String); - function LoadProc(const aName: AnsiString): Pointer; + function LoadProc(const aName: String): Pointer; begin - result := GetProcedureAddress(libHandle, aName); + result := GetAddr(libHandle, aName); if not Assigned(result) then raise Exception.CreateFmt('unable to load ''%s'' from ''%s''', [aName, aLibName]); end; +var + e: Cardinal; + err: TlsfErrorCode; begin - libHandle := LoadLibrary(aLibName); + libHandle := LibOpen(aLibName, e); if (libHandle = 0) then - raise Exception.CreateFmt('unable to load library: %s', [aLibName]); + raise Exception.CreateFmt('unable to load library (%d): %s', [e, aLibName]); + lsf_ShaderFile_create := Tlsf_ShaderFile_create( LoadProc('lsf_ShaderFile_create')); lsf_ShaderFile_setLogCallback := Tlsf_ShaderFile_setLogCallback( LoadProc('lsf_ShaderFile_setLogCallback')); lsf_ShaderFile_loadFromFile := Tlsf_ShaderFile_loadFromFile( LoadProc('lsf_ShaderFile_loadFromFile')); @@ -118,7 +213,9 @@ begin lsf_Generator_create := Tlsf_Generator_create( LoadProc('lsf_Generator_create')); lsf_Generator_getPropertyNames := Tlsf_Generator_getPropertyNames( LoadProc('lsf_Generator_getPropertyNames')); lsf_Generator_getProperty := Tlsf_Generator_getProperty( LoadProc('lsf_Generator_getProperty')); + lsf_Generator_getPropertyByName := Tlsf_Generator_getPropertyByName( LoadProc('lsf_Generator_getPropertyByName')); lsf_Generator_setProperty := Tlsf_Generator_setProperty( LoadProc('lsf_Generator_setProperty')); + lsf_Generator_setPropertyByName := Tlsf_Generator_setPropertyByName( LoadProc('lsf_Generator_setPropertyByName')); lsf_Generator_generateCode := Tlsf_Generator_generateCode( LoadProc('lsf_Generator_generateCode')); lsf_Generator_destroy := Tlsf_Generator_destroy( LoadProc('lsf_Generator_destroy')); @@ -128,8 +225,9 @@ begin lsf_getLastErrorTrace := Tlsf_getLastErrorTrace( LoadProc('lsf_getLastErrorTrace')); lsf_finish_intern := Tlsf_finish( LoadProc('lsf_finish')); - if (lsf_init_intern() <> LSF_ERR_NONE) then - raise Exception.Create('error while initializing library: ' + lsf_getLastErrorMsg()); + err := lsf_init_intern(); + if (err <> errNone) then + raise TlsfException.Create('error while initializing library: ' + lsf_getLastErrorMsg(), err); end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -146,10 +244,174 @@ begin lsf_getLastErrorMsg := nil; lsf_getLastErrorTrace := nil; if (libHandle <> 0) then begin - FreeLibrary(libHandle); + LibClose(libHandle); libHandle := 0; end; end; +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure LogCallback(const aLogLevel: TlsfLogLevel; const aMsg: PAnsiChar; const aUserArgs: Pointer); stdcall; +begin + TlsfShaderFile(aUserArgs).LogMsg(aLogLevel, String(aMsg)); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//TlsfShaderFile//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TlsfShaderFile.LogMsg(const aLogLevel: TlsfLogLevel; const aMsg: String); +begin + // DUMMY +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TlsfShaderFile.LoadFromFile(const aFilename: String); +var + err: TlsfErrorCode; +begin + err := lsf_ShaderFile_loadFromFile(fHandle, PAnsiChar(AnsiString(aFilename))); + if (err <> errNone) then + raise TlsfException.Create('error while loading from file: ' + lsf_getLastErrorMsg(), lsf_getLastErrorCode()); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TlsfShaderFile.SaveToFile(const aFilename: String); +var + err: TlsfErrorCode; +begin + err := lsf_ShaderFile_saveToFile(fHandle, PAnsiChar(AnsiString(aFilename))); + if (err <> errNone) then + raise TlsfException.Create('error while saving to file: ' + lsf_getLastErrorMsg(), lsf_getLastErrorCode()); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +constructor TlsfShaderFile.Create; +var + err: TlsfErrorCode; +begin + inherited Create; + + fHandle := lsf_ShaderFile_create(); + if not Assigned(fHandle) then + raise TlsfException.Create('error while creating shader file: ' + lsf_getLastErrorMsg(), lsf_getLastErrorCode()); + + err := lsf_ShaderFile_setLogCallback(fHandle, @LogCallback, self); + if (err <> errNone) then + raise TlsfException.Create('error while settings log callback: ' + lsf_getLastErrorMsg(), lsf_getLastErrorCode()); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +destructor TlsfShaderFile.Destroy; +begin + if Assigned(fHandle) then begin + lsf_ShaderFile_destroy(fHandle); + fHandle := nil; + end; + inherited Destroy; +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//TlsfGenerator///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +function TlsfGenerator.GetProperties: TStrings; +begin + result := fProperties; +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +function TlsfGenerator.GetProperty(const aName: String): Variant; +var + s: PAnsiChar; +begin + s := lsf_Generator_getPropertyByName(fHandle, PAnsiChar(AnsiString(aName))); + if not Assigned(s) then + raise TlsfException.Create('error while getting property by name: ' + lsf_getLastErrorMsg(), lsf_getLastErrorCode()); + result := String(s); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +function TlsfGenerator.GetProperty(const aIndex: Integer): Variant; +var + s: PAnsiChar; +begin + s := lsf_Generator_getProperty(fHandle, aIndex); + if not Assigned(s) then + raise TlsfException.Create('error while getting property by index: ' + lsf_getLastErrorMsg(), lsf_getLastErrorCode()); + result := String(s); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TlsfGenerator.SetProperty(const aName: String; const aValue: Variant); +var + err: TlsfErrorCode; + s: AnsiString; +begin + s := AnsiString(aValue); + err := lsf_Generator_setPropertyByName(fHandle, PAnsiChar(AnsiString(aName)), PAnsiChar(s)); + if (err <> errNone) then + raise TlsfException.Create('error while settings property by name: ' + lsf_getLastErrorMsg(), err); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TlsfGenerator.SetProperty(const aIndex: Integer; const aValue: Variant); +var + err: TlsfErrorCode; + s: AnsiString; +begin + s := AnsiString(aValue); + err := lsf_Generator_setProperty(fHandle, aIndex, PAnsiChar(s)); + if (err <> errNone) then + raise TlsfException.Create('error while settings property by name: ' + lsf_getLastErrorMsg(), err); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +function TlsfGenerator.GenerateCode: String; +var + s: PAnsiChar; +begin + s := lsf_Generator_generateCode(fHandle); + if not Assigned(s) then + raise TlsfException.Create('error while generating code: ' + lsf_getLastErrorMsg(), lsf_getLastErrorCode()); + result := String(s); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +constructor TlsfGenerator.Create(const aShaderFile: TlsfShaderFile; const aName: String); +var + s: PAnsiChar; +begin + inherited Create; + fProperties := TStringList.Create; + fShaderFile := aShaderFile; + fName := aName; + fHandle := lsf_Generator_create(fShaderFile.Handle, PAnsiChar(AnsiString(aName))); + if not Assigned(fHandle) then + raise TlsfException.Create('error while opening generator: ' + lsf_getLastErrorMsg(), lsf_getLastErrorCode()); + + s := lsf_Generator_getPropertyNames(fHandle); + if not Assigned(s) then + raise TlsfException.Create('error while generating code: ' + lsf_getLastErrorMsg(), lsf_getLastErrorCode()); + fProperties.Text := string(s); +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +destructor TlsfGenerator.Destroy; +begin + if Assigned(fHandle) then begin + lsf_Generator_destroy(fHandle); + fHandle := nil; + end; + FreeAndNil(fProperties); + inherited Destroy; +end; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//TlsfException///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +constructor TlsfException.Create(const aMsg: string; const aError: TlsfErrorCode); +begin + inherited Create(aMsg); + fErrorCode := aError; +end; + end. diff --git a/library/libShaderFile.lpr b/library/libShaderFile.lpr index 24b75d0..0bd20fa 100644 --- a/library/libShaderFile.lpr +++ b/library/libShaderFile.lpr @@ -80,7 +80,6 @@ type destructor Destroy; override; end; - TShaderFile = class(TengShaderFile) private fLogUserArgs: Pointer; diff --git a/uengShaderPartCntr.pas b/uengShaderPartCntr.pas index b02de18..80b3c64 100644 --- a/uengShaderPartCntr.pas +++ b/uengShaderPartCntr.pas @@ -156,7 +156,7 @@ begin aArgs.AddToken(s); try for p in fChildren do - p.GenerateCodeIntern(aArgs); + p.GenerateCodeIntern(aArgs); finally aArgs.AddToken(TOKEN_END); end;