ChatGPT + Purebasic

Share your advanced PureBasic knowledge/code with the community.
jamirokwai
Enthusiast
Enthusiast
Posts: 799
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

ChatGPT + Purebasic

Post by jamirokwai »

Hi there,

out of curiosity, I asked ChatGPT (4) if it can convert this PureBasic-Code to JavaScript. The Code is from the help.

Code: Select all

If OpenWindow(0, 100, 200, 300, 200, "2D Drawing Test")

  ; Create an offscreen image, with a green circle in it.
  ; It will be displayed later
  ;
  If CreateImage(0, DesktopScaledX(300), DesktopScaledY(200))
    If StartDrawing(ImageOutput(0))
      Circle(100,100,50,RGB(0,0,255))  ; a nice blue circle...

      Box(150,20,20,20, RGB(0,255,0))  ; and a green box
      
      FrontColor(RGB(255,0,0)) ; Finally, red lines..
      For k=0 To 20
        LineXY(10,10+k*8,200, 0)
      Next
      
      DrawingMode(#PB_2DDrawing_Transparent)
      BackColor(RGB(0,155,155)) ; Change the text back and front colour
      FrontColor(RGB(255,255,255))
      DrawText(10,50,"Hello, this is a test")

      StopDrawing()
    EndIf
  EndIf

  ; Create a gadget to display our nice image
  ;
  ImageGadget(0, 0, 0, 0, 0, ImageID(0))
  
  ;
  ; This is the 'event loop'. All the user actions are processed here.
  ; It's very easy to understand: when an action occurs, the Event
  ; isn't 0 and we just have to see what have happened...
  ;
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow  ; If the user has pressed on the window close button
  
EndIf

End   ; All the opened windows are closed automatically by PureBasic
This was the output... :-O

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2D Drawing Test</title>
</head>
<body>
    <canvas id="drawingCanvas" width="300" height="200"></canvas>

    <script>
        // Get the canvas and its context
        const canvas = document.getElementById('drawingCanvas');
        const ctx = canvas.getContext('2d');

        // Draw a blue circle
        ctx.fillStyle = 'rgb(0, 0, 255)';
        ctx.beginPath();
        ctx.arc(100, 100, 50, 0, 2 * Math.PI);
        ctx.fill();

        // Draw a green box
        ctx.fillStyle = 'rgb(0, 255, 0)';
        ctx.fillRect(150, 20, 20, 20);

        // Draw red lines
        ctx.strokeStyle = 'rgb(255, 0, 0)';
        for (let k = 0; k <= 20; k++) {
            ctx.beginPath();
            ctx.moveTo(10, 10 + k * 8);
            ctx.lineTo(200, 0);
            ctx.stroke();
        }

        // Draw text
        ctx.fillStyle = 'rgb(255, 255, 255)';
        ctx.font = '16px Arial';
        ctx.fillText('Hello, this is a test', 10, 50);

        // Event listener for closing the window (or tab in this case)
        window.addEventListener('beforeunload', (event) => {
            event.preventDefault();
            event.returnValue = '';
        });
    </script>
</body>
</html>
Regards,
JamiroKwai
jamirokwai
Enthusiast
Enthusiast
Posts: 799
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: ChatGPT + Purebasic

Post by jamirokwai »

JavaScript back to PureBasic, this was the result. The ; -> lines show the original code and I asked to correct them. The resulting image is incorrect as well (regarding the original code). The resulting code is a start, so I think, that's impressive!?

Code: Select all

; ------------------------------------------------------------
;   PureBasic - 2D Drawing example
; ------------------------------------------------------------
If OpenWindow(0, 0, 0, 300, 200, "2D Drawing Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  ; Create an offscreen image to draw on
  If CreateImage(0, 300, 200)
    If StartDrawing(ImageOutput(0))
      
      ; Draw a blue circle
      Circle(100, 100, 50, RGB(0, 0, 255))
      
      ; Draw a green box
      Box(150, 20, 20, 20, RGB(0, 255, 0))
      
      ; Draw red lines
      FrontColor(RGB(255, 0, 0))
      For k = 0 To 20
        ; -> Line(10, 10 + k * 8, 200, 0)
        Line(10, 10 + k * 8, 200, 10 + k * 8)
      Next
      
      ; Draw text
      FrontColor(RGB(255, 255, 255))
      ; -> FontID(0) ; Assuming a default font is set
      DrawText(10, 50, "Hello, this is a test")
      
      StopDrawing()
    EndIf
  EndIf
  
  ; Display the image on the window
  ; -> ImageGadget(0, 0, 0, ImageID(0), ImageID(0))
  ImageGadget(0, 0, 0, 300, 200, ImageID(0))
  
  ; Main event loop
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
  
EndIf
Regards,
JamiroKwai
Fred
Administrator
Administrator
Posts: 18390
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: ChatGPT + Purebasic

Post by Fred »

Definitely looks much better than ChatGPT 3.5
Post Reply