Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use slice function instead of SliceDynArray. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 15 additions & 74 deletions bgrabitmaptypes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,21 @@ function CreateBGRAImageWriter(AFormat: TBGRAImageFormat; AHasTransparentPixels:

{$IFDEF BDS}
type
ArrayofBGRASingle = array of Single;
Arrayofword = array of BGRAWord;

procedure DynArrayToOpenArray(rOpen : array of TPointF; rDyn : array of TPointF);
function SliceDynArray(rOpen : array of TPointF; Count : integer): arrayofTPointF; overload;
function SliceDynArray(rOpen : array of TBGRAPixel; Count : integer): ArrayOfTBGRAPixel; overload;
function SliceDynArray(rOpen : array of Single; Count : Integer): ArrayofBGRASingle; overload;
function SliceDynArray(rOpen : array of BGRAWord; Count : Integer): Arrayofword; overload;
function SliceDynArray(rOpen : array of TColorF; Count : integer): arrayofTColorF; overload;
// help types for calling slice function in Delphi.
THackArrayOfTPointF= array[0..MaxInt div SizeOf(TPointF)-1] of TPointF;
PHackArrayOfTPointF=^THackArrayOfTPointF;

THackArrayOfTBGRAPixel= array[0..MaxInt div SizeOf(TBGRAPixel)-1] of TBGRAPixel;
PHackArrayOfTBGRAPixel=^THackArrayOfTBGRAPixel;

THackArrayOfTColorF= array[0..MaxInt div SizeOf(TColorF)-1] of TColorF;
PHackArrayOfTColorF=^THackArrayOfTColorF;

THackArrayOfSingle= array[0..MaxInt div SizeOf(Single)-1] of Single;
PHackArrayOfSingle=^THackArrayOfSingle;

THackArrayOfBGRAWord= array[0..MaxInt div SizeOf(BGRAWord)-1] of BGRAWord;
PHackArrayOfBGRAWord=^THackArrayOfBGRAWord;
{$ENDIF}


Expand All @@ -528,71 +534,6 @@ implementation
{$ENDIF}
BGRAWritePNG;

{$IFDEF BDS}
function SliceDynArray(rOpen : array of TColorF; Count : integer): arrayofTColorF;
var
i : integer;
Open : arrayofTColorF;
begin
SetLength(Open, Length(rOpen));
for i := 0 to High(rOpen) do
Open[i] := rOpen[i];
Result := Copy(Open, 0, Count);
end;

function SliceDynArray(rOpen : array of TBGRAPixel; Count : integer): ArrayOfTBGRAPixel;
var
i : integer;
Open : ArrayOfTBGRAPixel;
begin
SetLength(Open, Length(rOpen));
for i := 0 to High(rOpen) do
Open[i] := rOpen[i];
Result := Copy(Open, 0, Count);
end;

procedure DynArrayToOpenArray(rOpen : array of TPointF; rDyn : array of TPointF);
var
i : integer;
begin
for i := 0 to High(rDyn) do
rOpen[i] := rDyn[i];
end;

function SliceDynArray(rOpen : array of TPointF; Count : integer): arrayofTPointF;
var
i : integer;
Open : arrayofTPointF;
begin
SetLength(Open, Length(rOpen));
for i := 0 to High(rOpen) do
Open[i] := rOpen[i];
Result := Copy(Open, 0, Count);
end;

function SliceDynArray(rOpen : array of Single; Count : Integer): ArrayofBGRASingle;
var
i : integer;
Open : ArrayofBGRASingle;
begin
SetLength(Open, Length(rOpen));
for i := 0 to High(rOpen) do
Open[i] := rOpen[i];
Result := Copy(Open, 0, Count);
end;

function SliceDynArray(rOpen : array of BGRAWord; Count : Integer): Arrayofword;
var
i : integer;
Open : Arrayofword;
begin
SetLength(Open, Length(rOpen));
for i := 0 to High(rOpen) do
Open[i] := rOpen[i];
Result := Copy(Open, 0, Count);
end;
{$ENDIF}

{$DEFINE INCLUDE_IMPLEMENTATION}
{$I geometrytypes.inc}

Expand Down
70 changes: 57 additions & 13 deletions bgracanvas2d.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ procedure TBGRACanvas2D.roundRect(x, y, w, h, rx, ry: single);

procedure TBGRACanvas2D.openedSpline(const pts: array of TPointF;
style: TSplineStyle);
var transf: {$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
var transf:{$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
begin
if length(pts)=0 then exit;
transf := ApplyTransform(pts);
Expand All @@ -2155,18 +2155,26 @@ procedure TBGRACanvas2D.closedSpline(const pts: array of TPointF;
begin
if length(pts)=0 then exit;
transf := ApplyTransform(pts);
transf := BGRAPath.ComputeClosedSpline({$IFDEF BDS}SliceDynArray(transf{$ELSE}slice(transf{$ENDIF}, length(transf)-1),style);
{$IFDEF BDS}
transf := BGRAPath.ComputeClosedSpline(slice(PHackArrayOfTPointF(transf)^, length(transf)-1),style);
{$ELSE}
transf := BGRAPath.ComputeClosedSpline(slice(transf, length(transf)-1),style);
{$ENDIF}
AddPoints(transf);
FLastCoord := pts[high(pts)];
end;

procedure TBGRACanvas2D.spline(const pts: array of TPointF; style: TSplineStyle);
var transf: {$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
var transf:{$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
begin
if length(pts)=0 then exit;
transf := ApplyTransform(pts);
if (pts[0] = pts[high(pts)]) and (length(pts) > 1) then
transf := BGRAPath.ComputeClosedSpline({$IFDEF BDS}SliceDynArray(transf{$ELSE}slice(transf{$ENDIF}, length(transf)-1),style)
{$IFDEF BDS}
transf := BGRAPath.ComputeClosedSpline(slice(PHackArrayOfTPointF(transf)^, length(transf)-1),style)
{$ELSE}
transf := BGRAPath.ComputeClosedSpline(slice(transf, length(transf)-1),style)
{$ENDIF}
else
transf := BGRAPath.ComputeOpenedSpline(transf,style);
AddPoints(transf);
Expand Down Expand Up @@ -2362,31 +2370,51 @@ function TBGRACanvas2D.measureText(AText: string): TCanvas2dTextSize;
procedure TBGRACanvas2D.fill;
begin
if FPathPointCount = 0 then exit;
FillPoly({$IFDEF BDS}SliceDynArray(FPathPoints{$ELSE}slice(FPathPoints{$ENDIF},FPathPointCount));
{$IFDEF BDS}
FillPoly(slice(PHackArrayOfTPointF(FPathPoints)^,FPathPointCount));
{$ELSE}
FillPoly(slice(FPathPoints,FPathPointCount));
{$ENDIF}
end;

procedure TBGRACanvas2D.stroke;
begin
if FPathPointCount = 0 then exit;
StrokePoly({$IFDEF BDS}SliceDynArray(FPathPoints{$ELSE}slice(FPathPoints{$ENDIF},FPathPointCount));
{$IFDEF BDS}
StrokePoly(slice(PHackArrayOfTPointF(FPathPoints)^,FPathPointCount));
{$ELSE}
StrokePoly(slice(FPathPoints,FPathPointCount));
{$ENDIF}
end;

procedure TBGRACanvas2D.fillOverStroke;
begin
if FPathPointCount = 0 then exit;
FillStrokePoly({$IFDEF BDS}SliceDynArray(FPathPoints{$ELSE}slice(FPathPoints{$ENDIF},FPathPointCount),true);
{$IFDEF BDS}
FillStrokePoly(slice(PHackArrayOfTPointF(FPathPoints)^,FPathPointCount),true);
{$ELSE}
FillStrokePoly(slice(FPathPoints,FPathPointCount),true);
{$ENDIF}
end;

procedure TBGRACanvas2D.strokeOverFill;
begin
if FPathPointCount = 0 then exit;
FillStrokePoly({$IFDEF BDS}SliceDynArray(FPathPoints{$ELSE}slice(FPathPoints{$ENDIF},FPathPointCount),false);
{$IFDEF BDS}
FillStrokePoly(slice(PHackArrayOfTPointF(FPathPoints)^,FPathPointCount),false);
{$ELSE}
FillStrokePoly(slice(FPathPoints,FPathPointCount),false);
{$ENDIF}
end;

procedure TBGRACanvas2D.clearPath;
begin
if FPathPointCount = 0 then exit;
ClearPoly({$IFDEF BDS}SliceDynArray(FPathPoints{$ELSE}slice(FPathPoints{$ENDIF},FPathPointCount));
{$IFDEF BDS}
ClearPoly(slice(PHackArrayOfTPointF(FPathPoints)^,FPathPointCount));
{$ELSE}
ClearPoly(slice(FPathPoints,FPathPointCount));
{$ENDIF}
end;

procedure TBGRACanvas2D.clip;
Expand All @@ -2402,9 +2430,17 @@ procedure TBGRACanvas2D.clip;
currentState.SetClipMask(surface.NewBitmap(width,height,BGRAWhite),True);
tempBmp := surface.NewBitmap(width,height,BGRABlack);
if antialiasing then
tempBmp.FillPolyAntialias({$IFDEF BDS}SliceDynArray(FPathPoints{$ELSE}slice(FPathPoints{$ENDIF},FPathPointCount),BGRAWhite)
{$IFDEF BDS}
tempBmp.FillPolyAntialias(slice(PHackArrayOfTPointF(FPathPoints)^,FPathPointCount),BGRAWhite)
{$ELSE}
tempBmp.FillPolyAntialias(slice(FPathPoints,FPathPointCount),BGRAWhite)
{$ENDIF}
else
tempBmp.FillPoly({$IFDEF BDS}SliceDynArray(FPathPoints{$ELSE}slice(FPathPoints{$ENDIF},FPathPointCount),BGRAWhite,dmSet);
{$IFDEF BDS}
tempBmp.FillPoly(slice(PHackArrayOfTPointF(FPathPoints)^,FPathPointCount),BGRAWhite,dmSet);
{$ELSE}
tempBmp.FillPoly(slice(FPathPoints,FPathPointCount),BGRAWhite,dmSet);
{$ENDIF}
currentState.clipMaskReadWrite.BlendImage(0,0,tempBmp,boDarken);
tempBmp.Free;
end;
Expand All @@ -2414,9 +2450,17 @@ procedure TBGRACanvas2D.unclip;
if FPathPointCount = 0 then exit;
if currentState.clipMaskReadOnly = nil then exit;
if antialiasing then
currentState.clipMaskReadWrite.FillPolyAntialias({$IFDEF BDS}SliceDynArray(FPathPoints{$ELSE}slice(FPathPoints{$ENDIF},FPathPointCount),BGRAWhite)
{$IFDEF BDS}
currentState.clipMaskReadWrite.FillPolyAntialias(slice(PHackArrayOfTPointF(FPathPoints)^,FPathPointCount),BGRAWhite)
{$ELSE}
currentState.clipMaskReadWrite.FillPolyAntialias(slice(FPathPoints,FPathPointCount),BGRAWhite)
{$ENDIF}
else
currentState.clipMaskReadWrite.FillPoly({$IFDEF BDS}SliceDynArray(FPathPoints{$ELSE}slice(FPathPoints{$ENDIF},FPathPointCount),BGRAWhite,dmSet);
{$IFDEF BDS}
currentState.clipMaskReadWrite.FillPoly(slice(PHackArrayOfTPointF(FPathPoints)^,FPathPointCount),BGRAWhite,dmSet);
{$ELSE}
currentState.clipMaskReadWrite.FillPoly(slice(FPathPoints,FPathPointCount),BGRAWhite,dmSet);
{$ENDIF}
if currentState.clipMaskReadOnly.Equals(BGRAWhite) then
currentState.SetClipMask(nil,true);
end;
Expand Down
52 changes: 42 additions & 10 deletions bgraopengl3d.pas
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ function TBGLRenderer3D.RenderFace(var ADescription: TFaceRenderingDescription;
end;
PtCenter3D := PtCenter3D *(1/NbVertices);
Normalize3D_128(NormalCenter3D);
ColorCenter := MergeBGRA({$IFDEF BDS}SliceDynArray(Colors{$ELSE}slice(Colors{$ENDIF},NbVertices));
ColorCenter := MergeBGRA({$IFDEF BDS}slice(PHackArrayOfTBGRAPixel(Colors)^{$ELSE}slice(Colors{$ENDIF},NbVertices));
end;
end;

Expand Down Expand Up @@ -944,9 +944,17 @@ function TBGLRenderer3D.RenderFace(var ADescription: TFaceRenderingDescription;
Positions3D[i].z := -Positions3D[i].z;

if NbVertices = 3 then
tex.DrawTriangle({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},3),{$IFDEF BDS}SliceDynArray(TexCoords{$ELSE}slice(TexCoords{$ENDIF},3),{$IFDEF BDS}SliceDynArray(FShadedColorsF{$ELSE}slice(FShadedColorsF{$ENDIF},3))
{$IFDEF BDS}
tex.DrawTriangle(slice(PHackArrayOfTPoint3D_128(Positions3D)^,3),slice(PHackArrayOfTPointF(TexCoords)^,3),slice(PHackArrayOfTColorF(FShadedColorsF)^,3))
{$ELSE}
tex.DrawTriangle(slice(Positions3D,3),slice(TexCoords,3),slice(FShadedColorsF,3))
{$ENDIF}
else if NbVertices = 4 then
tex.DrawQuad({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},4),{$IFDEF BDS}SliceDynArray(TexCoords{$ELSE}slice(TexCoords{$ENDIF},4),{$IFDEF BDS}SliceDynArray(FShadedColorsF{$ELSE}slice(FShadedColorsF{$ENDIF},4));
{$IFDEF BDS}
tex.DrawQuad(slice(PHackArrayOfTPoint3D_128(Positions3D)^,4),slice(PHackArrayOfTPointF(TexCoords)^,4),slice(PHackArrayOfTColorF(FShadedColorsF)^,4));
{$ELSE}
tex.DrawQuad(slice(Positions3D,4),slice(TexCoords,4),slice(FShadedColorsF,4));
{$ENDIF}
end else
begin
for i := 0 to NbVertices-1 do
Expand All @@ -956,9 +964,17 @@ function TBGLRenderer3D.RenderFace(var ADescription: TFaceRenderingDescription;
end;

if NbVertices = 3 then
tex.DrawTriangle({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},3),{$IFDEF BDS}SSESliceDynArray(Normals3D{$ELSE}slice(Normals3D{$ENDIF},3),{$IFDEF BDS}SliceDynArray(TexCoords{$ELSE}slice(TexCoords{$ENDIF},3))
{$IFDEF BDS}
tex.DrawTriangle(slice(PHackArrayOfTPoint3D_128(Positions3D)^,3),slice(PHackArrayOfTPoint3D_128(Normals3D)^,3),slice(PHackArrayOfTPointF(TexCoords)^,3))
{$ELSE}
tex.DrawTriangle(slice(Positions3D,3),slice(Normals3D,3),slice(TexCoords,3))
{$ENDIF}
else if NbVertices = 4 then
tex.DrawQuad({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},4),{$IFDEF BDS}SSESliceDynArray(Normals3D{$ELSE}slice(Normals3D{$ENDIF},4),{$IFDEF BDS}SliceDynArray(TexCoords{$ELSE}slice(TexCoords{$ENDIF},4));
{$IFDEF BDS}
tex.DrawQuad(slice(PHackArrayOfTPoint3D_128(Positions3D)^,4),slice(PHackArrayOfTPoint3D_128(Normals3D)^,4),slice(PHackArrayOfTPointF(TexCoords)^,4));
{$ELSE}
tex.DrawQuad(slice(Positions3D,4),slice(Normals3D,4),slice(TexCoords,4));
{$ENDIF}
end;
end
else
Expand All @@ -977,7 +993,7 @@ function TBGLRenderer3D.RenderFace(var ADescription: TFaceRenderingDescription;
if NbVertices > 4 then
begin
ComputeCenter;
ColorCenter := FBGRAShader.Apply(PtCenter3D,NormalCenter3D,MergeBGRA({$IFDEF BDS}SliceDynArray(Colors{$ELSE}slice(Colors{$ENDIF},NbVertices)));
ColorCenter := FBGRAShader.Apply(PtCenter3D,NormalCenter3D,MergeBGRA({$IFDEF BDS}slice(PHackArrayOfTBGRAPixel(Colors)^{$ELSE}slice(Colors{$ENDIF},NbVertices)));

for i := 0 to NbVertices-1 do
Positions3D[i].z := -Positions3D[i].z;
Expand All @@ -997,9 +1013,17 @@ function TBGLRenderer3D.RenderFace(var ADescription: TFaceRenderingDescription;
Positions3D[i].z := -Positions3D[i].z;

if NbVertices = 3 then
FCanvas.FillTrianglesLinearColor({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},3),{$IFDEF BDS}SliceDynArray(FShadedColors{$ELSE}slice(FShadedColors{$ENDIF},3))
{$IFDEF BDS}
FCanvas.FillTrianglesLinearColor(slice(PHackArrayOfTPoint3D_128(Positions3D)^,3),slice(PHackArrayOfTBGRAPixel(FShadedColors)^,3))
{$ELSE}
FCanvas.FillTrianglesLinearColor(slice(Positions3D,3),slice(FShadedColors,3))
{$ENDIF}
else if NbVertices = 4 then
FCanvas.FillQuadsLinearColor({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},4),{$IFDEF BDS}SliceDynArray(FShadedColors{$ELSE}slice(FShadedColors{$ENDIF},4));
{$IFDEF BDS}
FCanvas.FillQuadsLinearColor(slice(PHackArrayOfTPoint3D_128(Positions3D)^,4),slice(PHackArrayOfTBGRAPixel(FShadedColors)^,4));
{$ELSE}
FCanvas.FillQuadsLinearColor(slice(Positions3D,4),slice(FShadedColors,4));
{$ENDIF}
end;
end else
begin
Expand All @@ -1025,9 +1049,17 @@ function TBGLRenderer3D.RenderFace(var ADescription: TFaceRenderingDescription;
end else
begin
if NbVertices = 3 then
FCanvas.FillTrianglesLinearColor({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},3),{$IFDEF BDS}SSESliceDynArray(Normals3D{$ELSE}slice(Normals3D{$ENDIF},3),{$IFDEF BDS}SliceDynArray(Colors{$ELSE}slice(Colors{$ENDIF},3))
{$IFDEF BDS}
FCanvas.FillTrianglesLinearColor(slice(PHackArrayOfTPoint3D_128(Positions3D)^,3),slice(PHackArrayOfTPoint3D_128(Normals3D)^,3),slice(PHackArrayOfTBGRAPixel(Colors)^,3))
{$ELSE}
FCanvas.FillTrianglesLinearColor(slice(Positions3D,3),slice(Normals3D,3),slice(Colors,3))
{$ENDIF}
else if NbVertices = 4 then
FCanvas.FillQuadsLinearColor({$IFDEF BDS}SSESliceDynArray(Positions3D{$ELSE}slice(Positions3D{$ENDIF},4),{$IFDEF BDS}SSESliceDynArray(Normals3D{$ELSE}slice(Normals3D{$ENDIF},4),{$IFDEF BDS}SliceDynArray(Colors{$ELSE}slice(Colors{$ENDIF},4));
{$IFDEF BDS}
FCanvas.FillQuadsLinearColor(slice(PHackArrayOfTPoint3D_128(Positions3D)^,4),slice(PHackArrayOfTPoint3D_128(Normals3D)^,4),slice(PHackArrayOfTBGRAPixel(Colors)^,4));
{$ELSE}
FCanvas.FillQuadsLinearColor(slice(Positions3D,4),slice(Normals3D,4),slice(Colors,4));
{$ENDIF}
end;
end;
end;
Expand Down
12 changes: 9 additions & 3 deletions bgrapen.pas
Original file line number Diff line number Diff line change
Expand Up @@ -835,9 +835,9 @@ function ComputeWidePolylinePoints(const linepts: array of TPointF; width: singl
len: single;
leftDir: TPointF;
end;
compPts: {$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
compPts: array of TPointF;
nbCompPts: integer;
revCompPts: {$IFDEF BDS}arrayofTPointF{$ELSE}array of TPointF{$ENDIF};
revCompPts: array of TPointF;
nbRevCompPts: integer;
pts: array of TPointF;
roundPrecision: integer;
Expand Down Expand Up @@ -999,7 +999,13 @@ function ComputeWidePolylinePoints(const linepts: array of TPointF; width: singl
-borders[high(pts)-1].leftSide.dir, false,true);
end;
posstyle := 0;
ApplyPenStyle({$IFDEF BDS}SliceDynArray(compPts{$ELSE}slice(compPts{$ENDIF},nbCompPts),{$IFDEF BDS}SliceDynArray(revCompPts{$ELSE}slice(revCompPts{$ENDIF},nbRevCompPts),penstyle,width,posstyle,enveloppe);
{$IFDEF BDS}
/// works ok
// ApplyPenStyle(slice(PHackArrayOfTPointF(@compPts[0])^,nbCompPts),slice(PHackArrayOfTPointF(@revCompPts[0])^,nbRevCompPts),penstyle,width,posstyle,enveloppe);
ApplyPenStyle(slice(PHackArrayOfTPointF(compPts)^,nbCompPts),slice(PHackArrayOfTPointF(revCompPts)^,nbRevCompPts),penstyle,width,posstyle,enveloppe);
{$ELSE}
ApplyPenStyle(slice(compPts,nbCompPts),slice(revCompPts,nbRevCompPts),penstyle,width,posstyle,enveloppe);
{$ENDIF}
if Result=nil then
begin
Result := enveloppe;
Expand Down
Loading