BGI library functions – Part 1 (Introduction)

  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) putpixel (int x, int y, int color)
b) getpixel (int x, int y)
c) kbhit ()
d) setcolor (int color)
e) outtextxy (int x, int y, char *text)
f) outtext (char *text)
 
a) putpixel (int x, int y, int color)
This function plots a pixel in the color defined by color at the location (x, y). It does not return any value.
For example,

#include <graphics.h>
#include <conio.h>

int main()
{
  int gd = DETECT, gm;
  initgraph(&gd, &gm, "c://turboc3//bgi");
  for (int i = 0; i<=200; i++)
    putpixel(100 + i, 100 + i, RED);
  getch();
  closegraph();
  return 0;
}

Output of this putpixel() program

Output of putpixel program

b) 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(100, 100, RED); // value of RED color is 4
  color = getpixel(100, 100);
  gotoxy(10, 10);
  cout<<"Pixel color at (100, 100) is :: "<<color;
  getch();
  closegraph();
  return 0;
}

Output of this getpixel() program

Output of this getpixel() program

c) kbhit ()
This function checks whether a keystroke is currently available. If it is, it can be retrieved using getch() or getche(). If a keystroke is available, it returns a non-zero integer and if there’s no keystroke, it returns zero.

For example,


#include <conio.h>
#include <iostream.h>

int main()
{
  cout<<"Press any key to continue ...";
  while (!kbhit())
    cout<<"A key was pressed !!!";
  getch();
  return 0;
}

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.

d) setcolor (int color)
This function sets the current drawing color, which ranges from 0 to getmaxcolor(). Valid colors depend on the current graphics driver and the current graphics mode.

For example,


#include <graphics.h>
#include <conio.h>

int main()
{
  int gd = DETECT, gm;
  initgraph(&gd, &gm, "c://turboc3//bgi");
  setcolor(RED);
  outtextxy(100, 100, "MANISH");
  setcolor(GREEN);
  outtextxy(100, 150, "KUMAR");
  getch();
  closegraph();
  return 0;
}

Output of this setcolor() program

Output of this setcolor() program

e) outtextxy (int x, int y, char *text)
This function displays text in the viewport at the position (x, y). 

For example,


#include <graphics.h>
#include <conio.h>

int main()
{
  int gd = DETECT, gm;
  initgraph(&gd, &gm, "c://turboc3//bgi");
  setcolor(RED);
  outtextxy(100, 100, "MANISH");
  setcolor(GREEN);
  outtextxy(100, 150, "KUMAR");
  getch();
  closegraph();
  return 0;
}

Output of this outtextxy() program

Output of this outtextxy() program

f) outtext (char *text)

This function displays a string at the current location of the cursor.

For example,


#include <graphics.h>
#include <conio.h>

int main()
{
  int gd = DETECT, gm;
  initgraph(&gd, &gm, "c://turboc3//bgi");
  moveto(300, 240);
  setcolor(RED);
  outtext("MANISH");
  setcolor(GREEN);
  outtext("KUMAR");
  getch();
  closegraph();
  return 0;
}

Output of this outtext() program

Output of this outtext() program

In the next article, we have discussed various drawing functions.

BGI library functions – Part 2 (Drawing Functions)

Did you like this article? Tell us in your comments below.

(Visited 478 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *