C Programming Code Examples
C > Games and Graphics Code Examples
3D diagram of an IC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* 3D diagram of an IC */
#include<graphics.h>
#include<stdio.h>
#include<time.h>
void liner(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int
x5,int y5,int i);
void shadow(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int
x5,int y5,int x6,int y6,int x7,int y7);
void rightlead( int x1,int y1,int x2,int y2,int x3,int y3,int x4,int
y4,int x5,int y5,int x6,int y6,int x7,int y7,int x8,int y8,int x9,int
y9,int x10,int y10,int x11, int y11);
void leftlead();
void identifier();
void bibin();
void main()
{
int gd=DETECT, gm,i=3;
initgraph(&gd,&gm,"c:\tc\bgi");
setbkcolor(6);
cleardevice();
setcolor(i);
line(263,192,108,151);
line(108,151,345,40);
line(263,192,507,68);
line(507,68,345,40);
setfillstyle(1,9);
floodfill(347,49,i);//for upper part
setcolor(i);
line(263,192,269,231);
line(269,231,89,179);
line(89,179,108,151);
setfillstyle(1,9);
floodfill(193,194,i);//for part left light blue
setcolor(i);
line(269,231,524,93);
line(524,93,506,69);
floodfill(454,113,i);//for part side light blue
setcolor(i);
line(269,231,264,260);
line(264,260,101,215);
line(101,215,89,179);
setfillstyle(1,1);//part left for blue
floodfill(193,234,i);
setcolor(i);
line(264,260,514,125);
line(525,94,514,125);
setfillstyle(1,1);
floodfill(489,126,i);
setcolor(i);//part side blue
//end of upperpart
line(102,315,292,383);
line(294,419,292,383);
line(294,419,101,348);
line(102,315,101,348);
setfillstyle(1,1);//lower left
floodfill(154,360,i);
line(542,222,292,383);
line(542,257,542,222);
line(542,257,294,419);
setfillstyle(1,1);
floodfill(480,282,i);//lower side
line(102,315,222,249);
line(407,183,542,222);
setfillstyle(1,1);
floodfill(206,274,i);
line(147,331,146,350);
line(259,390,146,350);
line(258,371,259,390);
line(147,331,411,184);
line(411,184,503,210);
line(503,210,258,371);
setfillstyle(1,13);
floodfill(273,297,i);
//end of base of low part
// start of holes
line(130,311,138,313);
line(138,313,147,307);
line(147,307,139,305);
line(139,305,130,311);
setfillstyle(1,8);
floodfill(139,309,i);
//left 1
liner(176,285,183,287,193,282,186,280,185,284,i);
liner(221,260,229,262,239,257,232,255,230,258,i);
liner(299,362,308,365,315,358,309,357,309,361,i);
liner(347,332,354,335,363,330,357,327,357,330,i);
liner(389,305,396,306,405,301,399,299,399,303,i);
liner(428,280,436,282,444,277,438,276,437,279,i);
liner(467,255,475,257,482,252,476,250,475,255,i);
liner(505,233,511,234,521,229,514,227,513,231,i);
delay(1500);
rightlead(283,225,297,226,301,215,314,220,298,280,317,280,305,282,313,282,
305,346,312,343,309,240);
shadow(282,226,294,231,294,281,300,285,301,346,305,346,296,280);
delay(500);
rightlead(333,197,344,200,348,189,360,196,346,253,362,252,350,256,358,255,
351,316,358,314,353,221);
shadow(333,197,342,206,343,254,348,258,348,316,351,316,349,315);//for
second lead
delay(500);
rightlead(373,175,385,178,391,166,402,172,388,228,404,225,393,232,400,231,
394,290,400,287,397,197);
shadow(373,175,383,182,385,229,390,233,391,291,394,290,392,280);
delay(500);
rightlead(417,152,428,155,432,143,442,148,430,205,446,202,435,208,441,207,
436,265,441,263,436,178);
shadow(417,152,426,161,426,205,431,211,431,267,436,265,425,155);
delay(500);
rightlead(456,130,466,133,471,122,479,125,469,181,483,180,473,184,479,184,
474,239,480,238,475,158);
shadow(456,130,464,137,465,183,471,187,472,241,474,239,473,220);
delay(500);
rightlead(493,110,501,114,506,102,517,107,504,160,519,159,508,163,515,162,
509,216,515,216,513,137);
shadow(493,110,499,117,499,162,505,165,505,218,509,216,506,213);
delay(500);
leftlead();
identifier();
getch();
bibin();
}
void liner(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int
x5,int y5,int i)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
line(x4,y4,x1,y1);
setfillstyle(1,8);
floodfill(x5,y5,i);//x1,y1 left most then go anticlockwise x5,y5 for
fill
}
void rightlead( int x1,int y1,int x2,int y2,int x3,int y3,int x4,int
y4,int x5,int y5,int x6,int y6,int x7,int y7,int x8,int y8,int x9,int
y9,int x10,int y10,int x11,int y11)
{
setcolor(15);
line(x1,y1,x3,y3);//for starting line
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
line(x2,y2,x5,y5);
line(x4,y4,x6,y6);
line(x5,y5,x7,y7);
line(x6,y6,x8,y8);
line(x7,y7,x9,y9);
line(x10,y10,x8,y8);
line(x10,y10,x9,y9);
setfillstyle(1,7);
floodfill(x11,y11,15);
}
void shadow(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int
x5,int y5,int x6,int y6,int x7,int y7)
{
setcolor(15);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
line(x5,y5,x4,y4);
line(x5,y5,x6,y6);
setfillstyle(1,8);
floodfill(x7,y7,15);
}
void leftlead()
{
setcolor(15);
line(132,224,133,232);
line(133,232,139,236);
line(139,236,140,297);
line(140,297,146,296);
line(146,296,146,237);
line(146,237,152,232);
line(152,232,152,229);
line(152,229,132,224);
setfillstyle(1,7);
floodfill(142,244,15);
line(129,224,128,234);
line(128,234,135,240);
line(135,240,137,298);
line(137,298,140,297);
line(132,224,129,224);
setfillstyle(1,8);
floodfill(131,236,15);
//end of first lead
line(181,237,181,273);
line(181,273,188,272);
line(188,272,188,241);
line(181,237,188,241);
setfillstyle(1,7);
floodfill(185,255,15);
line(177,237,181,239);
line(177,237,179,275);
line(179,275,181,273);
setfillstyle(1,8);
floodfill(179,256,15);
line(221,249,220,253);
line(220,253,226,252);
line(226,252,225,250);
line(225,250,221,249);
floodfill(223,251,15);
}
void identifier()
{
setcolor(6);
arc(196,177,350,160,26);
line(171,168,222,181);
setfillstyle(1,1);
floodfill(200,170,6);
}
void bibin()
{
int midx=320,midy=230;
cleardevice();
setbkcolor(RED);
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(GREEN);
settextstyle(5, VERT_DIR, 4);
return ;
}
line() Function in C
line() is a library function of graphics.c in c programming language which is used to draw a line from two coordinates. line() function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end points of the line.
Syntax for line() Function in C
#include <graphics.h>
void line(int x1, int y1, int x2, int y2);
x1
X coordinate of first point
y1
Y coordinate of first point.
x2
X coordinate of second point.
y2
Y coordinate of second point.
This is a predefined function named line which is used to draw a line on the output screen. It takes 4 arguments, first two parameters represent an initial point and the last two arguments are for the final points of the line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* draw a line from a point(x1,y1) to point(x2,y2) by line() function example */
/*C graphics program to draw a line.*/
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
//init graphics
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
/*
if you are using turboc2 use below line to init graphics:
initgraph(&gd, &gm, "C:/TC/BGI");
*/
//draw a line
/*
line() function description
parameter left to right
x1: 100
y1: 100
x2: 200
y2: 100
*/
line(100,100,200,100); //will draw a horizontal line
line(10,10,200,10); //will draw another horizonatl line
getch();
closegraph();
return 0;
}
setbkcolor() Function in C
setbkcolor() function is used to set the background color in graphics mode. The default background color is black and default drawing color as we know is white. setbkcolor() function takes only one argument it would be either the name of color defined in graphics.h header file or number associated with those colors. If we write setbkcolor(yellow) it changes the background color in Green.
Syntax for setbkcolor() Function in C
#include <graphics.h>
void setbkcolor(int color);
possible color values
• BLACK – 0
• BLUE – 1
• GREEN – 2
• CYAN – 3
• RED – 4
• MAGENTA – 5
• BROWN – 6
• LIGHTGRAY – 7
• DARKGRAY – 8
• LIGHTBLUE – 9
• LIGHTGREEN – 10
• LIGHTCYAN – 11
• LIGHTRED – 12
• LIGHTMAGENTA – 13
• YELLOW – 14
• WHITE – 15
On CGA and EGA systems, setbkcolor changes the background color by changing the first entry in the palette.
If you use an EGA or a VGA, and you change the palette colors with setpalette or setallpalette, the defined symbolic constants might not give you the correct color. This is because the parameter to setbkcolor indicates the entry number in the current palette rather than a specific color (unless the parameter passed is 0, which always sets the background color to black).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* set the background to the color specified by color by setbkcolor() function example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* _select driver and mode that supports multiple background colors*/
int gdriver = EGA, gmode = EGAHI, errorcode;
int bkcol, maxcolor, x, y;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) { /* an error occurred */
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* maximum color index supported */
maxcolor = getmaxcolor();
/* for centering text messages */
settextjustify(CENTER_TEXT, CENTER_TEXT);
x = getmaxx() / 2;
y = getmaxy() / 2;
/* loop through the available colors */
for (bkcol=0; bkcol<=maxcolor; bkcol++) {
/* clear the screen */
cleardevice();
/* select a new background color */
setbkcolor(bkcol);
/* output a messsage */
if (bkcol == WHITE)
setcolor(EGA_BLUE);
sprintf(msg, "Background color: %d", bkcol);
outtextxy(x, y, msg);
getch();
}
/* clean up */
closegraph();
return 0;
}
initgraph() Function in C
initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into 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. initgraph also resets all graphics settings to their defaults (current position, palette, color, viewport, and so on) and resets graphresult to 0.
Normally, initgraph loads a graphics driver by allocating memory for the driver (through _graphgetmem), then loading the appropriate .BGI file from disk. As an alternative to this dynamic loading scheme, you can link a graphics driver file (or several of them) directly into your executable program file.
Syntax for initgraph() Function in C
#include <graphics.h>
void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
pathtodriver
pathtodriver specifies the directory path where initgraph looks for graphics drivers. initgraph first looks in the path specified in pathtodriver, then (if they are not there) in the current directory. Accordingly, if pathtodriver is null, the driver files (*.BGI) must be in the current directory. This is also the path settextstyle searches for the stroked character font files (*.CHR).
graphdriver
graphdriver is an integer that specifies the graphics driver to be used. You can give it a value using a constant of the graphics_drivers enumeration type, which is defined in graphics.h and listed below.
• DETECT – 0 (requests autodetect)
• CGA – 1
• MCGA – 2
• EGA – 3
• EGA64 – 4
• EGAMONO – 5
• IBM8514 – 6
• HERCMONO – 7
• ATT400 – 8
• VGA – 9
• PC3270 – 10
graphmode
graphmode is an integer that specifies the initial graphics mode (unless *graphdriver equals DETECT; in which case, *graphmode is set by initgraph to the highest resolution available for the detected driver). You can give *graphmode a value using a constant of the graphics_modes enumeration type, which is defined in graphics.h and listed below.
initgraph always sets the internal error code; on success, it sets the code to 0. If an error occurred, *graphdriver is set to -2, -3, -4, or -5, and graphresult returns the same value as listed below:
• grNotDetected: -2 Cannot detect a graphics card
• grFileNotFound: -3 Cannot find driver file
• grInvalidDriver: -4 Invalid driver
• grNoLoadMem: -5 Insufficient memory to load driver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* initgraph initializes the graphics system by loading a graphics driver by initgraph() function example*/
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
/* draw a line */
line(0, 0, getmaxx(), getmaxy());
/* clean up */
getch();
closegraph();
return 0;
}
delay() Function in C
Delay function is used to suspend execution of a program for a particular time. delay() function requires a parameter which should be a number, defining the milliseconds for the delay. To use delay function in your program you should include the "dos.h" header file which is not a part of standard C library. Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds).
Syntax for delay() Function in C
#include<stdio.h>
void delay(unsigned int);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* suspend execution of a program for a particular time by delay() function example */
#include <stdio.h>
//to use 'delay()'
#include <dos.h>
int main()
{
// message for user
printf("After printing this message the program will get end in next 5 seconds \n");
// delay the process for 5 seconds as it takes integer value in milliseconds.
delay(5000);
// message for user.
printf("After printing this message the program will get delay for next 15 seconds\n");
// to terminate the process for next 15 seconds.
sleep(15);
// message for user
printf("After printing this message the program will get end in next 2 seconds \n");
// delay the process for 2 seconds as it takes integer value in milliseconds.
delay(2000);
return 0;
}
#include Directive in C
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
• Header File or Standard files: This is a file which contains C/C++ function declarations and macro definitions to be shared between several source files. Functions like the printf(), scanf(), cout, cin and various other input-output or other standard functions are contained within different header files. So to utilise those functions, the users need to import a few header files which define the required functions.
• User-defined files: These files resembles the header files, except for the fact that they are written and defined by the user itself. This saves the user from writing a particular function multiple times. Once a user-defined file is written, it can be imported anywhere in the program using the #include preprocessor.
Syntax for #include Directive in C
#include "user-defined_file"
#include <header_file>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* #include directive tells the preprocessor to insert the contents of another file into the source code at the point where the #include directive is found. */
// C program to illustrate file inclusion
// <> used to import system header file
#include <stdio.h>
// " " used to import user-defined file
#include "process.h"
// main function
int main()
{
// add function defined in process.h
add(10, 20);
// mult function defined in process.h
multiply(10, 20);
// printf defined in stdio.h
printf("Process completed");
return 0;
}
cleardevice() Function in C
The header file graphics.h contains cleardevice() function. cleardevice() is a function which is used to clear the screen by filling the whole screen with the current background color. It means that cleardevice() function is used to clear the whole screen with the current background color and it also sets the current position to (0,0). . Both clrscr() and cleardevice() functions are used to clear the screen but clrscr() is used in text mode and cleardevice function is used in the graphics mode.
Syntax for cleardevice() Function in C
#include <graphics.h>
void cleardevice();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* clear the screen in graphics mode and set the current position to (0,0) by cleardevice() function example.*/
#include <graphics.h>
#include <conio.h>
int main()
{
//initilizing graphic driver and
//graphic mode variable
int graphicdriver=DETECT,graphicmode;
//calling initgraph
initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");
//Printing message for user
outtextxy(20, 20 + 20, "Program to use graph default in C graphics");
//message to clear screen
outtextxy(20, 50 + 30, "Press any key to clear screen");
//getting character and clear the device screen
getch();
cleardevice();
//message to press key to get exit from program
outtextxy(20, 20 + 20, "Press any key to exit...");
getch();
return 0;
}
floodfill() Function in C
floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled,border specifies the color of boundary of area. To change fill pattern and fill color use setfillstyle.
floodfill fills an enclosed area on bitmap devices. (x,y) is a "seed point" within the enclosed area to be filled. The area bounded by the color border is flooded with the current fill pattern and fill color. If the seed point 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 that you can maintain code compatibility with future versions.
Syntax for floodfill() Function in C
#include <graphics.h>
void floodfill(int x, int y, int border);
x
X coordinate of point on the screen
y
Y coordinate of point on the screen
border
specifies the color of border of the enclosed area.
int values corresponding to colors:
• BLACK – 0
• BLUE – 1
• GREEN – 2
• CYAN – 3
• RED – 4
• MAGENTA – 5
• BROWN – 6
• LIGHTGRAY – 7
• DARKGRAY – 8
• LIGHTBLUE – 9
• LIGHTGREEN – 10
• LIGHTCYAN – 11
• LIGHTRED – 12
• LIGHTMAGENTA – 13
• YELLOW – 14
• WHITE – 15
If an error occurs while flooding a region, graphresult returns a value of -7.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* fill an enclosed area on bitmap devices by floodfill() function code example */
#include <graphics.h>
// driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
// initgraph initializes the
// graphics system by loading
// a graphics driver from disk
initgraph(&gd, &gm, " ");
// center and radius of circle
int x_circle = 250;
int y_circle = 250;
int radius=100;
// setting border color
int border_color = WHITE;
// set color and pattern
setfillstyle(HATCH_FILL,RED);
// x and y is a position and
// radius is for radius of circle
circle(x_circle,y_circle,radius);
// fill the color at location
// (x, y) with in border color
floodfill(x_circle,y_circle,border_color);
getch();
// closegraph function closes the
// graphics mode and deallocates
// all memory allocated by
// graphics system
closegraph();
return 0;
}
getch() Function in C
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.
Syntax for getch() Function in C
#include <conio.h>
int getch(void);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* wait for any character input from keyboard by getch() function example. */
// C code to illustrate working of
// getch() to accept hidden inputs
#include <conio.h>
#include <dos.h> // delay()
#include <stdio.h>
#include <string.h>
void main()
{
// Taking the password of 8 characters
char pwd[9];
int i;
// To clear the screen
clrscr();
printf("Enter Password: ");
for (i = 0; i < 8; i++) {
// Get the hidden input
// using getch() method
pwd[i] = getch();
// Print * to show that
// a character is entered
printf("*");
}
pwd[i] = '\0';
printf("\n");
// Now the hidden input is stored in pwd[]
// So any operation can be done on it
// Here we are just printing
printf("Entered password: ");
for (i = 0; pwd[i] != '\0'; i++)
printf("%c", pwd[i]);
// Now the console will wait
// for a key to be pressed
getch();
}
settextstyle() Function in C
Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text. settextstyle sets the text font, the direction in which text is displayed, and the size of the characters. A call to settextstyle affects all text output by outtext and outtextxy.
Syntax for settextstyle() Function in C
#include <graphics.h>
void settextstyle(int font, int direction, int charsize);
font
One 8x8 bit-mapped font and several "stroked" fonts are available. The 8x8 bit-mapped font is the default. The enumeration font_names, which is defined in graphics.h, provides names for these different font settings:
• 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
• SCRIPT_FONT – 5 Stroked script font
• SIMPLEX_FONT – 6 Stroked triplex script font
• TRIPLEX_SCR_FONT – 7 Stroked triplex script font
• COMPLEX_FONT – 8 Stroked complex font
• EUROPEAN_FONT – 9 Stroked European font
• BOLD_FONT – 10 Stroked bold font
The default bit-mapped font is built into the graphics system. Stroked fonts are stored in *.CHR disk files, and only one at a time is kept in memory. Therefore, when you select a stroked font (different from the last selected stroked font), the corresponding *.CHR file must be loaded from disk.
To avoid this loading when several stroked fonts are used, you can link font files into your program. Do this by converting them into object files with the BGIOBJ utility, then registering them through registerbgifont.
direction
Font directions supported are horizontal text (left to right) and vertical text (rotated 90 degrees counterclockwise). The default direction is HORIZ_DIR. The size of each character can be magnified using the charsize factor. If charsize is nonzero, it can affect bit-mapped or stroked characters. A charsize value of 0 can be used only with stroked fonts.
charsize
• If charsize equals 1, outtext and outtextxy displays characters from the 8x8 bit-mapped font in an 8x8 pixel rectangle onscreen.
• If charsize equals 2, these output functions display characters from the 8x8 bit-mapped font in a 16*16 pixel rectangle, and so on (up to a limit of ten times the normal size).
• When charsize equals 0, the output functions outtext and outtextxy magnify the stroked font text using either the default character magnification factor (4) or the user-defined character size given by setusercharsize.
Always use textheight and textwidth to determine the actual dimensions of the text.
This function needs to be called before the outtextxy() function, otherwise there will be no effect on text and output will be the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* set the text font, the direction in which text is displayed, and the size of the characters by settextstyle() function example. */
// C++ implementation for
// settextstyle() function
#include <graphics.h>
// driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
// initgraph initializes the
// graphics system by loading
// a graphics driver from disk
initgraph(&gd, &gm, "");
// location of text
int x = 150;
int y = 150;
// font style
int font = 8;
// font direction
int direction = 0;
// font size
int font_size = 5;
// for setting text style
settextstyle(font, direction, font_size);
// for printing text in graphics window
outtextxy(x, y, "Happy Codings");
getch();
// closegraph function closes the
// graphics mode and deallocates
// all memory allocated by graphics
// system .
closegraph();
return 0;
}
main() Function in C
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts.
main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined.
main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
Its first function or entry point of the program from where program start executed, program's execution starts from the main. So main is an important function in c , c++ programming language.
Syntax for main() Function in C
void main()
{
.........
// codes start from here
.........
}
void
is a keyword in C language, void means nothing, whenever we use void as a function return type then that function nothing return. here main() function no return any value.
In place of void we can also use int return type of main() function, at that time main() return integer type value.
main
is a name of function which is predefined function in C library.
• An operating system always calls the main() function when a programmers or users execute their programming code.
• It is responsible for starting and ends of the program.
• It is a universally accepted keyword in programming language and cannot change its meaning and name.
• A main() function is a user-defined function in C that means we can pass parameters to the main() function according to the requirement of a program.
• A main() function is used to invoke the programming code at the run time, not at the compile time of a program.
• A main() function is followed by opening and closing parenthesis brackets.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* basic c program by main() function example */
#include <stdio.h>
#include <conio.h>
main()
{
printf (" It is a main() function ");
int fun2(); // jump to void fun1() function
printf ("\n Finally exit from the main() function. ");
}
void fun1()
{
printf (" It is a second function. ");
printf (" Exit from the void fun1() function. ");
}
int fun2()
{
void fun1(); // jump to the int fun1() function
printf (" It is a third function. ");
printf (" Exit from the int fun2() function. ");
return 0;
}
setfillstyle() Function in C
The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. Current fill pattern and fill color is used to fill the area. setfillstyle sets the current fill pattern and fill color. To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call setfillpattern.
Syntax for setfillstyle() Function in C
#include<graphics.h>
void setfillstyle(int pattern, int color);
color
Specify the color
• BLACK – 0
• BLUE – 1
• GREEN – 2
• CYAN – 3
• RED – 4
• MAGENTA – 5
• BROWN – 6
• LIGHTGRAY – 7
• DARKGRAY – 8
• LIGHTBLUE – 9
• LIGHTGREEN – 10
• LIGHTCYAN – 11
• LIGHTRED – 12
• LIGHTMAGENTA – 13
• YELLOW – 14
• WHITE – 15
pattern
Specify the pattern
• EMPTY_FILL – 0
• SOLID_FILL – 1
• LINE_FILL – 2
• LTSLASH_FILL – 3
• SLASH_FILL – 4
• BKSLASH_FILL – 5
• LTBKSLASH_FILL – 6
• HATCH_FILL – 7
• XHATCH_FILL – 8
• INTERLEAVE_FILL – 9
• WIDE_DOT_FILL – 10
• CLOSE_DOT_FILL – 11
• USER_FILL – 12
If invalid input is passed to setfillstyle, graphresult returns -1(grError), and the current fill pattern and fill color remain unchanged.
Note: The EMPTY_FILL style is like a solid fill using the current background color (which is set by setbkcolor).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* set the current fill pattern and fill color by setfillstyle() function example */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm,bkcolor;
initgraph(&gd,&gm," ");
setfillstyle(EMPTY_FILL,YELLOW);
bar3d(2,150,100,200,25,1);
setfillstyle(SOLID_FILL,RED);
bar3d(150,150,250,200,25,1);
setfillstyle(LINE_FILL,BLUE);
bar3d(300,150,400,200,25,1);
setfillstyle(LTSLASH_FILL,GREEN);
bar3d(450,150,550,200,25,1);
setfillstyle(SLASH_FILL,CYAN);
bar3d(2,250,100,300,25,1);
setfillstyle(BKSLASH_FILL,BROWN);
bar3d(150,250,250,300,25,1);
setfillstyle(LTBKSLASH_FILL,MAGENTA);
bar3d(300,250,400,300,25,1);
setfillstyle(HATCH_FILL,LIGHTRED);
bar3d(450,250,550,300,25,1);
setfillstyle(XHATCH_FILL,DARKGRAY);
bar3d(2,350,100,400,25,1);
setfillstyle(INTERLEAVE_FILL,YELLOW);
bar3d(150,350,250,400,25,1);
setfillstyle(WIDE_DOT_FILL,LIGHTMAGENTA);
bar3d(300,350,400,400,25,1);
setfillstyle(CLOSE_DOT_FILL,LIGHTGRAY);
bar3d(450,350,550,400,25,1);
getch();
closegraph();
}
arc() Function in C
In the C programming language, there is an option to create an arc of a circle of a given radius with a given center coordinates and degree of the arc. The arc() function is used to create an arc. This arc function is included in graphics.h library in C which contains methods that can draw figures on the output screen.
The function to make an arc(), accepts five parameters for x, y co-ordinate, starting angle, end angle and radius. This will make the arc will all the values are fine. The Example below takes care of all these things as it have four arcs implemented.
Syntax for arc() Function in C
#include <graphics.h>
void arc(int x, int y, int start_angle, int end_angle, int radius);
x
x coordinate of the center of the arc
y
y coordinate of the center of the arc
start_angle
starting angle of arc
end_angle
ending angle of arc
radius
radius of the arc
The header file graphics.h contains arc() function which draws an arc with center at (x, y) and given radius. start_angle is the starting point of angle and end_angle is the ending point of the angle. The value of the angle can vary from 0 to 360 degree.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* create an arc of a circle of a given radius with a given center coordinates and degree of the arc by arc() function code example */
#include <graphics.h>
#include <conio.h>
int main()
{
//initilizing graphic driver and
//graphic mode variable
int graphicdriver=DETECT,graphicmode;
//calling initgraph with parameters
initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");
//Printing message for user
outtextxy(10, 10 + 10, "Program to draw arcs of diffrent sizes in C graphics");
// creating arcs using arc function.
arc(80, 80, 0, 12, 50);
arc(200, 100, 0, 200, 50);
arc(300, 100, 0, 240, 50);
arc(400, 100, 0, 120, 50);
getch();
return 0;
}
settextjustify() Function in C
Text output after a call to settextjustify is justified around the current position horizontally and vertically, as specified. The default justification settings are LEFT_TEXT (for horizontal) and TOP_TEXT (for vertical). The enumeration text_just in graphics.h provides names for the horiz and vert settings passed to settextjustify.
Syntax for settextjustify() Function in C
#include <graphics.h>
void settextjustify(int horiz, int vert);
horiz
• LEFT_TEXT – 0 left-justify text
• CENTER_TEXT – 1 center text
• RIGHT_TEXT – 2 right-justify text
vert
• BOTTOM_TEXT – 0 bottom-justify text
• CENTER_TEXT – 1 center text
• TOP_TEXT – 2 top-justify text
If horiz is equal to LEFT_TEXT and direction equals HORIZ_DIR, the CP's x component is advanced after a call to outtext(string) by textwidth(string).
settextjustify affects text written with outtext and cannot be used with text mode and stream functions.
If invalid input is passed to settextjustify, graphresult returns -11, and the current text justification remains unchanged.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* justify text output around the current position (CP) horizontally and vertically, as specified by settextjustify() function example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* function prototype */
void xat(int x, int y);
/* horizontal text justification settings */
char *hjust[] = { "LEFT_TEXT", "CENTER_TEXT", "RIGHT_TEXT" };
/* vertical text justification settings */
char *vjust[] = { "BOTTOM_TEXT", "CENTER_TEXT", "TOP_TEXT" };
int main(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, hj, vj;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) { /* an error occurred */
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* loop through text justifications */
for (hj=LEFT_TEXT; hj<=RIGHT_TEXT; hj++)
for (vj=LEFT_TEXT; vj<=RIGHT_TEXT; vj++) {
cleardevice();
/* set the text justification */
settextjustify(hj, vj);
/* create a message string */
sprintf(msg, "%s %s", hjust[hj], vjust[vj]);
/* create crosshairs on the screen */
xat(midx, midy);
/* output the message */
outtextxy(midx, midy, msg);
getch();
}
/* clean up */
closegraph();
return 0;
}
void xat(int x, int y) /* draw an x at (x,y) */
{
line(x-4, y, x+4, y);
line(x, y-4, x, y+4);
}
setcolor() Function in C
setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color.
setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.
Syntax for setcolor() Function in C
#include <graphics.h>
void setcolor(int color);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* set the current drawing color to color, which can range from 0 to getmaxcolor by setcolor() function example */
// C Implementation for setcolor()
#include <graphics.h>
#include <stdio.h>
// driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm, color;
// initgraph initializes the
// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");
// Draws circle in white color
// center at (100, 100) and radius
// as 50
circle(100, 100, 50);
// setcolor function
setcolor(GREEN);
// Draws circle in green color
// center at (200, 200) and radius
// as 50
circle(200, 200, 50);
getch();
// closegraph function closes the
// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();
return 0;
}
C program code example Initialize random number generator: how to use rand/srand. Generating random numbers. Print a number between 0 and 50, print a number between
Print help and exit with exval. Print version and exit with exval. Output some additional info. Very basic test of ip4 dotted quad address format. Convert address to base 10
C program to input binary number from user and find ones complement of binary number using loop. What is Ones complement? Ones complement of a binary number is defined as
C Program code to use structure within union & display the contents of structure elements. Problem statement: Create one structure and declare it inside union. Then accept values for
Program code to find equivalent resistance of parallel combination of resistive circuits. Enter the number of resistances. Enter value of each resistance. Printing equivalent series
C program to find total number of vowels and consonants in a string using 'Loop and If Else'. Input string from user, store it in a variable str Initialize two other variables to store vowel &
C Program code finds the sum of the main & opposite diagonal elements of a MxN Matrix. The program accepts an MxN matrix. Then adds main diagonal of matrix as well as the