January 25, 2018

BGI graphics library - Part 5



   In the previous article, we discussed some BGI library functions, namely, setfillstyle(), setfillpattern(), floodfill(), cleardevice(), setviewport() etc. In the present article, we shall discuss some more BGI functions.


This article discusses the following functions :

a) int textheight(char *textstring)
b) int textwidth(char *textstring)
c) void settextstyle(int font, int direction, int charsize)
d) void settextjustify(int horiz, int vert)
e) void setlinestyle(int linestyle, unsigned upattern, int thickness)
f) unsigned imagesize(int left, int top, int right, int bottom)
g) void getimage(int left, int top, int right, int bottom, void *bitmap)
h) void putimage(int left, int top, void *bitmap, int op)


a) int textheight(char *textstring)

This function takes the current font size and multiplication factor, and determines the height of textstring in pixels.

b) int textwidth(char *textstring)

This function takes the string length, current font size, and multiplication factor, and determines the width of textstring in pixels. 


textheight() and textwidth() functions are useful for adjusting the spacing between lines, computing viewport heights, sizing a title to make it fit on a graph or in a box, etc..



c) void settextstyle(int font, int direction, int charsize)

This function 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.

font : One 8x8 bit-mapped font and several "stroked" fonts are available. The 8x8 bit-mapped font, the default, is built into the graphics system.


The enumeration font_names, defined in GRAPHICS.H, provides names for the different font settings.


font names


direction : Font directions supported are horizontal text (left to right) and vertical text (rotated 90 degrees counterclockwise). The default direction is HORIZ_DIR

    Name         Value    Direction

  HORIZ_DIR       0       Left to right
      VERT_DIR        1       Bottom to top

charsize : 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.

  • 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 onscreen.
  • If charsize equals 3, these output functions display characters from the 8x8 bit-mapped font in a 24*24 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.

For example, 

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

// the names of the text styles supported
char *fname[] = { "DEFAULT font",
          "TRIPLEX font",
          "SMALL font",
          "SANS SERIF font",
          "GOTHIC font"
        };

int main(void)
{
   int gdriver = DETECT, gmode;
   int style, x=300, y=100;
   int size = 1;

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


   settextjustify(CENTER_TEXT, CENTER_TEXT);

   // loop through the available text styles
   for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
   {
      if (style == TRIPLEX_FONT)
     size = 4;

      // select the text style
      settextstyle(style, HORIZ_DIR, size);

      //output a message
      outtextxy(x, y, fname[style]);
      y+=4*textheight("H");
      x+=textwidth("H");
   }

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


Output of this settextstyle() program

settextstyle() - Output


d) void settextjustify(int horiz, int vert)

Text output after a call to settextjustify is justified around the current position (CP) 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, as shown here :


text justify
settextjustify affects text written with outtext and can't be used with text-mode and stream functions.

For example,

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


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

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

  
   settextjustify(LEFT_TEXT, BOTTOM_TEXT);
   outtextxy(getmaxx()/2, getmaxy()/2, "(Left,Bottom)");
   settextjustify(CENTER_TEXT, CENTER_TEXT);
   outtextxy(getmaxx()/2, getmaxy()/2 + textheight("H"), "(Center,Center)");
   settextjustify(RIGHT_TEXT, TOP_TEXT);
   outtextxy(getmaxx()/2, getmaxy()/2 + 2*textheight("H"), "(Right,Top)");
 
   getch();
   closegraph();
   return 0;
}

Output of this settextjustify() program

settextjustify() - Output

If invalid input is passed to settextjustify, graphresult returns -11, and the current text justification remains unchanged.

e) void setlinestyle(int linestyle, unsigned upattern, int thickness)

This function sets the style for all lines drawn by line, lineto, rectangle, drawpoly, etc.

The linesettingstype structure is defined in graphics.h as follows:
    struct linesettingstype {
    int       linestyle;
    unsigned  upattern;
    int       thickness;
  };

linestyle specifies in which of several styles subsequent lines will be drawn (such as solid, dotted, centered, dashed). The enumeration line_styles, which is defined in graphics.h, gives names to these operators:

linestyle


thickness specifies whether the width of subsequent lines drawn will be normal or thick.


thickness


upattern is a 16-bit pattern that applies only if linestyle is USERBIT_LINE (4). In that case, whenever a bit in the pattern word is 1, the corresponding pixel in the line is drawn in the current drawing color. For example, a solid line corresponds to a upattern of 0xFFFF (all pixels drawn), while a dashed line can correspond to a upattern of 0x3333 or 0x0F0F or 0x3F3F.


If invalid input is passed to setlinestyle, graphresult returns -11, and the current line style remains unchanged.


For example,

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

// the names of the line styles supported
char *lname[] = {
   "SOLID_LINE",
   "DOTTED_LINE",
   "CENTER_LINE",
   "DASHED_LINE",
   "USERBIT_LINE"
   };


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

   int style, x=100, y=200, userpat;
   char stylestr[40];

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

   // a user defined line pattern
   // binary: "0000000000000001"

   userpat = 1;

   for (style=SOLID_LINE; style<=USERBIT_LINE; style++)
   {
      // select the line style
      setlinestyle(style, userpat, 1);

      // convert style into a string
      strcpy(stylestr, lname[style]);

      // draw a line
      line(x, y, 300, y);

      // output a message
      outtextxy(305, y, stylestr);

     y+=20;

   }

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

  
  

Output of this setlinestyle() program

setlinestyle() - Output



f) unsigned imagesize(int left, int top, int right, int bottom)

This function determines the size of the memory area required to store a bit image.

On success, returns the size of the required memory area in bytes. On error (if the size required for the selected image is >= (64K - 1) bytes), returns 0xFFFF (-1).


g) void getimage(int left, int top, int right, int bottom, void *bitmap)

This function copies an image from the screen to memory. The values stored by its arguments have been listed below :

# left : x-coordinate of the top-left corner of the block.

# top : y-coordinate of the top-left corner of the block.

# right : x-coordinate of the bottom-right corner of the block.

# bottom : y-coordinate of the bottom-right corner of the block.

# bitmap : the address of the memory location where the image would be stored.



h) void putimage(int left, int top, void *bitmap, int op)

This function puts an image (that was previously saved with getimage) back to the screen, with the upper-left corner of the image placed at (left, top). The address of the memory location from where the image is to be retrieved is represented by bitmap. The last argument op specifies a combination operator that controls how the color for each destination pixel on the screen is computed.

There are five possible values for op. These are listed below :

combination operators


For example,

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


void *buff;

int main()
{
  int gd = DETECT, gm, errorcode;
  initgraph (&gd, &gm, "c://turboc3//bgi");
  errorcode = graphresult ();
  if (errorcode != grOk)
  {
       cout<<"Graphics error :: "<<grapherrormsg (errorcode);
       cout<<"\n Press any key to Exit";
       getch();
       exit(1);
 }

/* setbkcolor (int color) : Sets the current background color to the color specified by color. */


  setbkcolor (LIGHTCYAN);

  setcolor (RED);
  setfillstyle (SOLID_FILL,LIGHTRED);
  fillellipse(100,100,50,60);

  setcolor (WHITE);
  outtextxy (75, 100, "MANISH");
  buff = malloc (imagesize (40, 40, 160, 160));
  getimage (40, 40, 160, 160, buff);

  putimage (40, 40, buff, XOR_PUT); /* Erase image */

  putimage (100, 100, buff, COPY_PUT);
  putimage (300, 100, buff, COPY_PUT);
  putimage (500, 100, buff, COPY_PUT);
  putimage (100, 300, buff, COPY_PUT);
  putimage (300, 300, buff, COPY_PUT);
  putimage (500, 300, buff, COPY_PUT);

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


 Output of this putimage() program


putimage() - Output



Now, we have discussed the functions of BGI library. Using all the functions listed in the five articles of this series, you can create high quality graphical applications of several types.

To see some examples of this, you can view the following programs created using BGI graphics :




 


1 comment: