For all who wish to learn c graphics programming, no knowledge of graphics concepts is required. C Graphics programming is very easy and interesting. You can use graphics programming for developing your own games, in making projects, for animation etc. It's not like traditional C programming in which you have to apply complex logic in your program and then you end up with a lot of errors and warnings in your program. In C graphics programming you have to use standard library functions ( need not worry if you don't know functions ) to get your task done. Just you pass arguments to the functions and it's done. On this website you will find almost all functions with detailed explanation and a sample program showing the usage of a function. To make things easy you are provided with executable files which you can download and execute. Firstly you should know the function initgraph which is used to initialize the graphics mode . To initialize graphics mode we use initgraph function in our program. initgraph function is present in "graphics.h" header file, so your every graphics program should include "graphics.h" header file. We will discuss initgraph withe help of following sample program:-
2. Intro
• There are two modes of the standard output device:
• Text Mode
• Graphics Mode
• The programming for video games, animation and multimedia is
difficult in text mode as they predominantly work with computer
graphics.
• In graphics mode we work with tiny dots on the screen called pixels
(picture elements)
Ashim Lamichhane 2
3. Intro..
• The pixels are even present in text mode as they are used to form
characters that appear on the screen with only difference that they are
predefined pattern of pixels.
• However, the graphics mode provides the ability to manipulate the
individual pixels.
• To work with graphics, we can use graphics.h header file
Ashim Lamichhane 3
4. Graphics Characteristics
1. Pixels
• Short for Picture Element, a pixel is a single point (i.e. dot) in graphic image.
• Graphics monitor displays pictures by dividing the display screen into thousands of
pixels arranged in rows and columns.
• The pixels are so close together that they appear connected.
• The computer screen is a two dimensional; each pixel on the screen has some
location, illustrated by x and y values
• x is the horizontal offset from the left side of the screen
• y is the vertical offset from the top of the screen.
• So x=0 and y=0 represent top left corner of computer screen.
Ashim Lamichhane 4
5. Fig. representation of co-ordinates on computer screen
Ashim Lamichhane 5
(0,0)
(200,0)
y
(0,479)
(639,0)
x
(320,240)
(639,450)
(639,479)
Suppose to plot a pixel on the
Screen at say x=70 and y=100,
We’d put pixel information into
that two dimensional array,
at element array [100][70]
6. 2. Resolution
• The number of pixels used on the screen is called resolution.
• There are fixed number of rows and each rows contains certain numbers of
pixels
• The frequently used resolutions supported by most of adapters are 640*480,
800*600,1024*768, 1152*864, 1220*1024 etc.
• The resolution 640*480 (640 by 480) means that there are 640 pixels in
horizontal direction(i.e. x-axis) and 480 pixels in vertical direction (i.e y-axis).
• In general for higher resolution, the picture is more pleasing.
Ashim Lamichhane 6
7. 3. Colors
• Some graphics modes support more colors than other ranging from 2 to
millions colors.
• A particular mode may support only two colors at a time while other my
support 256 colors.
• These groups of colors are known as color palettes.
Ashim Lamichhane 7
8. 4. Video Adapters
• Video adapters are drivers for display
• Each video adapter handles graphics in different way.
• Once a video adapter is initialized by the program for particular
graphics mode then it can use it to plot various elements as well as to
display text in different fonts.
• Some examples of video adapters are CGA, VGA and EGA
Ashim Lamichhane 8
9. Initializing Graphics Hardware
• The built in function initgraph() is used to initialize the graphics system
and put the computer screen in specified graphics mode.
• initgraph() initializes the graphics system by loading a graphics driver
from disk then putting the system into graphics mode.
• It also resets all graphics settings(color, palette, current position etc) to
their defaults.
• Ex:
initgraph(&graphics_driver,&graphics_mode,”path_to_driver”);
Ashim Lamichhane 9
10. initgraph(&graphics_driver,&graphics_mode,”path_to_driver”);
• graphics_driver is a variable of type int initialized to some constants that is
defined in graphics.h header file. It species the graphics driver to be used.
• C offers certain graphics drivers and these are the files with .BGI extension.
• Depending on what adapter is used, one of these drivers gets selected.
• Some constants defined in graphics.h file for this argument are
• DETECT(=0)
• CGA(=1)
• EGA(=3)
• EGA64(=4)
• EGAMONO(=5)
• VGA(=9)
Ashim Lamichhane 10
11. initgraph(&graphics_driver,&graphics_mode,”path_to_driver”);
• graphics_mode is also a type int that is initialized to the mode for the
particular video adapter to use.
• This variable tells which monitor we are using and its resolution, the
number of video pages it supports and the colors that are available.
• The third argument is path_to_driver is string constant that specifies
the directory containing .bgi files and .chr files.
• Its value may be like “C:TCBGI”
Ashim Lamichhane 11
12. Auto-Initialization of Graphics Hardware
• In the use of initgraph() function we have explicitly told initgraph()
what graphics driver and mode to use by assigning the values to driver
and mode arguments.
• It is possible to let the program to find out the video adapter installed
in the computer and use the best driver and mode.
• i.e. the combination that gives the highest resolution.
Ashim Lamichhane 12
13. • There are two approaches for auto initialization of graphics hardware.
• DETECT is used for driver argument. In this method program doesn’t
know in advance what mode will be used and cannot assume anything
about the resolution. Ex.
int gd,gm;
gd=DETECT;
initgraph(gd,&gm,”C:TCBGI”);
• A function called detectgraph() is used that returns value for the
best driver and mode.
int gd,gm;
Detectgraph(&gd,&gm);
initgraph(&gd,&gm,”C:TCBGI”);
Ashim Lamichhane 13
14. Closing Graphic Mode
• Once the program has finished its job using the graphics facilities, then
it should restore the system to the mode that was previously in use.
• If graphics mode is not closed explicitly by the programmer,
undesirable effects may be felt.
• The closegraph() function is used to restore the screen to the mode it
was in before we called initgraph() and deallocates all memory
allocated by the graphics system.
Ashim Lamichhane 14
15. Observation of graphics result
• The library function graphresult() is used to determine whether a
certain graphics operation succeeded or not.
• This function returns an error code for the last unsuccessful graphics
operation.
• grOk represents that there is no error. The variable maintained by
graphresult is reset to 0 after graphresult has been called.
• Therefore we should store the value of graphresult into a temporary
variable and then test it.
Ashim Lamichhane 15
16. Library Functions
• Plotting and getting points
• putpixel()
• Plots a point with a specified color
putpixel(int x, int y, int color);
• getpixel()
• Gets color of specified pixel
integer_variable=getpixel(int x, int y);
• Changing drawing/foreground and background color
• setcolor(): changes current fg color
setcolor(int color);
• stbkcolor(): changes current bg color
Setbkcolor(int color);
Ashim Lamichhane 16
17. • Drawing Lines
• line()
• Draws line from point having co-ordinate x1, y1 to x2, y2 using current settings for the line
drawing and current drawing color.
line(int x1, int x1, int x2, int y2);
Similarly
setlinestyle(int style, unsigned int pattern, int thickness);
• lineto()
• It draws a line from current position to point (x,y).
• The current position of point can be changed using moveto(x,y) function
lineto(int x, int y);
And
moveto(int x1,y1);
• linerel()
• Draws a line a relative distance from current position
linerel(int dx, int dy);
Ashim Lamichhane 17
18. • Drawing Shapes
• circle()
• Draws a circle having center point (x,y) and radius r with current color.
circle(int x,int y, int r);
• ellipse()
• Draws an ellipse with current color
ellipse(int x,int y, int startAngle, int endAngle, int xRadius,int yRadius);
• arc()
• Draws a circular arc in a portion of circle
arc(int x, int y, int startAngle, int endAngle, int radius);
• rectangle()
• Draws rectangle from two end points of a diagonal of the rectangle
reactangle(int x1, int y1, int x2, int y2);
Ashim Lamichhane 18
19. • drawpoly(): draws the outline of a polygon using required points
• fillpoly(): draws and fills polygon
• Draws the outline of a polygon using required points
drawpoly(int numberOfPoints, int points[]);
fillpoly(int numberOfPoints, int points[]);
• To draw a closed polygon with N vertices we must pass N+1 co-ordinates to
drawpoly() or fillpoly() where N+1th co-ordinate must be same as first co-
ordinate.
• Thus to draw hexagon we need seven points where first and seventh point is
same
Ashim Lamichhane 19
20. • Displaying text in graphics mode
• outtext(): it displays the string at the current position
outtext(string text);
• outtextxy(): it displays the string at point(x,y)
outtextxy(int x, int y, string text);
• settextstyle(): it changes font, size and direction of characters.
settextstyle(int font, int direction, int size);
Ashim Lamichhane 20
21. • pieslice(): draws a sector with current color and font setting
pieslice(int x,int y,int startAngle,int endAngle, int r);
• bar():
• draws bar diagram using two points left-top corner and bottom corner
bar(int left, int top, int right, int bottom);
• getmaxx()
• Returns max x value for current graphics driver and mode
integer_variable=getmaxx()
• getmaxy()
• Returns max y value for current graphics driver and mode
integer_variable=getmaxy()
Ashim Lamichhane 21