0
votes

So I'm a complete newbie to programming, and would be appreciative of any help/'pointing' in the right direction. If not much is required, then I'd appreciate it if someone could actually post the solution.

Basically, I'm creating a projectile motion simulator. The simulator works perfectly, however I have one slight problem: the number of points I need to connect in my program vary. At the moment, I'm just using a huge array size (eg of 10,000) to compensate. Really I'd like a cleaner way of doing this. Now I'm sure you're all thinking: dynamic arrays! However, when trying to use dynamic arrays, I get pages and pages of errors. I'm told it's because the Windows API functions don't have access to the dynamic array's memory heap. Here's my current code:

procedure TNewPageControl.PaintWindow(DC : HDC);
  var
    Points : Array [1..10000] of TPoint;
    NumOfPoints : integer;
  begin
    {Generate Point Co-Ordinates and Increment NumOfPoints}
    PolyLine(DC, Points, NumOfPoints);
  end;   

As I said, whilst my code works perfectly, I'd just like a more sophisticated solution than what I have at the moment. I've been told I can use pointers (which I know nothing about) to act almost like a dynamic array which the Windows function can understand. Does anyone know how I could do this?

I'm using Lazarus, if that makes any difference. If I don't get any answers here, I'll try asking on the Lazarus forums.

Thanks :)

1

1 Answers

2
votes

You can use dynamic arrays safely with API calls as long as they're of a data type that is compatible. In the case of TPoint, the type is actually a Windows API type, and therefore is perfectly capable of being passed to PolyLine:

procedure TNewPageControl.PaintWindow(DC : HDC);
var
  Points : array of TPoint;
  NumOfPoints : integer;
begin
  {Generate Point Co-Ordinates and Increment NumOfPoints, 
   using SetLength(Points) there to size array properly}

  // Pass reference to first element of array
  PolyLine(DC, Points[0], NumOfPoints); 
end;

In fact, since the number of points needed for PolyLine is a constant, NumOfPoints isn't needed at all:

PolyLine(DC, Points[0], Length(Points));