Previous: 4.51.2.4.39. ogIsBlending() To the Table of Contents Next: 4.51.2.4.41. ogLoadPal()
4.51.2.4.39. ogIsBlending() Table of Contents 4.51.2.4.41. ogLoadPal()

- 4.51.2.4.40. -
Table of Contents
4. Standard Units
4.51. ObjGfx40 - ObjectGraphics 4.0 Unit
4.51.2. ObjGfx40 Unit Object Types
4.51.2.4. ogSurface object
4.51.2.4.40. ogLine()


4.51.2.4.40. ogLine()

Targets: MS-DOS, Win32 console


ObjGfx40 Unit

Draws a line from the point (x1, y1) to (x2, y2).

Declaration:
  procedure ogLine(x1, y1, x2, y2:int32; colour:uInt32); virtual;
Remarks:

Draws a line from (x1,y1) to (x2,y2). Will draw an anti-aliased line if antiAlias is set to TRUE. For 8BPP surfaces, this can be very slow.

See also: Sample code:
{ogLine.pas}

(*
 * Original program by:
 * Brian Tomkins / Kable
 * Finished 11-13-96
 *)

uses
  ObjGfx40, CRT;

const SPEED               = 4;
const PLUS                = 1;
const MINUS               = 0;
const MAX_COLORS          = 256;
const MAX_COLOR_PALETTES  = 8;

type
  ogLinePoint = record
    x, y, x1, y1 : int32;
    dirx, diry   : int32;
    dirx1, diry1 : int32;
  end;

type
  ogLinefire = object (newSurface)
   public
    constructor ogInit;
    procedure averagePix;
    procedure change_pals(pal_num:int32);
    procedure set_colors;
    procedure run;
    destructor ogDone; virtual;
   private
    palettes           : array [ 0..MAX_COLOR_PALETTES-1 ] of ogPal;
    chkflg             : boolean;
    point              : ogLinePoint;
  end;

procedure ogLineFire.averagepix;
var 
  bufPtr:pointer;
begin
 // ogGetPtr() is a virtual method and doing it in assembler might
 // be confusing
  bufPtr:=ogGetPtr(1,1);
asm
        mov     eax, self
        push    ebp
        mov     ebp, eax
        call    ogGetMaxX
        mov     edx, eax
        pop     ebp

        mov     eax, self
        push    ebp
        mov     ebp, eax
        call    ogGetMaxY
        dec     eax
        mov     ecx, eax
        pop     ebp

        inc     edx
        mov     esi, bufPtr
        mov     ebx, edx

@@olop: push    ecx
        push    edx
        push    esi

@@ilop:
        xor     eax,eax
        xor     ecx,ecx
        mov     al,[esi-1]
        add     ecx,eax
        mov     al,[esi+1]
        add     ecx,eax
        neg     ebx
        mov     al,[esi+ebx]
        neg     ebx
        add     ecx,eax
        mov     al,[esi+ebx]
        add     ecx,eax
        shr     ecx,2
        mov     [esi],cl
        inc     esi
        dec     edx
         jnz    @@ilop
        pop     esi
        add     esi,ebx
        pop     edx
        pop     ecx
        dec     ecx
         jnz    @@olop
end;
end;

constructor ogLinefire.ogInit;
begin
 inherited ogInit;
 randomize;
 
 if not screen^.ogCreate(800, 600, OG_PIXFMT_8BPP) then
   begin
     writeln('Error setting video mode');
     halt
   end;
  ogClone(screen^);
  buf.ogSetAntiAliasing(FALSE);
  
  point.x  := random(ogGetMaxX);
  point.y  := random(ogGetMaxY);
  point.x1 := random(ogGetMaxX);
  point.y1 := random(ogGetMaxY);
  
  point.dirx := PLUS;
  point.diry := MINUS;
  point.dirx1 := MINUS;
  point.diry1 := PLUS;
  set_colors;
end;

procedure ogLinefire.change_pals(pal_num:int32);
var
  ctr:int32;
  r, g, b:uInt8;
begin
  chkflg:=TRUE;
  for ctr:=0 to MAX_COLORS-1 do
    begin
      ogUnpackRGB(ctr, r, g, b);

      if (r < palettes[pal_num][ctr].red) then
        inc(r)
      else
        if (r > palettes[pal_num][ctr].red) then dec(r);

      if (g < palettes[pal_num][ctr].green) then
        inc(g)
      else
        if (g > palettes[pal_num][ctr].green) then dec(g);

      if (b < palettes[pal_num][ctr].blue) then
        inc(b)
      else
        if (b > palettes[pal_num][ctr].blue) then dec(b);

      ogSetRGBPalette(ctr, r, g, b);

      chkflg := chkflg and ((r = palettes[pal_num][ctr].red) and
                            (g = palettes[pal_num][ctr].green) and
                            (b = palettes[pal_num][ctr].blue));
    end;

  screen^.ogCopyPal(self);
end;

procedure ogLinefire.set_colors;
var 
  index, ctr:uInt32;
begin

  index:=0;
  for ctr:=0 to 63 do
    begin
      ogSetRGBPalette(index,   ctr*4+3, 0, 0);
      ogSetRGBPalette(index+1, ctr*4+3, 0, 0);
      ogSetRGBPalette(index+2, ctr*4+3, 0, 0);
      ogSetRGBPalette(index+3, ctr*4+3, 0, 0);
      inc(index,4);
    end;

  screen^.ogCopyPal(self);
  
  index:=0;
  for ctr:=0 to 63 do
    begin
      palettes[0][index].red     := 0;
      palettes[0][index+1].red   := 0;
      palettes[0][index+2].red   := 0;
      palettes[0][index+3].red   := 0;
      palettes[0][index].green   := ctr*4+3;      {green}
      palettes[0][index+1].green := ctr*4+3;
      palettes[0][index+2].green := ctr*4+3;
      palettes[0][index+3].green := ctr*4+3;
      palettes[0][index].blue    := 0;
      palettes[0][index+1].blue  := 0;
      palettes[0][index+2].blue  := 0;
      palettes[0][index+3].blue  := 0;
      inc(index,4);
    end;

  index:=0;
  for ctr:=0 to 63 do
    begin
      palettes[1][index].red     := 0;
      palettes[1][index+1].red   := 0;
      palettes[1][index+2].red   := 0;
      palettes[1][index+3].red   := 0;
      palettes[1][index].green   := 0;
      palettes[1][index+1].green := 0;
      palettes[1][index+2].green := 0;
      palettes[1][index+3].green := 0;
      palettes[1][index].blue    := ctr*4+3;
      palettes[1][index+1].blue  := ctr*4+3;      {blue}
      palettes[1][index+2].blue  := ctr*4+3;
      palettes[1][index+3].blue  := ctr*4+3;
      inc(index,4);
    end;

  index:=0;
  for ctr:=0 to 63 do
    begin
      palettes[2][index].red     := ctr*4+3;
      palettes[2][index+1].red   := ctr*4+3;
      palettes[2][index+2].red   := ctr*4+3;
      palettes[2][index+3].red   := ctr*4+3;
      palettes[2][index].green   := ctr*4+3;
      palettes[2][index+1].green := ctr*4+3;      {white}
      palettes[2][index+2].green := ctr*4+3;
      palettes[2][index+3].green := ctr*4+3;
      palettes[2][index].blue    := ctr*4+3;
      palettes[2][index+1].blue  := ctr*4+3;
      palettes[2][index+2].blue  := ctr*4+3;
      palettes[2][index+3].blue  := ctr*4+3;
      inc(index,4);
    end;

  index:=0;
  for ctr:=0 to 63 do
    begin
      palettes[3][index].red     := ctr*4+3;      {red+green}
      palettes[3][index+1].red   := ctr*4+3;
      palettes[3][index+2].red   := ctr*4+3;
      palettes[3][index+3].red   := ctr*4+3;
      palettes[3][index].green   := ctr*4+3;
      palettes[3][index+1].green := ctr*4+3;
      palettes[3][index+2].green := ctr*4+3;
      palettes[3][index+3].green := ctr*4+3;
      palettes[3][index].blue    := 0;
      palettes[3][index+1].blue  := 0;
      palettes[3][index+2].blue  := 0;
      palettes[3][index+3].blue  := 0;
      inc(index,4);
    end;

  index:=0;
  for ctr:=0 to 63 do
    begin
      palettes[4][index].red     := ctr*4+3;      {red+blue}
      palettes[4][index+1].red   := ctr*4+3;
      palettes[4][index+2].red   := ctr*4+3;
      palettes[4][index+3].red   := ctr*4+3;
      palettes[4][index].green   := 0;
      palettes[4][index+1].green := 0;
      palettes[4][index+2].green := 0;
      palettes[4][index+3].green := 0;
      palettes[4][index].blue    := ctr*4+3;
      palettes[4][index+1].blue  := ctr*4+3;
      palettes[4][index+2].blue  := ctr*4+3;
      palettes[4][index+3].blue  := ctr*4+3;
      inc(index,4);
    end;

  index:=0;
  for ctr:=0 to 63 do
    begin
      palettes[5][index].red     := 0;
      palettes[5][index+1].red   := 0;
      palettes[5][index+2].red   := 0;
      palettes[5][index+3].red   := 0;
      palettes[5][index].green   := ctr*4+3;
      palettes[5][index+1].green := ctr*4+3;
      palettes[5][index+2].green := ctr*4+3;
      palettes[5][index+3].green := ctr*4+3;
      palettes[5][index].blue    := ctr*4+3;
      palettes[5][index+1].blue  := ctr*4+3;      {green+blue}
      palettes[5][index+2].blue  := ctr*4+3;
      palettes[5][index+3].blue  := ctr*4+3;
      inc(index,4);
    end;
end;

procedure ogLineFire.Run;
var 
  v, xx, yy, temp, temp1, temp2, temp3,
  randd1, randd2, randd3, randd4, 
  randd5, randd6, randd7, randd8:int32;
    
begin
  randd1 := (random(32000) mod 4)+1;
  randd2 := (random(32000) mod 1)+1;
  randd3 := (random(32000) mod 2)+1;
  randd4 := (random(32000) mod 3)+1;
  randd5 := (random(32000) mod 3)+1;
  randd6 := (random(32000) mod 4)+1;
  randd7 := (random(32000) mod 2)+1;
  randd8 := (random(32000) mod 3)+1;

  v := 0;

  repeat
    change_pals(v);
    if (chkflg) then v := random(10) mod 6;
    
    if (point.x > buf.ogGetMaxX-10) then
      begin
        point.dirx := MINUS;
        randd1 := (random(32000) mod SPEED)+1;
      end;

    if (point.x < 10) then
      begin
        point.dirx := PLUS;
        randd2 := (random(32000) mod SPEED)+1;
      end;

    if (point.y > buf.ogGetmaxY-10) then
      begin
        point.diry := MINUS;
        randd3 := (random(32000) mod SPEED)+1;
      end;

    if (point.y < 10) then
      begin
        point.diry := PLUS;
        randd4 := (random(32000) mod SPEED)+1;
      end;

    if (point.x1 > buf.ogGetMaxX-10) then
      begin
        point.dirx1 := MINUS;
        randd5 := (random(32000) mod SPEED)+1;
      end;

    if (point.x1 < 10) then
      begin
        point.dirx1 := PLUS;
        randd6 := (random(32000) mod SPEED)+1;
      end;
      
    if (point.y1 > buf.ogGetMaxY-10 ) then
      begin
        point.diry1 := MINUS;
        randd7 := (random(32000) mod SPEED)+1;
      end;
      
    if (point.y1 < 10) then
      begin
        point.diry1 := PLUS;
        randd8 := (random(32000) mod SPEED)+1;
      end;

    {move the points}
    if (point.dirx = PLUS) then inc(point.x, randd1);
    if (point.dirx = MINUS) then dec(point.x, randd2);
    if (point.diry = PLUS) then inc(point.y, randd3);
    if (point.diry = MINUS) then dec(point.y, randd4);
 
    if (point.dirx1 = PLUS) then inc(point.x1, randd5);
    if (point.dirx1 = MINUS) then dec(point.x1, randd6);
    if (point.diry1 = PLUS) then inc(point.y1, randd7);
    if (point.diry1 = MINUS) then dec(point.y1, randd8);

    with point do
      begin
        ogLine(x, y, x1, y1, 245);
        ogLine(ogGetMaxX-x,ogGetMaxY-y,ogGetMaxX-x1,ogGetMaxY-y1,245);
        ogLine(x+1, y+1, x1+1, y1+1, 245);
        ogLine(ogGetMaxX-x+1, ogGetMaxY-y+1, ogGetMaxX-x1+1, ogGetmaxY-y1+1, 245);
        ogRect(0, 0, ogGetMaxX, ogGetMaxX, 0)
      end;
    averagepix;
    screen^.ogCopy(self);
  until keyPressed;

  while keyPressed do readKey
end;

destructor ogLineFire.ogDone;
begin
  inherited ogDone
end;

var 
  demo:^ogLineFire;
begin
  new(demo, ogInit);
  demo^.run;
  dispose(demo, ogDone)
end.



Previous: 4.51.2.4.39. ogIsBlending() To the Table of Contents Next: 4.51.2.4.41. ogLoadPal()
4.51.2.4.39. ogIsBlending() Table of Contents 4.51.2.4.41. ogLoadPal()

- 4.51.2.4.40. -