+ All Categories
Home > Technology > C Graphics Functions

C Graphics Functions

Date post: 06-Apr-2017
Category:
Upload: shakoor-ab
View: 621 times
Download: 1 times
Share this document with a friend
13
Graphics Under C/IITM/Shakoor Ab-2012 Page 1 Graphics under C Graphics modes Graphics mode is a way of displaying images on a computer screen or other graphics device such that the basic unit is the pixel. Lines and characters on the screen are drawn pixel by pixel. The resolution and complexity of the image depends on how many pixels there are in total, and how many bits are assigned to each pixel. The more bits per pixel, the more different colors or shades of gray. A single graphics device can operate in a number of different graphics modes with different resolutions and color selections. A common mode for a desktop PC would be 1024 by 768 pixels with 256 different colors – chosen from a much larger number – available for each pixel. Many video adapters support several different modes of resolution, all of which are divided into two general categories: character mode and graphics mode. Of the two modes, graphics mode is the more sophisticated. Programs that run in graphics mode can display an unlimited variety of shapes and fonts, whereas programs running in character mode are severely limited. Programs that run entirely in graphics mode are called graphics-based programs. In character mode, the display screen is treated as an array of blocks, each of which can hold one ASCII character. In graphics mode, the display screen is treated as an array of pixels. Characters and other shapes are formed by turning on combinations of pixels.
Transcript
Page 1: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 1

Graphics under C Graphics modes Graphics mode is a way of displaying images on a computer screen or other graphics device such that the basic unit is the pixel. Lines and characters on the screen are drawn pixel by pixel. The resolution and complexity of the image depends on how many pixels there are in total, and how many bits are assigned to each pixel. The more bits per pixel, the more different colors or shades of gray. A single graphics device can operate in a number of different graphics modes with different resolutions and color selections. A common mode for a desktop PC would be 1024 by 768 pixels with 256 different colors – chosen from a much larger number – available for each pixel. Many video adapters support several different modes of resolution, all of which are divided into two general categories: character mode and graphics mode. Of the two modes, graphics mode is the more sophisticated. Programs that run in graphics mode can display an unlimited variety of shapes and fonts, whereas programs running in character mode are severely limited. Programs that run entirely in graphics mode are called graphics-based programs. In character mode, the display screen is treated as an array of blocks, each of which can hold one ASCII character. In graphics mode, the display screen is treated as an array of pixels. Characters and other shapes are formed by turning on combinations of pixels.

Page 2: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 2

initgraph():Initialize the Graphics System #include <graphics.h> void far initgraph(gdriver,gmode,dpath); int far *gdriver; /* graphics driver */ int far *gmode; /* graphics mode */ char far *dpath; /* directory path of driver*/ initgraph() is a graphics system control function. It is used to initialize the graphics system. It should be the first graphics function called. initgraph() loads the graphics driver, after allocating memory for it, then puts the system in graphics mode. initgraph() can be used in either of two ways: 'gdriver' must be set to one of the constants: CONSTANT NUMERIC VALUE DETECT 0 CGA 1 MCGA 2 EGA 3 EGA64 4 EGAMONO 5 IBM8514 6 HERCMONO 7 ATT400 8 VGA 9 PC3270 10 If 'gdriver' is set to DETECT (autodetection), it calls detectgraph() and automatically selects the highest resolution graphics mode for 'gmode'. To override this selection, 'gdriver' can be set to one of the other 10 constants in the above chart. 'gmode' can then be set to one of the following constants (defined in <graphics.h>): GRAPHICS DRIVER GRAPHICS_MODES VALUE RES PALETTE PAGES CGA CGAC0 0 320x200 C0 1 CGAC1 1 320x200 C1 1 CGAC2 2 320x200 C2 1 CGAC3 3 320x200 C3 1 CGAHI 4 640x200 2 color 1 MCGA MCGAC0 0 320x200 C0 1 MCGAC1 1 320x200 C1 1 MCGAC2 2 320x200 C2 1 MCGAC3 3 320x200 C3 1 MCGAMED 4 640x200 2 color 1 MCGAHI 5 640x480 2 color 1 EGA EGALO 0 640x200 16 color 4 EGAHI 1 640x350 16 color 2 EGA64 EGA64LO 0 640x200 16 color 1 EGA64HI 1 640x350 4 color 1

Page 3: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 3

EGAMONO EGAMONOHI 3 640x350 2 color 1* EGAMONOHI 3 640x350 2 color 4** HERCMONO HERCMONOHI 0 720x348 2 color 2 ATT400 ATT400C0 0 320x200 C0 1 ATT400C1 1 320x200 C1 1 ATT400C2 2 320x200 C2 1 ATT400C3 3 320x200 C3 1 ATT400MED 4 640x200 2 color 1 ATT400HI 5 640x400 2 color 1 VGA VGALO 0 640x200 16 color 4 VGAMED 1 640x350 16 color 2 VGAHI 2 640x480 16 color 1 PC3270 PC3270HI 0 720x350 2 color 1 IBM8514 IBM8514HI 0 640x480 256 colors IBM8514LO 1 1024x768 256 colors *64K EGAMONO card **256K EGAMONO card 'dpath' names the directory path where the graphic driver files are located. If they are not found in 'dpath' or 'dpath' is NULL, the current directory is searched. When initgraph() is called, all graphic settings (current position, palette, color, etc) are reset to their defaults. graphresult() (which returns graphic error code) is reset to 0. Returns: Nothing. initgraph() always sets the internal error code. On success, graphresult() returns 0. On error, graphresult() (and 'gdriver') returns: -2 Cannot find a graphics card -3 Cannot find a driver file -4 Invalid driver -5 Insufficient memory to load driver Notes: closegraph() should be called to shut down the graphics system. A call to closegraph() restores the screen to the mode it was in before initgraph() was called. Overview of Graphics System

There are many new graphics functions in C. They are all in the library, GRAPHICS.LIB. These functions allow you to control the graphics system, manipulate screens and viewports, output text to the graphics screen, draw and fill shapes, control the background and foreground color, handle graphics errors and inquire into the state of the graphics system. In order to use these graphics functions, you must

Page 4: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 4

(1) include <graphics.h> in your program, (2) initialize the graphics system and put it in the graphics mode, (3) shut down the graphics system. Therefore, programs using graphics functions should have the following as a basic framework: #include <graphics.h> main() { int graphdriver = DETECT, graphmode; initgraph(); /* graphics functions */ closegraph(); } arc():Draw a Circular Arc #include <graphics.h> void far arc(x,y,start,end,rad); int x; int y; int start, end; int rad; arc() draws the outline of an arc in the current drawing color. The circular arc is centered at 'x','y' with a radius of 'rad'. The arc travels from 'start' to 'end. bar():Draw and Fills a Bar #include <graphics.h> void far bar(l,t,r,b); int l; int t; int r; int b; bar() draws and fills in a rectangular bar. The bar is drawn using the upper left coordinates ('l','t') and the lower right coordinates ('r','b'). The current fill pattern and fill color is used to fill in the bar. The bar is not outlined. bar3d():Draw and Fills a 3-Dimensional Bar #include <graphics.h>

Page 5: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 5

void far bar3d(l,t,r,b,depth,tflag); int l; int t; int r; int b; int depth; int tflag; bar3d() draws and fills in a three-dimensional rectangular bar. The bar is drawn using the upper left coordinates ('l','t') and the lower right coordinates ('r','b'). The current fill pattern and fill color is used to fill in the bar. The bar is outlined using the current line style and color. 'depth' determines the bar's depth. 'tflag' not equal to zero signals that a three-dimensional top is to be put on the bar. circle():Draw a Circle #include <graphics.h> voidfar circle(x,y,rad); int x; int y; int rad; circle() draws a circle. Its center is at coordinates ('x','y') and its radius is 'rad'. cleardevice():Clear the Graphics Screen #include <graphics.h> void far cleardevice(void); cleardevice() erases the entire graphics screen and moves the current position (CP) to home position which is the upper left hand corner of the screen with coordinates (0,0). All other graphics system settings, such as the viewport settings, the line settings, the style settings, etc., remain the same. clearviewport():Erase the Current Viewport #include <graphics.h> void far clearviewport(void); clearviewport() clears the current viewport. The current position (CP) is returned to home position (0,0). closegraph():Shut Down the Graphics System #include <graphics.h>

Page 6: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 6

void far closegraph(void); closegraph() shuts down the graphics system by deallocating all the memory that was allocated by initgraph(). The screen is restored to the mode it was in before initgraph() was called. closegraph() is a graphics system control function and should be the last graphics function called. drawpoly():Draw a Polygon #include <graphics.h> void far drawpoly(npoint,ppoints); int npoint; int far *ppoints; drawpoly() draws a polygon with 'npoint' points. 'ppoints' points to a sequence of pairs of integers. Each pair of integers represents the ('x','y') coordinates of 'npoint'. There should be twice as many 'ppoints' as 'npoint'. You must always 'close' the polygon you are drawing, by repeating the first coordinates given. (A triangle, with three points, should have four sets of coordinates, the fourth being the same as the first.) The polygon is drawn using the current line style and color. ellipse():Draw an Elliptical Arc #include <graphics.h> void far ellipse(x,y,start,end,xrad,yrad); int x, y; int start, end; int xrad, yrad; ellipse() draws an elliptical arc. ('x','y') are the coordinates of its center. 'xrad' is the horizontal axis and 'yrad' is the vertical axis. The ellipse is drawn from 'start' to 'end'. (To draw a complete ellipse, 'start' = 0 and 'end' = 360.) fillellipse():Draw and Fill and Ellipse #include <graphics.h> void far fillellipse(x, y, xrad, yrad); int x, y; Center point int xrad; Horizontal radius int yrad; Vertical radius This function draws and fills an ellipse with center point at (x, y), horizontal radius xrad, and vertical radius yrad. The current fill color is used.

Page 7: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 7

floodfill():Fill a Bounded Region #include <graphics.h> void far floodfill(x,y,border); int x; int y; int border; floodfill() fills figures drawn using arc(), circle(), ellipse(), drawpoly(), line(), lineto(), linerel() and rectangle(). The bounded area is filled with the current fill pattern and fill color. The coordinates ('x','y') represent a point within the area to be flooded. If the point is within an enclosed area, the area inside the border will be filled. If the point is outside an enclosed area, the area outside the border will be filled. getbkcolor():Get the Current Background Color #include <graphics.h> int far getbkcolor(void); getbkcolor() returns the current background color which can be one of the following values defined in <graphics.h> getcolor():Get the Current Drawing Color #include <graphics.h> int far getcolor(void); getcolor() returns the current drawing color. The value returned is actually an index into the palette which contains the exact color information. Pixels on the screen are set to this value when lines or figures are drawn. getgraphmode():Get the Current Graphics Mode #include <graphics.h> int far getgraphmode(void); getgraphmode() returns the current graphics mode set by a previously successful call to initgraph() or setgraphmode(). With the current graphics mode stored in a temporary variable, the system can be set to various graphics modes supported by the driver, or to text mode, and then restored via setgraphmode(). getmaxcolor():Get the Maximum Color Value for Current Mode #include <graphics.h>

Page 8: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 8

int far getmaxcolor(void); getmaxcolor() returns the highest valid pixel value for the current graphics driver and mode. A pixel value is an index into a color table called a 'palette'. The range of pixel values (0 - (size-1)) is determined by the size of the palette. getmaxx():Get the Current x Resolution #include <graphics.h> int far getmaxx(void); getmaxx() returns the maximum x screen coordinate for the current graphics mode. This function is very useful for positioning text and graphics on the screen. getmaxy():Get the Current y Resolution #include <graphics.h> int far getmaxy(void); getmaxy() returns the maximum y screen coordinate for the current graphics mode. This function is very useful for positioning text and graphics on the screen. getdrivername():Get Current Graphics Driver Name #include <graphics.h> char *far getdrivername(void); The getdrivername() function returns a pointer to a string holding the name of the current graphics driver. Returns: A far pointer to a string which identifies the current driver. getmodename():Return Mode Name for Specified Driver #include <graphics.h> char * far getmodename(mode_num); int mode_num; Mode you want information about The getmodename() function returns a pointer to a string which contains the name of the indicated graphics mode, as specified by mode_num. getpixel():Get the Color of the Given Pixel

Page 9: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 9

#include <graphics.h> unsigned far getpixel(x,y); int x; int y; getpixel() gets the color of the pixel specified by coordinates ('x','y'). Returns: The color of the specified pixel. line():Draw a Line #include <graphics.h> void far line(x1,y1,x2,y2); int x1,y1; int x2,y2; line() draws a line between two specified points, ('x1','y1'), and ('x2','y2'). The line is drawn in the current color, line style and thickness. The current position (CP) in not affected by a call to this function. outtextxy():Output a String to Screen at Specified Position #include <graphics.h> void far outtextxy(x,y,tstring); int x; int y; char far *tstring; outtextxy() outputs graphics text at the specified position ('x','y') relative to the current viewport. The text is output using the current text font, text direction, character size and text justification settings. (See settextstyle() and settextjustify() for more information regarding these text attributes.) pieslice():Draw a Pieslice #include <graphics.h> void far pieslice(x,y,stangle,endangle,radius); int x; int y; int stangle, endangle; int radius; pieslice() draws a pieslice shape with the center at ('x','y') and with a radius 'radius'. The slice starts at 'stangle' and goes counterclockwise toward 'endangle'. 0 degrees is at 3 o'clock and 90 degrees is at 12 o'clock.

Page 10: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 10

putpixel():Plot a Pixel at Specified Coordinates #include <graphics.h> void putpixel(x,y,pcolor); int x; int y; int pcolor; putpixel() draws a pixel at the specified coordinates ('x','y'). The pixel is displayed in the color 'pcolor'. rectangle():Draw a Rectangle #include <graphics.h> void far rectangle(l,t,r,b); int l; int t; int r; int b; rectangle() draws a rectangle at the given coordinates, 'l' left, 't' top, 'r' right, and 'b' bottom. The rectangle is drawn using the current color, line style and thickness. sector():Draw an Elliptical Sector #include <graphics.h> void far sector(x, y, stangle, endangle, xradius, yradius); int x, y; Center point int stangle, Starting angle int endangle; Ending angle int xradius; Horizontal radius int yradius; Vertical radius This function is the same as pieslice, except that it draws an elliptical pie slice. The center point is at (x, y), stangle and endangle are the starting and ending angles, the horizontal radius is xradius, and the vertical radius is yradius. setbkcolor():Set the Background Color #include <graphics.h> void far setbkcolor(color); int color; setbkcolor() sets the current background color which can be one of the following values defined in <graphics.h>: Value Name

Page 11: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 11

0 BLACK 1 BLUE 2 GREEN 3 CYAN 4 RED 5 MAGENTA 6 BROWN 7 LIGHTGRAY 8 DARKGRAY 9 LIGHTBLUE 10 LIGHTGREEN 11 LIGHTCYAN 12 LIGHTRED 13 LIGHTMAGENTA 14 YELLOW 15 WHITE setcolor():Set the Current Drawing Color #include <graphics.h> void far setcolor(color); int color; setcolor() sets the current drawing color to 'color', which represents an index into the palette. The range of values 'color' can have is determined by the size of the palette. getmaxcolor() will return the highest possible color value, which is (size-1). Therefore, the range of possible color values is 0 to (size-1). setfillstyle():Set the Fill Pattern and Fill Color #include <graphics.h> void far setfillstyle(pattern,color); int pattern; int color; setfillstyle() sets the current fill pattern and fill color used by bar(), bar3d(), fillpoly(), floodfill() and pieslice(). There are 11 predefined fill patterns. In addition you can fill a shape in with the background color or a user-defined pattern. The names for the predefined patterns are found in 'fill_patterns' in <graphics.h>: Name Value Description EMPTY_FILL 0 Fill with background color SOLID_FILL 1 Solid fill LINE_FILL 2 Fill with horizontal lines LTSLASH_FILL 3 Fill with ///, regular lines SLASH_FILL 4 Fill with ///, thick lines BKSLASH_FILL 5 Fill with \\\, thick lines LTBKSLASH_FILL 6 Fill with \\\, regular lines HATCH_FILL 7 Fill with hatch fill XHATCH_FILL 8 Fill with heavy hatch fill INTERLEAVE_FILL 9 Interleaving line fill WIDE_DOT_FILL 10 Widely spaced dot fill CLOSE_DOT_FILL 11 Closely spaced dot fill

Page 12: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 12

USER_FILL 12 User-defined fill pattern All patterns except EMPTY_FILL use the current fill color. settextstyle():Set Current Text Font, Direction and Size #include <graphics.h> void far settextstyle(font,dir,charsize); int font; int dir; int charsize; settextstyle() sets the current text font, direction and character size. All calls to outtext() and outtextxy() are affected by the new settings. The graphics library includes an 8x8 bit-mapped font, in which each character is defined by a matrix of pixels, and several stroked fonts, in which each character is defined by a series of vectors which determine how that character is drawn. 'font' can be an 8x8 bit-mapped font or one of several "stroked" fonts available. The different types of fonts available are defined in the enumeration 'font_names', defined in <graphics.h> as: Name Value Description DEFAULT_FONT 0 8x8 bit_mapped font TRIPLEX_FONT 1 Stroked triplex font SMALL_FONT 2 Stroked small font SANS_SERIF_FONT 3 Stroked sans-serif font GOTHIC_FONT 4 Stroked gothic font The 8x8 bit-mapped font is the default font and is always available. The stroked fonts are kept in separate .CHR files on disk and can be loaded at run time or converted to .OBJ files (with the BGIOBJ utility) and linked to your .EXE files. (See registerbgifont().) 'dir' can be either horizontal text (left to right) or vertical text (rotated 90 degrees counterclockwise). The values for 'dir' are defined in <graphics.h> as: Name Value Description HORIZ_DIR 0 Left to right VERT_DIR 1 Bottom to top The default direction is HORIZ_DIR. 'charsize' allows each character to be magnified by a specific factor. If 'charsize' has a value of 0, the stroked font is magnified using the default character magnification factor of 4, or the user- defined character size set using setusercharsize(). If 'charsize' has a value greater than 0, the 8x8 bit-mapped or stroked font is

Page 13: C Graphics Functions

Graphics Under C/IITM/Shakoor Ab-2012 Page 13

magnified by that factor. (For example, if 'charsize' is 1, an 8x8 bit-mapped font is displayed in an 8x8 pixel rectangle. If 'charsize' is 2, an 8x8 bit-mapped font is displayed in a 16x16 pixel rectangle, etc.) settextstyle() affects all subsequent calls to outtext() and outtextxy(). setviewport():Set the Current Graphics Viewport #include <graphics.h> void far setviewport(left,top,right,bottom,clflag); int left; int top; int right; int bottom; int clflag; setviewport() defines a rectangular viewport, a "virtual screen", on the screen. The viewport's position is defined in terms of absolute screen coordinates, ('left','top'), ('right','bottom'). All subsequent graphics output is written to this viewport. If 'clflag' is set to a non-zero value, all drawings will be clipped or truncated at its boundaries. initgraph() and setgraphmode() can be used to reset the viewport to the entire screen.


Recommended