In the previous article, we discussed the BGI library and a sample program using this library. From this article onwards, there will be several articles containing BGI library functions and their examples. This article discusses the following functions :
a) int getmaxcolor (void)
b) void setcolor (int color)
c) int getcolor (void)
d) void putpixel (int x, int y, int color)
e) unsigned getpixel (int x, int y)
f) int kbhit (void)
g) int getmaxx (void)
h) int getmaxy (void)
i) void delay (unsigned milliseconds)
j) void outtextxy (int x, int y, char *textstring)
k) void outtext (char *textstring)
a) int getmaxcolor (void)
This function returns the highest valid color value that can be passed to setcolor for the current graphics driver and mode.
For example, on a 256K EGA, getmaxcolor always returns 15. This means that any call to setcolor with a value from 0 to 15 is valid.
On a CGA in high-resolution mode ( or on a Hercules monochrome adapter) getmaxcolor returns 1.
For example,
b) void setcolor (int color)
This function sets the current drawing color to color, which ranges from 0 to getmaxcolor. To select a drawing color with setcolor, you can pass either the color number or the equivalent color name.
The drawing color is the value that pixels are set to when the program draws lines, etc. Valid colors depend on the current graphics driver and the current graphics mode.
For example,
On a CGA in high-resolution mode ( or on a Hercules monochrome adapter) getmaxcolor returns 1.
For example,
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
int midx, midy;
char colstr[80];
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
//grab the color info. and convert it to a string
sprintf(colstr, "This mode supports colors 0..%d", getmaxcolor());
//display the information
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, colstr);
getch();
closegraph();
return 0;
}
Output of this getmaxcolor() program
b) void setcolor (int color)
This function sets the current drawing color to color, which ranges from 0 to getmaxcolor. To select a drawing color with setcolor, you can pass either the color number or the equivalent color name.
The drawing color is the value that pixels are set to when the program draws lines, etc. Valid colors depend on the current graphics driver and the current graphics mode.
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
setcolor(CYAN);
outtextxy(200, 240, "Just");
setcolor(GREEN);
outtextxy(240, 240, "Coding");
setcolor(12);
outtextxy(200, 260, "Just");
setcolor(14);
outtextxy(240, 260, "Coding");
getch();
closegraph();
return 0;
}
Output of this setcolor() program
c) int getcolor (void)
This function returns the current drawing color.
For example, in CGAC0 mode (palette number 0), the palette contains four colors (background, light green, light red, and yellow). If getcolor returns 1, the current drawing color is light green.
For example,
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
int color;
char colname[35];
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
setcolor(RED);
// get the current drawing color
color = getcolor();
// convert color value into a string
itoa(color, colname, 10);
strcat(colname," is the current drawing color.");
// display a message
outtextxy(200, 240, colname);
getch();
closegraph();
return 0;
}
Output of this getcolor() program
d) void putpixel (int x, int y, int color)
This function plots a pixel in the color defined by color at the grid location (x, y). It does not return any value.
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
for (int i = 0; i<=200; i++)
{
putpixel(200 + i, 100, RED);
putpixel(200, 100 + i, GREEN);
putpixel(200 + i, 300, CYAN);
putpixel(400, 100+i, YELLOW);
}
getch();
closegraph();
return 0;
}
Output of this putpixel() program
e) unsigned getpixel (int x, int y)
This function gets the color of the pixel located at (x, y). It returns the color of that pixel.
For example,
#include <graphics.h>
#include <conio.h>
#include <iostream.h>
int main()
{
int gd=DETECT, gm;
int color;
initgraph(&gd,&gm,"c://turboc3//bgi");
putpixel(200, 100, GREEN); // value of GREEN color is 2
color = getpixel(200, 100);
gotoxy(10, 10);
cout<<"Pixel color at (200, 100) is :: "<<color;
getch();
closegraph();
return 0;
}
Output of this getpixel() program
f) int kbhit (void)
This function is used to determine if a key has been pressed or not. If it is, it can be retrieved using getch() or getche(). If a key has been pressed then it returns a non-zero integer otherwise returns zero.
For example,
#include <conio.h>
#include <iostream.h>
int main()
{
while(!kbhit()) // do nothing
cout<<" You haven't pressed a key."<<endl;
getch();
return 0;
}
Output of this kbhit() program
Note : kbhit() is defined in conio.h. Though not itself a graphics function, it serves a very important purpose while creating graphics which interact with the user.
g) int getmaxx (void)
This function returns the maximum x value (screen-relative) for the current graphics driver and mode.
For example, on a CGA in 320 x 200 mode, getmaxx returns 319.
h) int getmaxy (void)
This function returns the maximum y value (screen-relative) for the current graphics driver and mode.
For example, on a CGA in 320 x 200 mode, getmaxy returns 219.
Example of getmaxx( ) and getmaxy( ) :
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
int midx, midy;
char xrange[80], yrange[80];
initgraph(&gdriver,&gmode,"c://turboc3//bgi");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
// convert max resolution values into strings
sprintf(xrange, "X values range from 0..%d", getmaxx());
sprintf(yrange, "Y values range from 0..%d", getmaxy());
// display the information
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, xrange);
outtextxy(midx, midy+10, yrange);
getch();
closegraph();
return 0;
}
Output of this getmaxx() and getmaxy() program
i) void delay (unsigned milliseconds)
This function suspend the current program from execution for the time specified by the argument milliseconds.
It is not necessary to make a calibration call to delay before using it.
delay() is accurate to one millisecond.
For example,
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
int main()
{
int gd=DETECT, gm;
int i=10;
initgraph(&gd,&gm,"c://turboc3//bgi");
while(!kbhit())
{
if(i>getmaxy())
i=10;
setcolor(random(getmaxcolor())+1);
outtextxy(10+i,10+i,"Manish Kumar ");
delay(200);
i+=10;
}
closegraph();
return 0;
}
Output of this delay() program
Note : delay() is defined in dos.h. Though not itself a graphics function, it serves a very important purpose while creating graphics which interact with the user.
j) void outtextxy (int x, int y, char *textstring)
This function displays a string in the viewport at the position (x, y).
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
setcolor(CYAN);
outtextxy(200, 240, "Just");
setcolor(YELLOW);
outtextxy(240, 240, "Coding");
getch();
closegraph();
return 0;
}
Output of this outtextxy() program
k) void outtext (char *textstring)
This function displays a string at the current location of the cursor.
For example,
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
moveto(300, 240);
setcolor(YELLOW);
outtext("Just ");
setcolor(GREEN);
outtext("Coding");
getch();
closegraph();
return 0;
}
Output of this outtext() program
No comments:
Post a Comment