Announcement

Collapse
No announcement yet.

Simple Hello World, X and Y

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple Hello World, X and Y

    Hello

    To bring back the popularity of the Asphyre Extreme 3.1 era, I believe we need simple to understand examples like the one attached...

    This way it can become a product for global use.

    The code is currently too complex for beginners, although it is amazing.

    My sample:

    Click image for larger version  Name:	Hello world.png Views:	3 Size:	14.7 KB ID:	571

    Code:
    unit MainFm;
    
    interface
    
    uses
      System.Types, System.Classes, Vcl.Graphics, Vcl.Forms, Vcl.Dialogs, Vcl.Controls, PXT.Types, PXT.Graphics,
      PXT.Controls, MyAsphyreDevice;
    
    type
      TMainForm = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
      public
    
       procedure CreateRenderingControl;
       procedure RenderingControlResize(Sender: TObject);
       procedure RenderingControlPaint(Sender: TObject);
      end;
    
    var
      MainForm: TMainForm;
    
    implementation
    {$R *.dfm}
    
    uses
      System.SysUtils, System.Math, PXT.Headers, PXT.Graphics.Consts;
    
    
    procedure TMainForm.CreateRenderingControl;
    begin
      FRenderingControl := TRenderingControl.Create(nil);
      FRenderingControl.Parent := Self;
      FRenderingControl.Align := alClient;
      FRenderingControl.Width := 256;
      FRenderingControl.Height := 256;
      FRenderingControl.OnPaint := RenderingControlPaint;
      FRenderingControl.OnResize := RenderingControlResize;
    end;
    
    procedure TMainForm.FormCreate(Sender: TObject);
    begin
    CreateRenderingControl;
    
    MyAsphyreDevice.CreateAsphyreDevice(MainForm.PixelsPerInch, MainForm.ClientWidth, MainForm.ClientHeight);
    end;
    
    procedure TMainForm.FormDestroy(Sender: TObject);
    begin
    MyAsphyreDevice.DestroyAsphyreDevice;
    end;
    
    procedure TMainForm.RenderingControlResize(Sender: TObject);
    begin
    MyAsphyreDevice.AsphyreRenderingControlResize;
    end;
    
    procedure TMainForm.RenderingControlPaint(Sender: TObject);
    var
    
    
      MyTextPosition: TPoint2f;
    
    
    begin
    
    
        MyAsphyreDevice.CanvasBeginScene;
    
    
    
        MyAsphyreDevice.SetupFont;
    
    
    
          MyTextPosition.X := 150.0;
          MyTextPosition.Y := 50.0;
    
          FTextRenderer.DrawCentered(MyTextPosition, 'Hello World!', ColorPair($FF00FF00, $FFFFFFFF));
    
    
    
    
    
    
       MyAsphyreDevice.CanvasEndScene;
    
    end;
    
    
    
    end.​

    Thanks​
    Last edited by anthonyedwardstark; 06-29-2025, 09:28 PM.

  • #2
    Here is my alpha demo in attach...
    Attached Files
    Last edited by anthonyedwardstark; 06-29-2025, 09:11 PM.

    Comment


    • #3
      If you want it as simple as possible then I would remove the textures for this example. You can draw the text directly onto the swapchain and canvas.

      Also.. this might be just me but I think that any "beginscene" and "endscene" combination should be in the same code block to prevent confusion. Personally I also only outsource code into another procedure when It is used more than once.

      Code:
      unit MainFm;
      
      interface
      
      uses
        System.Types, System.Classes, Vcl.Graphics, Vcl.Forms, Vcl.Dialogs, Vcl.Controls, PXT.Types, PXT.Graphics,
        PXT.Controls;
      
      type
        TMainForm = class(TForm)
          procedure FormCreate(Sender: TObject);
          procedure FormDestroy(Sender: TObject);
        private
        public
         procedure CreateRenderingControl;
         procedure RenderingControlResize(Sender: TObject);
         procedure RenderingControlPaint(Sender: TObject);
        end;
      
      var
        MainForm: TMainForm;
        FRenderingControl: TRenderingControl;
        FDisplaySize: TPoint2i;
        FDisplayScale: Single;
      
        FDevice: TDevice;
        FSwapChain: TSwapChain;
        FCanvas: TCanvas;
        FTextRenderer: TTextRenderer;​
      
      implementation
      {$R *.dfm}
      
      uses
        System.SysUtils, System.Math, PXT.Headers, PXT.Graphics.Consts;
      
      
      procedure TMainForm.CreateRenderingControl;
      begin
        FRenderingControl := TRenderingControl.Create(nil);
        FRenderingControl.Parent := Self;
        FRenderingControl.Align := alClient;
        FRenderingControl.Width := 256;
        FRenderingControl.Height := 256;
        FRenderingControl.OnPaint := RenderingControlPaint;
        FRenderingControl.OnResize := RenderingControlResize;
      end;
      
      procedure TMainForm.FormCreate(Sender: TObject);
      begin
        CreateRenderingControl;
        FDisplaySize := Point2i(ClientWidth, ClientHeight);
        FDisplayScale := PixelsPerInch / 96.0;
        try
          FDevice := TDevice.Create;
          FSwapChain := TSwapChain.Create(FDevice, FRenderingControl.WindowHandle, FDisplaySize);
          FCanvas := TCanvas.Create(FDevice);
          FTextRenderer := TTextRenderer.Create(FCanvas, Point2i(512, 512), TPixelFormat.LA8);
      
        except
          on E: EAfterwarpException do
          begin
            MessageDlg(E.Message, mtError, [mbOK], 0);
            Application.Terminate;
            Exit;
          end;
        end;​
      end;
      
      procedure TMainForm.FormDestroy(Sender: TObject);
      begin
        FTextRenderer.Free;
        FCanvas.Free;
        FSwapChain.Free;
        FDevice.Free;​
      end;
      
      procedure TMainForm.RenderingControlResize(Sender: TObject);
      begin
         if FSwapChain.Handle <> nil then
        begin
          FDisplaySize := Point2i(FRenderingControl.Width, FRenderingControl.Height);
          FSwapChain.Resize(FDisplaySize);
      
          FRenderingControl.Invalidate;
        end;​
      end;
      
      procedure TMainForm.RenderingControlPaint(Sender: TObject);
      var
        MyTextPosition: TPoint2f;
        LFontParams: TFontParameters;
      
      begin
           // Arial Regular 16px.
           LFontParams := TFontParameters.Create('Arial', 16.0 * FDisplayScale);
           LFontParams.Effect.BorderType := TFontBorder.Normal;
           LFontParams.Effect.ShadowOpacity := 1.0;
           FTextRenderer.FontParameters := LFontParams;​
      
      
        FSwapChain.BeginScene;
      
          FDevice.Clear([TClearLayer.Color], FloatColor($FF404040));
      
          FCanvas.BeginScene;
      
            FCanvas.ContextState := TCanvasContextState.FlatText;
            FCanvas.SamplerState := TCanvasSamplerState.Default;
            FCanvas.Attributes := [];
      
            MyTextPosition.X := Rendercontrol.Width / 2;
            MyTextPosition.Y := Rendercontrol.Height / 2;
      
            FTextRenderer.DrawCentered(MyTextPosition, 'Hello World!',
                               ColorPair($FF00FF00, $FFFFFFFF));​
      
        FCanvas.Endscene;
      
       FSwapChain.EndScene;​
      
      end;
      
      end.​

      Comment


      • #4
        Originally posted by Zimond View Post
        If you want it as simple as possible then I would remove the textures for this example. You can draw the text directly onto the swapchain and canvas.

        Also.. this might be just me but I think that any "beginscene" and "endscene" combination should be in the same code block to prevent confusion. Personally I also only outsource code into another procedure when It is used more than once.

        Code:
        unit MainFm;
        
        interface
        
        uses
        System.Types, System.Classes, Vcl.Graphics, Vcl.Forms, Vcl.Dialogs, Vcl.Controls, PXT.Types, PXT.Graphics,
        PXT.Controls;
        
        type
        TMainForm = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        private
        public
        procedure CreateRenderingControl;
        procedure RenderingControlResize(Sender: TObject);
        procedure RenderingControlPaint(Sender: TObject);
        end;
        
        var
        MainForm: TMainForm;
        FRenderingControl: TRenderingControl;
        FDisplaySize: TPoint2i;
        FDisplayScale: Single;
        
        FDevice: TDevice;
        FSwapChain: TSwapChain;
        FCanvas: TCanvas;
        FTextRenderer: TTextRenderer;​
        
        implementation
        {$R *.dfm}
        
        uses
        System.SysUtils, System.Math, PXT.Headers, PXT.Graphics.Consts;
        
        
        procedure TMainForm.CreateRenderingControl;
        begin
        FRenderingControl := TRenderingControl.Create(nil);
        FRenderingControl.Parent := Self;
        FRenderingControl.Align := alClient;
        FRenderingControl.Width := 256;
        FRenderingControl.Height := 256;
        FRenderingControl.OnPaint := RenderingControlPaint;
        FRenderingControl.OnResize := RenderingControlResize;
        end;
        
        procedure TMainForm.FormCreate(Sender: TObject);
        begin
        CreateRenderingControl;
        FDisplaySize := Point2i(ClientWidth, ClientHeight);
        FDisplayScale := PixelsPerInch / 96.0;
        try
        FDevice := TDevice.Create;
        FSwapChain := TSwapChain.Create(FDevice, FRenderingControl.WindowHandle, FDisplaySize);
        FCanvas := TCanvas.Create(FDevice);
        FTextRenderer := TTextRenderer.Create(FCanvas, Point2i(512, 512), TPixelFormat.LA8);
        
        except
        on E: EAfterwarpException do
        begin
        MessageDlg(E.Message, mtError, [mbOK], 0);
        Application.Terminate;
        Exit;
        end;
        end;​
        end;
        
        procedure TMainForm.FormDestroy(Sender: TObject);
        begin
        FTextRenderer.Free;
        FCanvas.Free;
        FSwapChain.Free;
        FDevice.Free;​
        end;
        
        procedure TMainForm.RenderingControlResize(Sender: TObject);
        begin
        if FSwapChain.Handle <> nil then
        begin
        FDisplaySize := Point2i(FRenderingControl.Width, FRenderingControl.Height);
        FSwapChain.Resize(FDisplaySize);
        
        FRenderingControl.Invalidate;
        end;​
        end;
        
        procedure TMainForm.RenderingControlPaint(Sender: TObject);
        var
        MyTextPosition: TPoint2f;
        LFontParams: TFontParameters;
        
        begin
        // Arial Regular 16px.
        LFontParams := TFontParameters.Create('Arial', 16.0 * FDisplayScale);
        LFontParams.Effect.BorderType := TFontBorder.Normal;
        LFontParams.Effect.ShadowOpacity := 1.0;
        FTextRenderer.FontParameters := LFontParams;​
        
        
        FSwapChain.BeginScene;
        
        FDevice.Clear([TClearLayer.Color], FloatColor($FF404040));
        
        FCanvas.BeginScene;
        
        FCanvas.ContextState := TCanvasContextState.FlatText;
        FCanvas.SamplerState := TCanvasSamplerState.Default;
        FCanvas.Attributes := [];
        
        MyTextPosition.X := Rendercontrol.Width / 2;
        MyTextPosition.Y := Rendercontrol.Height / 2;
        
        FTextRenderer.DrawCentered(MyTextPosition, 'Hello World!',
        ColorPair($FF00FF00, $FFFFFFFF));​
        
        FCanvas.Endscene;
        
        FSwapChain.EndScene;​
        
        end;
        
        end.​
        Hello friend!

        Thank you!

        I think that we need a wrapper to reduce complex code.

        I think it's not smart to have to type a lot of code to reinvent the wheel for each project.
        Last edited by anthonyedwardstark; 07-04-2025, 12:12 AM.

        Comment


        • #5
          Originally posted by anthonyedwardstark View Post

          Hello friend!

          Thank you!

          I think that we need a wrapper to reduce complex code.

          I think it's not smart to have to type a lot of code to reinvent the wheel for each project.
          To make it popular as a commercial project, I think that for Delphi the framework needs classes that will be easy to learn for a child aged 12 years.

          I mean like Feynman Technique, developed by physicist Richard Feynman​

          With wrapper classes like Asphyre 3.1, even with visual components if necessary.

          Currently, the framework is suitable for PhDs at the level of Yuriy Kotsarenko.​
          Last edited by anthonyedwardstark; 07-11-2025, 07:31 PM.

          Comment


          • #6
            Imho I don't think this Framework is very suitable for kids learning coding. I would rather use something like Freebasic to get my son into it. Also the PXL library is still usable and a lot simpler with only featuring 2D.

            Comment


            • anthonyedwardstark
              anthonyedwardstark commented
              Editing a comment
              When I mentioned children, it's a metaphor for creating cleaner and objetive code, more productive code, making the product more accessible to a wider audience.

              But anyway thank you my friend
          Working...
          X