Recent

Author Topic: New 2D StarField  (Read 1368 times)

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
New 2D StarField
« on: February 28, 2025, 04:39:00 pm »
Hi,
To test my new Ryzen 7 labtop here is a nice 8 playfield Amiga Style 2D starfield with speed and
H/V scrolling mode .

I will update this code to make a little Intro with module player.
For now here is the code Using BGRA component like always.
Maybe Usefull for you :)

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  9.   BGRAVirtualScreen, BGRABitmap, BGRABitmapTypes;
  10.  
  11. const
  12.  Version = '0.2';
  13.  
  14. type
  15.  
  16.   TStarfieldMode = (smVertical, smHorizontal);
  17.  
  18.   TStar = record
  19.     X, Y: Single;      // Pos x,y
  20.     Speed: Single;     // speed
  21.     Color: TBGRAPixel; // Color
  22.     Plane: Integer;    // Planes
  23.   end;
  24.  
  25.   { TForm1 }
  26.  
  27.   TForm1 = class(TForm)
  28.     BGRAVirtualScreen: TBGRAVirtualScreen;
  29.     Button1SpeedDown: TButton;
  30.     ButtonSpeedUp: TButton;
  31.     ButtonHorizontal: TButton;
  32.     ButtonVertical: TButton;
  33.     PanelConrols: TPanel;
  34.     Timer1: TTimer;
  35.  
  36.     procedure FormCreate(Sender: TObject);
  37.     procedure FormDestroy(Sender: TObject);
  38.     procedure Timer1Timer(Sender: TObject);
  39.     procedure BGRAVirtualScreenRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  40.     procedure ButtonVerticalClick(Sender: TObject);
  41.     procedure ButtonHorizontalClick(Sender: TObject);
  42.     procedure ButtonSpeedUpClick(Sender: TObject);
  43.     procedure ButtonSpeedDownClick(Sender: TObject);
  44.  
  45.   private
  46.  
  47.     FStars: array of TStar;
  48.     ScrollMode: TStarfieldMode;
  49.     SpeedMultiplier: Single;
  50.     procedure InitStars;
  51.     procedure UpdateStars;
  52.  
  53.   public
  54.  
  55.   end;
  56.  
  57. var
  58.   Form1: TForm1;
  59.  
  60. implementation
  61.  
  62. {$R *.lfm}
  63.  
  64.  
  65. { TForm1 }
  66.  
  67. procedure TForm1.InitStars;
  68. const
  69.   // 8 plans
  70.   STAR_COUNT: array[0..7] of Integer = (300, 250, 200, 150, 100, 75, 50, 25); //1150 stars
  71.   STAR_SPEED: array[0..7] of Double = (0.3, 0.5, 0.8, 1.2, 1.8, 2.5, 3.5, 5);
  72. var
  73.   i, j, MaxStars, CStar: Integer;
  74. begin
  75.  
  76.   MaxStars := 0;
  77.   for i := 0 to 7 do
  78.     Inc(MaxStars, STAR_COUNT[i]);
  79.  
  80.   SetLength(FStars, MaxStars);
  81.  
  82.   CStar := 0;
  83.   for i := 0 to 7 do
  84.   begin
  85.     for j := 0 to STAR_COUNT[i]-1 do
  86.     begin
  87.       FStars[CStar].X := Random(BGRAVirtualScreen.Width);
  88.       FStars[CStar].Y := Random(BGRAVirtualScreen.Height);
  89.       FStars[CStar].Speed := STAR_SPEED[i];
  90.       FStars[CStar].Color := BGRA(255, 255, 255, 255);  //  pas important
  91.       FStars[CStar].Plane := i;
  92.       Inc(CStar);
  93.     end;
  94.   end;
  95. end;
  96.  
  97. procedure TForm1.UpdateStars;
  98. var
  99.   i: Integer;
  100.   WW, HH: Integer;
  101. begin
  102.   WW := BGRAVirtualScreen.Width;
  103.   HH := BGRAVirtualScreen.Height;
  104.  
  105.   for i := 0 to Length(FStars) - 1 do
  106.   begin
  107.     case ScrollMode of
  108.  
  109.       smVertical:
  110.         begin
  111.           FStars[i].Y := FStars[i].Y + FStars[i].Speed * SpeedMultiplier;
  112.           if SpeedMultiplier > 0 then
  113.           begin
  114.             if FStars[i].Y > HH then
  115.             begin
  116.               FStars[i].Y := 0;
  117.               FStars[i].X := Random(WW);
  118.             end;
  119.           end
  120.           else
  121.           begin
  122.             if FStars[i].Y < 0 then
  123.             begin
  124.               FStars[i].Y := HH;
  125.               FStars[i].X := Random(WW);
  126.             end;
  127.           end;
  128.         end;
  129.  
  130.       smHorizontal:
  131.         begin
  132.           FStars[i].X := FStars[i].X + FStars[i].Speed * SpeedMultiplier;
  133.           if SpeedMultiplier > 0 then
  134.           begin
  135.             if FStars[i].X > WW then
  136.             begin
  137.               FStars[i].X := 0;
  138.               FStars[i].Y := Random(HH);
  139.             end;
  140.           end
  141.           else
  142.           begin
  143.  
  144.             if FStars[i].X < 0 then
  145.             begin
  146.               FStars[i].X := WW;
  147.               FStars[i].Y := Random(HH);
  148.             end;
  149.           end;
  150.         end;
  151.     end;
  152.   end;
  153.  
  154.   BGRAVirtualScreen.RedrawBitmap;  // refresh
  155. end;
  156.  
  157. procedure TForm1.BGRAVirtualScreenRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  158. var
  159.   i: Integer;
  160.   col: TBGRAPixel;
  161.   brightness: Byte;
  162. begin
  163.  
  164.   for i := 0 to Length(FStars) - 1 do
  165.   begin
  166.  
  167.     brightness := 40 + Round(215 * (FStars[i].Speed - 0.3) / 4.7);
  168.     col := BGRA(brightness, brightness, brightness);
  169.     Bitmap.FillRect(
  170.       Round(FStars[i].X),
  171.       Round(FStars[i].Y),
  172.       Round(FStars[i].X) + 2,
  173.       Round(FStars[i].Y) + 2,
  174.       col
  175.     );
  176.   end;
  177. end;
  178.  
  179. procedure TForm1.FormCreate(Sender: TObject);
  180. begin
  181.  
  182.   ScrollMode := smHorizontal;  // init mode H/V
  183.   SpeedMultiplier := 0.5;      // speed
  184.  
  185.   Caption := 'BGRA 8-PlayField StarField - GIGATRON 2025';
  186.   BGRAVirtualScreen.Color := clBlack;
  187.  
  188.   Randomize;
  189.   InitStars;
  190.  
  191. end;
  192.  
  193. procedure TForm1.FormDestroy(Sender: TObject);
  194. begin
  195.      SetLength(FStars, 0);
  196. end;
  197.  
  198. procedure TForm1.Timer1Timer(Sender: TObject);
  199. begin
  200.      UpdateStars;
  201. end;
  202.  
  203. procedure TForm1.ButtonVerticalClick(Sender: TObject);
  204. begin
  205.     ScrollMode := smVertical;
  206. end;
  207.  
  208. procedure TForm1.ButtonHorizontalClick(Sender: TObject);
  209. begin
  210.     ScrollMode := smHorizontal;
  211. end;
  212.  
  213. procedure TForm1.ButtonSpeedUpClick(Sender: TObject);
  214. begin
  215.   if SpeedMultiplier < 5 then SpeedMultiplier := SpeedMultiplier + 0.5;
  216. end;
  217.  
  218. procedure TForm1.ButtonSpeedDownClick(Sender: TObject);
  219. begin
  220.   if SpeedMultiplier > -5 then SpeedMultiplier := SpeedMultiplier - 0.5;
  221. end;
  222.  
  223.  
  224. end.
  225.  
Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #1 on: March 01, 2025, 04:38:56 pm »
Perfect

Now let's transform this 2D Starfiled to 3D Starfield with Left/Right Rotation, the result is really good !!

You think that the code is long, but in the past the demos on Amiga were more than 20,000 lines of code so 260 lines of code understandable by humans is nothing. Plus everything was in 68000 assembler!!

Regards

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  9.   BGRAVirtualScreen, BGRABitmap, BGRABitmapTypes, Math;
  10.  
  11. const
  12.   Version = '0.3';
  13.  
  14. type
  15.   TStarfieldMode = (smForward, smBackward);
  16.  
  17.   TStar = record
  18.     X, Y, Z: Single;     // x,y,z
  19.     Speed: Double;       // Speed
  20.     Color: TBGRAPixel;   // Color
  21.   end;
  22.  
  23.   { TForm1 }
  24.  
  25.   TForm1 = class(TForm)
  26.     BGRAVirtualScreen: TBGRAVirtualScreen;
  27.     ButtonResetR: TButton;
  28.     ButtonRotateLeft: TButton;
  29.     ButtonRotateRight: TButton;
  30.     ButtonBackward: TButton;
  31.     ButtonForward: TButton;
  32.     Button1SpeedDown: TButton;
  33.     ButtonSpeedUp: TButton;
  34.     PanelConrols: TPanel;
  35.     PanelControls: TPanel;
  36.     Timer1: TTimer;
  37.  
  38.     procedure ButtonResetRClick(Sender: TObject);
  39.     procedure ButtonRotateRightClick(Sender: TObject);
  40.     procedure FormCreate(Sender: TObject);
  41.     procedure FormDestroy(Sender: TObject);
  42.     procedure Timer1Timer(Sender: TObject);
  43.     procedure BGRAVirtualScreenRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  44.     procedure ButtonForwardClick(Sender: TObject);
  45.  
  46.     procedure ButtonBackwardClick(Sender: TObject);
  47.     procedure ButtonRotateLeftClick(Sender: TObject);
  48.     procedure ButtonSpeedUpClick(Sender: TObject);
  49.     procedure ButtonSpeedDownClick(Sender: TObject);
  50.  
  51.   private
  52.     FStars: array of TStar;
  53.     ScrollMode: TStarfieldMode;
  54.     SpeedMultiplier: Single;
  55.     RotationSpeed: Single;
  56.     CenterX, CenterY: Integer;
  57.     MaxZ, MinZ, FocusZ: Single;
  58.  
  59.     procedure InitStars;
  60.     procedure UpdateStars;
  61.     procedure RotatePoint(var X, Y: Single; Angle: Single);
  62.     function Project3Dto2D(X, Y, Z: Double; out ScX, ScY: Integer; out Size: Integer): Boolean;
  63.  
  64.   public
  65.  
  66.   end;
  67.  
  68. var
  69.   Form1: TForm1;
  70.  
  71. implementation
  72.  
  73. {$R *.lfm}
  74.  
  75. { TForm1 }
  76.  
  77. procedure TForm1.InitStars;
  78. const
  79.   STAR_COUNT = 1000;  //
  80.   MAX_Z_DISTANCE = 2000.0;
  81. var
  82.   i: Integer;
  83. begin
  84.   SetLength(FStars, STAR_COUNT);
  85.  
  86.   MinZ := 1.0;
  87.   MaxZ := MAX_Z_DISTANCE;
  88.   FocusZ := 200.0; // 200
  89.  
  90.   for i := 0 to STAR_COUNT - 1 do
  91.   begin
  92.     // Random position in 3D space
  93.     FStars[i].X := Random(2000) - 1000;  //  -1000 to 1000
  94.     FStars[i].Y := Random(2000) - 1000;  //  -1000 to 1000
  95.     FStars[i].Z := MinZ + Random * (MaxZ - MinZ); // Range from MinZ to MaxZ
  96.     FStars[i].Speed := 1.0 + Random(4);
  97.     FStars[i].Color := BGRA(255, 255, 255, 255);
  98.   end;
  99. end;
  100.  
  101. procedure TForm1.RotatePoint(var X, Y: Single; Angle: Single);
  102. var
  103.   NewX, NewY: Single;
  104.   Cosine, Sine: Single;
  105. begin
  106.   Cosine := Cos(Angle);
  107.   Sine := Sin(Angle);
  108.  
  109.   NewX := X * Cosine - Y * Sine;
  110.   NewY := X * Sine + Y * Cosine;
  111.  
  112.   X := NewX;
  113.   Y := NewY;
  114. end;
  115.  
  116. function TForm1.Project3Dto2D(X, Y, Z: Double; out ScX, ScY: Integer; out Size: Integer): Boolean;
  117. var
  118.   Scale: Single;
  119. begin
  120.   Result := False;
  121.  
  122.   if Z <= 0 then Exit;  // ! if Div  0
  123.  
  124.   Scale := FocusZ / Z;
  125.   ScX := CenterX + Round(X * Scale);
  126.   ScY := CenterY + Round(Y * Scale);
  127.   Size := Max(1, Round(4 * (1 - Z / MaxZ)));
  128.   Result := (ScX >= 0) and (ScX < BGRAVirtualScreen.Width) and
  129.             (ScY >= 0) and (ScY < BGRAVirtualScreen.Height);
  130. end;
  131.  
  132. procedure TForm1.UpdateStars;
  133. var
  134.   i: Integer;
  135.   SpeedFactor: Double;
  136. begin
  137.   SpeedFactor := SpeedMultiplier;
  138.  
  139.   for i := 0 to Length(FStars) - 1 do
  140.   begin
  141.  
  142.     if RotationSpeed <> 0 then
  143.       RotatePoint(FStars[i].X, FStars[i].Y, RotationSpeed);
  144.  
  145.     case ScrollMode of
  146.       smForward:
  147.         begin
  148.           FStars[i].Z := FStars[i].Z - FStars[i].Speed * SpeedFactor;
  149.           // reset z position
  150.           if FStars[i].Z < MinZ then
  151.           begin
  152.             FStars[i].X := Random(2000) - 1000;
  153.             FStars[i].Y := Random(2000) - 1000;
  154.             FStars[i].Z := MaxZ;
  155.           end;
  156.         end;
  157.  
  158.       smBackward:
  159.         begin
  160.           FStars[i].Z := FStars[i].Z + FStars[i].Speed * SpeedFactor;
  161.           if FStars[i].Z > MaxZ then
  162.           begin
  163.             FStars[i].X := Random(2000) - 1000;
  164.             FStars[i].Y := Random(2000) - 1000;
  165.             FStars[i].Z := MinZ;
  166.           end;
  167.         end;
  168.     end;
  169.   end;
  170.  
  171.   BGRAVirtualScreen.RedrawBitmap;  // refresh
  172. end;
  173.  
  174. procedure TForm1.BGRAVirtualScreenRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  175. var
  176.   i: Integer;
  177.   ScreenX, ScreenY, StSize: Integer;
  178.   Brightness: Integer;
  179.   col: TBGRAPixel;
  180.   Is_Visible: Boolean;
  181. begin
  182.   // Bitmap.Fill(BGRA(0, 0, 0, 255));
  183.  
  184.   for i := 0 to Length(FStars) - 1 do
  185.   begin
  186.     // 3D position to 2D
  187.     Is_Visible := Project3Dto2D(FStars[i].X, FStars[i].Y, FStars[i].Z, ScreenX, ScreenY, StSize);
  188.  
  189.     if Is_Visible then
  190.     begin
  191.       Brightness := Max(40, Round(255 * (1 - (FStars[i].Z / (MaxZ/1.5)))));
  192.       col := BGRA(Brightness, Brightness, Brightness);
  193.       if StSize <= 1 then
  194.         Bitmap.SetPixel(ScreenX, ScreenY, col)
  195.       else
  196.         Bitmap.FillRect(
  197.           ScreenX - StSize div 2,
  198.           ScreenY - StSize div 2,
  199.           ScreenX + StSize div 2 + 1,
  200.           ScreenY + StSize div 2 + 1, col);
  201.     end;
  202.   end;
  203. end;
  204.  
  205. procedure TForm1.FormCreate(Sender: TObject);
  206. begin
  207.   // init defautl params
  208.   ScrollMode := smForward;   // forward
  209.   SpeedMultiplier := 1.0;    // speed
  210.   RotationSpeed := 0;        // Rotation = 0
  211.  
  212.   CenterX := BGRAVirtualScreen.Width div 2;
  213.   CenterY := BGRAVirtualScreen.Height div 2;
  214.   Caption := 'BGRA 3D StarField - GIGATRON 2025 V' + Version;
  215.   BGRAVirtualScreen.Color := clBlack;
  216.  
  217.   Randomize;
  218.   InitStars;
  219. end;
  220.  
  221. procedure TForm1.FormDestroy(Sender: TObject);
  222. begin
  223.   SetLength(FStars, 0);
  224. end;
  225.  
  226. procedure TForm1.Timer1Timer(Sender: TObject);
  227. begin
  228.   UpdateStars;
  229. end;
  230.  
  231. procedure TForm1.ButtonForwardClick(Sender: TObject);
  232. begin
  233.   ScrollMode := smForward;
  234. end;
  235. procedure TForm1.ButtonBackwardClick(Sender: TObject);
  236. begin
  237.     ScrollMode := smBackward;
  238. end;
  239.  
  240. procedure TForm1.ButtonSpeedUpClick(Sender: TObject);
  241. begin
  242.   if SpeedMultiplier < 5.0 then
  243.     SpeedMultiplier := SpeedMultiplier + 0.5;
  244. end;
  245.  
  246. procedure TForm1.ButtonSpeedDownClick(Sender: TObject);
  247. begin
  248.   if SpeedMultiplier > -5.0 then
  249.     SpeedMultiplier := SpeedMultiplier - 0.5;
  250. end;
  251.  
  252. procedure TForm1.ButtonRotateLeftClick(Sender: TObject);
  253. begin
  254.      RotationSpeed := -0.01;
  255. end;
  256.  
  257. procedure TForm1.ButtonResetRClick(Sender: TObject);
  258. begin
  259.       RotationSpeed := 0.0;
  260. end;
  261.  
  262. procedure TForm1.ButtonRotateRightClick(Sender: TObject);
  263. begin
  264.      RotationSpeed := 0.01;
  265. end;
  266.  
  267. end.

*Edit : The speed never be under 0 !

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ButtonSpeedDownClick(Sender: TObject);
  2. begin
  3.   if SpeedMultiplier >0.0 then
  4.     SpeedMultiplier := SpeedMultiplier - 0.5;
  5. end;  
« Last Edit: March 01, 2025, 05:52:14 pm by Gigatron »
Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #2 on: March 02, 2025, 01:05:42 am »
Hi,

And now we can use Blitter Object BOB to make 4 planes Bob Scrolling with 2d Starfield;

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  9.   BGRAVirtualScreen, BGRABitmap, BGRABitmapTypes;
  10.  
  11. const
  12.  Version = '0.1';
  13.  
  14. type
  15.  
  16.   TStarfieldMode = (smVertical, smHorizontal);
  17.  
  18.   TStar = record
  19.     X, Y: Single;      // Pos x,y
  20.     Speed: Single;     // speed
  21.     Color: TBGRAPixel; // Color
  22.     Plane: Integer;    // Planes
  23.   end;
  24.  
  25.   { TForm1 }
  26.  
  27.   TForm1 = class(TForm)
  28.     BGRAVirtualScreen: TBGRAVirtualScreen;
  29.     Button1SpeedDown: TButton;
  30.     ButtonSpeedUp: TButton;
  31.     ButtonHorizontal: TButton;
  32.     ButtonVertical: TButton;
  33.     PanelConrols: TPanel;
  34.     Timer1: TTimer;
  35.  
  36.     procedure FormCreate(Sender: TObject);
  37.     procedure FormDestroy(Sender: TObject);
  38.     procedure Timer1Timer(Sender: TObject);
  39.     procedure BGRAVirtualScreenRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  40.     procedure ButtonVerticalClick(Sender: TObject);
  41.     procedure ButtonHorizontalClick(Sender: TObject);
  42.     procedure ButtonSpeedUpClick(Sender: TObject);
  43.     procedure ButtonSpeedDownClick(Sender: TObject);
  44.  
  45.   private
  46.  
  47.     FStars: array of TStar;
  48.     ScrollMode: TStarfieldMode;
  49.     SpeedMultiplier: Single;
  50.     procedure InitStars;
  51.     procedure UpdateStars;
  52.  
  53.   public
  54.  
  55.   end;
  56.  
  57. var
  58.   Form1: TForm1;
  59.   bob1  : TBGRABitmap;
  60.   bob1Scale: array[0..3] of TBGRABitmap;
  61.  
  62. implementation
  63.  
  64. {$R *.lfm}
  65.  
  66.  
  67. { TForm1 }
  68.  
  69. procedure TForm1.InitStars;
  70. const
  71.   // 8 plans
  72.   STAR_COUNT: array[0..3] of Integer = (20,18, 15, 10); // bobs plane distribution Back to Front 0/1/2/3
  73.   STAR_SPEED: array[0..3] of Double = ( 1.0, 2.0, 3.0, 4.0);
  74. var
  75.   i, j, MaxStars, CStar: Integer;
  76. begin
  77.  
  78.   MaxStars := 0;
  79.   for i := 0 to 3 do
  80.     Inc(MaxStars, STAR_COUNT[i]);
  81.  
  82.   SetLength(FStars, MaxStars);
  83.  
  84.   CStar := 0;
  85.   for i := 0 to 3 do
  86.   begin
  87.     for j := 0 to STAR_COUNT[i]-1 do
  88.     begin
  89.       FStars[CStar].X := Random(BGRAVirtualScreen.Width);
  90.       FStars[CStar].Y := Random(BGRAVirtualScreen.Height);
  91.       FStars[CStar].Speed := STAR_SPEED[i];
  92.       FStars[CStar].Color := BGRA(255, 255, 255, 255);  //  pas important
  93.       FStars[CStar].Plane := i;
  94.       Inc(CStar);
  95.     end;
  96.   end;
  97. end;
  98.  
  99. procedure TForm1.UpdateStars;
  100. var
  101.   i: Integer;
  102.   WW, HH: Integer;
  103. begin
  104.   WW := BGRAVirtualScreen.Width;
  105.   HH := BGRAVirtualScreen.Height;
  106.  
  107.   for i := 0 to Length(FStars) - 1 do
  108.   begin
  109.     case ScrollMode of
  110.  
  111.       smVertical:
  112.         begin
  113.           FStars[i].Y := FStars[i].Y + FStars[i].Speed * SpeedMultiplier;
  114.           if SpeedMultiplier > 0 then
  115.           begin
  116.             if FStars[i].Y > HH+64 then
  117.             begin
  118.               FStars[i].Y := -64;
  119.               FStars[i].X := Random(WW);
  120.             end;
  121.           end
  122.           else
  123.           begin
  124.             if FStars[i].Y < -64 then
  125.             begin
  126.               FStars[i].Y := HH+64;
  127.               FStars[i].X := Random(WW);
  128.             end;
  129.           end;
  130.         end;
  131.  
  132.       smHorizontal:
  133.         begin
  134.           FStars[i].X := FStars[i].X + FStars[i].Speed * SpeedMultiplier;
  135.           if SpeedMultiplier > 0 then
  136.           begin
  137.             if FStars[i].X > WW+64 then
  138.             begin
  139.               FStars[i].X := -64;
  140.               FStars[i].Y := Random(HH);
  141.             end;
  142.           end
  143.           else
  144.           begin
  145.  
  146.             if FStars[i].X < -64 then
  147.             begin
  148.               FStars[i].X := WW+64;
  149.               FStars[i].Y := Random(HH);
  150.             end;
  151.           end;
  152.         end;
  153.     end;
  154.   end;
  155.  
  156.   BGRAVirtualScreen.RedrawBitmap;  // refresh
  157. end;
  158.  
  159. procedure TForm1.BGRAVirtualScreenRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  160. var
  161.   i, Pidx: Integer;   // bob plane index
  162. begin
  163.  
  164.   for i := 0 to Length(FStars) - 1 do
  165.   begin
  166.     Pidx := FStars[i].Plane;
  167.     Bitmap.PutImage(Trunc(FStars[i].X), Trunc(FStars[i].Y), bob1Scale[Pidx], dmLinearBlend);
  168.   end;
  169.  
  170. end;
  171.  
  172. procedure TForm1.FormCreate(Sender: TObject);
  173. begin
  174.  
  175.   bob1 := TBGRABitmap.Create('06.png');
  176.   bob1Scale[0] := bob1.Resample(Round(bob1.Width * 0.4), Round(bob1.Height * 0.4), rmFineResample);
  177.   bob1Scale[1] := bob1.Resample(Round(bob1.Width * 0.6), Round(bob1.Height * 0.6), rmFineResample);
  178.   bob1Scale[2] := bob1.Resample(Round(bob1.Width * 0.8), Round(bob1.Height * 0.8), rmFineResample);
  179.   bob1Scale[3] := bob1.Resample(Round(bob1.Width ), Round(bob1.Height ), rmFineResample);
  180.  
  181.   ScrollMode := smHorizontal;  // init mode H/V
  182.   SpeedMultiplier := 2.0;      // speed
  183.  
  184.   Caption := 'BGRA 4-PlayField Shape Scrolling - GIGATRON 2025 V'+version;
  185.   BGRAVirtualScreen.Color := clBlack;
  186.  
  187.   Randomize;
  188.   InitStars;
  189.  
  190. end;
  191. // Free Stars and Bob Table
  192. procedure TForm1.FormDestroy(Sender: TObject);
  193. var
  194.   i: Integer;
  195. begin
  196.   for i := 0 to 3 do     // 4 bob scale table !
  197.     bob1Scale[i].Free;
  198.  
  199.   bob1.Free;
  200.   SetLength(FStars, 0);
  201. end;
  202.  
  203. procedure TForm1.Timer1Timer(Sender: TObject);
  204. begin
  205.      UpdateStars;
  206. end;
  207.  
  208. procedure TForm1.ButtonVerticalClick(Sender: TObject);
  209. begin
  210.     ScrollMode := smVertical;
  211. end;
  212.  
  213. procedure TForm1.ButtonHorizontalClick(Sender: TObject);
  214. begin
  215.     ScrollMode := smHorizontal;
  216. end;
  217.  
  218. procedure TForm1.ButtonSpeedUpClick(Sender: TObject);
  219. begin
  220.   if SpeedMultiplier < 10 then SpeedMultiplier := SpeedMultiplier + 1.0;
  221. end;
  222.  
  223. procedure TForm1.ButtonSpeedDownClick(Sender: TObject);
  224. begin
  225.   if SpeedMultiplier > -10 then SpeedMultiplier := SpeedMultiplier - 1.0;
  226. end;
  227.  
  228.  
  229. end.
  230.  
Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #3 on: March 03, 2025, 06:51:08 pm »
Hi

Here is the final release of this Amiga Style intro ! Source is commented for your understand !

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   BGRAVirtualScreen, BGRABitmap, BGRABitmapTypes,libxmp,mmsystem,windows;
  10.  
  11. const
  12.   Version = '0.15';
  13.   SampleRate = 44100;
  14.   Channels = 2;
  15.   BitsPerSample = 16;
  16.   BufferSize = 8192; // buffer size=8192 is now Ok  !!!
  17.   BufferCount = 2;
  18.  
  19. type
  20.  
  21.   TStarfieldMode = (smVertical, smHorizontal);
  22.  
  23.   TStar = record
  24.     X, Y: Single;      // Pos x,y
  25.     Speed: Single;     // speed
  26.     Color: TBGRAPixel; // Color
  27.     Plane: Integer;    // Planes
  28.   end;
  29.  
  30.   { TForm1 }
  31.  
  32.   TForm1 = class(TForm)
  33.     BGRAVirtualScreen: TBGRAVirtualScreen;
  34.     Timer1: TTimer;
  35.  
  36.     procedure FormCreate(Sender: TObject);
  37.     procedure FormDestroy(Sender: TObject);
  38.     procedure Timer1Timer(Sender: TObject);
  39.     procedure BGRAVirtualScreenRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  40.  
  41.   private
  42.  
  43.      ctx: xmp_context;
  44.      buffers: array[0..BufferCount-1] of array[0..BufferSize-1] of Byte;
  45.      waveHeaders: array[0..BufferCount-1] of TWaveHdr;
  46.      currentBuffer: Integer;
  47.  
  48.     FStars: array of TStar;
  49.     ScrollMode: TStarfieldMode;
  50.     SpeedMultiplier: Single;
  51.     procedure InitStars;
  52.     procedure UpdateStars;
  53.  
  54.   public
  55.  
  56.   end;
  57.  
  58. var
  59.   Form1: TForm1;
  60.   waveOut: HWAVEOUT;
  61.   waveHeader: TWaveHdr;
  62.  
  63.   s_data : array [0..255] of integer = ( // sinus data from the champs sidewinder cracktro
  64.            80,80,81,83,86,90,94,99,104,110,116,123,130,137,145,152,159,167,173,180,186,192,197,
  65.            202,206,209,212,214,215,216,216,216,214,213,210,208,205,201,197,193,189,185,181,177,
  66.            173,169,166,162,160,157,155,153,152,151,151,151,151,152,153,155,156,158,160,162,164,
  67.            166,168,170,171,172,173,174,174,174,173,172,170,168,166,163,160,156,152,148,143,139,
  68.            134,129,125,120,116,112,108,105,102,100,98,97,97,97,98,100,102,106,109,114,119,125,131,
  69.            137,144,151,159,166,174,182,189,196,203,209,215,221,226,230,233,236,238,239,239,238,237,
  70.            235,232,228,224,219,214,208,202,195,188,181,174,167,160,154,147,141,135,130,125,121,118,
  71.            115,112,111,110,109,109,110,112,113,116,118,121,124,128,131,135,139,142,146,149,152,155,
  72.            157,159,161,162,163,163,163,163,162,161,160,158,156,154,152,150,148,146,144,142,141,139,
  73.            139,138,138,139,139,141,143,145,148,151,155,159,163,168,173,178,183,188,194,199,204,208,
  74.            212,216,220,222,225,226,227,227,227,225,223,221,217,213,208,203,197,190,183,176,169,161,
  75.            153,145,138,130,123,116,110,104,99,94,90,87,84,83,82);
  76.  
  77.   bob1  : TBGRABitmap;
  78.   bob1Scale: array[0..3] of TBGRABitmap; // scaled bobs table 4
  79.  
  80.   logo_g, logo_t, logo_r :  TBGRABitmap;
  81.   sin_ctg, sin_ctt, sin_ctr : integer;
  82.  
  83.   sc_txt :Array of PChar;
  84.   sc_xpos : Single;
  85.  
  86. implementation
  87. {$R *.lfm}
  88.  
  89. procedure FillBuffer(bufferIndex: Integer);
  90. begin
  91.    xmp_play_buffer(Form1.ctx, @Form1.buffers[bufferIndex][0], BufferSize, 0);
  92. end;
  93. function WaveOutCallback(hwo: HWAVEOUT; uMsg: UINT; dwInstance, dwParam1, dwParam2: DWORD_PTR): DWORD; stdcall;
  94. begin
  95.  
  96.   if uMsg = WOM_DONE then
  97.   begin
  98.     FillBuffer(Form1.currentBuffer);
  99.     waveOutWrite(hwo, @Form1.waveHeaders[Form1.currentBuffer], SizeOf(TWaveHdr));
  100.     Form1.currentBuffer := (Form1.currentBuffer + 1) mod BufferCount;
  101.   end;
  102.   Result := 0;
  103. end;
  104.  
  105. procedure InitAudio;
  106. var
  107.   wFormat: TWaveFormatEx;
  108.   i: Integer;
  109. begin
  110.  
  111.   SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_LOWEST);
  112.  
  113.   with wFormat do
  114.   begin
  115.     wFormatTag := WAVE_FORMAT_PCM;
  116.     nChannels := Channels;
  117.     nSamplesPerSec := SampleRate;
  118.     wBitsPerSample := BitsPerSample;
  119.     nBlockAlign := (wBitsPerSample * nChannels) div 8;
  120.     nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
  121.     cbSize := 0;
  122.   end;
  123.  
  124.   if waveOutOpen(@waveOut, WAVE_MAPPER, @wFormat, QWORD(@WaveOutCallback), 0, CALLBACK_FUNCTION) <> MMSYSERR_NOERROR then
  125.     raise Exception.Create('Erreur ouverture periph audio');
  126.  
  127.   // buffers
  128.   for i := 0 to BufferCount - 1 do
  129.   begin
  130.     ZeroMemory(@Form1.waveHeaders[i], SizeOf(TWaveHdr));
  131.     with Form1.waveHeaders[i] do
  132.     begin
  133.       lpData := @Form1.buffers[i][0];
  134.       dwBufferLength := BufferSize;
  135.     end;
  136.     waveOutPrepareHeader(waveOut, @Form1.waveHeaders[i], SizeOf(TWaveHdr));
  137.     FillBuffer(i);
  138.     waveOutWrite(waveOut, @Form1.waveHeaders[i], SizeOf(TWaveHdr));
  139.   end;
  140.  
  141.   Form1.currentBuffer := 0;
  142. end;
  143.  
  144. procedure CloseAudio;
  145. begin
  146.   waveOutUnprepareHeader(waveOut, @waveHeader, SizeOf(TWaveHdr));
  147.   waveOutClose(waveOut);
  148. end;
  149.  
  150.  
  151. { TForm1 }
  152.  
  153. procedure TForm1.InitStars;
  154. const
  155.   // 4 plans
  156.   STAR_COUNT: array[0..3] of Integer = (20,18, 15, 10); // bobs plane distribution Back to Front 0/1/2/3
  157.   STAR_SPEED: array[0..3] of Single = ( 2.0, 3.0, 4.0, 5.0);
  158. var
  159.   i, j, MaxStars, CStar: Integer;
  160. begin
  161.  
  162.   MaxStars := 0;
  163.   for i := 0 to 3 do
  164.     Inc(MaxStars, STAR_COUNT[i]);
  165.  
  166.   SetLength(FStars, MaxStars);
  167.  
  168.   CStar := 0;
  169.   for i := 0 to 3 do
  170.   begin
  171.     for j := 0 to STAR_COUNT[i]-1 do
  172.     begin
  173.       FStars[CStar].X := Random(BGRAVirtualScreen.Width);
  174.       FStars[CStar].Y := 50+Random(BGRAVirtualScreen.Height-150);
  175.       FStars[CStar].Speed := STAR_SPEED[i];
  176.       FStars[CStar].Color := BGRA(255, 255, 255, 255);  //  pas important
  177.       FStars[CStar].Plane := i;
  178.       Inc(CStar);
  179.     end;
  180.   end;
  181. end;
  182.  
  183. procedure TForm1.UpdateStars;
  184. var
  185.   i: Integer;
  186.   WW, HH: Integer;
  187. begin
  188.   WW := BGRAVirtualScreen.Width;
  189.   HH := BGRAVirtualScreen.Height;
  190.  
  191.   for i := 0 to Length(FStars) - 1 do
  192.   begin
  193.     case ScrollMode of
  194.  
  195.       smVertical:
  196.         begin
  197.           FStars[i].Y := FStars[i].Y + FStars[i].Speed * SpeedMultiplier;
  198.           if SpeedMultiplier > 0 then
  199.           begin
  200.             if FStars[i].Y > HH+64 then
  201.             begin
  202.               FStars[i].Y := -64;
  203.               FStars[i].X := Random(WW);
  204.             end;
  205.           end
  206.           else
  207.           begin
  208.             if FStars[i].Y < -64 then
  209.             begin
  210.               FStars[i].Y := HH+64;
  211.               FStars[i].X := Random(WW);
  212.             end;
  213.           end;
  214.         end;
  215.  
  216.       smHorizontal:
  217.         begin
  218.           FStars[i].X := FStars[i].X + FStars[i].Speed * SpeedMultiplier;
  219.           if SpeedMultiplier > 0 then
  220.           begin
  221.             if FStars[i].X > WW+64 then
  222.             begin
  223.               FStars[i].X := -64;
  224.               FStars[i].Y := 50+Random(HH-150);
  225.             end;
  226.           end
  227.           else
  228.           begin
  229.  
  230.             if FStars[i].X < -64 then
  231.             begin
  232.               FStars[i].X := WW+64;
  233.               FStars[i].Y := 50+Random(HH-150);
  234.             end;
  235.           end;
  236.         end;
  237.     end;
  238.   end;
  239.  
  240.   BGRAVirtualScreen.RedrawBitmap;  // refresh
  241. end;
  242.  
  243. procedure TForm1.BGRAVirtualScreenRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  244. var
  245.   i, Pidx : Integer;   // bob plane index
  246. begin
  247.   // bobs
  248.   for i := 0 to Length(FStars) - 1 do
  249.   begin
  250.     Pidx := FStars[i].Plane;
  251.     Bitmap.PutImage(Trunc(FStars[i].X), Trunc(FStars[i].Y), bob1Scale[Pidx], dmLinearBlend);
  252.   end;
  253.  
  254.   // Gtr sprites
  255.   Bitmap.PutImage(-160+s_data[sin_ctr]*3,140,logo_g,dmLinearBlend);
  256.   Bitmap.PutImage(-160+s_data[sin_ctt]*3,140,logo_t,dmLinearBlend);
  257.   Bitmap.PutImage(-160+s_data[sin_ctg]*3,140,logo_r,dmLinearBlend);
  258.  
  259.   inc(sin_ctg,1);
  260.   sin_ctg := sin_ctg mod 256;
  261.   inc(sin_ctt,1);
  262.   sin_ctt := sin_ctt mod 256;
  263.   inc(sin_ctr,1);
  264.   sin_ctr := sin_ctr mod 256;
  265.  
  266.   // rectangles
  267.   Bitmap.Rectangle(0,0,BGRAVirtualScreen.Width,80,Bgra(30,140,150),80);
  268.   Bitmap.Rectangle(0,440,BGRAVirtualScreen.Width,440+80,Bgra(30,140,150),80);
  269.  
  270.   // Scroll Text
  271.   bitmap.FontName:='Courier New';
  272.   bitmap.FontStyle := [fsBold];
  273.   bitmap.FontHeight := 40;
  274.   bitmap.FontAntialias := false;
  275.  
  276.   for  i := 0 to  Length(sc_txt[0])-1 do
  277.     begin
  278.        bitmap.TextOut(i*26+sc_xpos,460+Cos(sc_xpos*0.10)*4, sc_txt[0][i],BGRA(250,250,250));
  279.         if sc_xpos < -Length(sc_txt[0]) * 26  then sc_xpos := 850;
  280.         sc_xpos := sc_xpos -0.02;
  281.     end;
  282.  
  283. end;
  284.  
  285. procedure TForm1.FormCreate(Sender: TObject);
  286. begin
  287.     // music xmp
  288.     ctx := xmp_create_context;
  289.     InitAudio;
  290.     if xmp_load_module(ctx, 'defjam.mod') <> 0 then    // by Fusion
  291.     begin
  292.       ShowMessage('Load module error.');
  293.       Exit;
  294.     end;
  295.     xmp_start_player(ctx, SampleRate, 0) ;
  296.  
  297.   bob1 := TBGRABitmap.Create('07.png');
  298.  
  299.   bob1Scale[0] := bob1.Resample(Round(bob1.Width * 0.6), Round(bob1.Height * 0.6), rmFineResample);
  300.   bob1Scale[1] := bob1.Resample(Round(bob1.Width * 0.8), Round(bob1.Height * 0.8), rmFineResample);
  301.   bob1Scale[2] := bob1.Resample(Round(bob1.Width * 1.2), Round(bob1.Height * 1.2), rmFineResample);
  302.   bob1Scale[3] := bob1.Resample(Round(bob1.Width * 1.6), Round(bob1.Height * 1.6), rmFineResample);
  303.  
  304.   ScrollMode := smHorizontal;  // init mode H/V
  305.   SpeedMultiplier := 3.0;      // speed
  306.  
  307.   Caption := 'BGRA BOB DEMO GIGATRON 2025 ';
  308.  
  309.   logo_g := TBGRABitmap.Create('g.png');
  310.   logo_t := TBGRABitmap.Create('t.png');
  311.   logo_r := TBGRABitmap.Create('r.png');
  312.   sin_ctg := 0; // counter start pos 0 at sin data
  313.   sin_ctt := 18;// '' start pos 18 at sin data
  314.   sin_ctr := 36;// '' start pos 36 at sin data
  315.  
  316.   SetLength(sc_txt, 1);
  317.   sc_txt[0] := '........ GIGATRON PRESENTS LAZARUS 8.0 AND FPC SERVAL 6.0 CRACKED ON 03.06.2034 DYNAMIC COMPILATION '+
  318.                'CODE PATCHED BY GIGATRON SFX : ESTRAYK GFX : WWW GREETINGS TO ALL MEMBERS OF LAZARUS FORUM MAY THE'+
  319.                ' FORCES BE WITH YOU SEE YOU ON NEXT PRODUCTION ........ ';
  320.   sc_xpos := 850;  // text start outside the screen at pos 850
  321.  
  322.   Randomize;
  323.   InitStars;
  324.  
  325. end;
  326. // Free Stars and Bob Table and close audio
  327. procedure TForm1.FormDestroy(Sender: TObject);
  328. var
  329.   i: Integer;
  330. begin
  331.   for i := 0 to 3 do     // 4 bob scale table !
  332.     bob1Scale[i].Free;
  333.  
  334.   bob1.Free;
  335.   SetLength(FStars, 0);
  336.   CloseAudio;
  337. end;
  338.  
  339. procedure TForm1.Timer1Timer(Sender: TObject);
  340. begin
  341.      UpdateStars;
  342. end;
  343.  
  344. end.
  345.  
Sub Quantum Technology ! Gigatron 68000 Colmar France;

korba812

  • Sr. Member
  • ****
  • Posts: 461
Re: New 2D StarField
« Reply #4 on: March 03, 2025, 10:41:29 pm »
Eh, that reminded me of the "Starry Night" screensaver from Norton Commander ;)

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #5 on: March 04, 2025, 01:57:37 am »
Eh, that reminded me of the "Starry Night" screensaver from Norton Commander ;)

Sure , here is another example  port from js source , create a cycle of canvas to perform nice fx called
unlimited bob demo from Dragon Amiga. We are using Classic Paintbox !
https://youtu.be/F_z4bYO6OL8?si=I00Ogg3ES6h9lS9i&t=497

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Spin,
  7.   Buttons, StdCtrls;
  8.  
  9. type
  10.   { TForm1 }
  11.   TForm1 = class(TForm)
  12.     Button1: TButton;
  13.     FloatSpinEdit1: TFloatSpinEdit;
  14.     FloatSpinEdit2: TFloatSpinEdit;
  15.     FloatSpinEdit3: TFloatSpinEdit;
  16.     FloatSpinEdit4: TFloatSpinEdit;
  17.     Timer1: TTimer;
  18.     PaintBox1: TPaintBox;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure FormDestroy(Sender: TObject);
  22.     procedure Timer1Timer(Sender: TObject);
  23.     procedure PaintBox1Paint(Sender: TObject);
  24.   private
  25.     FBall: TBitmap;
  26.     FCinema: array[0..7] of TBitmap;
  27.     FCtrCinema: Integer;
  28.     X1, X2, Y1, Y2: Single;
  29.     FSprites: Integer;
  30.     procedure Balls(DestCanvas: TCanvas);
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. procedure TForm1.Button1Click(Sender: TObject);
  41. var
  42.   i: integer;
  43. begin
  44.    for i := 0 to 7 do
  45.   begin
  46.     FCinema[i].Canvas.Brush.Color := clBlack;
  47.     FCinema[i].Canvas.FillRect(0, 0, 800, 600);
  48.   end;
  49. end;
  50.  
  51. procedure TForm1.FormCreate(Sender: TObject);
  52. var
  53.   i: Integer;
  54. begin
  55.  
  56.   for i := 0 to 7 do
  57.   begin
  58.     FCinema[i] := TBitmap.Create;
  59.     FCinema[i].Width := 800;
  60.     FCinema[i].Height := 600;
  61.     FCinema[i].Transparent := True;
  62.     FCinema[i].TransparentColor := clBlue;
  63.     FCinema[i].Canvas.Brush.Color := clBlack;
  64.     FCinema[i].Canvas.FillRect(0, 0, 800, 600);
  65.   end;
  66.  
  67.   // Bob Load
  68.   FBall := TBitmap.Create;
  69.   FBall.LoadFromFile('bobs/00.bmp');
  70.  
  71.   FCtrCinema := 0;   FSprites := 0;
  72.   // Init float val
  73.   X1 := 0;   X2 := 0;    Y1 := 0;  Y2 := 0;
  74.  
  75. end;
  76.  
  77. procedure TForm1.FormDestroy(Sender: TObject);
  78. var
  79.   i: Integer;
  80. begin
  81.   FBall.Free;
  82.  
  83.   for i := 0 to 7 do
  84.     FCinema[i].Free;
  85. end;
  86.  
  87. procedure TForm1.Timer1Timer(Sender: TObject);
  88. begin
  89.   PaintBox1.Invalidate;
  90. end;
  91.  
  92. procedure TForm1.PaintBox1Paint(Sender: TObject);
  93. var
  94.   i: Integer;
  95. begin
  96.  
  97.   for i := 0 to 7 do
  98.     Balls(FCinema[i].Canvas);
  99.   //****************
  100.   FCtrCinema := (FCtrCinema + 1) and 7;
  101.   PaintBox1.Canvas.Draw(0, 0, FCinema[FCtrCinema]);
  102. end;
  103.  
  104. procedure TForm1.Balls(DestCanvas: TCanvas);
  105. var
  106.   x, y: Integer;
  107. begin
  108.   x := Round(320 + 280 * Cos(X1)) ;//+ 135 * Sin(X2));
  109.   y := Round(240 + 240  * Sin(Y1)) ;// + 85 * Cos(Y2));
  110.   DestCanvas.Draw(x, y, FBall);
  111.   X1 := X1 + FloatSpinEdit1.Value;
  112.  // X2 := X2 + FloatSpinEdit2.Value;
  113.   Y1 := Y1 + FloatSpinEdit3.Value;
  114.  // Y2 := Y2 + FloatSpinEdit4.Value;
  115.  
  116.   Inc(FSprites);
  117. end;
  118.  
  119. end.
« Last Edit: March 04, 2025, 02:03:13 am by Gigatron »
Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #6 on: March 04, 2025, 11:56:25 pm »
Ok,

This unlimited bob is now finished , you can improve it to include your own formula, i used
simple formula on www and they can be wrong !!
All needed is attached to zip file;

Have fun;

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Spin,
  7.   Buttons, StdCtrls;
  8.  
  9. type
  10.    TMotionType = (mtSimple, mtLissajous, mtSpiral, mtFlower,
  11.                   mtButterfly, mtRose, mtSpirograph, mtHarmonic,
  12.                   mtDoublePendule, mtTourbillon, mtSuperposition,
  13.                   mtSpiraleLog, mtFractale, mtKaleidoscope );
  14.  
  15.    { TForm1 }
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     ComboBox1: TComboBox;
  19.     ComboBox2: TComboBox;
  20.     FloatSpinEdit1, FloatSpinEdit2, FloatSpinEdit3, FloatSpinEdit4 : TFloatSpinEdit;
  21.     Label1: TLabel;
  22.     Label2: TLabel;
  23.     Label3: TLabel;
  24.     Label4: TLabel;
  25.     Panel1: TPanel;
  26.     Timer1: TTimer;
  27.     PaintBox1: TPaintBox;
  28.  
  29.     procedure Button1Click(Sender: TObject);
  30.     procedure ComboBox1Change(Sender: TObject);
  31.     procedure ComboBox2Change(Sender: TObject);
  32.     procedure FormCreate(Sender: TObject);
  33.     procedure FormDestroy(Sender: TObject);
  34.     procedure Timer1Timer(Sender: TObject);
  35.     procedure PaintBox1Paint(Sender: TObject);
  36.   private
  37.  
  38.     FBall: TBitmap;
  39.     FCinema: array[0..7] of TBitmap;
  40.     FCtrCinema: Integer;
  41.     X1, X2, Y1, Y2: Single;
  42.     FSprites: Integer;
  43.     procedure Balls(DestCanvas: TCanvas);
  44.     Procedure ClearBalls;
  45.  
  46.  
  47.   end;
  48.  
  49. var
  50.   Form1: TForm1;
  51.   FMotionType: TMotionType;
  52.   BallColor  : integer; // index of combobox !
  53.  
  54. implementation
  55.  
  56. {$R *.lfm}
  57.  
  58. procedure TForm1.FormCreate(Sender: TObject);
  59. var
  60.   i: Integer;
  61. begin
  62.  
  63.   for i := 0 to 7 do
  64.   begin
  65.     FCinema[i] := TBitmap.Create;
  66.     FCinema[i].Width := 800;
  67.     FCinema[i].Height := 600;
  68.     FCinema[i].Transparent := True;
  69.     FCinema[i].TransparentColor := clBlue;
  70.     FCinema[i].Canvas.Brush.Color := clBlack;
  71.     FCinema[i].Canvas.FillRect(0, 0, 800, 600);
  72.   end;
  73.  
  74.   // Bob Load
  75.   FBall := TBitmap.Create;
  76.   FBall.LoadFromFile('bobs/00.bmp');
  77.   FCtrCinema := 0;   FSprites := 0;
  78.   // Init float val
  79.   X1 := 0;   X2 := 0;    Y1 := 0;  Y2 := 0;
  80.  
  81.   ComboBox1.Items.Add('Simple');
  82.   ComboBox1.Items.Add('Lissajous');
  83.   ComboBox1.Items.Add('Spirale');
  84.   ComboBox1.Items.Add('Fleur');
  85.   ComboBox1.Items.Add('Papillon');
  86.   ComboBox1.Items.Add('Rose');
  87.   ComboBox1.Items.Add('Spirographe');
  88.   ComboBox1.Items.Add('Harmonique Composé');
  89.   ComboBox1.Items.Add('Double Pendule');
  90.   ComboBox1.Items.Add('Tourbillon Hypnotique');
  91.   ComboBox1.Items.Add('Superposition d''Harmoniques');
  92.   ComboBox1.Items.Add('Spirale Logarithmique');
  93.   ComboBox1.Items.Add('Fractale Simple');
  94.   ComboBox1.Items.Add('Kaléidoscope');
  95.  
  96.   ComboBox1.ItemIndex := 0; // red Ball first
  97.   FMotionType := TMotionType(ComboBox1.ItemIndex);
  98.  
  99.   ComboBox1.ItemIndex := 0;
  100.   ComboBox2.Items.Add('Red');
  101.   ComboBox2.Items.Add('Green');
  102.   ComboBox2.Items.Add('Blue');
  103.   ComboBox2.Items.Add('Grey');
  104.   ComboBox2.Items.Add('Magenta');
  105.   ComboBox2.Items.Add('Yellow');
  106.   ComboBox2.Items.Add('Orange');
  107.   ComboBox2.Items.Add('Dark Grey');
  108.   ComboBox2.Items.Add('8 Bits');
  109.  
  110.  
  111. end;
  112.  
  113. procedure TForm1.FormDestroy(Sender: TObject);
  114. var
  115.   i: Integer;
  116. begin
  117.   FBall.Free;
  118.  
  119.   for i := 0 to 7 do
  120.     FCinema[i].Free;
  121. end;
  122.  
  123. procedure TForm1.Timer1Timer(Sender: TObject);
  124. begin
  125.   PaintBox1.Invalidate;
  126. end;
  127.  
  128. procedure TForm1.PaintBox1Paint(Sender: TObject);
  129. var
  130.   i: Integer;
  131. begin
  132.  
  133.   for i := 0 to 7 do
  134.     Balls(FCinema[i].Canvas);
  135.   //****************
  136.   FCtrCinema := (FCtrCinema + 1) and 7;
  137.   PaintBox1.Canvas.Draw(0, 0, FCinema[FCtrCinema]);
  138. end;
  139.  
  140. procedure TForm1.Balls(DestCanvas: TCanvas);
  141. var
  142.   x, y: Integer;
  143.   radius, angle, bigRadius, distance: Single;
  144. begin
  145.   case FMotionType of
  146.     mtSimple:
  147.     begin
  148.       x := Round(320 + 280 * Cos(X1));
  149.       y := Round(240 + 240 * Sin(Y1));
  150.     end;
  151.  
  152.     mtLissajous:
  153.     begin
  154.       x := Round(320 + 280 * Sin(X1) * Cos(X2));
  155.       y := Round(240 + 240 * Sin(Y1) * Cos(Y2));
  156.     end;
  157.  
  158.     mtSpiral:
  159.     begin
  160.       x := Round(320 + X1 * Cos(X1) * FloatSpinEdit2.Value + X2 * Sin(X2));
  161.       y := Round(240 + Y1 * Sin(Y1) * FloatSpinEdit4.Value + Y2 * Cos(Y2));
  162.     end;
  163.  
  164.     mtFlower:
  165.     begin
  166.       x := Round(320 + 280 * Cos(X1) * Sin(X2 * FloatSpinEdit2.Value));
  167.       y := Round(240 + 240 * Sin(Y1) * Cos(Y2 * FloatSpinEdit4.Value));
  168.     end;
  169.  
  170.     mtButterfly:
  171.     begin
  172.       x := Round(320 + 280 * Sin(X1) * Cos(Y1) * Sin(X2));
  173.       y := Round(240 + 240 * Sin(X1) * Sin(Y1) * Cos(Y2));
  174.     end;
  175.  
  176.     mtRose:
  177.     begin
  178.       radius := 280 * Cos(FloatSpinEdit2.Value * X1) * Sin(FloatSpinEdit4.Value * X2);
  179.       x := Round(320 + radius * Cos(X1));
  180.       y := Round(240 + radius * Sin(Y1));
  181.     end;
  182.  
  183.     mtSpirograph:
  184.     begin
  185.       bigRadius := 145 + 30 * Sin(X2);
  186.       radius := FloatSpinEdit2.Value * 10 + 5 * Cos(Y2);
  187.       distance := FloatSpinEdit4.Value * 10;
  188.       x := Round(320 + (bigRadius-radius) * Cos(X1) + distance * Cos(((bigRadius-radius)/radius) * X1));
  189.       y := Round(240 + (bigRadius-radius) * Sin(Y1) - distance * Sin(((bigRadius-radius)/radius) * Y1));
  190.     end;
  191.  
  192.     mtHarmonic:
  193.     begin
  194.       x := Round(320 + 280 * Sin(X1) + 100 * Sin(X2 * FloatSpinEdit2.Value));
  195.       y := Round(240 + 200 * Cos(Y1) + 80 * Cos(Y2 * FloatSpinEdit4.Value));
  196.     end;
  197.  
  198.     mtDoublePendule:
  199.     begin
  200.       x := Round(320 + 150 * Sin(X1) + 130 * Sin(X1 + X2));
  201.       y := Round(240 + 150 * Cos(Y1) + 130 * Cos(Y1 + Y2));
  202.     end;
  203.  
  204.     mtTourbillon:
  205.     begin
  206.       radius := 200 * (Sin(X1) * Sin(X2) + Cos(Y1) * Cos(Y2));
  207.       angle := X1 * 3 + Y1 * 2;
  208.       x := Round(320 + radius * Cos(angle));
  209.       y := Round(240 + radius * Sin(angle));
  210.     end;
  211.  
  212.     mtSuperposition:
  213.     begin
  214.       x := Round(320 + 280 * Sin(X1) * Cos(X2 * 2) + 50 * Sin(X1 * 3 + X2 * 2));
  215.       y := Round(240 + 240 * Sin(Y1) * Cos(Y2 * 3) + 40 * Sin(Y1 * 2 + Y2 * 5));
  216.     end;
  217.  
  218.     mtSpiraleLog:
  219.     begin
  220.       radius := 0.2 * Exp(0.1 * X1) * (1 + 0.3 * Sin(X2 * 10));
  221.       angle := Y1 + 0.2 * Sin(Y2 * 2);
  222.       x := Round(320 + radius * Cos(angle));
  223.       y := Round(240 + radius * Sin(angle));
  224.     end;
  225.  
  226.     mtFractale:
  227.     begin
  228.       x := Round(320 + 300 * Sin(X1) * Sin(X1 * X2 * 0.01));
  229.       y := Round(240 + 300 * Cos(Y1) * Cos(Y1 * Y2 * 0.01));
  230.     end;
  231.  
  232.     mtKaleidoscope:
  233.     begin
  234.       angle := X1 + Sin(X2);
  235.       radius := 200 * Sin(Y1 * 3) * Cos(Y2 * 2);
  236.       x := Round(320 + radius * Cos(angle));
  237.       y := Round(240 + radius * Sin(angle));
  238.     end;
  239.  
  240.   end;
  241.  
  242.     DestCanvas.Draw(x, y, FBall);
  243.  
  244.     X1 := X1 + FloatSpinEdit1.Value;
  245.     X2 := X2 + FloatSpinEdit2.Value;
  246.     Y1 := Y1 + FloatSpinEdit3.Value;
  247.     Y2 := Y2 + FloatSpinEdit4.Value;
  248.     Inc(FSprites);
  249.  
  250. end;
  251.  
  252. procedure TForm1.Button1Click(Sender: TObject);
  253. begin
  254.  ClearBalls;
  255. end;
  256.  
  257. procedure TForm1.ComboBox1Change(Sender: TObject);
  258. begin
  259.     ClearBalls;
  260.     FMotionType := TMotionType(ComboBox1.ItemIndex);
  261. end;
  262.  
  263. procedure TForm1.ComboBox2Change(Sender: TObject);
  264.  
  265. begin
  266.     ClearBalls;
  267.     BallColor := ComboBox2.ItemIndex;
  268.  
  269.     BallColor := ComboBox2.ItemIndex;
  270.     FBall.LoadFromFile(Format('bobs/%.2d.bmp', [BallColor]));
  271. end;
  272.  
  273. procedure TForm1.ClearBalls;
  274. var
  275.   i : integer;
  276. begin
  277.        for i := 0 to 7 do
  278.       begin
  279.        FCinema[i].Canvas.Brush.Color := clBlack;
  280.        FCinema[i].Canvas.FillRect(0, 0, 800, 600);
  281.       end;
  282. end;
  283.  
  284. end.

Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #7 on: March 08, 2025, 10:05:37 pm »
Hi

Another nice fx using BGRA Component called 3d Tunnel under 130 lines of code ;

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics,  ExtCtrls, StdCtrls,
  9.   Spin, BGRAVirtualScreen, BGRABitmap, BGRABitmapTypes, Math;
  10.  
  11. type
  12.   { TForm1 }
  13.   TForm1 = class(TForm)
  14.     Button1: TButton;
  15.     Button2: TButton;
  16.     Button3: TButton;
  17.     SpinEdit1: TSpinEdit;
  18.     VirtualScreen: TBGRAVirtualScreen;
  19.     Timer: TTimer;
  20.     procedure BGRAVirtualScreenRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure Button2Click(Sender: TObject);
  23.     procedure Button3Click(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure SpinEdit1Change(Sender: TObject);
  26.     procedure TimerTimer(Sender: TObject);
  27.   private
  28.     Speed, RotSpeed: Single;
  29.     Segments: Integer;
  30.     PathTime, RotAngle: Single;
  31.     function GetCl(Value, Agl, Rd: Single): TBGRAPixel;
  32.   public
  33.   end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41.  
  42. { TForm1 }
  43.  
  44. procedure TForm1.FormCreate(Sender: TObject);
  45. begin
  46.   Speed := 5.0;
  47.   Segments := 12;
  48.   RotSpeed := 0.0;
  49.   PathTime := 0.0001;
  50.   RotAngle := 0;
  51. end;
  52.  
  53. function TForm1.GetCl(Value, Agl, Rd: Single): TBGRAPixel;
  54. begin
  55.   if Value > 0.5 then Result := BGRA(230,210,0)
  56.   else  Result := BGRA(0, 0, 160);
  57. end;
  58.  
  59. procedure TForm1.TimerTimer(Sender: TObject);
  60. begin
  61.   PathTime := PathTime + (Speed * 0.01);
  62.   RotAngle := RotAngle - RotSpeed;
  63.   if RotAngle > 2 * Pi then RotAngle := RotAngle - (2 * Pi);
  64.   VirtualScreen.RedrawBitmap;
  65. end;
  66.  
  67. procedure TForm1.BGRAVirtualScreenRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  68. var
  69.   x, y: Integer;
  70.   CenterX, CenterY, MinRadius : Single;
  71.   UVX, UVY, RotX, RotY, Dot, TunnelDist, ZFactor, Angle, Tunnel: Single;
  72.   Clr: TBGRAPixel;
  73. begin
  74.   Bitmap.Fill(clBlack);
  75.   CenterX := VirtualScreen.Width / 2 ;
  76.   CenterY := VirtualScreen.Height / 2 ;
  77.   MinRadius := 0.015;
  78.  
  79.   for y := 0 to VirtualScreen.Height - 1 do
  80.     for x := 0 to VirtualScreen.Width - 1 do
  81.     begin
  82.       UVX := (x - CenterX) / VirtualScreen.Height;
  83.       UVY := (y - CenterY) / VirtualScreen.Height;
  84.       RotX := UVX * Cos(RotAngle) - UVY * Sin(RotAngle);
  85.       RotY := UVX * Sin(RotAngle) + UVY * Cos(RotAngle);
  86.       Dot := RotX * RotX + RotY * RotY;
  87.       if Dot < MinRadius then Continue;
  88.       ZFactor := 1.0 ;
  89.       TunnelDist := 4.0 / (Sqrt(Dot) * ZFactor);
  90.       Angle := ArcTan2(RotY, RotX);
  91.       Tunnel := TunnelDist + PathTime ;
  92.       Tunnel := Tunnel + Round(Angle * 180.0 / Pi / (360 / Segments));
  93.       Clr := GetCl(Tunnel * 0.5 mod 1, Angle, TunnelDist);
  94.       Bitmap.SetPixel(x, y, Clr);
  95.     end;
  96. end;
  97.  
  98. procedure TForm1.Button1Click(Sender: TObject);
  99. begin
  100.        RotSpeed := -0.01;
  101. end;
  102.  
  103. procedure TForm1.Button2Click(Sender: TObject);
  104. begin
  105.   RotSpeed := 0.01;
  106. end;
  107.  
  108. procedure TForm1.Button3Click(Sender: TObject);
  109. begin
  110.       RotSpeed := 0.0;
  111. end;
  112.  
  113. procedure TForm1.SpinEdit1Change(Sender: TObject);
  114. begin
  115.      Speed := SpinEdit1.value div 10;
  116. end;
  117.  
  118. end.
  119.  
Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #8 on: March 12, 2025, 03:27:32 am »
Hi

Today, I read for 3 hours of the doc and sources of various components (more BGRA) .

To reduce the code for the beginner in their project, I decided to make a 2dStarfield component.

Start a new application project with Form
Go to the Package tab and open the.lpk file (included in .zip);
Compile and Install
After that, if all went well, you have a new palette of 2DStarfield components;
Insert T2DStarfield into your project This component works in the mod editor and based on TPaintbox.

The color of the stars is white, but consider this my first attempt at making the component.
Try it and send me feedback please, my operating system is win10 x64bit.

After compiling this project without debugger and uPX compression i ve got 802 kb of exec file.. good !!

Regards

« Last Edit: March 12, 2025, 03:33:29 am by Gigatron »
Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #9 on: March 12, 2025, 02:46:44 pm »
Hi
Here is a new version of  this component;

You can now reduce intensity of colors ; val between(0 to  1.0)
For eg : if you want Blue stars set R=0 G=0 B=1
For Green :                                 R=0 G=1 B=0
For Yellow :                                 R=1 G=1 B=0

Regards
« Last Edit: March 12, 2025, 05:36:13 pm by Gigatron »
Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #10 on: March 15, 2025, 12:16:26 am »
Hi

Playing with .lpk to produce FX *with* minimal code ? That's possible;

3 component running in editor exemple ; I will upload them when finished sure ;

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, uCopperBar,
  9.   uScrollText, u2DStarfield;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     CopperBar1: TCopperBar;
  17.     ScrollText1: TScrollText;
  18.     T2DStarfield1: T2DStarfield;
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. end.
  33.  
« Last Edit: March 15, 2025, 12:25:19 am by Gigatron »
Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #11 on: March 18, 2025, 11:19:00 pm »
Nice again;

I you want to make effects with shader i ve simplified the code for your understand;

This project use the Awesome BGRA GL package from @circular !

We have 2 memo to store vertex and fragment shader. Don't touch vertex shader;
Project is attached in .zip format , shader used is different ;

Good Luck !

go to Glslsandbox;

https://glslsandbox.com/   www contains some pixel shaders.

Copy shader and past to fragMemo Lines [Tstrings] and then Click Run Shader;


for example take the first shader on top left;

https://glslsandbox.com/e#370395.0

Fragmemo is now like :
Code: Pascal  [Select][+][-]
  1. #ifdef GL_ES
  2. precision mediump float;
  3. #endif
  4. // dashxdr was here 20120228
  5. // whoops the rainbow colors weren't correct...
  6. // ugh, can't stand the previous version... too dizzy
  7.  
  8. uniform float time;
  9. uniform vec2 mouse;
  10. uniform vec2 resolution;
  11.  
  12.  
  13. //float rainbow(float x)
  14. //{
  15. //      x=fract(0.16666 * abs(x));
  16. //      if(x>.5) x = 1.0-x;
  17. //      return smoothstep(.166666, .333333, 0.7);
  18. //}
  19.  
  20. void main( void ) {
  21.  
  22.         vec2 position = ( 2.0*gl_FragCoord.xy - resolution) / resolution.xx;
  23. float ti=time*0.;
  24.         vec3 color = vec3(0.0);
  25.  
  26.         float r = length(position);
  27.         float a = atan(position.y, position.x);
  28.  
  29.         float b = a*3.0/3.14159;
  30.         color = vec3(0.0, 0.0, 1.0);
  31.  
  32.         float t = .5*(1.0 + cos(a + 40.0 * r * (1.0 + sin(a*24.0)*.1) - ti) * (5.0 / (r+5.0)));
  33.         //t = (t<0.5) ? 0.0 : 1.0;
  34.         gl_FragColor.rgba = vec4(color*t, 1.0);
  35.  
  36. }

And so on ; don't use Backbuffered or Sampler2d shaders for now;
« Last Edit: March 19, 2025, 01:10:00 am by Gigatron »
Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #12 on: March 19, 2025, 12:32:32 pm »
Hi

Here is the final version of the fx shader example, you can now edit the shader code On Memotext.
Load your own  texture image with pascal code , Uniform SamplerD2 is used in this case ;

The code is now very very simple to include pixel shader on your projects.

I didn't implement the error capture exception, so don't make a mistake when editing the shader code otherwise the program crashes if the shader code has an error !

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  9.   BGRAOpenGL, BGRABitmapTypes, BGRAOpenGL3D, BGLVirtualScreen, GL;
  10.  
  11. type
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button2: TButton;
  16.     Memo1: TMemo;
  17.     ShaderScreen1: TBGLVirtualScreen;
  18.     FragMemo: TMemo;
  19.     Timer1: TTimer;
  20.     VertexMemo: TMemo;
  21.     procedure Button2Click(Sender: TObject);
  22.     procedure ShaderScreen1LoadTextures(Sender: TObject; BGLContext: TBGLContext);
  23.     procedure ShaderScreen1Redraw(Sender: TObject; BGLContext: TBGLContext);
  24.     procedure ShaderScreen1UnloadTextures(Sender: TObject; BGLContext: TBGLContext);
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure Timer1Timer(Sender: TObject);
  27.     procedure Display_GlInfo; //
  28.   private
  29.  
  30.   public
  31.        gl_surface : IBGLTexture; shader3 : TBGLShader3D;
  32.        ctx  : TBGLContext;
  33.        t : single; // uniform time
  34.        fshader : string ; vshader : string; // fragment and vertex
  35.   end;
  36.  
  37. var
  38.    Form1: TForm1;
  39.  
  40. implementation
  41.  
  42. {$R *.lfm}
  43.  
  44. { TForm1 }
  45.  
  46. procedure TForm1.FormCreate(Sender: TObject);
  47. begin
  48.   fshader := FragMemo.text;
  49.   vshader := VertexMemo.Text;
  50. end;
  51.  
  52. procedure TForm1.ShaderScreen1Redraw(Sender: TObject; BGLContext: TBGLContext);
  53. begin
  54.  
  55.   if gl_surface <> nil then
  56.      BGLContext.Canvas.StretchPutImage(0, 0, ShaderScreen1.Width, ShaderScreen1.Height, gl_surface);
  57.  
  58.   if shader3 <> nil then
  59.   begin
  60.     BGLContext.Canvas.Lighting.ActiveShader := shader3;
  61.   end;
  62. end;
  63.  
  64. procedure TForm1.ShaderScreen1LoadTextures(Sender: TObject; BGLContext: TBGLContext);
  65. begin
  66.    try
  67.      gl_surface := BGLTexture(ResourceFile('gl.png'));
  68.     // Create shader
  69.     shader3 := TBGLShader3D.Create(
  70.       BGLContext.Canvas,
  71.       vshader,   // Vertex shader
  72.       fshader,   // Fragment shader
  73.       'varying vec2 texCoord;',
  74.       '130');    // Version GLSL
  75.        ctx := BGLContext;
  76.   except
  77.     on E: Exception do
  78.       raise exception.Create('Shader Error : ' + E.Message);
  79.   end;
  80. end;
  81.  
  82. procedure TForm1.Button2Click(Sender: TObject);
  83. begin
  84.   try
  85.     fshader := FragMemo.Text;
  86.     vshader := VertexMemo.Text;
  87.  
  88.     if Assigned(shader3) then
  89.     begin
  90.       ctx.Canvas.Lighting.ActiveShader := nil;
  91.       FreeAndNil(shader3);
  92.     end;
  93.  
  94.     shader3 := TBGLShader3D.Create(ctx.Canvas, vshader, fshader, 'varying vec2 texCoord;', '130');
  95.     // if shader error,  exception raised by BGRAOpenGLD3 nice :);
  96.     Display_GlInfo;       //  infos OpenGL
  97.     Timer1.Enabled := True; // timer1
  98.   except
  99.     on E: Exception do
  100.       ShowMessage('Shader Error : ' + E.Message);
  101.   end;
  102. end;
  103.  
  104. procedure TForm1.ShaderScreen1UnloadTextures(Sender: TObject;
  105.   BGLContext: TBGLContext);
  106. begin
  107.   gl_surface  := nil;
  108.   FreeAndNil(shader3);
  109.   BGLContext.Canvas.Lighting.ActiveShader := nil;
  110. end;
  111.  
  112. procedure TForm1.Timer1Timer(Sender: TObject);
  113. begin
  114.   if shader3 <> nil then
  115.   begin
  116.     shader3.UniformPointF['resolution'].Value := Pointf(Round(ShaderScreen1.Width),Round(ShaderScreen1.Height));
  117.     shader3.UniformSingle['time'].Value := t;
  118.     shader3.UniformSingle['speed'].Value := 0.8;
  119.  
  120.     shader3.UniformSingle['r'].Value := 0.0;
  121.     shader3.UniformSingle['g'].Value := 0.8;
  122.     shader3.UniformSingle['b'].Value := 0.0;
  123.  
  124.     t := t + 0.014;
  125.     ShaderScreen1.Invalidate;
  126.   end;
  127. end;
  128.  
  129. procedure TForm1.Display_GlInfo;
  130. begin
  131.   Memo1.Clear;
  132.   // opengl infos
  133.   Memo1.Lines.Add('OpenGL Vendor: '   + glGetString(GL_VENDOR));
  134.   Memo1.Lines.Add('OpenGL Renderer: ' + glGetString(GL_RENDERER));
  135.   Memo1.Lines.Add('OpenGL Version: '  + glGetString(GL_VERSION));
  136.   Memo1.Lines.Add('EXT :');
  137.   Memo1.Lines.Add( glGetString(GL_EXTENSIONS));
  138. end;
  139.  
  140. end.

« Last Edit: March 21, 2025, 08:37:24 pm by Gigatron »
Sub Quantum Technology ! Gigatron 68000 Colmar France;

Gigatron

  • Full Member
  • ***
  • Posts: 203
  • Amiga Rulez !!
Re: New 2D StarField
« Reply #13 on: March 21, 2025, 08:06:29 pm »
Ok here are two lpk , 2DStarfield  and TextScroll, please uninstall 2DStarfield if you got it , 
these lpk is now use OldSchoolFX tab in package palette all My future extensions Tab.

2DStarfield and ScrollText are animated in DesignTime (Editor);

The goal is minimal code , like so :

Have Fun.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.    uScrollText, u2DStarfield;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     ScrollText1: TScrollText;
  17.     ScrollText2: TScrollText;
  18.     ScrollText3: TScrollText;
  19.     ScrollText4: TScrollText;
  20.     ScrollText5: TScrollText;
  21.     ScrollText6: TScrollText;
  22.     ScrollText7: TScrollText;
  23.     T2DStarfield1: T2DStarfield;
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure FormDestroy(Sender: TObject);
  26.  
  27.   private
  28.  
  29.   public
  30.  
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.   sc_txt :Array of PChar;
  36.  
  37. implementation
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm1 }
  42.  
  43. procedure TForm1.FormCreate(Sender: TObject);
  44. begin
  45.  
  46.   SetLength(sc_txt, 1);
  47.   sc_txt[0] := '........ GIGATRON PRESENTS LAZARUS 8.0 AND FPC SERVAL 6.0 CRACKED ON 03.06.2034 DYNAMIC COMPILATION '+
  48.                'CODE PATCHED BY GIGATRON SFX : ESTRAYK GFX : WWW GREETINGS TO ALL MEMBERS OF LAZARUS FORUM MAY THE'+
  49.                ' FORCES BE WITH YOU SEE YOU ON NEXT PRODUCTION ........ ';
  50.  
  51.  
  52.  
  53.   ScrollText1.ScrollText:=sc_txt[0];
  54.   ScrollText2.ScrollText:=sc_txt[0];
  55.   ScrollText3.ScrollText:=sc_txt[0];
  56.   ScrollText4.ScrollText:=sc_txt[0];
  57.   ScrollText5.ScrollText:=sc_txt[0];
  58.   ScrollText6.ScrollText:=sc_txt[0];
  59.   ScrollText7.ScrollText:=sc_txt[0];
  60.  
  61. end;
  62.  
  63.  
  64. procedure TForm1.FormDestroy(Sender: TObject);
  65. begin
  66.  
  67. end;
  68.  
  69.  
  70. end.
  71.  
« Last Edit: March 21, 2025, 08:10:07 pm by Gigatron »
Sub Quantum Technology ! Gigatron 68000 Colmar France;

 

TinyPortal © 2005-2018