January 24, 2018

BGI graphics library - Part 4



   In the previous article, we discussed some BGI library functions, namely, rectangle(), bar(), bar3d(), sector(), fillpoly() etc. In the present article, we shall discuss some more BGI functions.


This article discusses the following functions :
a) void setfillstyle (int pattern, int color)
b) void setfillpattern (char *upattern, int color)
c) void getfillpattern (char *pattern)
d) void floodfill (int x, int y, int border_color)
e) void cleardevice (void)
f) void getfillsettings(struct fillsettingstype *fillinfo)
g) void setviewport(int left, int top, int right, int bottom, int clip)
h) void clearviewport(void)

a) void setfillstyle (int pattern, int color)

This function sets the current fill pattern and fill color. 

Fill patterns for this functions are given below :



setfillstyle patterns


To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle : instead, call setfillpattern.


For example,

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


int main(void)
{
   int gdriver = DETECT, gmode;

   initgraph(&gdriver, &gmode, "c://turboc3//bgi");
  

  
   setfillstyle(SOLID_FILL,RED);
   bar(100, 100, 500, 400);

 
   getch();
   closegraph();
   return 0;
}

Output of this setfillstyle() program

setfillstyle() - Output



If invalid input is passed to setfillstyle, graphresult returns -11(grError), and the current fill pattern and fill color remain unchanged.

b) void setfillpattern (char *upattern, int color)

This function sets the current fill pattern to a user-defined 8x8 pattern. The argument upattern points to a sequence of 8 bytes; each byte corresponds to 8 pixels in the user-defined pattern. Whenever a bit in a pattern's byte is set to 1, the corresponding pixel is plotted.


c) void getfillpattern (char *pattern)

This function copies the user-defined fill pattern ( set by setfillpattern) into the 8-byte area *pattern. The argument pattern points to a sequence of 8 bytes; each byte corresponds to 8 pixels in the pattern fetched.

Example of setfillpattern() and getfillpattern() :

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

int main(void)
{
   int gdriver = DETECT, gmode;
   int maxx, maxy;
   char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x25, 0x27, 0x04, 0x04};

   initgraph(&gdriver, &gmode, "c://turboc3//bgi");

   maxx = getmaxx();
   maxy = getmaxy();
   setcolor(getmaxcolor());

   // select a user defined fill pattern
   setfillpattern(pattern, getmaxcolor());

   // fill the screen with the pattern
   bar(0, 0, maxx, maxy);

   getch();

   // get the current user defined fill pattern
   getfillpattern(pattern);

   // alter the pattern we grabbed
   pattern[4] -= 1;
   pattern[5] -= 3;
   pattern[6] += 3;
   pattern[7] -= 4;

   // select our new pattern
   setfillpattern(pattern, getmaxcolor());

   // fill the screen with the new pattern
   bar(0, 0, maxx, maxy);

   getch();
   closegraph();
   return 0;
}

  
  

Output of this setfillpattern() and getfillpattern() program

setfillpattern() - Output
Old pattern

setfillpattern() - Output
New Pattern


d) void floodfill (int x, int y, int border_color)

This function fills an enclosed area on bitmap devices. The area bounded by the border_color is flooded with the current fill pattern and fill color. (x, y) is a seed point. If this seed is inside the enclosed area, the inner region of that enclosure will be filled. In case it is outside the enclosed area, the region outside the enclosure will be filled.

If an error occurs while flooding a region, graphresult returns -7.




For example,

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


int main(void)
{
   int gdriver = DETECT, gmode;

   initgraph(&gdriver, &gmode, "c://turboc3//bgi");
  

      setcolor(RED);
   setfillstyle(SOLID_FILL,GREEN);
  
ellipse(300, 200, 0, 360, 45, 135);
   floodfill(300, 200,RED);
  
   getch();
   closegraph();

   return 0;
}


Output of this floodfill() program

floodfill() - Output


e) void cleardevice (void)

This function clears the entire screen and moves the current position to (0, 0). ( Erasing consists of filling with the current background color.)

For example,

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


int main(void)
{
   int gdriver = DETECT, gmode;

   initgraph(&gdriver, &gmode, "c://turboc3//bgi");

   while(!kbhit())
   {
    setcolor(random(getmaxcolor()));
    rectangle(random(getmaxx()), random(getmaxy()), random(getmaxx()), random(getmaxy()));
    delay(100);
    cleardevice();
   }

   getch();
   closegraph();
   return 0;



Output of this cleardevice() program

cleardevice() - Output

The above program is an animation of rectangle.

f) void getfillsettings(struct fillsettingstype *fillinfo)

This function fills in a structure with information about the current fill pattern and fill color. The argument fillinfo points to the 'fillsettingstype' structure where getfillsettings stores the current fill pattern and color.

The fillsettingstype structure is defined in graphics.h as follows:

struct fillsettingstype {

   int pattern;            // current fill pattern
   int color;              // current fill color

};


The functions bar, bar3d, fillpoly, floodfill, and pieslice all fill an area with the current fill pattern in the current fill color. There are 11 predefined fill pattern styles (such as solid, crosshatch, dotted, etc.). Symbolic names for the predefined patterns are provided by the enumerated type fill_patterns in graphics.h, as shown here :


fill patterns


For example,


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

// the names of the fill styles supported
char *fname[] = { "EMPTY_FILL",
          "SOLID_FILL",
          "LINE_FILL",
          "LTSLASH_FILL",
          "SLASH_FILL",
          "BKSLASH_FILL",
          "LTBKSLASH_FILL",
          "HATCH_FILL",
          "XHATCH_FILL",
          "INTERLEAVE_FILL",
          "WIDE_DOT_FILL",
          "CLOSE_DOT_FILL",
          "USER_FILL"
           };

int main(void)
{

   int gdriver = DETECT, gmode, errorcode;
   struct fillsettingstype fillinfo;
   int midx, midy;
   char patstr[40], colstr[40];

   initgraph(&gdriver, &gmode, "c://turboc3//bgi");

   midx = getmaxx() / 2;
   midy = getmaxy() / 2;

   // get information about current fill pattern and color
   getfillsettings(&fillinfo);

   // convert fill information into strings
   sprintf(patstr, "%s is the fill style.", fname[fillinfo.pattern]);
   sprintf(colstr, "%d is the fill color.", fillinfo.color);

   // display the information
   settextjustify(CENTER_TEXT, CENTER_TEXT);
   outtextxy(midx, midy, patstr);
   outtextxy(midx, midy+2*textheight("H"), colstr);


   getch();
   closegraph();
   return 0;
}



Output of this getfillsettings() program

getfillsettings() - Output

g) void setviewport(int left, int top, int right, int bottom, int clip)

This function establishes a new viewport for graphics output. The viewport's corners are given in absolute screen coordinates by (left,top) and (right,bottom). The current position (CP) is moved to (0,0) in the new window.

The clip argument determines whether drawings are clipped (truncated) at the current viewport boundaries. If clip is non-zero, all drawings will be clipped to the current viewport. 

If invalid input is passed to setviewport, graphresult returns -11, and the current view settings remain unchanged.


h) void clearviewport(void)

This function erases the viewport and moves the current position to home(0,0), relative to the viewport.

Example of setviewport() and clearviewport() :


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

#define CLIP_ON 1   // activates clipping in viewport

int main(void)
{
   int gdriver = DETECT, gmode, errorcode;

   initgraph(&gdriver, &gmode, "c://turboc3//bgi");

   setcolor(getmaxcolor());

   // rectangle in default full-screen viewport
   rectangle(50,50,120,120);
   outtextxy(30, 40,"(50,50)");
   outtextxy(120, 120,"(120,120)");
   outtextxy(120, 100, " <-- rectangle in default viewport");

   // create a smaller viewport
   setviewport(150, 150, getmaxx()-50, getmaxy()-50, CLIP_ON);
   rectangle(50,50,120,120);
   outtextxy(30, 40,"(50,50)");
   outtextxy(120, 120,"(120,120)");

   // display some messages
   outtextxy(120, 100, " <-- rectangle in smaller viewport");
   outtextxy(20, 150, "Press any key to clear viewport:");

   // wait for a key
   getch();

   // clear the viewport
   clearviewport();

   // output another message
   outtextxy(100, 100, "Press any key to quit:");


   getch();
   closegraph();
   return 0;
}


 Output of this clearviewport() and setviewport() program


clearviewport() - Output





 


No comments:

Post a Comment