Recent

Author Topic: First it's a square, then a butterfly? and then finally a star field flying by  (Read 224 times)

TBMan

  • Full Member
  • ***
  • Posts: 135
I eventually got to the star field I wanted...   lmao

Code: Pascal  [Select][+][-]
  1. program starfielddemo;
  2.  
  3. uses
  4.   ptccrt,
  5.   ptcgraph,sysutils;
  6.  
  7.  
  8. const
  9.   MaxStars = 200;
  10.  
  11.  
  12. Type
  13.  
  14.    VideoPages = (Page0, Page1);
  15.  
  16.   IchangeType = record
  17.        xc,yc:double;
  18.   end;
  19.  
  20.   IArrayType = array[0..360] of IChangeType;
  21.  
  22.   StarType = record
  23.     xposition,yposition,
  24.     xc,yc:double;
  25.     color:integer;
  26.   end;
  27.  
  28. var
  29.   InertiaTable:IArrayType;
  30.   Page: VideoPages = Page0;
  31.   gdriver,gmode:smallint;
  32.   Index:integer;
  33.   RightLStars,
  34.   LeftLStars,
  35.   RightUStars,
  36.   LeftUstars:array[0..MaxStars] of startype;
  37.  
  38.  
  39.  
  40. {***************** Page flipping**************}
  41.  
  42.  procedure Nextactivepage;
  43.   begin
  44.     if page = page0 then page := page1
  45.     else
  46.       page := page0;
  47.       SetactivePage(Ord(Page));
  48.   end;
  49.  
  50. procedure NextVisualPage;
  51.   begin
  52.     SetVisualpage(Ord(Page));
  53.   end;
  54.  
  55. {*********  Timer ****************************}
  56.  procedure frametick2(Milliseconds:integer);
  57. var
  58.   scount,ecount:qword;
  59. begin
  60.   scount := getTickCount64;
  61.   repeat
  62.     ecount := gettickcount64;
  63.   until ecount > scount+Milliseconds;
  64. end;
  65.  
  66. {*************  Inertia Table *******************}
  67. Procedure BuildInertiaTable;
  68.  
  69. const
  70.     CoordIncrement = 0.01111111111;
  71.  
  72. var
  73.    index:integer;
  74.    Xchange,
  75.    Ychange,
  76.    Xaccum,
  77.    YAccum:double ;
  78. begin
  79. fillchar(InertiaTable,sizeof(InertiaTable),0);// ? is this needed?
  80. Ychange :=  CoordIncrement;
  81.  
  82. YAccum := -1;
  83. InertiaTable[0].yc := -1.0;
  84. Inertiatable[360].yc := -1.0;
  85. InertiaTable[0].xc := 0;
  86. Inertiatable[360].xc := 0;
  87. Xaccum := 0;
  88. xchange := -CoordIncrement;
  89. // build portion  to 90 degrees left the yc decreases to 0 , x builds up to -1
  90. for index := 359 downto 270 do
  91. begin
  92.    inertiatable[index].yc := YAccum;
  93.    inertiatable[index].xc := XAccum;
  94.    Xaccum := Xaccum+Xchange;
  95.    YAccum := YAccum+Ychange;
  96. end;
  97.  
  98. // now y will increase to 1 and x will increase to 0;
  99. Ychange := CoordIncrement;
  100. xchange := CoordIncrement;
  101. xaccum := -1.0;
  102. Yaccum :=  0.0;
  103. for index := 269 downto 180 do
  104. begin
  105.    inertiatable[index].yc := YAccum;
  106.    inertiatable[index].xc := XAccum;
  107.    Yaccum := Yaccum+ychange;
  108.    Xaccum := Xaccum+xchange;
  109.  end;
  110.  
  111. Yaccum := 1;
  112. Xaccum := 0;
  113. Xchange := CoordIncrement;
  114. YChange := -CoordIncrement;
  115.  
  116. // now y will decrease from 1 to 0 and x will increase from 0 to 1;
  117. for index := 179 downto 90 do
  118. begin
  119.    inertiatable[index].yc := YAccum;
  120.    inertiatable[index].xc := XAccum;
  121.    Yaccum := Yaccum+ychange;
  122.    Xaccum := Xaccum+xchange;
  123. end;
  124.  
  125. // now y will decrease from 0 to -1 and x will decrease from 1 to 0;
  126.  
  127. Xaccum := 1;
  128. Yaccum := 0;
  129. Xchange := -coordincrement;
  130. Ychange := -coordincrement;
  131.  for index := 89 downto 0 do
  132. begin
  133.    inertiatable[index].yc := YAccum;
  134.    inertiatable[index].xc := XAccum;
  135.    Yaccum := Yaccum+ychange;
  136.    Xaccum := Xaccum+xchange;
  137.  
  138. end;
  139.  
  140. end;
  141.  
  142. { *************  Make initial "stars" ****************}
  143.  
  144. procedure seedStars;
  145. var
  146.   d :integer;
  147.   i:integer;
  148. begin
  149.    // do left upper quad
  150.    for i := 1 to MaxStars do
  151.    begin
  152.       d := random(90)+270;
  153.       with LeftUstars[i] do
  154.             begin
  155.                xposition := 400;
  156.                yposition := 300;
  157.                xc := inertiatable[d].xc;
  158.                yc := inertiatable[d].yc;
  159.                color :=random(18)+1;
  160.             end;
  161.    end;
  162.    // upper right
  163.     for i := 1 to MaxStars do
  164.    begin
  165.           d := random(90);
  166.       with RightUstars[i] do
  167.             begin
  168.                xposition := 400;
  169.                yposition := 300;
  170.                xc := inertiatable[d].xc;
  171.                yc := inertiatable[d].yc;
  172.                color :=random(18)+1;
  173.             end;
  174.    end;
  175. // lower left
  176.  for i := 1 to MaxStars do
  177.    begin
  178.           d := random(90)+180;
  179.       with LeftLstars[i] do
  180.             begin
  181.                xposition := 400;
  182.                yposition := 300;
  183.                xc := inertiatable[d].xc;
  184.                yc := inertiatable[d].yc;
  185.                color := random(18)+1;
  186.             end;
  187.    end;
  188.  
  189.  //lower right
  190.  for i := 1 to MaxStars do
  191.    begin
  192.           d := random(90)+90;
  193.       with RightLstars[i] do
  194.             begin
  195.                xposition := 400;
  196.                yposition := 300;
  197.                xc := inertiatable[d].xc;
  198.                yc := inertiatable[d].yc;
  199.                color :=random(18)+1;
  200.             end;
  201.    end;
  202.  
  203. end;
  204.  
  205. {**********  Animate the stars **************}
  206.  
  207. procedure AnimateStars(i:integer);
  208.  begin
  209.    with LeftUstars[i] do
  210.         begin
  211.           putpixel(round(xposition),round(Yposition),color);
  212.           xposition := xposition+xc;
  213.           yposition := yposition+yc;
  214.           if (xposition <=0) or (Yposition <=0) then
  215.              begin
  216.              xposition := 400;
  217.              yposition := 300;
  218.              end;
  219.         end;
  220.     with RightUstars[i] do
  221.         begin
  222.           putpixel(round(xposition),round(Yposition),color);
  223.           xposition := xposition+xc;
  224.           yposition := yposition+yc;
  225.           if (xposition >=getmaxx) or (Yposition <=0) then
  226.              begin
  227.              xposition := 400;
  228.              yposition := 300;
  229.              end;
  230.         end;
  231.    with LeftLstars[i] do
  232.         begin
  233.           putpixel(round(xposition),round(Yposition),color);
  234.           xposition := xposition+xc;
  235.           yposition := yposition+yc;
  236.           if (xposition <0 ) or (Yposition >=getmaxy) then
  237.              begin
  238.              xposition := 400;
  239.              yposition := 300;
  240.  
  241.              end;
  242.         end;
  243.    with RightLstars[i] do
  244.         begin
  245.           putpixel(round(xposition),round(Yposition),color);
  246.           xposition := xposition+xc;
  247.           yposition := yposition+yc;
  248.           if (xposition >getmaxx ) or (Yposition >=getmaxy) then
  249.              begin
  250.              xposition := 400;
  251.              yposition := 300;
  252.              end;
  253.         end;
  254.  end;
  255.  
  256. begin
  257.  
  258.   gdriver := vesa;
  259.  
  260.   // uses two video pages
  261.  
  262.   Gmode := installusermode(800, 600, 256, 2, 8000, 6000);
  263.   WindowTitle := 'StarField';
  264.   initgraph(gdriver, gmode, '');
  265.  
  266.   BuildInertiatable;
  267.   randomize;
  268.   SeedStars;
  269.  
  270. repeat
  271.   nextactivepage;
  272.   clearviewport;
  273.    for index := 0 to maxstars do animateStars(index);
  274.    nextvisualpage;
  275.    frametick2(10);
  276. until keypressed;;
  277.  
  278. Closegraph;
  279.  
  280. end.
  281.  

 

TinyPortal © 2005-2018