{==============================================================================

  Quad engine header file for CodeGear™ Delphi®

     ╔═══════════╦═╗
     ║           ║ ║
     ║           ║ ║
     ║ ╔╗ ║║ ╔╗ ╔╣ ║
     ║ ╚╣ ╚╝ ╚╩ ╚╝ ║
     ║  ║ engine   ║
     ║  ║          ║
     ╚══╩══════════╝

  For further information please visit:
  http://quad-engine.com

==============================================================================}

unit QuadEngine;

interface

uses
  windows, Direct3d9;

const
  libraryName : PChar = 'qei.dll';
  CreateQuadRenderProcName : PChar = 'CreateQuadRender';
  CreateQuadWindowProcName : PChar = 'CreateQuadWindow';
  SecretMagicFunctionProcName : PChar = 'SecretMagicFunction';

type
  // Blending mode types
  TQuadBlendMode = (qbmNone        = 0,     { Without blending }
                    qbmAdd         = 1,     { Add source to dest }
                    qbmSrcAlpha    = 2,     { Blend dest with alpha to source }
                    qbmSrcAlphaAdd = 3,     { Add source with alpha to dest }
                    qbmSrcAlphaMul = 4,     { Multiply source alpha with dest }
                    qbmMul         = 5,     { Multiply Source with dest }
                    qbmSrcColor    = 6,     { Blend source with color weight to dest }
                    qbmSrcColorAdd = 7);    { Blend source with color weight and alpha to dest }

  // Vector record declaration
  TVector = packed record
    x : Single;
    y : Single;
    z : Single;
  end;

  // vertex record declaration
  TVertex = packed record
    x, y, z  : single;         { X, Y of vertex. Z not used }
    normal   : TVector;        { Normal vector }
    color    : Cardinal;       { Color }
    u, v     : single;         { Texture UV coord }
    tangent  : TVector;        { Tangent vector }
    binormal : TVector;        { Binormal vector }
  end;

  // forward interfaces declaration
  IQuadRender  = interface;
  IQuadTexture = interface;
  IQuadShader  = interface;
  IQuadFont    = interface;
  IQuadLog     = interface;
  IQuadTimer   = interface;
  IQuadWindow  = interface;

  { Quad Render }

  IQuadRender = interface(IUnknown)
    ['{D9E9C42B-E737-4CF9-A92F-F0AE483BA39B}']
    function  CreateFont : IQuadFont; stdcall;
    function  CreateShader : IQuadShader; stdcall;
    function  CreateTexture : IQuadTexture; stdcall;
    function  CreateTimer : IQuadTimer; stdcall;
    function  GetAvailableTextureMemory: Cardinal; stdcall;
    function  GetD3DDevice : IDirect3DDevice9; stdcall;
    function  GetLastError : PAnsiChar; stdcall;
    function  GetMaxAnisotropy: Cardinal; stdcall;
    function  GetMaxTextureHeight: Cardinal; stdcall;
    function  GetMaxTextureStages: Cardinal; stdcall;
    function  GetMaxTextureWidth: Cardinal; stdcall;
    Function  GetMonitorsCount : Byte; stdcall;
    function  GetPixelShaderVersionString: PAnsiChar; stdcall;
    function  GetPSVersionMajor: byte; stdcall;
    function  GetPSVersionMinor: byte; stdcall;
    function  GetQuadLog : IQuadLog; stdcall;
    function  GetRenderTexture(index: Integer): IDirect3DTexture9; stdcall;
    function  GetVertexShaderVersionString: PAnsiChar; stdcall;
    function  GetVSVersionMajor: byte; stdcall;
    function  GetVSVersionMinor: Byte; stdcall;
    procedure AddTrianglesToBuffer(Vertexes : array of TVertex; Count : Cardinal); stdcall;
    procedure BeginRender; stdcall;
    procedure ChangeResolution(aWidth, aHeight : Word); stdcall;
    procedure Clear(aColor : Cardinal); stdcall;
    procedure CreateOrthoMatrix; stdcall;
    procedure CreateRenderTexture(aWidth, aHeight : Word; aIndex : Byte); stdcall;
    procedure DrawDistort(x1, y1, x2, y2, x3, y3, x4, y4: Double; u1, v1, u2, v2 : Double; Color : Cardinal); stdcall;
    procedure DrawRect(x, y, x2, y2 : Double; u1, v1, u2, v2 : Double; Color : Cardinal); stdcall;
    procedure DrawRectRot(x, y, x2, y2, ang, Scale : Double; u1, v1, u2, v2 : Double; Color : Cardinal); stdcall;
    procedure DrawRectRotAxis(x, y, x2, y2, ang, Scale, xA, yA : Double; u1, v1, u2, v2 : Double; Color : Cardinal); stdcall;
    procedure DrawLine(x, y, x2, y2 : Single; Color : Cardinal); stdcall;
    procedure DrawPoint(x, y : Single; Color : Cardinal); stdcall;
    procedure EndRender; stdcall;
    procedure Finalize; stdcall;
    procedure FlushBuffer; stdcall;
    procedure Initialize(aHandle : THandle; aWidth, aHeight : Integer; Fullscreen : Boolean; CreateLog : Boolean = true); stdcall;
    procedure Rectangle(x, y, x2, y2 : Double; Color : Cardinal); overload; stdcall;
    procedure Rectangle(x, y, x2, y2 : Double; Color1, Color2, Color3, Color4 : Cardinal); overload; stdcall;
    procedure RenderToTexture(aIntoTexture : boolean; Count : Byte); stdcall;
    procedure ResetDevice; stdcall;
    procedure SetActiveMonitor(MonitorIndex : byte); stdcall;
    procedure SetBlendMode(qbm : TQuadBlendMode); stdcall;
    procedure SetClipRect(X, Y, X2, Y2 : Cardinal); stdcall;
    procedure SetPointSize(Size : Cardinal); stdcall;
    procedure SetTexture(aRegister : byte; aTexture : IDirect3DTexture9); stdcall;
    procedure SetTextureAdressing(mode : TD3DTextureAddress); stdcall;
    procedure SetTextureFiltering(mode : TD3DTextureFilterType); stdcall;
    procedure SkipClipRect; stdcall;
    procedure SwapRenderTargets; stdcall;
  end;

  { Quad Texture }

  IQuadTexture = interface(IUnknown)
    ['{9A617F86-2CEC-4701-BF33-7F4989031BBA}']
    function  GetIsLoaded : Boolean; stdcall;
    function  GetPatternCount: Integer; stdcall;
    function  GetTexture(i: byte): IDirect3DTexture9; stdcall;
    procedure AddTexture(aRegister: Byte; aTexture : IDirect3DTexture9); stdcall;
    procedure CreateFromI(aTexture : IDirect3DTexture9; width, height : Word; aRegister : Byte); stdcall;
    procedure CreateFromRenderTarget(RenderTargetIndex : Byte; aRegister : Byte = 0); stdcall;
    procedure Draw(x, y : Double; Color : Cardinal); overload; stdcall;
    procedure Draw(x, y : Double; Color : Cardinal; Pattern : Word); overload; stdcall;
    procedure DrawDistort(x1, y1, x2, y2, x3, y3, x4, y4 : Double; Color : Cardinal); stdcall;
    procedure DrawMap(x, y, x2, y2, u1, v1, u2, v2 : Double; Color : Cardinal); stdcall;
    procedure DrawRot(x, y, angle, Scale : Double; Color : Cardinal); overload; stdcall;
    procedure DrawRot(x, y, angle, Scale : Double; Color : Cardinal; Pattern : Word); overload; stdcall;
    procedure DrawRotAxis(x, y, angle, Scale, xA, yA : Double; Color : Cardinal); overload; stdcall;
    procedure DrawRotAxis(x, y, angle, Scale, xA, yA : Double; Color : Cardinal; Pattern : Word); overload; stdcall;
    procedure LoadFromFile(aRegister: Byte; aFilename : PAnsiChar; ColorKey : Integer = -1); overload; stdcall;
    procedure LoadFromFile(aRegister: Byte; aFilename : PAnsiChar; PatternWidth, PatternHeight : Integer; ColorKey : Integer = -1); overload; stdcall;
  end;

  { Quad Shader }

  IQuadShader = interface(IUnknown)
    ['{7B7F4B1C-7F05-4BC2-8C11-A99696946073}']
    function  GetVertexShader : IDirect3DVertexShader9; stdcall;
    function  GetPixelShader : IDirect3DPixelShader9; stdcall;
    procedure BindVariableToPS(aRegister : Byte; aVariable : Pointer; aSize : Byte); stdcall;
    procedure BindVariableToVS(aRegister : Byte; aVariable : Pointer; aSize : Byte); stdcall;
    procedure LoadComplexShader(VS, PS : PAnsiChar); stdcall;
    procedure LoadPixelShader(PS : PAnsiChar); stdcall;
    procedure LoadVertexShader(VS : PAnsiChar); stdcall;
    procedure SetShaderState(Enabled : Boolean); stdcall;
  end;

  { Quad Font }

  // font alignments
  TqfAlign = (qfaLeft   = 0,      { Align by left }
              qfaRight  = 1,      { Align by right }
              qfaCenter = 2);     { Align by center }

  IQuadFont = interface(IUnknown)
    ['{A47417BA-27C2-4DE0-97A9-CAE546FABFBA}']
    function  GetIsLoaded: Boolean; stdcall;
    function  GetKerning : Single; stdcall;
    function  TextWidth(text : PAnsiChar; Scale : Single = 1.0) : Single; stdcall;
    procedure LoadFromFile(aTextureName, aUVname : PAnsiChar); stdcall;
    procedure SetKerning(Value : Single); stdcall;
    procedure TextOut(x, y, scale : Single; text : PAnsiChar; Color : Cardinal); stdcall;
    procedure TextOutAligned(x, y, scale : Single; text : PAnsiChar; Color : Cardinal; Align : TqfAlign = qfaLeft); stdcall;
    procedure TextOutCentered(x, y, scale : Single; text : PAnsiChar; Color : Cardinal); stdcall;
  end;

  {Quad Log}

  IQuadLog = interface(IUnknown)
    ['{7A4CE319-C7AF-4BF3-9218-C2A744F915E6}']
    procedure Write(aString : string); stdcall;
  end;

  {Quad Timer}

  TTimerProcedure = procedure(var delta : Double);
  { template:
    procedure OnTimer(var delta : Double);
    begin

    end;
  }
  IQuadTimer = interface(IUnknown)
    ['{EA3BD116-01BF-4E12-B504-07D5E3F3AD35}']
    function GetCPUload : Single; stdcall;
    function GetFPS : Single; stdcall;
    function GetDelta : Double; stdcall;
    function GetWholeTime : Double; stdcall;
    procedure SetCallBack(Proc: TTimerProcedure); stdcall;
    procedure SetInterval(Interval : Word); stdcall;
    procedure SetState(Enabled : Boolean); stdcall;
    procedure ResetWholeTimeCounter; stdcall;
  end;

  {Quad Sprite}     {not implemented yet. do not use}

  IQuadSprite = interface(IUnknown)
  ['{3E6AF547-AB0B-42ED-A40E-8DC10FC6C45F}']
    procedure SetPosition(X, Y : double); stdcall;
    procedure SetVelocity(X, Y : double); stdcall;
    procedure Draw; stdcall;
  end;

  {Quad Window}     {not implemented yet. do not use}
  IQuadWindow = interface(IUnknown)
  ['{E8691EB1-4C5D-4565-8B78-3FC7C620DFFB}']
    function GetHandle : Cardinal; stdcall;
    procedure SetPosition(Top, Left : Integer); stdcall;
    procedure SetDimentions(Width, Height : Integer); stdcall;
    procedure CreateWindow; stdcall;
  end;


  TCreateQuadRender    = function : IQuadRender;
  TCreateQuadWindow    = function : IQuadWindow;
  TSecretMagicFunction = function : PAnsiChar;

  function CreateQR : IQuadRender;
  function CreateWindow : IQuadWindow;

implementation

// Creating of main Quad interface object
function CreateQR : IQuadRender;
var
  h : THandle;
  Creator : TCreateQuadRender;
begin
  h := LoadLibrary(LibraryName);
  Creator := GetProcAddress(h, CreateQuadRenderProcName);
  if Assigned(Creator) then
  Result := Creator;
end;

// Creating of Quad window interface object
function CreateWindow : IQuadWindow;
var
  h : THandle;
  Creator : TCreateQuadWindow;
begin
  h := LoadLibrary(LibraryName);
  Creator := GetProcAddress(h, CreateQuadWindowProcName);
  if Assigned(Creator) then
  Result := Creator;
end;

end.
