Sunteți pe pagina 1din 12

initgraph() <GRAPHICS.

H>

Initializes the graphics system

Declaration:
void far initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);

Remarks:
To start the graphics system, you must first call initgraph.

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.

Argument What It Is/Does

*graphdriver Integer that specifies the graphics driver to be used. You can give graphdriver
a value using a constant of the graphics_drivers enumeration type.

*graphmode 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.

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).

*graphdriver and *graphmode must be set to valid graphics_drivers and graphics_mode values or
you'll get unpredictable results. (The exception is graphdriver = DETECT.)

After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set to
the current graphics mode.

You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached
video adapter at run time and pick the corresponding driver.

If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode.

Normally, initgraph loads a graphics driver by allocating memory for the driver (through
_graphgetmem), then loading the appropriate .BGI file from disk.

Return Value:
initgraph always sets the internal error code.
- On success, initgraph sets the code to 0
- On error, initgraph sets *graphdriver to -2, -3, -4, or -5, and graphresult returns the same value.
closegraph()

Shuts down the graphics system

Declaration: void far closegraph(void);

Remarks:

closegraph deallocates all memory allocated by the graphics system.


It then restores the screen to the mode it was in before you called initgraph.
(The graphics system deallocates memory, such as the drivers, fonts, and an
internal buffer, through a call to _graphfreemem.)

Return Value: None

graphics_modes <GRAPHICS.H>

Enum: Graphics modes for each BGI driver

Graphics:

driver graphics modes Value Column x Row Palette Pages

CGA ウ CGAC0 0 320 x 200 C0


ウ CGAC1 1 320 x 200 C1
ウ CGAC2 2 320 x 200 C2
ウ CGAC3 4 640 x 200 2 color

EGA ウ EGALO 0 640 x 200 16 color


ウ EGAHI 1 640 x 350 16 color

VGA ウ VGALO 0 640 x 200 16 color


ウ VGAMED 1 640 x 350 16 color
ウ VGAHI 2 640 x 480 16 color

graphics_drivers <GRAPHICS.H>

Enum: BGI graphics drivers


Constant Value

DETECT 0 (requests auto detection)


CGA 1
MCGA 2
EGA 3
EGA64 4
EGAMONO 5
IBM8514 6
HERCMONO 7
ATT400 8
VGA 9
PC3270 10

line(), linerel, lineto <GRAPHICS.H>

 line draws a line between two specified points


 linerel draws a line a relative distance from the current position (CP)
 lineto draws a line from the current position (CP) to (x,y)

Declaration:
 void far line(int x1, int y1, int x2, int y2);
 void far linerel(int dx, int dy);
 void far lineto(int x, int y);

Remarks:
 line draws a line from (x1, y1) to (x2, y2) using the current color, line style, and thickness. It
does not update the current position (CP).

 linerel draws a line from the CP to a point that is a relative distance (dx, dy) from the CP, then
advances the CP by (dx, dy).

 lineto draws a line from the CP to (x, y), then moves the CP to (x, y).

Return Value: None

rectangle() <GRAPHICS.H>

Draws a rectangle (graphics mode)

Declaration:
void far rectangle(int left, int top, int right, int bottom);

Remarks:
rectangle draws a rectangle in the current line style, thickness, and drawing color.

(left, top) is the upper left corner of the rectangle, and (right, bottom) is its lower right corner.

Return Value: None

bar() <GRAPHICS.H>

Draws a bar

Declaration: void far bar(int left, int top, int right, int bottom);

Remarks:
bar draws a filled-in, rectangular, two-dimensional bar.

The bar is filled using the current fill pattern and fill color. bar does
not outline the bar.

To draw an outlined two-dimensional bar, use bar3d with depth = 0.

Parameters What they are

(left, top) the rectangle's upper left corner


(right, bottom) the rectangle's lower right corner

The coordinates are in pixels.

Return Value: None

arc(), circle(), pieslice() <GRAPHICS.H>

 arc draws a circular arc


 circle draws a circle
 pieslice draws and fills a circular pie slice

Declaration:
 void far arc(int x, int y, int stangle, int endangle, int radius);
 void far circle(int x, int y, int radius);
 void far pieslice(int x, int y, int stangle, int endangle, int radius);

Remarks:
arc draws a circular arc in the current drawing color.
circle draws a circle in the current drawing color.
pieslice draws a pie slice in the current drawing color, then fills it using the current fill pattern and
fill color.

Argument What It Is/Does

(x,y) Center point of arc, circlew, or pie slice


stangle Start angle in degrees
endangle End angle in degrees
radius Radius of arc, circle, and pieslice

The arc or slice travels from stangle to endangle.


If stangle = 0 and endangle = 360, the call to arc draws a complete circle.
If your circles are not perfectly round, use setaspectratio to adjust the aspect ratio.
Angle for arc, circle, and pieslice (counter-clockwise)

90
degrees

180 0 degrees,
degrees 360 degrees

270
degrees

The linestyle parameter does not affect arcs, circles, ellipses, or pie slices. Only the thickness
parameter is used.
If you're using a CGA in high resolution mode or a monochrome graphics adapter, examples in this
Help system that show how to use graphics functions might not produce the expected results.
If your system runs on a CGA or monochrome adapter, pass 1 to those functions that alter the fill or
drawing color (don't pass one of the symbolic color constants defined in GRAPHICS.H).

Return Value: None

ellipse(), fillellipse(), sector() <GRAPHICS.H>

 ellipse draws an elliptical arc


 fillellipse draws and fills an ellipse
 sector draws and fills an elliptical pie slice
Declaration:
 void far ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
 void far fillellipse(int x, int y, int xradius, int yradius);
 void far sector(int x, int y, int stangle, int endangle, int xradius, int yradius);

Remarks:
ellipse draws an elliptical arc in the current drawing color.

fillellipse draws an ellipse, then fills the ellipse with the current fill color and fill pattern.

sector draws and fills an elliptical pie slice in the current drawing color, then fills it using the pattern
and color defined by setfillstyle or setfillpattern.

Argument What It Is

(x, y) Center of ellipse


xradius Horizontal axis
yradius Vertical axis
stangle Starting angle
endangle Ending angle

The ellipse or sector travels from stangle to endangle.

If stangle = 0 and endangle = 360, the call to ellipse draws a complete ellipse.

Angle for ellipse, fillellipse, and sector (counter-clockwise)

90 degrees

0 degrees, 360 degrees

270 degrees

The linestyle parameter does not affect arcs, circles, ellipses, or pie slices. Only the thickness
parameter is used.

Return Value: None

getbkcolor(), setbkcolor() <GRAPHICS.H>

 getbkcolor returns the current background color


 setbkcolor sets the current background color using the palette

Declaration:
 int far getbkcolor(void);
 void far setbkcolor(int color);

Remarks:
getbkcolor returns the current background color.
setbkcolor sets the background to the color specified by color.
Color: color is either a number or symbolic name specifying the color to set.

For example, if you want to set the background color to blue, you can call

setbkcolor(BLUE) /* or */ setbkcolor(1)

On CGA and EGA systems, setbkcolor changes the background color by changing
the first entry in the palette.

On an EGA or a VGA, if you call setpalette or setallpalette to change the palette colors, the defined
symbolic constants might not give the correct color.

This is because the color parameter to setbkcolor indicates the entry number in the current palette,
rather than a specific color. (Except 0, which always sets the background color to black).

Return Value:
 getbkcolor returns the current background color.
 setbkcolor does not return.

floodfill() <GRAPHICS.H>

Flood-fills a bounded region

Declaration: void far floodfill(int x, int y, int border);

Remarks:
floodfill fills an enclosed area on bitmap devices.

The area bounded by the color border is flooded with the current fill pattern and fill color.

(x,y) is a "seed point".


 If the seed is within an enclosed area, the inside will be filled.
 If the seed is outside the enclosed area, the exterior will be filled.

Use fillpoly instead of floodfill whenever possible so you can maintain code compatibility with
future versions.

floodfill does not work with the IBM-8514 driver.

Return Value:
If an error occurs while flooding a region, graphresult returns -7.

drawpoly(), fillpoly() <GRAPHICS.H>


 drawpoly draws the outline of a polygon
 fillpoly draws and fills a polygon

Declaration:
 void far drawpoly(int numpoints, int far *polypoints);
 void far fillpoly(int numpoints, int far *polypoints);

Remarks:
drawpoly draws a polygon using the current line style and color.

fillpoly draws the outline of a polygon using the current line style and color, then fills the polygon
using the current fill pattern and fill color.

Argument What It Does

numpoints Specifies number of points


*polypoints Points to a sequence of (numpoints x 2) integers

Each pair of integers gives the x and y coordinates of a point on the polygon.

To draw a closed figure with N vertices, you must pass N+1 coordinates to drawpoly, where the Nth
coordinate == the 0th coordinate.

Return Value: None

TEXT MODE FUNCTIONS

window() <conio.h>

Defines active text-mode window

Declaration: void window(int left, int top, int right, int bottom);

The top left corner of the screen is (1,1).

Remarks:
window defines a text window onscreen. If the coordinates are in any way invalid, the call to
window is ignored.

(left, top) is the (x, y) position of the window's upper left corner.

(right, bottom) is the (x, y) position of the window's lower right corner.

The minimum size of the text window is one column by one line.

The default window is full screen, with these coordinates:


 80-column mode: (1, 1, 80, 25)
 40-column mode: (1, 1, 40, 25)

Return Value: None

/* window function example */


#include <conio.h>

void main(void)
{
clrscr();
window(10,10,20,20);
textcolor(BLACK);
textbackground(WHITE);
cprintf("This is a test\r\n");
getch();
}
gotoxy() <CONIO.H>

Positions cursor in text window

Declaration: void gotoxy(int x, int y);

Remarks:
gotoxy moves the cursor to the given position in the current text window.

If the coordinates are invalid, the call to gotoxy is ignored.


Example of invalid coordinates:
gotoxy(40,30) /* (35,25) = window's bottom right position */

Return Value: None


textattr, textbackground, textcolor <conio.h>

 textattr sets text attributes for text-window functions


 textbackground selects a new text background color
 textcolor selects a new character color in text mode

Declaration:
 void textattr(int newattr);
 void textbackground(int newcolor);
 void textcolor(int newcolor);
Remarks:
These functions work for functions that produce text-mode output directly to
the screen (console output functions).

 textattr sets both the foreground and background colors in a single call.
(Normally, you set the attributes with textcolor and textbackground.)

 textbackground selects the background color.

 textcolor selects the foreground character color.

These functions do not affect any characters currently on the screen. Once you have called one of
these three functions, all subsequent functions using direct video output (such as cprintf) will use the
new attributes or colors.

If you use symbolic color constants, the following limitations apply to the background colors you
select:
 You can only select one of the first eight colors (0--7).
 With textattr, you must shift the selected background color left by
4 bits to move it into the correct "bbb" bit positions.

 NOTE: If you use the symbolic color constants, you must include conio.h.

newattr:

This is how color information is encoded in the newattr argument of textattr:

7 6 5 4 3 2 1 0
B b b b f f f f

In this 8-bit newattr parameter,


ffff = 4-bit foreground color (0 to 15)
bbb = 3-bit background color (0 to 7)
B = blink-enable bit

Blinking characters:
If the blink-enable bit is on, the character blinks. To turn the bit on in a
call to textattr, you add the constant BLINK to the attribute.

To make the characters blink in a call to textcolor, you add 128 to the
foreground color. The predefined constant BLINK exists for this purpose; for
example,

textcolor(CYAN + BLINK);

textbackground and textcolor example


#include <conio.h>
/* PROGRAM TO DISPLAY TEXT WITH DIFFERENT BACKGROUND AND FOREGROUND
COLORS */
void main(void)
{
int i, j;

clrscr();
for (i=0; i<9; i++)
{
cprintf("C.W.I.T.");
cprintf("\r\n");
textcolor(i+1);
textbackground(i);
}
for (i=0; i<9; i++)
{
cprintf("C.W.I.T.");
cprintf("\r\n");
textcolor(i+1+BLINK);
textbackground(i);
}

Text attribute example

/* textattr example */
/* PROGRAM TO CHANGE TEXT ATTRIBUTE */

#include <conio.h>

void main(void)
{
int i;
// background color = CYAN = 3 = 011
// foreground color = RED = 4 = 0100
// blink true
// control word = 1(blink) 011 (background color) 0100 (foreground color)
// control word = 10110100 = 180 decimal

i=180;
clrscr();
textattr(i);
cprintf("This is a test\r\n");
}

text_modes <conio.h>

Enum: Standard video modes

Constant Value Text Mode

BW40 0 Black and white 40 columns


C40 1 Color 40 columns
BW80 2 Black and white 80 columns
C80 3 Color 80 columns

cprintf function

cprintf sends formatted output to the text window on the screen

Declaration:
int cprintf (const char *format [, argument, ...]);

NOTE : If textcolor or textbackground is changed then cprintf or cputs


functions must be used to display the text with colors

cputs function

Writes a string to the text window on the screen


Declaration: int cputs(const char *str);

Remarks:
cputs writes the null-terminated string str to the current text window. It
does not append a newline character.

S-ar putea să vă placă și