unit RoadGraphic;

interface
  uses  windows, RoadSys, classes;
  type
    TGraphicFont  = class
      Font  : TFont;
      procedure DrawText2D(X,Y : Single; Text : String);virtual; abstract;
      procedure DrawText3D(X,Y,Z : Single; Text : String);virtual; abstract;
      destructor Destroy;override;
    end;

    TBaseRender  =  class
      DC    : HDC;
      procedure DrawTriangles(VertexBuffer : pointer; NormalBuffer : pointer; TexBuffer : pointer; TrianglesCount : integer);virtual;abstract;
      procedure DrawQuads(VertexBuffer : pointer; NormalBuffer : pointer; TexBuffer : pointer; TrianglesCount : integer);virtual;abstract;
      function CreateFont(FontName : string; FontHeight : integer) : TGraphicFont;virtual;abstract;
      function CreateVectorFont(FontName : string; FontHeight : integer) : TGraphicFont;virtual;abstract;
      procedure InitRender(Handle : Cardinal; vDC  : HDC);virtual;abstract;
      procedure FreeRender;virtual;abstract;
      procedure SetRenderContext;virtual;abstract;
      procedure FreeRenderContext;virtual;abstract;
      procedure ClearBuffer;virtual;abstract;
      procedure SetPerspective;virtual;abstract;
      procedure ResetProjectionMatrix;virtual;abstract;
      procedure ResetModelViewMatrix;virtual;abstract;
      procedure LookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ: Single);virtual;abstract;
      procedure SwapBuffers;virtual;abstract;
    end;


    TGraphicClass = class of TGraphicBase;
    TGraphicBase  = class(TBaseObject)
      Parent      : pointer;
      constructor Create(pParent : pointer);virtual;
      procedure Draw;virtual;abstract;
    end;



  TBitmap  = class
    width,
    height,
    bpp       : integer;
    bits      : pointer;
    DC        : HDC;
    Handle    : HBitmap;
    constructor Create(iWidth, iHeight, iBPP : integer);overload;
    constructor Create;overload;
    procedure Draw(targetDC : HDC; X,Y : integer);
    procedure StretchDraw(targetDC : HDC; X,Y : integer; iWidth, iHeight : integer);
    procedure LoadFromFile(sFileName : string);
    procedure SaveToFile(sFileName : string);
    private
      procedure SwapRGB(data : Pointer; Size : Integer);
  end;


implementation
  uses  RoadGlobal;
{ TGLRender }



procedure TBitmap.SwapRGB(data : Pointer; Size : Integer);
asm
  mov ebx, eax
  mov ecx, size

@@loop :
  mov al,[ebx+0]
  mov ah,[ebx+2]
  mov [ebx+2],al
  mov [ebx+0],ah
  add ebx,3
  dec ecx
  jnz @@loop
end;

constructor TBitmap.Create(iWidth, iHeight, iBPP: integer);
var
  p2    : TBitmapInfo;
begin
  width   :=  iWidth;
  height  :=  iHeight;
  bpp     :=  iBPP;
  DC      :=  CreateCompatibleDC(GetDC(0));
  ZeroMemory(@p2,SizeOf(p2));
  with p2.bmiHeader do
    begin
      biSize  :=  SizeOf(p2.bmiHeader);
      biWidth :=  width;
      biHeight  :=  height;
      biPlanes  :=  1;
      biBitCount  :=  bpp;
      biCompression :=  BI_RGB;
      biSizeImage   :=  width*height*bpp div 8;
      biXPelsPerMeter :=  0;
      biYPelsPerMeter :=  0;
      biClrUsed       :=  0;
      biClrImportant  :=  0;
    end;
  Handle  :=  CreateDIBSection(DC,p2, DIB_RGB_COLORS,bits,0,0);
  SelectObject(DC,Handle);
end;

constructor TBitmap.Create;
begin

end;

procedure TBitmap.LoadFromFile(sFileName: string);
var
  pbmpFH : PBitmapFileHeader;
  pbmpIH : PBitmapInfoHeader;
  F     : TMappedFile;
  p2    : TBitmapInfo;
begin
  F :=  TMappedFile.Create;
  F.MapFileCopyWrite(sFileName);
  pbmpFH :=  F.Memory;
  pbmpIH  :=  pointer(integer(pbmpFH)+SizeOf(TBitmapFileHeader));
  SwapRGB(pointer(integer(pbmpFH)+pbmpFH^.bfOffBits),pbmpIH^.biWidth*pbmpIH^.biHeight);
  width   :=  pbmpIH^.biWidth;
  height  :=  pbmpIH^.biHeight;
  bpp     :=  pbmpIH^.biBitCount;
  DC      :=  CreateCompatibleDC(GetDC(0));
  ZeroMemory(@p2,SizeOf(p2));
  with p2.bmiHeader do
    begin
      biSize  :=  SizeOf(p2.bmiHeader);
      biWidth :=  width;
      biHeight  :=  height;
      biPlanes  :=  1;
      biBitCount  :=  bpp;
      biCompression :=  BI_RGB;
      biSizeImage   :=  width*height*bpp div 8;
      biXPelsPerMeter :=  0;
      biYPelsPerMeter :=  0;
      biClrUsed       :=  0;
      biClrImportant  :=  0;
    end;
  Handle  :=  CreateDIBSection(DC,p2, DIB_RGB_COLORS,bits,0,0);
  SelectObject(DC,Handle);
  CopyMemory(bits,pointer(integer(pbmpFH)+pbmpFH^.bfOffBits),p2.bmiHeader.biSizeImage);
  F.UnMapFile;
  F.Free;
end;

procedure TBitmap.SaveToFile(sFileName: string);
var
  pbmpFH : PBitmapFileHeader;
  pbmpIH : PBitmapInfoHeader;
  F     : TMappedFile;
  p2    : TBitmapInfo;
begin
{  F :=  TMappedFile.Create;
  F.MapFile(sFileName,GENERIC_READ or GENERIC_WRITE);
  F.FileSize  :=  SizeOf(pbmpFH)+SizeOf(pbmpIH)+(width*height*bpp div 8);
  pbmpFH :=  F.Memory;
  pbmpIH  :=  pointer(integer(pbmpFH)+SizeOf(TBitmapFileHeader));
  SwapRGB(pointer(integer(pbmpFH)+pbmpFH^.bfOffBits),pbmpIH^.biWidth*pbmpIH^.biHeight);
  width   :=  pbmpIH^.biWidth;
  height  :=  pbmpIH^.biHeight;
  bpp     :=  pbmpIH^.biBitCount;
  DC      :=  CreateCompatibleDC(GetDC(0));
  ZeroMemory(@p2,SizeOf(p2));
  with p2.bmiHeader do
    begin
      biSize  :=  SizeOf(p2.bmiHeader);
      biWidth :=  width;
      biHeight  :=  height;
      biPlanes  :=  1;
      biBitCount  :=  bpp;
      biCompression :=  BI_RGB;
      biSizeImage   :=  width*height*bpp div 8;
      biXPelsPerMeter :=  0;
      biYPelsPerMeter :=  0;
      biClrUsed       :=  0;
      biClrImportant  :=  0;
    end;
  Handle  :=  CreateDIBSection(DC,p2, DIB_RGB_COLORS,bits,0,0);
  SelectObject(DC,Handle);
  CopyMemory(bits,pointer(integer(pbmpFH)+pbmpFH^.bfOffBits),p2.bmiHeader.biSizeImage);
  F.UnMapFile;
  F.Free;}
end;

procedure TBitmap.Draw(targetDC: HDC; X, Y: integer);
begin
  BitBlt(targetDC,X,Y,width,height,DC,0,0,SRCCOPY);
end;

procedure TBitmap.StretchDraw(targetDC: HDC; X, Y, iWidth,
  iHeight: integer);
begin
  StretchBlt(targetDC,X,Y,iWidth,iHeight,DC,0,0,Width,Height,SRCCOPY);
end;

{ TGraphicFont }

destructor TGraphicFont.Destroy;
begin
  Font.Free;
  inherited;
end;

{ TGraphicBase }

constructor TGraphicBase.Create(pParent: pointer);
begin
  Parent  :=  pParent;
end;

end.
