unit SpanBuf;

interface

procedure SP_Setup(Min,Max,Spans:integer);
procedure SP_AddSpan(SpanNum,Min,Max:integer);register;
procedure SP_DrawVerticalSpans(cindex : Byte);

implementation

uses
  Windows, SysUtils, FrameBuf;
  
const
  MAX_SPANS = 2048;

type
  TSpan = array[0..1] of integer;

var
  SpanMax : integer;
  SpanMin : integer;
  NumSpans : integer;
  SpanBuffer : array[0..MAX_SPANS] of TSpan;

procedure SP_Clear;
var
  i : integer;
begin
  // Invalidate every span line
  i:=NumSpans-1;
  while i>=0 do
  begin
    SpanBuffer[i][0]:=SpanMax;
    SpanBuffer[i][1]:=SpanMin;
    i := i-1;
  end;
end;

procedure SP_Setup(Min,Max,Spans:integer);
begin
  if Spans < 0 then
    Spans := 0;
  if Spans > MAX_SPANS then
  begin
    MessageBox(GetForegroundWindow(),
      PChar('SP_Setup(): Spans='+IntToStr(Spans)+
      ' > MAX_SPANS='+IntToStr(MAX_SPANS)+'.'+#13+
      'Increase MAX_SPANS and recompile.'),
      'Oscilloscope: Fatal error:',
      MB_ICONHAND);
    Halt(1);
  end;
  NumSpans := Spans;
  SpanMin := Min;
  SpanMax := Max;
  SP_Clear;
end;

procedure SP_AddSpan(SpanNum,Min,Max:integer);register;
begin
  if (SpanNum < 0) or (SpanNum >= NumSpans) then
    exit;
  if SpanBuffer[SpanNum][0] > Min then
    SpanBuffer[SpanNum][0] := Min;
  if SpanBuffer[SpanNum][1] < Max then
    SpanBuffer[SpanNum][1] := Max;
end;

procedure SP_DrawVerticalSpans(cindex : Byte);
var
  x,y,y_max : integer;
begin
  x:=NumSpans-1;
  while x>=0 do
  begin
    // draw next span
    y := SpanBuffer[x][0];
    y_max := SpanBuffer[x][1];
    while y <= y_max do
    begin
      FB_DrawClippedPixel(x,y,cindex);
      y:=y+1;
    end;
    x:=x-1;
  end;
end;

end.
