smarthaert / phoenixlib

Automatically exported from code.google.com/p/phoenixlib
0 stars 0 forks source link

DrawArc is very Slow #79

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Game.Device.SetTexture(nil);
Game.Canvas.Pen.Style := psSolid;
Game.Canvas.Pen.Color := clrSilver;
Game.Canvas.Brush.Color := Color4f(clrRed, 0.8);
Game.Canvas.Arc(FBarraPoderes.FLeftInterface.X + 54, 
FBarraPoderes.FLeftInterface.Y + 126,20,20,0,180);

This make my game FPS drop from 200 to 20.

Original issue reported on code.google.com by wagenhei...@gmail.com on 23 Sep 2010 at 12:32

GoogleCodeExporter commented 9 years ago
Here is how FillArc works in Asphyre Engine. It is a lot faster there!

//---------------------------------------------------------------------------
procedure TAsphyreCanvas.FillArc(const Pos, Radius: TPoint2; InitPhi,
 EndPhi: Single; Steps: Integer; const Colors: TColor4;
 Effect: TDrawingEffect);
var
 Vertices: array of TPoint2;
 VColors : array of Cardinal;
 Indices : array of Integer;
 Pt1, Pt2: TPoint2;
 cs: TAsphyreColor4;
 i: Integer;
 Alpha: Single;
 xAlpha, yAlpha: Integer;
 NoVertex: Integer;
begin
 if (Steps < 1) then Exit;

 // (1) Convert 32-bit RGBA colors to fixed-point color set.
 cs:= ColorToFixed4(Colors);

 // (2) Find (x, y) margins for color interpolation.
 Pt1:= Pos - Radius;
 Pt2:= Pos + Radius;

 // (3) Before doing anything else, check cache availability.
 SetLength(Vertices, Steps + 2);
 SetLength(VColors, Length(Vertices));
 SetLength(Indices, Steps * 3);

 NoVertex:= 0;

 // (4) Insert initial vertex placed at the arc's center
 Vertices[NoVertex]:= Pos;
 VColors[NoVertex] := (cs[0] + cs[1] + cs[2] + cs[3]) * 0.25;
 Inc(NoVertex);

 // (5) Insert the rest of vertices
 for i:= 0 to Steps - 1 do
  begin
   // initial and final angles for this vertex
   Alpha:= (i * (EndPhi - InitPhi) / Steps) + InitPhi;

   // determine second and third points of the processed vertex
   Vertices[NoVertex].x:= Pos.x + Cos(Alpha) * Radius.x;
   Vertices[NoVertex].y:= Pos.y - Sin(Alpha) * Radius.y;

   // find color interpolation values
   xAlpha:= Round((Vertices[NoVertex].x - Pt1.x) * 255.0 / (Pt2.x - Pt1.x));
   yAlpha:= Round((Vertices[NoVertex].y - Pt1.y) * 255.0 / (Pt2.y - Pt1.y));

   VColors[NoVertex]:= cBlend(cBlend(cs[0], cs[1], xAlpha),
    cBlend(cs[3], cs[2], xAlpha), yAlpha);

   // insert new index buffer entry
   Indices[(i * 3) + 0]:= 0;
   Indices[(i * 3) + 1]:= NoVertex;
   Indices[(i * 3) + 2]:= NoVertex + 1;

   Inc(NoVertex);
  end;

 // find the latest vertex to finish the arc
 Vertices[NoVertex].x:= Pos.x + Cos(EndPhi) * Radius.x;
 Vertices[NoVertex].y:= Pos.y - Sin(EndPhi) * Radius.y;

 // find color interpolation values
 xAlpha:= Round((Vertices[NoVertex].x - Pt1.x) * 255.0 / (Pt2.x - Pt1.x));
 yAlpha:= Round((Vertices[NoVertex].y - Pt1.y) * 255.0 / (Pt2.y - Pt1.y));

 VColors[NoVertex]:= cBlend(cBlend(cs[0], cs[1], xAlpha),
  cBlend(cs[3], cs[2], xAlpha), yAlpha);

 DrawIndexedTriangles(@Vertices[0], @VColors[0], @Indices[0], Length(Vertices),
  Steps, Effect);
end;

procedure TOGLCanvas.DrawIndexedTriangles(Vertices: PPoint2; Colors: PCardinal;
 Indices: PInteger; NoVertices, NoTriangles: Integer; Effect: TDrawingEffect);
var
 i, i0, i1, i2: Integer;
 Vertex: PPoint2;
 Color : PCardinal;
begin
 RequestEffect(Effect);
 RequestTexture(nil);
 RequestScene(ccmTris);

 for i:= 0 to NoTriangles - 1 do
  begin
   i0:= Indices^; Inc(Indices);

   i1:= Indices^; Inc(Indices);

   i2:= Indices^; Inc(Indices);

   // Vertex 0
   Vertex:= Vertices;
   Inc(Vertex, i0);

   Color:= Colors;
   Inc(Color, i0);

   AddPointGL(Vertex^, Color^);

   // Vertex 1
   Vertex:= Vertices;
   Inc(Vertex, i1);

   Color:= Colors;
   Inc(Color, i1);

   AddPointGL(Vertex^, Color^);

   // Vertex 2
   Vertex:= Vertices;
   Inc(Vertex, i2);

   Color:= Colors;
   Inc(Color, i2);

   AddPointGL(Vertex^, Color^);
  end;
end;

Original comment by wagenhei...@gmail.com on 23 Sep 2010 at 12:51

GoogleCodeExporter commented 9 years ago
Did some optimisations to remove some function call and lowered resolution of 
the arc and the render time went from 3 ms to 0,08ms 3700% speed increase!

Original comment by andreas....@gmail.com on 11 Nov 2010 at 8:45

GoogleCodeExporter commented 9 years ago

Original comment by andreas....@gmail.com on 11 Nov 2010 at 8:47