Microsoft KB Archive/66689

How to Program Graphics in OS/2 Protected-Mode COBOL Program

PSS ID Number: Q66689 Article last modified on 11-19-1990

4.00 OS/2

Summary: The code example below demonstrates how COBOL version 4.00 can program color graphics in a 320x200 pixel resolution screen under OS/2 protected mode. This is done by calling a series of API functions (of the VIO category) and writing directly to the video buffer. This information applies to Microsoft COBOL Professional Development System (PDS) version 4.00 for MS OS/2.

More Information: The following is a list of the API functions involved in the order in which they must be called:

After a selector to the screen is obtained (but before the screen is unlocked), the COBOL subprograms X“95” and X“86” can be used to write pixel values to the video buffer. In 320x200 graphics mode with 256 colors available, each pixel takes up one byte. For more information on VioSetMode, VioScrLock, VioGetPhysBuf, and VioScrUnLock, consult pages 614-615, 611, 603, and 612 (respectively) of “Advanced OS/2 Programming” by Ray Duncan (Microsoft Press, 1989). The program below (GRAPHICS.CBL) draws a horizontal, pink (color 13) line across the screen. When a key is pressed, the program terminates.

GRAPHICS.CBL
IDENTIFICATION DIVISION. PROGRAM-ID. Graphics. * Specifies to use calling convention of the OS/2 API (Pascal). SPECIAL-NAMES. CALL-CONVENTION 3 IS OS2API. DATA DIVISION. WORKING-STORAGE SECTION. *  Data buffer passed to VioSetMode. Specifies color graphics *  mode, 256 colors, 40 text columns, 25 text rows, 320 pixel *  columns, and 200 pixel rows. 01 VioModeInfo. 05 ParmLength    PIC 9(4) COMP-5 VALUE 12. 05 ModeType      PIC 9(2) COMP-5 VALUE 3. 05 Colors        PIC 9(2) COMP-5 VALUE 8. 05 TextCols      PIC 9(4) COMP-5 VALUE 40. 05 TextRows      PIC 9(4) COMP-5 VALUE 25. 05 GraphCols     PIC 9(4) COMP-5 VALUE 320. 05 GraphRows     PIC 9(4) COMP-5 VALUE 200. *  Data buffer passed to VioGetPhysBuf. Specifies absolute *  starting address of video memory (A0000 Hex) and the length *  of it. Each pixel gets 1 byte: 320 * 200 = 64000. 01 PhysBuffer. 05 BufferStart   PIC 9(8) COMP-5 VALUE 655360. 05 BufferLength  PIC 9(8) COMP-5 VALUE 64000. 05 Selector1     PIC 9(4) COMP-5. 05 Selector2     PIC 9(4) COMP-5. *  Status of physical screen lock returned by VioScrLock. 01 LockStatus       PIC 9(2) COMP-5. *  Segment (Selector) value used in X&quot;95&quot; and X&quot;86&quot; calls. 01 ScrnSegment      PIC 9(5). *  Offset value used in X&quot;95&quot; and X&quot;86&quot; calls. 01 ScrnOffset       PIC 9(5). *  Used to get key from keyboard buffer. 01 KeyCode          PIC 99   COMP-X. PROCEDURE DIVISION. *   Put the screen into a 320x200 graphics mode, 256 colors. CALL OS2API &quot;__VioSetMode&quot; USING BY REFERENCE VioModeInfo, BY VALUE    0 SIZE 2. *   Lock out other processes from accessing physical screen. CALL OS2API &quot;__VioScrLock&quot; USING BY VALUE     1 SIZE 2, BY REFERENCE LockStatus, BY VALUE    0 SIZE 2. *   Get a selector (segment address) to physical screen buffer. CALL OS2API &quot;__VioGetPhysBuf&quot; USING BY REFERENCE PhysBuffer, BY VALUE    0 SIZE 2. *   Move the physical screen selector into a segment variable *   usable by the X&quot;95&quot; and X&quot;86&quot; subprograms. MOVE Selector1 TO ScrnSegment. *   Clear screen by writing 0 (black) to every pixel location. PERFORM VARYING ScrnOffset FROM 0 BY 2 UNTIL ScrnOffset = 64000 CALL X&quot;95&quot; USING ScrnSegment, ScrnOffset, X&quot;0000&quot; END-PERFORM. *   Draw a pink horizontal line across center of screen. PERFORM VARYING ScrnOffset FROM 32000 BY 1 UNTIL ScrnOffset > 32319 CALL X&quot;86&quot; USING ScrnSegment, ScrnOffset, X&quot;0D&quot; END-PERFORM. *   Unlock the physical screen. CALL OS2API &quot;__VioScrUnLock&quot; USING BY VALUE 0 SIZE 2. *   Pause until a key is pressed, then end. CALL X&quot;83&quot; USING KeyCode. STOP RUN. Copyright Microsoft Corporation 1990.