From: iantw@castle.ed.ac.uk (Ian Tweedie)
Newsgroups: rec.games.programmer
Subject: ANY ONE GOT A GOOD STARFIELD?
Date: 15 Sep 92 18:00:47 GMT
Distribution: rec
Organization: Edinburgh University

I wrote this simple and very basic starfield in TURBO PASCAL V6.0

The only problem is I WANT MORE STARS AND LESS PROCESSOR TIME USED. I
have tried to write one in assembler but am only half-way there. Can
anyone provide either ASM code of BETTER TP6 CODE? Take this as a
challenge?

________________________ CODE ___________ TPC VECTSTR.PAS________________

program starfield_using_vectors;
uses crt;

const nostars = 50;
      maxX = 25000;
      maxY = 20000;

type points = record
                 X : integer;
                 Y : integer;
                 z : integer;
                 oldx : integer;
                 oldy : integer;
              end;

var p : array[1..nostars] of points;
    counter : integer;
    X,Y : integer;
    dec,loop : integer;
    ch : char;

procedure vidmod; assembler;
asm
   mov ax,13h
   int 10h
end;


procedure plot_point(pt :integer; col : byte); assembler;
asm
                mov     ax,0a000h
                mov     es,ax
                mov     al,col
                mov     bx,pt
                mov     es:[bx],al
end;

procedure newpts(loop : integer);
begin
     p[loop].X := random(maxX);
     while p[loop].X < 1160 do p[loop].X := random(maxX);
     p[loop].Y := random(maxY);
     p[loop].Z := 32010;
end;

procedure setintrandoms;
var loop : integer;
begin
     for loop := 1 to nostars do newpts(loop);
end;

procedure chkpts(X,Y,loop : integer);
var divider : integer;
begin
     if (X < 0) or (X > 320) or
        (Y < 0) or (Y > 200) or
        (p[loop].Z < 160)        then
        begin
             newpts(loop);
             p[loop].X := p[loop].x-(MaxX div 2) ;
             p[loop].Y := p[loop].Y-(MaxY div 2) ;
             divider := round(p[loop].Z / 320);
             X := ((p[loop].X) div divider)+160;
             Y := ((p[loop].Y) div divider)+100;
        end;

end;

procedure maptoscreen;
var loop : integer;
    divider : integer;
    col : word;
    z : real;
begin
     for loop := 1 to nostars do
     begin
          plot_point(p[loop].oldX+(p[loop].oldY*320),0);
          p[loop].X := p[loop].x-(MaxX div 2);
          p[loop].Y := p[loop].Y-(MaxY div 2);
          divider := round(p[loop].Z / 320);
          if divider = 0 then inc(divider);
          X := 160 + (p[loop].X) div divider;
          if x = 0 then x := 1;
          Y := 100 + (p[loop].Y) div divider;
          y := y + (y div 4);
          chkpts(X,Y,loop);
          p[loop].X := p[loop].X + (MaxX div 2);
          p[loop].Y := p[loop].Y+  (MaxY div 2);
          col := 34-((p[loop].Z) div 2134);
          if col > 31 then col := 31;
          plot_point(X+(Y*320),col);
          p[loop].oldx := X;
          p[loop].oldy := Y;
     end;
end;

procedure incZ;
var loop : integer;
begin
     for loop := 1 to nostars do
     begin
          p[loop].Z := p[loop].Z - dec;
     end;
end;

procedure chkkey;

begin
     ch := readkey;
     case ch of
          chr(45) : if dec <> 0 then dec := dec -10;
          chr(43) : dec := dec +10;
     end;
end;


begin
     counter := 29999;
     dec := 600;
     ch := 'h';
     vidmod;
     randomize;
     setintrandoms;
     maptoscreen;
     repeat
           counter := counter +1;
           incZ;
           maptoscreen;
           if keypressed then chkkey;
     until ch = chr(27);
     textmode(co80);
     writeln('Coded by lamanga..... Apr 1992');
end.


-- 
Ian Tweedie Yes! THE Ian Tweedie |"There is a justice higher than that of man,
AI,CS Undergraduate		 |      I will be judged by him." Misery 1991
Janet    : iat@dcs.ed.ac.uk      |"My cousin was a Harley Davidson"
Internet : iantw@castle.ed.ac.uk |      Johnny Five, Short Circuit 2 1991
Xref: sparky rec.games.programmer:3741 comp.os.msdos.programmer:9171