Graphics Programming: Appendix
Graphics Programming: Appendix
Appendix
E Graphics Programming
The C programming language has a good collection of The function initgraph initializes the graphics system
graphics libraries. To start with graphics programming, by loading a graphics driver from disk (or validating a
let us write a small program that displays a circle on the registered driver) and then putting the system into graphics
screen. mode. The function resets all graphics settings (color,
palette, current position, viewport, etc.) to their defaults
#include < graphics.h>
and then resets graphresult to 0.
#include < conio.h>
*graphdriver is an integer that specifies the graphics
main()
driver to be used. graphdriver can be assigned a value
{
using a constant of the graphics drivers enumeration type.
int gd = DETECT, gm;
*graphmode is an integer that specifies the initial
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
circle(250, 150, 100);
graphics mode (unless *graphdriver = DETECT). If
getch(); *graphdriver = DETECT, initgraph sets *graphmode
closegraph(); to the highest resolution available for the detected driver.
} *graphmode can be assigned a value using a constant of
the graphics_modes enumeration type.
To run this program, you must include "graphics.h" pathtodriver is used to specify the directory path
header file, graphics.lib library file, and Graphics where initgraph looks for graphics drivers (*.BGI)
driver (BGI file) in the program folder. These files are part first. If the driver is not present there, initgraph looks in
of Turbo C package. In the programs in this annexure we the current directory. If pathtodriver is null, the driver
will be using 640 × 480 VGA monitor, so the programs are files must be in the current directory.
according to that specification. If your screen resolution closegraph() function clears the screen and switches
is different then you must make the necessary changes to back the screen from graphics mode to text mode. Any
your programs. For VGA monitor, the graphics driver used graphics program must make a call to the closegraph()
is EGAVGA.BGI. function at the end of graphics. Otherwise DOS screen
Here, initgraph() function initializes the graphics, will not go to text mode after running the program. Here,
i.e., mode and clears the screen initializes the graphics closegraph() is called after getch() because the screen
system. Its prototype can be given as: must not be cleared until the user hits a key.
In case you have the BGI file in the same folder of your
void far initgraph(int far *graphdriver, int far
program, you can just leave it as "".You need not mention
*graphmode, char far *pathtodriver);
*graphmode if you give *graphdriver as DETECT.
similar grounds, fillpoly() draws the outline of a Note that instead of writing the color name, you may
polygon using the current line style and colour, then fill also specify the color code like setcolor(14);.
the polygon using the current fill pattern and fill color. The function setbkcolor() is used to set the back-
Let us now have a quick look at the prototype of these ground colour for drawing. The function setfillstyle()
functions: is used to set fill pattern and fill colors. After calling
void far drawpoly(int numpoints, int far setfillstyle(), if we use functions like floodfill(),
*polypoints); fillpoly(), bar etc., the shapes will be filled with fill
void far fillpoly(int numpoints, int far color and pattern set using setfillstyle(). The proto-
*polypoints); types of these functions can be given as
In the above prototypes, void far setfillstyle(int pattern, int color);
void far setcolor(int color);
∑ numpoints is used to specify the number of points
void far setbkcolor(int color);
∑ polypoints gives the sequence of integers which
is equal to (numpoints x 2). Each pair of integers The parameter pattern in setfillstyle is as follows:
gives the x- and y- coordinates of a point on the STYLE VALUE EFFECT
polygon. EMPTY_FILL O Back ground color
Basically there are 16 colors declared in graphics.h SOLID_FILL 1 Solid Fill
as listed below. LINE_FILL 2 ---
LTSLASH_FILL 3 ///
COLOR COLOR CODE
SLASH_FILL 4 /// (thick lines)
BLACK 0
BKSLASH_FILL 5 \\\ (thick lines)
BLUE 1
LTBACKSLASH_FILL 6 \\\
GREEN 2 HATCH_FILL 7 Light hatch
CYAN 3 XHATCH_FILL 8 Heavy cross hatch
RED 4 INTERLEAVE_FILL 9 Interleaving lines
MAGENTA 5 WIDE_DOT_FILL 10 Widely spaced dots
BROWN 6 CLOSE_DOT_FILL 11 Closely spaced dots
USER_FILL 12 User-defined fill paƩern
LIGHTGRAY 7
Look at the program given below which demon-
DARKGRAY 8
strates the use of setfillstyle(), setcolor () and
LIGHTBLUE 9
setbkcolor().
LIGHTGREEN 10
#include <graphics.h>
LIGHTCYAN 11 #include <conio.h>
LIGHTRED 12 main()
{
LIGHTMAGENTA 13 int gd = DETECT, gm;
YELLOW 14 initgraph(&gd, &gm, "c:\\turboc3\\bgi");
WHITE 15
setbkcolor(11);
These colors can be used by using functions such as setcolor(4);
setcolor(), setbkcolor(), and setfillstyle(). setfillstyle(SOLID_FILL, LIGHTRED);
bar(100, 100, 300, 250);
The setcolor() function is used to set the current
drawing color. If we use setcolor(YELLOW) then if you
getch();
draw any shape, line or text after that, the drawing will be closegraph();
in yellow color. }