unit uMeSprite;

{$mode delphi}

interface

uses
  Classes, SysUtils, zglHeader;

type

  { TDRect }

  TDRect = record
    R : zglTRect;
    dX, dY, dW, dH : Integer;
  end;

{ TMeSprite }

TMeSprite = class
  X, Y, W, H, NUM : Integer;
  DR : array of TDRect;
  Texture : zglPTexture;
  constructor Create(pTexture : zglPTexture; pW, pH: Integer);
  function Add(pX, pY : Integer) : Integer; overload;
  function Add(pX, pY, pW, pH : Integer) : Integer; overload;

  procedure Draw(pX, pY, pT : Integer); overload;
  procedure DrawCB(pX, pY, pT : Integer); overload;
  procedure DrawC(pX, pY, pT : Integer); overload;
  procedure Draw(pX, pY, pT : Integer; pA : Byte); overload;
end;

implementation

{ TMeSprite }

constructor TMeSprite.Create(pTexture: zglPTexture; pW, pH: Integer);
begin
  X := - pW div 2;
  Y := - pH;
  W := pW;
  H := pH;
  Texture := pTexture;
end;

function TMeSprite.Add(pX, pY : Integer) : Integer;
var
  I : Integer;
begin
  I := Length(DR);
  SetLength(DR, I + 1);
  DR[I].R.X := pX;
  DR[I].R.Y := pY;
  DR[I].R.W := W;
  DR[I].R.H := H;
  DR[I].dX := X;
  DR[I].dY := Y;
  DR[I].dW := W;
  DR[I].dH := H;
  Result := I;
end;

function TMeSprite.Add(pX, pY, pW, pH: Integer): Integer;
var
  I : Integer;
begin
  I := Add(pX, pY);
  DR[I].R.W := pW;
  DR[I].R.H := pH;
  DR[I].dW  := pW;
  DR[I].dH  := pH;
  DR[I].dX := 0;//- pW div 2;
  DR[I].dY := - pH;
end;

procedure TMeSprite.Draw(pX, pY, pT: Integer);
begin
  with DR[pT] do
    csprite2d_Draw(Texture, pX, pY, dW, dH, 0, R, 255);
end;

procedure TMeSprite.DrawCB(pX, pY, pT: Integer);
begin
  with DR[pT] do
    csprite2d_Draw(Texture, pX, pY - dH, dW, dH, 0, R, 255);
end;

procedure TMeSprite.Draw(pX, pY, pT: Integer; pA: Byte);
begin
  with DR[pT] do
    csprite2d_Draw(Texture, pX, pY, dW, dH, 0, R, pA);
end;

procedure TMeSprite.DrawC(pX, pY, pT: Integer);
begin
  with DR[pT] do
    csprite2d_Draw(Texture, pX + X, pY + Y div 2, dW, dH, 0, R, 255);
end;

end.

