[转] Turbo C graphics function

xiaoxiao2021-03-06  14

Graphics function

Graphics function

Turbo C provides a very rich graphics function, all of which are in graphics. H

In this section, this section mainly introduces the initialization of graphics mode, the establishment of a separate graphics program, basic graphics function, diagram

Functions such as text output in graphics mode. In addition, make sure there is a display when using a graphical function.

Graphics LIB selected in Options / Linker at the same time

For ON, only this can only be used correctly.

1. Initialization of graphics mode

Different display adapters have different graphical resolutions. That is, the same display adapter, in different

There are also different resolutions in mode. Therefore, before the screen mapping, it must be based on the display adapter species.

The display is set to be a graphical mode. Before the graphical mode is not set, the microcomputer system default screen is text.

This mode (80 columns, 25 line character mode), all graphics functions cannot be worked. Set the screen for graphics

Mode, you can use the following graphical initialization functions:

Void Far INITGRAPH (Int Far * GDRIVER, INT FAR * GMODE, CHAR * PATH);

Where gDriver and gmode indicate graphics drivers and patterns, Path refers to the graphics driver.

Directory path. The symbol constant of the graphics drive, the graphics mode and the corresponding resolution are shown in Table 2.

The graphics driver is provided by the Turbo C publisher and the file extension is .bgi. According to different graphics

The adapter has a different graphics driver. For example, for EGA, VGA graphics adapter calls the driver

Egavga.bgi.

Table 2. Graphics drivers, mode symbol constants and values

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Graphics Drive (GDRIVER) Mode (GMODE)

──────────────────── Color resolution

Symbol constant numerical symbol constant value

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

CGA 1 CGAC0 0 C0 320 * 200

CGAC1 1 C1 320 * 200

CGAC2 2 C2 320 * 200

CGAC3 3 C3 320 * 200

CGAHI 4 2 color 640 * 200

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

MCGA 2 MCGAC0 0 C0 320 * 200

MCGAC1 1 C1 320 * 200

MCGAC2 2 C2 320 * 200

MCGAC3 3 C3 320 * 200

McGAMED 4 2 color 640 * 200

McGahi 5 2 color 640 * 480

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

EGA 3 EGALO 0 16 Color 640 * 200

EGAHI 1 16 color 640 * 350

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

EGA64 4 EGA64LO 0 16 Color 640 * 200

EGA64HI 1 4 color 640 * 350

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Egamon 5 Egamonhi 0 2 color 640 * 350 ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

IBM8514 6 IBM8514LO 0 256 color 640 * 480

IBM8514HI 1 256 color 1024 * 768

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Herc 7 Hercmonohi 0 2 color 720 * 348

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

ATT400 8 ATT400C0 0 C0 320 * 200

ATT400C1 1 C1 320 * 200

ATT400C2 2 C2 320 * 200

ATT400C3 3 C3 320 * 200

ATT400MED 4 2 color 320 * 200

ATT400HI 5 2 color 320 * 200

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

VGA 9 Vgalo 0 16 Color 640 * 200

VGAMED 1 16 color 640 * 350

VGAHI 2 16 color 640 * 480

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

PC3270 10 PC3270HI 0 2 color 720 * 350

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Detect 0 for hardware testing

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Example 4. Set VGA High Resolution Graphics Mode using graphical initialization functions

#include

int main ()

{

INT GDRIVER, GMODE;

GDRIVER = VGA;

Gmode = VGAHI;

INITGRAPH (& gdriver, & gmode, "c: // tc");

BAR3D (100, 100, 300, 250, 50, 1); / * Painting a long square * /

Getch ();

Closegraph ();

Return 0;

}

Sometimes the programmer does not know the type of graphic display adapter used, or the program that needs to be written

For different graphics drivers, Turbo C provides a function of automatically detecting monitor hardware, which calls

The format is:

Void Far DetectGraph (INT * GDRIVER, * GMODE);

The meaning of GDRIVER and GMODE is the same as above.

Example 5. During graphic initialization after the hardware test

#include

int main ()

{

INT GDRIVER, GMODE;

DetectGraph (& gDriver, & gmode); / * Automatic test hardware * /

Printf ("The Graphics Driver IS% D, Mode IS% D / N", GDRIVER,

Gmode); / * Output test results * /

Getch ();

INITGRAPH (& gdriver, & gmode, "c: // tc");

/ * Initialize graphics * / bar3d (10, 10, 130, 250, 20, 1) according to test results;

Getch ();

Closegraph ();

Return 0;

}

The graphic display is automatically detected in the above program, and then initialize the graphical initialization function.

Set, but Turbo C provides a simpler method, that is, use the GDriver = Detect statement to follow

The initgraph () function is OK. After this method, the above example can be changed to:

Example 6.

#include

int main ()

{

INT GDRIVER = Detect, GMODE;

INITGRAPH (& gdriver, & gmode, "c: // tc");

BAR3D (50, 50, 150, 30, 1);

Getch ();

Closegraph ();

Return 0;

}

In addition, Turbo C provides a function closegraph () with an exit pattern state, and its call format is:

Void Far Closegraph (Void);

After calling this function, you can exit the graphics status and enter the text mode (Turbo C default), and release

System memory for saving graphics drivers and fonts.

2. Establishment of independent graphics running programs

Turbo C For graphics initialization programs that use initgraph () functions, compile and link

There is no corresponding driver (* .bgi) to the executive, when the program is processed to intitgraph ()

When the statement, when the third form parameter char * specified in the third form, the path specified in the path specified in the path.

driver. If there is no driver, it will be found in the C: / TC, such as the C: / TC still not or Tc does not exist.

An error will occur:

BGI Error: Graphics Not Initialized (Use 'IITGRAPH')

Therefore, in order to use convenience, you should establish an executable to run independently without the need for a driver.

The graphics program, Turbo C specifies the following steps (here as an EGA, VGA display as an example):

1. Enter the command in the C: / TC subdirectory: BGIOBJ Egavga

This command converts the driver Egavga.bgi into the target file of Egavga.obj.

2. Enter the command in the C: / TC subdirectory: Tlib LIB / Graphics.lib Egavga

This command means that the target module of Egavga.obj is put into the Graphics.lib library file.

3. Plus a sentence before the initgraph () function call in the program:

Registerbgidriver: Egavga_Driver:

This function tells the connection program to load the EGAVGA driver into the user's executable when the connection is connected.

After processing, the execution program after compiling links can be run on any directory or other compatible machine.

Assuming that the first two steps have been made, if the REGISTERBGIDRIVER () function is added to the Example 6:

Example 7:

#include

#include

int main ()

{

INT GDRIVER = Detect, GMODE;

RegisterBgidriver: / * Create a separate graphic running program * /

INITGRAPH (GDRIVER, GMODE, "C: // TC");

BAR3D (50, 50, 250, 150, 20, 1);

Getch ();

Closegraph ();

Return 0;

}

The execution program generated after the above example is built separately.

If it is not initialized into EGA or CGA resolution, it is only necessary to initialize the CGA resolution.

In the step of EGAVGA, it can be replaced by CGA.

3. Settings and clearing screen functions for screen colors

For the screen color settings of the graphic mode, the settings and foreground color settings are divided into background colors. in

The following two functions are used below Turbo C.

Set background color: Void Far SetBkcolor (Int Color);

Set the graphic color: Void Far setColor (int color); where Color is a specified value of the color of the graphic mode, the EGA, VGA display adapter, related

The symbol constant and numerical value of the color are shown in the table below.

Table 3 Symbol constant tables about screen colors

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Symbol constant numerical meaning symbol constant numerical meaning

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Black 0 black Darkgray 8 dark gray

Blue 1 blue lightblue 9 darkland

Green 2 Green LightGreen 10 light green

CYAN 3 cyan lightcyan 11 light blue

Red 4 red lightred 12 light red

Magenta 5 magenta LightMagenta 13 dawn

Brown 6 Brown Yellow 14 Yellow

Lightgray 7 Dollar White 15 White

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

For the CGA adapter, the background color can be one of the 16 colors in Table 3, but the foreground is dependent on

Palette. There are four palettes, and there are four colors available on each palette. Different palette

The corresponding primary color is shown in Table 4.

Table 4 CGA palette and color value table

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Palette color value

───────────────────────────

Symbol constant value 0 1 2 3

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

C0 0 background green red yellow

C1 1 Background Qingyanghong White

C2 2 background light red yellow

C3 3 background light blue

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Clear the graphical screen content Use the clear screen function, which call the format as follows:

Voide Far ClearDevice (Void);

For color settings, use of clear screen functions, see Example 8.

Example 8:

#include

#include

int main ()

{

INT GDRIVER, GMODE, I;

gdriver = detect;

Registerbgidriver (egavga_driver); / * Establish a separate graphic running program * /

INITGRAPH (& gdriver, & gmode "," "); / * Graphics initialization * /

Setbkcolor (0); / * Set graphic background * /

ClearDevice ();

For (i = 0; i <= 15; i ) {

SetColor (i); / * Set different graphic color * /

Circle (320, 240, 20 i * 10); / * Different circles with different radiies * /

Delay (100); / * Delay 100 milliseconds * /

}

For (i = 0; i <= 15; i )

{

SetBkcolor (i); / * Set different background color * /

ClearDevice ();

Circle (320, 240, 20 i * 10);

DELAY (100);

}

Closegraph ();

Return 0;

}

In addition, Turbo C also provides several functions that obtain current color settings.

INT FAR GETBKCOLOR (Void); Returns the current background color value.

INT FAR getColor (Void); returns the current mapping color value.

INT FAR getMaxColor (Void); Returns the highest available color value.

4. Basic graphics function

The basic graphics functions include a function of drawing points, lines, and other basic graphics. This section is for these functions

Make a comprehensive introduction.

First, draw points

Draw point function

Void Far Putpixel (int x, int y, int color);

This function represents a point that is specified by a specified element that is determined by Color. For color color

The value can be obtained from Table 3 and the X, Y is the coordinates of the picture element.

In graphics mode, it is defined according to the icon. For VGA adapters, its highest resolution is

640x480, where 640 is the number of all aspects of the left to right, 480 is from top to

Number of all aspects of the elephant. The upper left corner coordinate of the screen is (0, 0), the lower right corner coordinate is (639, 479), water

The square direction is forward from left to right as an x-axis, and the vertical direction is from top to bottom as the Y axis. Turbo C graphics function

It is relative to the graphic screen coordinates, that is, the like.

Another function about the point is:

Int far getpixel (int x, int y);

It obtains color values ​​of current points (x, y).

2. Functions related to coordinate positions

Int Far getMaxx (Void);

Returns the maximum value of the x-axis.

INT FAR GETMAXY (VOID);

Returns the maximum value of the Y axis.

Int Far getx (void);

Returns the position of the cursor at the X axis.

Void Far gety (void);

Returns the position where the cursor has a Y axis.

Void Far MoveTo (int X, int y);

Mobile cursors to (x, y), not painting points, can also draw in the mobile process.

Void Far Moverel (int Dx, int DY);

The mobile cursor moves from the current location (x, y) to the position of (x DX, Y DY), and does not have a point during the movement.

Second, the line

Picture of line functions

Turbo C provides a series of line functions, which are described below:

Void Far Line (int X0, int y0, int x1, int y1);

Draw a straight line from points (X0, Y0) to (X1, Y1).

Void Far Lineto (int X, int y);

Draw a straight line from the current cursor to the point (x, y).

Void Far LineRel (int DX, INT DY);

Draw a straight line from the current cursor (X, Y) to the relative increment (X DX, Y DY).

Void Far Circle (int X, int y, int RADIUS);

With (x, y) is round, RADIUS is a radius, draw a circle.

Void Far Arc (int X, int y, int in);

With (x, y) is round, radius is a radius, starting from STANGLE to endangle (usage)

Draw a circular arc. In Turbo C, the x-axis is specified in 0 degrees, and the counterclockwise direction is rotated one week, and it is 90, 180, 270, and 360 degrees (other related functions, it is not repeated).

Void Ellipse (int X, int y, int tel, int end, int xradius,

Int Yradius);

Take (x, y), XRADIUS, YRADIUS is the X-axis and Y-axis radius, from the angle Stangle to

Endangle ends a section of an elliptical line, when stangle = 0, Endangle = 360, draw a complete

oval.

Void Far Rectangle (INT X1, INT Y1, INT X2, INTY2);

With (x1, y1) of the upper left corner, (x2, y2) is a rectangular box in the lower right corner.

Void Far DrawPoly (int Numpoints, int far * polypoints);

Draw a vertex number is numpoints, the vertex coordinates are given by polypoints.

The Polypoints integer array must have at least 2 times a few apex. The coordinates of each vertex are defined as X,

Y, and x is before. It is worth noting that when a closed polygon is painted, Numpoints' value is actually

The number of vertices of the polygon is added, and the first and the coordinates of the first point in the array PolyPoints are the same.

The following is an example of drawing arrows with a drawpoly () function.

Example 9:

#include

#include

int main ()

{

INT GDRIVER, GMODE, I;

INT ARW [16] = {200, 102, 300, 102, 300, 107, 330,

100, 300, 93, 300, 98, 200, 98, 200, 102};

gdriver = detect;

RegisterBgidriver; Egavga_Driver;

INITGRAPH (& GDriver, & gmode, ");

Setbkcolor (Blue);

ClearDevice ();

SetColor (12); / * Set the color * /

DrawPoly (8, ARW); / * Draw an arrow * /

Getch ();

Closegraph ();

Return 0;

}

2. Set the line type function

Turbo C uses its default line before setting the characteristics of the line, namely a wide solid line,

However, Turbo C also provides functions that can change line type. Line types include: width and shape. Where the width is only

Two choices: a little width and three-point width. There are five shapes in the shape of the line. Let's introduce a letter about line type

number.

Void Far SetLinesTyle (int LineStyle, unsigned upattern, int

Thickness);

This function is used to set the information of the line, where lineestyle is a line shape, see Table 5.

Table 5. The shape of the related line (LINESTYLE)

━━━━━━━━━━━━━━━━━━━━━━━━━

Numerical meaning of symbol constant

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── /

Solid_line 0 solid line

Dotted_Line 1 Layer

Center_Line 2 Center Line

Dashed_Line 3 Picture Line

Userbit_line 4 user-defined line

━━━━━━━━━━━━━━━━━━━━━━━━━

Thickness is the width of the line, see Table 6.

Table 6. Related line width (Thickness)

━━━━━━━━━━━━━━━━━━━━━━━━━

Number of symbol constants - ──────────────────

NORM_WIDTH 1 little width

Thic_width 3 three-point wide

━━━━━━━━━━━━━━━━━━━━━━━━━

For Upattern, only the LINESTYLE selection userbit_line is meaningful (select other line style,

Uppattern takes 0). Every one of the 16-bit binary counts of UPPATTERN represents a voyage, such as

The bit is 1, then the element is turned on, otherwise the icon is closed.

Void Far getLineSettings (Struct Linesettingstype Far * LineInfo);

This function stores information about the line to the structure pointed to by LineInfo, in the table

The structure of LinesettingStype is as follows:

Struct LinesettingStype {

Int lineestyle;

Unsigned Upattern;

Int thickness;

}

For example, the following two sentences can read the characteristics of the current line.

Struct LinesettingStype * Info;

GetLineSettings (Info);

Void Far Setwritemode (int M);

This function specifies the way the line is drawn. If Mode = 0, the original information will be drawn when the line is drawn.

Covered (this is the default mode of Turbo C). If Mode = 1, the line with the current characteristics when the line is drawn.

Different or xor operations with the original lines drawn, the line actually drawn is the original line and the present

The line is different or the result. Therefore, when the characteristics of the wire are unchanged, the two line operations are not equal.

Picture.

Examples of line type settings and line functions are shown below.

Example 10.

#include

#include

int main ()

{

INT GDRIVER, GMODE, I;

gdriver = detect;

RegisterBgidriver; Egavga_Driver;

INITGRAPH (& GDriver, & gmode, ");

Setbkcolor (Blue);

ClearDevice ();

SetColor (Green);

Circle (320, 240, 98);

SetLinesTyle (0, 0, 3); / * Set three-point wide solid line * /

SetColor (2);

Rectangle (220, 140, 420, 340);

SetColor (White);

SetLinesTyle (4, 0xAaaa, 1); / * Set a bit wide user-defined line * /

LINE (220, 240, 420, 240);

LINE (320, 140, 320, 340);

Getch ();

Closegraph ();

Return 0;

}

5. Close the fill of the graph

The padding is filled with a predetermined color and a pattern mold.

First, draw the outline to fill

Turbo C provides some drawing of basic graphical outlines, and then populates the entire seal according to the specified map.

Closed graphic function. Turbo C fills by default when there is no filling mode. Here is this

Some functions.

Void Far Bar (int X1, int y1, int x2, int y2);

Determine a rectangular window (x1, y1) as the upper left corner, (x2, y2) is the rectangular window in the lower right corner, and then press the specified image

Mold and color fill.

Note: This function does not draw a border, so fills the color as a border.

Void Far Bar3d (int X1, int y1, int x2, int y2, int defth, int in

Topflag);

Draw a three-dimensional rectangular square when topflag is not 0. When topflag is 0, the three-dimensional graphic is not

Capacity, actually use this.

Description: In the BAR3D () function, the direction of the third dimension of the rectangular square does not change, ie the direction of 45 degrees.

Void Far Piemelice (int X, int y, int standle, int endangle, int

RADIUS);

Draw a center with (x, y), radius is a radius, and stangle is the starting point, endangle is

Fill the angle, fill it according to the specified manner. When stangle = 0, endangle = 360 becomes one

The solid circle and painted a radius along the X-axis in the circle.

Void Far Sector (int X, int y, int stanle, intendangle, int in

XRADIUS, INT YRADIUS;

Draw anx in XRADIUS, YRADIUS as Xradius, Yradius, respectively, STANGLE

For the starting angle, Endangle is an elliptical fan shape of the termination angle, and then populates according to the specified manner.

Second, set the filling method

Turbo C has four functions related to filling. The following introduces:

Void Far SetFillStyle (int pattern, int color);

The value of the color is the valid value of the color when the current screen graphics mode. Pattern's value and equivalent

The symbol constant is shown in Table 7.

Table 7. Regulations on Filled Model Pattern

━━━━━━━━━━━━━━━━━━━━━━━━━━━

Numerical meaning of symbol constant

─────────────────────

EMPTY_FILL 0 filled with background color

Solid_fill 1 with real filled

LINE_FILL 2 is filled in line

LTSLASH_FILL 3 is filled with oblique lines (shadow lines)

Slash_fill 4 filled with a crude slant (coarse shadroom)

Bkslash_fill 5 filled with crude reselling (coarse shadroom)

LTBKSLASH_FILL 6 is filled with anti-oblique line (shadow line)

Hatch_fill 7 fills in a straight grid

XHATCH_FILL 8 is filled with oblique mesh

IntTerLeave_fill 9 is filled with interval points

WIDE_DOT_FILL 10 is filled with sparse points

Close_DOS_FILL 11 is filled with intensive points

USER_FILL 12 fills in user-defined models

━━━━━━━━━━━━━━━━━━━━━━━━━━━

In addition to user_fill (user-defined fill model), other fill models can be set by setFillStyle ()

Function settings. When elect User_Fill, the function does not change any changes to the padding pattern and the color. Reason

To define User_Fill mainly because this item is used when obtaining the filler information.

Void Far setfillPattern (Char * Upattern, Int Color);

Set the color of the user-defined padding drawing to be filled with the closed graphic.

Where upattern is a pointer to 8 bytes. These 8 bytes define 8x8 dot matrix graphics.

The 8-bit binary number of each byte represents 8 points, 8 bytes represent 8 lines, then use this as a model to a seal.

Closed area fill.

Void Far getfillpattern (Char * Upattern);

This function stores the user-defined padding diagram into the memory area pointing to the Upattern pointer.

Void Far getFillSetings (Struct FillSettingstype Far * FillInfo);

Get the color of the current map mold and store the structural pointer variable Fillinfo. FillSettingStype

The structure is defined as follows:

Struct FillSettingStype {

INT PATTERN; / * Current Fill Mode * / INT Color; / * Current Fill Mode * /

}

See the routine below for the color of the color of the graphic fill map.

Example 11:

#include

MAIN () {

Char STR [8] = {10, 20, 30, 40, 50, 60, 70, 80}; / * User Defining Timulation * /

INT GDRIVER, GMODE, I;

Struct FillSettingSType Save; / * Defines a structural variable used to store fill information * /

gdriver = detect;

INITGRAPH (& gdriver, & gmode, "c: // tc");

Setbkcolor (Blue);

ClearDevice ();

For (i = 0; i <13; i )

{

SetColor (i 3);

SetFillStyle (i, 2 i); / * Set the fill type *

BAR (100, 150, 200, 50); / * Painting Rectangle and Fill * /

BAR3D (300, 100, 500, 200, 70, 1); / * Calling the square and fill * /

Piemelice (200, 300, 90, 180, 90); / * Film-shaped and filled * /

Sector (500, 300, 180, 270, 200, 100); / * Painting the ellipse fan and fill * /

DELAY (1000); / * delay 1 second * /

}

ClearDevice ();

SetColor (14);

SetFillPattern (STR, RED);

BAR (100, 150, 200, 50);

BAR3D (300, 100, 500, 200, 70, 0);

Piemelice (200, 300, 0, 360, 90);

Sector (500, 300, 0, 360, 100, 50);

Getch ();

getFillSettings (& Save); / * Get user-defined fill mode information * /

Closegraph ();

CLRSCR ();

Printf ("THE PATTERN IS% D, The Color Of Filling IS% D",

Save.pattern, save.color); / * Output Current Plug Mode and Color Value * /

Getch ();

}

After the above program runs, the constant value of the row-filled drawing and color is displayed on the screen.

Third, the filler of any closed graph

As of now, we can only fill the closed graphics of some specific shapes, but it is not

Any closed graph is filled. To this end, Turbo C provides a letter that can be filled with any closed graphics.

Number, its call format is as follows:

Void Floodfill (int X, int y, int border);

Where: x, y is any point in the closed graph. Border is the color of the boundary, that is, a closed picture

Shaped profile color. When the function is called, the entire closed graph will be filled with the specified color and diaphragm.

note:

1. If x or y is taken on the boundary, it will not be filled.

2. If it is not a closed graph, the fill will overflow from the place where the closed place will be overfill it else.

3. If x or y is outside the graph, the screen area outside the closed graph is populated.

4. The color value specified by the border must be the same as the color value of the graphical contour, but the filler is selectable.

The color. The following example is the usage of the floodfill () function, which is filled with BAR3D ()

The other two unfilled faces.

Example 12:

#include

#include

Main ()

{

INT GDRIVER, GMODE;

STRCT FILLSETTINGSTYPE SAVE;

gdriver = detect;

INITGRAPH (& GDriver, & gmode, ");

Setbkcolor (Blue);

CLEARDEVICE (); setColor (Lightred);

SetLinesTyle (0,0,3);

SetFillStyle (1,14); / * Set filling mode * /

BAR3D (100, 200, 400, 350, 200, 1); / * Calling the square and fill * /

FLOODFILL (450, 300, lightred); / * Filled the rectangular square two sides * /

FLOODFILL (250, 150, lightred);

Rectanle (450, 400, 500, 450); / * Painting a rectangle * /

FLOODFILL (470, 420, LightRed); / * Fill rectangle * /

Getch ();

Closegraph ();

}

6. About the graphics window and graphics screen operation functions

First, graphics window operation

Like a screen window, you can also be on a certain area on the screen in a graphical mode.

Set the window, just set the graphical window, and the subsequent graphics operation will be in this window.

The upper left corner (0, 0) is used as the coordinate origin, and can be in contact with the area other than the window.

Thus, all graphical operations are defined within the window.

Void Far SetViewPort (int XL, int yl, int x2, int y2, int clipflag);

Set a graphical window with a (XL, YL) element of the upper left corner, (x2, y2) is the lower right corner,

In X1, Y1, X2, Y2 are coordinates relative to the entire screen. If clipflag is non-0, the set graphics are set.

Partially cannot be contacted, if clipflag is 0, the graphics window can be in contact.

Void Far ClearViewPort (Void);

Clear the content of the current graphics window.

Void Far getViewSettings (Struct ViewPortType Far * ViewPort);

Get information about the current window and store it in ViewPortType defined structural variables Viewport

In, the structural description of ViewPortType is as follows:

Struct viewporttype {

INT LEFT, TOP, RIGHT, BOTTOM

Int cliplag;

}

Note:

1. Setting the window color settings as the previous screen color setting, but the screen background color and windows

The background colors can only be a color, if the window background is changed, the background color of the entire screen will also change this with

Text window is different.

2. You can set multiple windows on the same screen, but you can only have a current window work,

Other window operations can be used once by defining the setViewPort () function of that window.

3. The functions of the graphics screen operation are suitable for the operation of the window.

Second, the screen operation

In addition to the clear screen function, there is still the following functions regarding the screen operation:

Void Far setActivePage (int Pagenum);

Void Far SetvisualPage (int Pagenum);

These two functions are only used in EGA, VGA, and Hercules graphics adapters. SetctivePage () function

Is the activation page for the graphic output. The so-called activation page refers to the output of the subsequent graphics being written to the function.

Pagenum page, this page is not necessarily visible. SetvisualPage () function to make Pagenum specified

The page turns into a visible page. The page starts from 0 (Turbo C Default page). If you first use setActivePage ()

The function draws an image on different pages, and use the setvisualpage () function to alternately display, you can

Implement some animations.

Void Far GetImage (int xl, int yl, int x2, int y2, void far * mapbuf);

Void Far Putimge (INT X, INT, Y, VOID * MAPBUF, INTOP);

Unsined Far ImageSize (int XL, int yl, int x2, int y2);

These three functions are used to copy images on the screen to memory, and then send the image in the memory back to

on the screen. First, through the function imageSize () test, save all the contents of the top left corner (XL, YL), the upper right corner (x2, y2) graphics screen area, and then assign a measured by MapBuf

Number of memory spatial pointers. Save the image in this area by calling the getImage () function

In the memory, you can use the powdiMage () function to output the image to the point (x, y) position,

Where the parameter op in the getImage () function specifies how to release the memory in memory.

See Table 8 for the definition of this parameter.

Table 8. OP value in the power () function

━━━━━━━━━━━━━━━━━━━━━━━━━━

Number of symbol constants

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── /

COPY_PUT 0 replication

XOR_PUT 1 with screen image is different or replication

OR_PUT 2 with the screen image or post copy

AND_PUT 3 with screen images and post copy

NOT_PUT 4 Copy the image of the object

━━━━━━━━━━━━━━━━━━━━━━━━━━

For the imageSize () function, only the image area of ​​the number of bytes is less than 64K bytes, otherwise it will

Error returns -1 when an error is wrong.

The functions described in this section are very useful in image animation processing, menu design techniques.

Example 13: The following program simulates two mutual dynamic collision process.

#include

#include

int main ()

{

INT I, GDRIVER, GMODE, SIZE

Void * BUF;

gdriver = detect;

INITGRAPH (& GDriver, & gmode, ");

Setbkcolor (Blue);

ClearDevice ();

SetColor (Lightred);

SetLinesTyle (0,0,1);

SetFillStyle (1, 10);

Circle (100, 200, 30);

FLOODFILL (100, 200, 12);

Size = imageSize (69, 169, 131, 231);

BUF = malloc (size);

GetImage (69, 169, 131, 231, buf);

PutiMage (500, 269, buf, copy_put);

For (i = 0; i <185; i ) {

PUTIMAGE (70 i, 170, buf, copy_put);

PUTIMAGE (500-i, 170, buf, copy_put);

}

For (i = 0; i <185; i ) {

PUTIMAGE (255-i, 170, buf, copy_put);

PUTIMAGE (315 i, 170, buf, copy_put);

}

Getch ();

Closegraph ();

}

7. Text output in graphics mode

In graphics mode, only standard output functions, such as printf (), PUTS (), PUTCHAR ()

The number output text to the screen. In addition, other output functions (such as window output functions) cannot be used, ie

The standard function that can be output is also available in white, and outputs 80 columns, 25 lines of text.

Turbo C2.0 also provides some text output functions specifically for in graphic display mode. the following

The presentation will be described separately.

First, text output function

Void Far Outtext (Char Far * TextString);

This function outputs the text referred to in the string pointer TextString in the current location.

Void Far Outtextxy (int X, int y, char far * textstring);

This function outputs the text referred to in the string pointer TextString in the specified (x, y) position. Where X and Y are like a metallic coordinate.

Description:

These two functions are output strings, but they often encounter output value or other type of data.

At this point, you must use the formatted output function sprintf ().

The call format of the sprintf () function is:

INT Sprintf (Char * STR, Char * Format, variable-list);

It is different from the printf () function to write the string of Str to the content specified by the format.

In the middle, the return value is equal to the number of characters written.

E.g:

Sprintf (s, "Your Toefl Score% D", Mark);

Here S should be a string pointer or array, and Mark is an integer variable.

Second, the setting of text font, font, and output

For text output functions in graphical mode, you can set the output text through the setColor () function.

colour. Alternatively, you can change the size of the text font and the selection is the horizontal direction output or the vertical direction.

Out.

Void Far setTexjustify (int horiz, int us);

This function is used to locate the output string.

Output output using OutTextxy (int X, int y, char far * strsytstring) function

The string, which point corresponds to the positioning coordinates (X, Y) in Turbo C2.0. in case

Take a string as a rectangular graphic, displayed in the horizontal direction, the string long square shape is vertical

The direction can be divided into three positions in the top, the middle and bottom, and the horizontal direction can be divided into left, middle, and right.

There are 9 positions in both combination.

The first parameter of the setTextJustify () function horiz pointed out one of the three positions in the horizontal direction.

The second parameter VERT indicates one of the three positions in the vertical direction, and the two determine one of them.

When this location specifies this location, the string is output when the string is output with the outtextxy () function.

The predetermined location is the (x, y) position in the function. This is used to output a string with an outtext () function, this

A specified location is located in the current cursor. See Table 9 for the value of parameters Horiz and VERT.

Table 9. Value of parameters Horiz and VERT

━━━━━━━━━━━━━━━━━━━━━━━

Number of symbol constants is used for

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── /

LEFT_TEXT 0 level

Right_Text 2 level

Bottom_text 0 vertical

TOP_TEXT 2 vertical

Center_text 1 horizontally or vertical

━━━━━━━━━━━━━━━━━━━━━━━

Void Far SetTextStyle (int Font, int Direction, int CHARSIZE);

This function is used to set the glyph of the output character (determined by font), the output direction (determined by the Direction)

And character size (determined by Charsize). Turbo C2.0 is shown in various parameters in functions

The columns shown in each table:

Table 10. Value of Font

━━━━━━━━━━━━━━━━━━━━━━━

Numerical meaning of symbol constant

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── /

Default_font 0 8 * 8-point quaque (default)

Triplex_Font 1 triple pen pen

Small_Font 2 small stroke font

Sansserif_Font 3 noble stroke font

Gothic_Font 4 black pen

━━━━━━━━━━━━━━━━━━━━━━━

Table 11. Direction of Direction

━━━━━━━━━ 含 含 含 含 含 含 ━ 常 含 meaning

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── /

Horiz_dir 0 from left to right

Vert_dir 1 from bottom to top

━━━━━━━━━━━━━━━━━━━━━━━

Table 12. Value of Charsize

━━━━━━━━━━━━━━━━━━━━━━━

Symbol constant or numerical meaning

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── /

1 8 * 8 o'clock

2 16 * 16 o'clock

3 24 * 24 o'clock

4 32 * 32 o'clock

5 40 * 40 o'clock

6 48 * 48 o'clock

7 56 * 56 o'clock

8 64 * 64 o'clock

9 72 * 72 o'clock

10 80 * 80 o'clock

User_char_size = 0 User-defined character size

━━━━━━━━━━━━━━━━━━━━━━━

If you look at the text output and font type setting function under the graphics screen, please see the case:

Example 14:

#include

#include

int main ()

{

INT I, GDRIVER, GMODE;

Char s [30];

gdriver = detect;

INITGRAPH (& GDriver, & gmode, ");

Setbkcolor (Blue);

ClearDevice ();

SetViewPort (100, 100, 540, 380, 1); / * Define a graphics window * /

SetFillStyle (1, 2); / * Green with real estip * /

SetColor (YELLOW);

Rectangle (0, 0, 439, 279);

FLOODFILL (50, 50, 14);

SetColor (12);

SetTextStyle (1, 0, 8); / * Triple pen drawing font, magnification 8 times * /

Outtextxy (20, 20, "good better");

SetColor (15);

SetTextStyle (3, 0, 5); / * Unlining stroke font, horizontal magnification 5 times * /

Outtextxy (120, 120, "good better");

SetColor (14);

SetTextStyle (2, 0, 8);

i = 620;

Sprintf (s, "Your score is% d", i); / * Transform numbers to strings * /

OutTextxy (30, 200, s); / * Specify location output string * /

SetColor (1);

SetTextStyle (4, 0, 3);

Outtextxy (70, 240, s);

Getch ();

Closegraph ();

Return 0;

}

Third, the user settings for text characters

The setTextStyle () function introduced earlier can set the font of the text characters in a graphic mode.

And size but for stroke fonts (in addition to 8 * 8-point words), only horizontal and vertical direction

The same magnification is amplified. To this end, Turbo C2.0 provides another setUsercharsize () letter.

Number, the stroke font can set the magnification of the magnification in the horizontal and vertical directions, respectively. The modulation format of this function is:

Void Far Setusercharsize (int Mulx, Int Divx, Int Muly, INT Divy); This function is used to set the stroke and magnification factor, which is only in the setTextStyle () function.

Charsize played for 0 (or user_char_size), and the font is a function setTextStyle ()

The specified font. After calling the function setUsercharsize (), each displayed on the screen is

The default size is multiplied by MULX / DIVX to output character wide, multiplying MULY / DIVY to output characters high. Functional

See the example below.

Example 15:

#include

#include

int main ()

{

INT GDIRVER, GMODE;

GDRIVER = DETETC;

INITGRAPH (& GDriver, & gmode, ");

Setbkcolor (Blue);

ClearDevice ();

SetFillStyle (1, 2); / * Set filling mode * /

SetColor (White); / * Set white chart * /

Rectangle (100, 100, 330, 380);

FLOODFILL (50, 50, 14); / * Area outside the fill box * /

SetColor (12); / * makes the color to light red * /

SetTextStyle (1, 0, 8); / * Triple pen drawing font, zoom in 8 times * /

Outtextxy (120, 120, "very good");

Setusercharsize (2, 1, 4, 1); / * level is magnified 2 times, vertical zoom 4 times * /

SetColor (15);

SetTextStyle (3, 0, 5); / * No lineage stroke, zoom in 5 times * /

Outtextxy (220, 220, "very good");

Setusercharsize (4, 1, 1, 1);

SetTextStyle (3, 0, 0);

Outtextxy (180, 320, "good");

Getch ();

Closegraph ();

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-49841.html

New Post(0)