Graphics subsystem

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.

graphic application vga driver BIOS and vgacard output block diagram

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.

InitGraph

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.

Declaration:

void initgraph(int *graph_driver, int *graph_mode, const char  *pathtodriver);

Arguments:

  • 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 MCGA EGA EGAMONOHI, HERCMONOHI, ATT VGA PC3270 IBM8514)
  • 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

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.
void         closegraph(void);
Here, closegraph() is called after getch() since screen should not clear until user hits a key.
#include<graphics.h>
#include<string.h>
#include<stdio.h>
int main (int argc, char *argv[])
{
  int gd = DETECT, gm;
  initgraph(&gd, &gm, "C:\\TC\\BGI");
  strcpy(str, getdrivername());
10    if(getgraphmode() == 2) {
11      strcat(str, " 640x480 16 color");
12    }
13    outtextxy(0, 0, str);
14    getch();
15    closegraph();
16    return 0;
17  }
18 
Additional functions:
void detectgraph (int  *graphdriver,int  *graphmode);
int  getgraphmode(void);
char *getmodename( int mode_number );
void  getmoderange(int graphdriver, int  *lomode, int  *himode);
int   getmaxmode(void);
int  getmaxcolor(void);
int  getmaxx(void);
int  getmaxy(void);
10  void  setgraphmode(int mode);
11 

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.
#include<graphics.h>
#include<conio.h>
int main(int argc, char *argv[])
{
  int gd=DETECT, gm, gresult;
  initgraph(&gd, &gm, ""); /*path is incorrect*/
  gresult = graphresult();
10    if(gresult != 0) {
11      printf("initgraph error %d %s\n", gresult, grapherrormsg(gresult));
12      return -1;
13    }
14    getch();
15    closegraph();
16   }
 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)
  • grInvalidDriver -4 Invalid device driver file (EGAVGA.BGI)
  • grNoLoadMem -5 Not enough memory to load driver
  • grNoScanMem -6 Out of memory in scan fill
  • grNoFloodMem -7 Out of memory in flood fill
  • grFontNotFound -8 Font file not found ()
  • grNoFontMem -9 Not enough memory to load font
  • 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);

Colors

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-
  • BLACK
  • BLUE,
  • GREEN,
  • CYAN,
  • RED,
  • MAGENTA,
  • BROWN,
  • LIGHTGRAY,
  • DARKGRAY,
  • LIGHTBLUE,
  • LIGHTGREEN,
  • LIGHTCYAN,
  • LIGHTRED,
  • LIGHTMAGENTA,
  • YELLOW,
  • WHITE
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);
int  getbkcolor(void);
int  getcolor(void);
int  getmaxcolor(void);
void setpalette(int colornum, int color);

Lines

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. Line Style:
  • SOLID_LINE = 0
  • DOTTED_LINE = 1
  • CENTER_LINE = 2
  • DASHED_LINE = 3
  • USERBIT_LINE = 4
Line width:
  • NORM_WIDTH = 1
  • THICK_WIDTH = 3
void setlinestyle(int linestyle, unsigned upattern, int thickness);
void getlinesettings(struct linesettingstype  *lineinfo);
void line(int x1, int y1, int x2, int y2);
void linerel(int dx, int dy);
void lineto(int x, int y);
void moverel(int dx, int dy);
void moveto(int x, int y);

Texture and filling

Graphical shapes and background can be filled with color and textures. Graphic library provides fill routines to do this work. Below are the list of fill patterns
  • EMPTY_FILL - fills area in background color
  • SOLID_FILL - fills area in solid fill color
  • LINE_FILL - fills area with horizontal lines/
  • LTSLASH_FILL - fills area with /// like lines
  • SLASH_FILL - fills area with /// like thick lines
  • BKSLASH_FILL - fills area with \\\ like thick lines
  • LTBKSLASH_FILL - fills area with \\\ like lines
  • HATCH_FILL -light hatch fill
  • XHATCH_FILL - heavy cross hatch fill
  • INTERLEAVE_FILL - interleaving line fill
  • WIDE_DOT_FILL - Widely spaced dot fill
  • CLOSE_DOT_FILL - Closely spaced dot fill
  • USER_FILL - user defined fill
void fillellipse( int x, int y, int xradius, int yradius );
void fillpoly(int numpoints, const int  *polypoints);
void floodfill(int x, int y, int border);
void setfillpattern(const char  *upattern, int color);
void setfillstyle(int pattern, int color);
void getfillpattern(char  *pattern);
void getfillsettings(struct fillsettingstype  *fillinfo);

Text display

Graphic library also provides Text display with outtext. Text display requires fonts to be installed and some fonts are installed by default. Font names:
  • DEFAULT_FONT = 0,
  • TRIPLEX_FONT = 1,
  • SMALL_FONT = 2,
  • SANS_SERIF_FONT = 3,
  • GOTHIC_FONT= 4,
  • SCRIPT_FONT= 5,
  • SIMPLEX_FONT = 6,
  • TRIPLEX_SCR_FONT = 7,
  • COMPLEX_FONT = 8,
  • EUROPEAN_FONT = 9,
  • BOLD_FONT = 10
Text alignment/justification:
  • LEFT_TEXT = 0,
  • CENTER_TEXT = 1,
  • RIGHT_TEXT = 2,
  • TOP_TEXT = 2
Text Direction:
  • HORIZ_DIR 0
  • VERT_DIR 1
Text Size:
Font size can be given from 1 to n. User defined fonts takes USER_CHAR_SIZE 0.
void settextjustify(int horiz, int vert);
void settextstyle(int font, int direction, int charsize);
void setusercharsize(int multx, int divx, int multy, int divy);
int  textheight(const char  *textstring);
int  textwidth(const char  *textstring);
void outtext(const char  *textstring);
void outtextxy(int x, int y, const char  *textstring);

Basic Shapes

Circle, ellipse, rectangle, cube etc are basic shapes. Function names and their arguments are self explanatory.
void circle(int x, int y, int radius);
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
void rectangle(int left, int top, int right, int bottom);

Extended shapes

Bar chart, 3D bar chart, poly shape, pie chart, sectors etc are extended shapes. Function names and their arguments are self explanatory.
void  arc(int x, int y, int stangle, int endangle, int radius);
void  bar(int left, int top, int right, int bottom);
void  bar3d(int left, int top, int right, int bottom, int depth, int topflag);
void  drawpoly(int numpoints, const int  *polypoints);
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 drawing

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 -
  • COPY_PUT,
  • XOR_PUT,
  • OR_PUT,
  • AND_PUT,
  • NOT_PUT
void getimage(int left, int top, int right, int bottom, void  *bitmap);
void putimage(int left, int top, const void  *bitmap, int op);
unsigned imagesize(int left, int top, int right, int bottom);

Utility functions

void  cleardevice(void);
void  clearviewport(void);
void  _graphfreemem(void  *ptr, unsigned size);
void  *   _graphgetmem(unsigned size);
void  getarccoords(struct arccoordstype  *arccoords);
void  getaspectratio(int  *xasp, int  *yasp);
struct palettetype  *   getdefaultpalette( void );
unsigned     getpixel(int x, int y);
void  getpalette(struct palettetype  *palette);
10  int      getpalettesize( void );
11  void  gettextsettings(struct textsettingstype  *texttypeinfo);
12  void  getviewsettings(struct viewporttype  *viewport);
13  int      getx(void);
14  int      gety(void);
15  int      installuserdriver( const char  *name,int huge (*detect)(void) );
16  int      installuserfont( const char  *name );
17  void  putpixel(int x, int y, int color);
18  void  restorecrtmode(void);
19  void  setactivepage(int page);
20  void  setallpalette(const struct palettetype  *palette);
21  void  setaspectratio( int xasp, int yasp );
22  unsigned setgraphbufsize(unsigned bufsize);
23  void  setrgbpalette(int colornum, int red, int green, int blue);
24  void  setviewport(int left, int top, int right, int bottom,
25  int clip);
26  void  setvisualpage(int page);
27  void  setwritemode( int mode );

Demo Application

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
 
 int  main(int argc, char *argv[])
 {
     int gd=DETECT, gm, gresult;
     int poly[]={531,361, 578,382, 578,421, 531,441, 483,422, 483,380, 531,361 };
     char str[50];
10       initgraph(&gd, &gm, "C:\\tc\\BGI");
11       gresult = graphresult();
12       if(gresult != 0){
13          printf("error %d %s\n", gresult, grapherrormsg(gresult)); return;
14       }
15       strcpy(str, getdrivername());
16       if(getgraphmode() == 2) strcat(str, " 640x480 16 color");
17       outtextxy(0, 0, str);
18       setcolor(BLUE);
19       line(0, 160, 639, 160);
20       line(0, 320, 639, 320);
21       line(213, 0, 213, 479);
22       line(426, 0, 426, 479);
23       setcolor(RED);
24       circle(106,80,50);
25       setfillstyle(SOLID_FILL, LIGHTRED);
26       floodfill(106,80, RED);
27       outtextxy(72,143, "Circle");
28       rectangle(250,20,390,135);
29       setfillstyle(LINE_FILL, BLUE);
30       floodfill(260, 25, RED);
31       outtextxy(279,143, "Rectangle");
32       setfillstyle(LTSLASH_FILL, CYAN);
33       ellipse(530, 80,0,340, 80,50);
34       fillellipse(530,80,80,50);
35       outtextxy(495, 143, "Ellipse");
36       setfillstyle(SOLID_FILL, LIGHTMAGENTA);
37       setlinestyle(SOLID_LINE, 1, 1);
38       setcolor(WHITE);
39       bar(27,224,67,290);
40       bar(72,200,116,290);
41       bar(123,183,167,290);
42       setcolor(RED);
43       outtextxy(72,300,"Bar Graphs");
44       settextstyle(SANS_SERIF_FONT, LEFT_TEXT, 1);
45       outtextxy(240,180,"Font SANS SERIF");
46       setfillstyle(SOLID_FILL, LIGHTGREEN);
47       settextstyle(GOTHIC_FONT, LEFT_TEXT, 1);
48       outtextxy(240,210,"Font GOTHIC");
49       settextstyle(SCRIPT_FONT, LEFT_TEXT, 1);
50       outtextxy(240,230,"Font SCRIPT");
51       settextstyle(SIMPLEX_FONT, LEFT_TEXT, 1);
52       outtextxy(240,250,"Font SIMPLEX");
53       setfillstyle(SOLID_FILL, LIGHTGREEN);
54       settextstyle(DEFAULT_FONT, LEFT_TEXT, 1);
55 
56       bar3d(440+27,224,440+67,290, 10, 10);
57       bar3d(440+72,200,440+116,290, 10, 10);
58       bar3d(440+123,183,440+167,290, 10, 10);
59       setcolor(RED);
60       outtextxy(490,300,"3D Bar graphs");
61       setfillstyle(INTERLEAVE_FILL, LIGHTBLUE);
62       pieslice(300,400, 10, 280, 40);
63       setcolor(RED);
64       outtextxy(280,463,"Pie slice");
65       setfillstyle(CLOSE_DOT_FILL, LIGHTBLUE);
66       sector(110, 400, 30, 300, 50,50);
67       setcolor(RED);
68       outtextxy(72, 463, "Sector");
69       setfillstyle(HATCH_FILL, BROWN);
70       setcolor(WHITE);
71       drawpoly(7, poly);
72       floodfill(530,400, WHITE);
73       setcolor(RED);
74       outtextxy(500, 463, "Polygon");
75       getch();
76       closegraph();
77   }

Output

graphic application demo

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

printf, scanf, ioctl, mmap, DOS low level IO access, DOS BIOS int86, sleep/delay, sound, nosound, DOS graphics, Turbo C graphics,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan