C language Graphic programming (II)-Graphic Display

Source: Internet
Author: User
1. Confirm the display card
2. Select the Display Mode
3. Graphic Display
20. detectgraph () display card Detection Function
21. initgraph () graph initialization Function
22. getdrivername () gets the pointer of the graphic driver name
23. getgraphmode (): used to obtain the graph mode function
24. getmoderange () gets the mode value range Function
25. getmaxmode () gets the maximum display mode function
26. getmodename () gets the display mode name Function
27. graphdefaults () Graphical reset Default Value Function
28. setgraphmode () sets the graphic mode function
29. restorecrtmode () restores the text display mode function
30. graphresult () graphic operation result Function
31. grpaherrormsg () graphic error message function
32. setgraphbufsize (): Specifies the image buffer size function.
33. setactivepage () sets the function to output the display page.
34. setvisualpage () sets the number of visible pages
35. closegraph () disables the graph mode function.

1. Confirm the display card
The display part of the microcomputer system consists of the monitor and the adapter. A display is an external device independent of a host. It is either a display card or a display adapter card, or a graphic card. It is a circuit board inserted on the host. However, some display cards are designed together with the motherboard. The display card consists of three parts: register group, memory and control circuit. The memory contains two parts: Ram and rom bios. The microcomputer performs all operations on the display screen through the display card. Therefore, to display images, you must first determine which display card is installed on the computer. One way is to ask a computer or end user to determine the type of display card to be used. This method is difficult, because even professional programmers cannot always determine what kind of hardware he is using. Another method is to use software to query hardware to identify the current configuration. If there are some hardware identification standards, this is easy. Borland C provides support for the various display cards currently used. You only need to call the detectgraph () function, which determines the type of display card used on the computer for the programmer.

2. Select the Display Mode
The display mode refers to the resolution supported by the display card and the corresponding color configuration. Each computer is configured with a certain type of display card, you can specify the display mode for this display card.

3. Graphic Display
To display the image, first determine the display card and then select its display mode. All these work can be done by calling graphical functions. In fact, it is to load the graphics driver suitable for the display card into the memory. If the graphic driver is not loaded into the memory, the graphic functions cannot be operated.

20. detectgraph () display card Detection Function
Function: The detectgraph () function is used to determine the display card type when a display card is installed on a computer.
Usage: This function is called in void detectgraph (int * driver, int * mode );
Note: The function sets the integer variable pointed to by the driver as the code of the graphic driver, set the integer variable pointed to by mode to the maximum valid mode supported by the display card (that is, the maximum resolution supported by the display card ).
The function header file is graphics. h.
Return Value: return the code (also known as equal value) that is suitable for the graphics driver of the display card, and place it in the variable pointed to by the driver. If no graphic hardware exists in the computer system, set the variable pointed to by the driver to-2.
For example, check whether the computer has a display card:
Int driver, mode;
Detectgraph (& driver, & mode );
If (driver =-2)
{
Printf ("no graphics adapter in the computer ");
Exit (1 );
}

21. initgraph () graph initialization Function
Function: The initgraph () function is used to initialize the graphics system, load the appropriate graphics driver into the memory, and set the display mode used by the graphic function.
Usage: Call the function in void initgraph (int * driver, int * mode, char * path );
(1) The parameter driver in the function points to the graphic Driver (or other value ).
In the header file graphics. H, the macro name of the graphic driver is defined as follows:
Macro name and other values
------------------------------
Detect 0
CGA 1
Mcga 2
Limit 3
Ega64 4
Egamono 5
Ibm8514 6
Hercmono 7
Att400 8
VGA 9
Pc3270 10
--------------------------------

Note: In the current computer, many display cards in the table above are out of date and are no longer used.

In this way, you don't have to worry about the accurate name of the graphic driver, but you just need to use the same value. If detect (equal value 0) is used, initgraph () the function automatically detects the display card type installed in the current computer and selects the display mode with the maximum resolution.
2) the mode parameter points to the display mode or the equivalent value (from 1 to 5 in the wind table). The value indicated by the mode must be one of the valid modes.
3) The parameter path points to the path of the graphic driver, for example, Path = "C:/TC/Bg1". If no path is specified, search for it in the current directory.
Generally, graphics drivers are included in. BGI files, which can be obtained by the system. The header file corresponding to this function is graphics. h.
Return Value: None
For example, call initgraph () to automatically detect the hardware graphics system and select the maximum resolution display mode.
Init drver, mode;
Driver = detect;
Mode = 0;
Initgraph (& driver, & mode ,"");

22. getdrivername () gets the pointer of the graphic driver name
Function: getdrivername () returns a pointer to the current graph driver name.
Usage: This function is called using char * getdrivername (void );
Note: this function can be used to detect the display card, but can only be called after the graph driver and display mode are set in initgraph.
The function header file is graphics. h.
Return Value: returns a pointer to a string containing the name of the current graphic driver.
Example: display the current graphic driver name:
# Include <graphics. h>
# Include <stdio. h>
Void main ()
{
Int graphdriver = detect, graphmode;
Char * s;
Initgraph (& graphdriver, & graphmode ,"");
S = getdrivername ();
Outtext ("using DRIVER :");
Outtext (s );
Getch ();
Closegraph ();
}

23. getgraphmode (): used to obtain the graph mode function
Function: The getgrapgmode () function returns the current graph mode.
Usage: The function is called in int getgraphmode (void );
Note: The getgraphmode () function has no parameter to call.
The header file corresponding to this function is graphics. h.
Return Value: returns the current graph mode value set by initgraph () or setgraphmode.
Example: code for displaying the current graph mode:
Printf ("graphics mode is % d", getgraphmode ());

24. getmoderange () gets the mode value range Function
Function: The getmoderange () function returns the minimum and maximum modes supported by the specified graphic driver.
Usage: function call method: void getmoderange (INT driver, int * lowmode, int himode );
Description: When the parameter driver is used to call this function, it specifies the value or macro name such as the graphic driver.
The valid values are as follows:
-------------------------------------
Macro name and other values
-------------------------------------
Detect 0
CGA 1
Mcga 2
Limit 3
Ega64 4
Egamono 5
Ibm8514 6
Hercmono 7
Att400 8
VGA 9
Pc3270 10
-------------------------------------
Note: Many of the displays in the table above are outdated on the current computer and are for reference only.

The header file for this function is graphics. h.
Return Value: the minimum and maximum values returned are stored in the integer variables indicated by the lowmode and himode parameters respectively.
Example: display the range value of the display mode of the current graphic hardware:
# Include "graphics. H"
Void main ()
{
Int driver, mode;
Int low, high;
Mode = 0;
Initgraph (& driver, & mode ,"");
Getmoderange (driver, & low, & High );
Printf ("mode range: % d -- % d, low, high );
Getch ();
Restorecrtmode ();
}

25. getmaxmode () gets the maximum display mode function
Function: The getmaxmode () function returns the maximum valid Display Mode Supported by the currently loaded graphics driver.
Usage: The function call method is int getmaxmode (void );
Note: This function is called without parameters and returns the maximum Display Mode Supported by the graphic driver. All graphic drivers support the display mode from 0 to getmaxmode () any integer in the function return value (corresponding display mode ).
The header file corresponding to this function is graphics. h.
Return Value: returns the maximum value of the display mode supported by the graphic driver.
For example, display the current maximum display mode:
Printf ("The maxmode of this driver is % d/N", getmaxmode ());

26. getmodename () gets the display mode name Function
Function: The getmodename () function obtains the name of the specified display mode supported by the current graphic driver.
Usage: This function is called using char * getmodename (INT modecode );
Description: The modecode parameter is an integer. This function is used to specify a display mode value.
Graphics. h header file corresponding to this function
Returned value: the display mode name of the specified mode value.
Example: display the current maximum display mode Name:
# Include <graphics. h>
# Include <stdio. h>
Void main ()
{
Int graphdriver = detect, graphmode;
Int modecode;
Char * s;
Initgraph (& graphdriver, & graphmode ,"");
Modecode = getmaxmode ();
S = getmodename (modecode );
Outtext ("The maxmode name is :");
Outtext (s );
Getch ();
Closegraph ();
}

27. graphdefaults () Graphical reset Default Value Function
Function: The grpahdefaults () function resets all images to their default values. This includes resetting the image View to the full screen, and positioning the current position at 0, 0; the color palette, background color, and drawing color are reset. fill color, fill pattern, text, and sorting method return the default value of its position.
Usage: This function is called by void graphdefaults (void );
Note: This function has no parameter to call.
The corresponding header file of this function is graphics. h.
Return Value: None
For example, restore the image system to its default value:
Graphdefaults ();

28. setgraphmode () sets the graphic mode function
Function: setgraphmode () is called in the valid graph mode of the current graph driver. Once called, setgraphmode () selects a New Graph mode. Note that the graphic display mode must be initialized by initgraph () in advance.
Usage: Call the function in void setgraphmode (INT mode );
Description: The mode value specified when the mode parameter is called.
The corresponding header file of this function is graphics. h.
Return Value: None
For example, set the VGA graphics card to vgamed mode:
Setgraphmode (vgamed); // The graphics system has been initialized.

29. restorecrtmode () restores the text display mode function
Function: The restrecrtmode () function restores the display mode to the initial text mode detected when initgraph () is called.
Usage: This function is called in void restorecrtmode (void );
Note: The restorecrtmode () and setgraphmode () functions can be used together to switch between text and graphic display.
The header file corresponding to this function is graphics. h.
Return Value: None
For example, restore the display mode to the initial text mode:
Restorecrtmode ();

30. graphresult () graphic operation result Function
Function: returns the code of the last graphic operation.
Usage: This function is called using int graphresult (void );
Note: C sets the code for the graph, which is an integer from-15 to 0. The meanings are shown in Table 1-6.
When this function is called, the error state is reset to 0, so the returned value (CODE) is best stored in the variable for detection as needed,
Perform the Next graphic operation.

Graphic operation information Name Code Meaning
Grok
Grnoinitgraph
Grnodetected
Grfilenofound
Grinvaliddriver
Grnoloadmem
Grnoscanmem
Grnofloadmem
Grfontnotfound
Grnofontmem
Grinvalidmode
Grerror
Grioerror
Grinvalidfont
Grinvaliddevicenum
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-11
-12
-13
-14
-15
Successful
No image Initialization
No graphic hardware Detected
No graphic driver file found
Invalid graphics driver
Insufficient memory to mount the graphics driver
Insufficient scan memory
Insufficient memory for flood Filling
No font file found
Insufficient memory for Font File Installation
Invalid image mode
General Image Error
Input/Output Error
Invalid font file
Invalid font number
Invalid device number

The header file corresponding to this function is graphics. h.
Return Value: return the code corresponding to the last graphic operation result.
For example, to call the graphresult () function, see the example of the grapherrormsg () function below.

31. grpaherrormsg () graphic error message function
Function: returns a pointer to the error message string corresponding to the error code.
Usage: This function is called in char * grapherrormsg (INT errcode );
Note: The errcode value is obtained by calling the graphresult () function. All error codes and corresponding error information are described in table 1-6.
The corresponding header file of this function is grpahics. h.
Return Value: returns a pointer to the error message string corresponding to the error code.
For example, the following error code is displayed in text format:
Int errorcode;
Errorcode = graphresult ();
Printf (% s ", grapherrormsg (errcode ));

32. setgraphbufsize (): Specifies the image buffer size function.
Function: setgraphbufsize () is used to set the memory buffer size used by some graphic functions (such as floodfill.
Usage: The function call method is unsigned setgraphbufsize (unsigned bufsize );
Note: here the unsigned parameter bufsize is the configured buffer size.
This function is generally not required. Only the memory buffer generated by the initgraph () function is enough. The default buffer size is 4 kb (4096 bytes ), enough to fill in a polygon with about 650 vertices. But to save memory, you can reduce the size of the buffer or use the setgraphbufsize () function to reduce the size of the buffer or increase the size of the buffer () to increase the buffer size. Note that the setgraphbufsize () function must be used before initgraph () is called.
Returned value: returns the number of bytes in the previously defined graphic buffer.
For example, call this function to set the image buffer to 8 KB:
Setgraphbufsize (0x2000 );

33. setactivepage () sets the function to output the display page.
Function: setactiveage () is used as the display page for graphic output.
Usage: This function is called by void setactivepage (INT pagenum );
Note: The pagenum parameter indicates the selected page number. If the call is successful, the subsequent graphic output is written to the selected output page. Zero page is used as the current output page. This function only works when the graphic driver and Its display mode support multiple display pages.
The header file of this function is graphics. h.
Return Value: None
For example, set page 1 to the output display page:
Setactivepage (1 );

34. setvisualpage () sets the number of visible pages
Function: setvisualpage () is used to set the display page that is visible on the screen.
Usage: The function call method is void setvisualpage (INT pagenum );
Note: The pagenum parameter is the set page number. Zero page is used as the current visible page. Calling this function is the same as calling setactivepage (). It is only valid when the graphic driver and Its display mode support multiple display pages. You can switch between them using any display pages supported by the system. Of course, only one display page can be visible on the screen at a time. Sometimes you need to create a backup image on one page, it is not displayed at the time. Once needed, you can switch to the page immediately. To achieve the animation effect, you only need to switch the display page number. Use the setvisualpage () function to select a visible page that is actually displayed on the screen, and use the setactivepage () function to select the current output page of the image, so as to achieve display switching between graphic pages, normally, both functions are default, and the output page is a visible page. You do not need to use these two functions if you do not draw an animation.
The header file corresponding to these two functions is graphics. h.
Return Value: None
For example, set the visible page to 1st:
Setvisualpage (1 );

35. closegraph () disables the graph mode function.
Function: The closegraph () function disables the system graphics mode, resets it to the initgraph () initial text mode, and releases the system memory occupied by the graphics driver, Font, and internal buffer.
Usage: This function is called by void closegraph (void );
Note: This function should be called when your program uses both Graphic Output and non-graphic output. In particular, when the program calls the initgraph () function multiple times, it needs to call the closegraph function accordingly to release the memory occupied by calling initgraph (). Otherwise, the memory will soon be full and the program will not be able to run, if the program ends, you can also use the restorecrtmode () function to replace this function, so that any allocated memory will be released.
The header file corresponding to this function is graphics. h.
Return Value: None
For example, disable the graphics system:
Closegraph ();

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.