Graphics subsystem is a complex software and hardware stack in our personal computer. Clear understanding of this subsystem is often necessary before starting graphics programming.
A VGA capable graphics card is a second processor after CPU. Its job is to convert image frame from frame buffer address 0xA0000 to analog VGA signal frames.
It is basically a digital to analog converter. It converts 2D pixel array bytes to pixel scan frame and feeds this VGA analog signal to output monitor.
It can work in character mode. Character mode works in frame address 0xB8000. Host driver transfers 80x25 character matrix and VGA card puts font image bitmap for each characters.
Output of this frame looks character console with 80 characters columns in each lines and total 25 lines.
VGA card has I/O registers and memory mapped frame buffer. Host side graphics program or graphics library can access I/O registers to control many operations.
Mode switching and accessing cursor and mouse pointer are the most often used control operations.
Frame buffer hardware memory is mapped from 0xA0000 to 0xBFFFF physical address. This is graphics card memory shared between host CPU.
Graphics card operates in two modes. Text mode is the simplest one.
It consists of a character matrix of 80x25. One character in VGA buffer is represented by an ASCII byte and next byte contains 4bit foreground and 4bit background color.
So total 80x25x2 bytes are consumed from 0xB8000.
Graphic mode is different form console mode. It has 640x480 pixels and each pixel consists of 16 colors. 16 colors can be represented by 4bits. VGA splits color planes in 4 different planes.
Host driver selects one plane at a time and sets respective bits. It has to access all 4 planes and has to set all respective color bits to complete one pixel operation.
VGA card converts pixel bits to color signals and constructs VGA frames. These VGA analog frames then feed to display monitor.
There are many ways to interruct with a VGA graphics card. Application can directly access VGA I/O registers and framebuffer. Else it can use BIOS software interrupts or it can use Turbo C BGI graphics
driver. BGI driver can talk to BIOS subsystem as well.
Turbo C graphics program should start with initializing graphics mode with initgraph. This function must be called in a graphics program. A successful initialization puts output console to VGA graphic mode.
VGA mode consists of 640x480 pixels with 16 colors for each pixel.
initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then putting the system into graphics mode.
initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.
void initgraph(int *graph_driver, int *graph_mode, const char *pathtodriver);
graph_driver: Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics drivers enumeration type ( DETECT, /* requests autodetection */
CGA, MCGA, EGA, EGA64, EGAMONO, IBM8514,HERCMONO, ATT400, VGA, PC3270,CURRENT_DRIVER)
*graph_mode : Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. You can give *graphmode a value using a constant of the graphics_modes enumeration type ( CGA
pathtodriver : Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first. If they're not there, initgraph looks in the current directory. If pathtodriver is null, the driver files must be in the current directory. This is also the path settextstyle searches for the stroked character font files (*.CHR).
Closegraph does the termination of graphics mode and puts console mode again to text mode.
A graphics program should have a closegraph function at the end of graphics. Otherwise DOS screen will not go to text mode after running the program.
Here, closegraph() is called after getch() since screen should not clear until user hits a key.
void getmoderange(int graphdriver, int *lomode, int *himode);
void setgraphmode(int mode);
Graph Error handling
Above program does not contain any error handling cases. It may possible that initgraph may fail. There could be many reasons and one of the main reason is the absence of EGAVGA.BGI runtime library driver.
graphresult returns the error code which graph subsystem encountered in the last call. An equivalent error string for each error code can be obtained passing the error code to grapherrormsg.
It returns a human understandable error message for the error code. This is necessary to explain the error to the user.
initgraph error -3 Device driver file not found (EGAVGA.BGI)
To run this program, you need graphics.h header file, graphics.lib library file and Graphics driver (TC\BGI\EGAVGA.BGI file) in the program folder.
These files are part of Turbo C package. In all our programs we used 640x480 VGA monitor and graphics driver used is EGAVGA.BGI.
So all the programs are according to that specification. You need to make necessary changes to your programs according to your screen resolution.
Here is list of error code and their explanations
grOk 0 No error
grNoInitGraph -1 (BGI) graphics not installed
grNotDetected -2 Graphics hardware not detected
grFileNotFound -3 Device driver file not found (EGAVGA.BGI)
grInvalidMode -10 Invalid graphics mode for selected driver
grError -11 Graphics error
grIOerror -12 Graphics I/O error
grInvalidFont -13 Invalid font file ()
grInvalidFontNum -14 Invalid font number
grError15 -15 Graphics error (65521)
grError16 -16 Invalid Printer Initialize
grError17 -17 Printer Module Not Linked
grInvalidVersion -18 Invalid File Version Number
int graphresult (void);
char * grapherrormsg (int errorcode);
void graphdefaults (void);
Graphic mode in Turbo C has maximum 16 colors. Color range depends on the mode. Other modes can have 2, 4, 8, 256 colors. Color values are defined as enum COLOR and the values are-
Colors are used for applying foreground and background color in drawing lines, circle, rectangle etc. They can be used for setting color pallet and texture of filling graphical objcets.
void setbkcolor(int color);
void setcolor(int color);
void setpalette(int colornum, int color);
Line drawings are the fundamental steps for any simple graphic objects. Graphic library provides standalone line drawing as well as drawing outer lines of basic shapes.
We can modify line style and width. Here are the list of possibilities.
SOLID_LINE = 0
DOTTED_LINE = 1
CENTER_LINE = 2
DASHED_LINE = 3
USERBIT_LINE = 4
NORM_WIDTH = 1
THICK_WIDTH = 3
void setlinestyle(int linestyle, unsigned upattern, int thickness);
void pieslice(int x, int y, int stangle, int endangle, int radius);
void sector( int X, int Y, int StAngle, int EndAngle, int XRadius, int YRadius );
Image bit block transfer functions are to copy image from one location to another.
getimage function copies image to byte stream and putimage transfers byte image to screen location.
Bitblock image putting operations are -
void getimage(int left, int top, int right, int bottom, void *bitmap);
void putimage(int left, int top, constvoid *bitmap, int op);
unsigned imagesize(int left, int top, int right, int bottom);