Draw a pixel: Difference between revisions

Content added Content deleted
(→‎{{header|Commodore BASIC}}: Might as well use GETKEY in versions of BASIC that have it)
(→‎{{header|Commodore BASIC}}: Add other graphical BASICs)
Line 669: Line 669:


=={{header|Commodore BASIC}}==
=={{header|Commodore BASIC}}==
The Commodore 8-bit machines only had 200 lines of vertical resolution (and the VIC-20 rarely used more than 160 of them), so these examples do not quite fit the task's 240-line requirement.


'''Example 1:''' Commodore 64
'''Example 1:''' VIC-20 with SuperExpander Cartridge


Version 2 of Commodore BASIC, which shipped on the VIC-20 and Commodore 64, did not have any graphics support. A stock VIC didn't have enough memory for high-resolution graphics anyway, so they mostly weren't used. But Commodore shipped a "SuperExpander" cartridge for the VIC which not only included enough extra RAM to support a 160x160-pixel bitmap, but also extended BASIC to include statements for drawing on it. One oddity about the VIC's iteration of the SuperExpander is that, despite the low 160x160 resolution, it treats the screen as having 1024x1024 pixels; the below program compensates for this by asking for a dot at 640,640, which maps to the physical pixel at 100,100.
There are no graphics commands in Commodore 64 BASIC. High resolution (hires) graphics are programmed by directly manipulating the hardware registers and memory.


<lang basic>10 COLOR 0,0,2,2: REM BLACK BACKGROUND AND BORDER, RED TEXT AND EXTRA COLOR
The Commodore 64 hires bitmap is 320&times;200, subdivided into 8&times;8 cells starting at the top left and moving right. Each cell is addressed top to bottom by 8 bytes. Each byte controls a horizontal row of 8 bits. This requires calculation on the programmer's part to translate X,Y coordinates into a specific memory address/value combination (lines 30 through 60).
20 GRAPHIC 2:SCNCLR:REM SELECT HI-RES GRAPHICS AND CLEAR THE SCREEN
30 POINT 2,640,640:REM DRAW A POINT AT 640/1024*160,640/1024*160
40 GET K$:IF K$="" THEN 40: REM WAIT FOR KEYPRESS
50 GRAPHIC 0:REM BACK TO TEXT MODE</lang>


'''Example 2:''' Commodore 64 with built-in BASIC

The C-64 shipped with much better graphics capabilities than the VIC-20, and was originally supposed to have an extended BASIC to take advantage of them, but Commodore ran out of time before the Christmas shopping deadline and shipped it with the same BASIC that was in the VIC. It was feasible to do graphics from the onboard BASIC, as this program demonstrates; it's just slow and awkward, so programmers usually built hi-res graphics routines using machine language.

To create graphics via manual memory manipulation, it's necessary to understand the layout of the screen. Unlike many other bitmap systems, the C64's bitmap was broken up into the 8x8-pixel cells used for text characters, even in high-resolution graphics mode. So while the first byte of video RAM unsurprisingly contains the leftmost 8 pixels on the top row of the screen, the second byte contains the 8 pixels directly ''below'' those, and so on for eight rows, before the ninth byte returns us to the top row for its 9th through 16th pixels. This requires calculation on the programmer's part to translate X,Y coordinates into a specific memory address/value combination (lines 30 through 60).


<lang gwbasic>10 REM PLOT A RED PIXEL AT 100,100
<lang gwbasic>10 REM PLOT A RED PIXEL AT 100,100
Line 694: Line 706:
90 POKE 53280,14:POKE 53281,6:POKE 646,14:END</lang>
90 POKE 53280,14:POKE 53281,6:POKE 646,14:END</lang>


'''Example 2:''' Commodore Plus/4 and 128
'''Example 3:''' Commodore 64 with SuperExpander Cartridge

<lang gwbasic>10 COLOR 0,1:COLOR 1,3: REM SET BACKGROUND TO BLACK AND PIXEL COLOR TO RED
Commodore released a version of the SuperExpander Cartridge for the C64 that was similar to the one for the VIC-20, but not identical. Besides some differences in the actual BASIC statements, it also does not have the artificial scale factor; graphics statements instead take physical pixel coordinates.

<lang basic>10 COLOR 0,2,,,0: REM SET BACKGROUND AND BORDER TO BLACK, FOREGROUND TO RED
20 GRAPHIC 2,1:REM SELECT HIRES GRAPHICS AND CLEAR THE SCREEN
30 DRAW 1,100,100:REM DRAW A PIXEL AT 100,100
40 GET K$:IF K$="" THEN 40:REM WAIT FOR A KEYPRESS
50 GRAPHIC 0:REM RETURN TO TEXT MODE</lang>


'''Example 4:''' Commodore Plus/4, C-16, and 128 (40-column display)

By the time the Commodore Plus/4 and C-16 were released, Commodore was ready to ship them with a version of BASIC, dubbed BASIC 3.5, that included graphic support out of the box. The statements were again similar to but slightly different from what had come before in the SuperExpander cartridges. When the Commodore 128 was released, its version of BASIC, dubbed 7.0, included all of 3.5 essentially unchanged, along with many other improvements.

<lang basic>10 COLOR 0,1:COLOR 1,3: REM SET BACKGROUND TO BLACK AND PIXEL COLOR TO RED
15 GRAPHIC 1,1 : REM ENTER BITMAP GRAPHICS MODE AND CLEAR SCREEN
15 GRAPHIC 1,1 : REM ENTER BITMAP GRAPHICS MODE AND CLEAR SCREEN
20 DRAW 1,100,100 : REM PLOT PIXEL AT 100,100
20 DRAW 1,100,100 : REM PLOT PIXEL AT 100,100
30 GETKEY K$
30 GETKEY K$: REM WAIT FOR KEYPRESS THE NEW WAY
40 GRAPHIC 0,1 : REM RETURN TO TEXT MODE</lang>
40 GRAPHIC 0,1 : REM RETURN TO TEXT MODE</lang>

'''Example 5:''' Commodore 128 (80-column display)

While the Commodore 128's BASIC 7.0 had impressive support for graphics on the 40-column display, it had no such support for graphics on the double-resolution 80-column display provided by its VDC chip. For that, we turn to BASIC 8, which was released as a separate software package that had to be loaded (or installed as a chip on the C128's motherboard). It was developed outside of Commodore and has a syntax very different from the standard BASIC's graphic commands, but it supports 3D graphics out of the box, including viewport clipping, scaling, etc. Note that the below program specifies a Z coordinate of 0 for the dot, which places it on the surface of the viewport.

<lang basic>10 @MODE,0:REM SELECT SCREEN SET
20 @COLOR,0,9,0:REM BACKGROUND BLACK, FOREGROUND BRIGHT RED
30 @SCREEN,0,0:REM SELECT MONOCHROME MODE
40 @CLEAR,0:REM CLEAR THE SCREEN
50 @DRWMODA,2,0,0,0,0,0,0:REM USE @COLOR COLORS
60 @SCALE,0:REM NO SCALING
70 @DOT,100,100,0:REM DRAW DOT
80 GETKEY K$:REM WAIT FOR KEYPRESS
90 @TEXT:REM BACK TO TEXT MODE</lang>


=={{header|Delphi}}==
=={{header|Delphi}}==