Software & Finance





Turbo C Graphics - setfillpattern function





 

setfillstyle function is used to set the fill style and color value where as setfillpattern defines the pattern with 8 X 8 matrix (live given below) that is with array of 8 characters.

 

1010 1010 = 0xAA

0101 0101 = 0x55

1010 1010 = 0xAA

0101 0101 = 0x55

1010 1010 = 0xAA

0101 0101 = 0x55

1010 1010 = 0xAA

0101 0101 = 0x55

 

Another example would be,

1111 1111 = 0xFF

0000 0000 = 0x00

1111 1111 = 0xFF

0000 0000 = 0x00

1111 1111 = 0xFF

0000 0000 = 0x00

1111 1111 = 0xFF

0000 0000 = 0x00

 

Just the change the upattern values upattern_solid values and see how the output changes:

unsigned char upattern[8] = {  0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 };

 

The possible style values used in setfillstyle function are, 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

 

The possible color values are from 0 - 15 BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE

 

Look at the following lines of code in the given example: Initially setfillstyle is used to set the SOLID_FILL and WHITE color. Then it got changed immediately with user defined pattern (0xFF or 255) that also represents SOLID_FILL with BLUE color. You can change the array values of upattern_solid with any other values to change the pattern. Now getfillsettings would retrive the recently set values by setfillpattern - USER_FILL (12) and BLUE (1).

 

      setfillstyle(SOLID_FILL, WHITE);

      // The above fillstyle is modifie with the user defined pattern.

      setfillpattern(upattern_solid, BLUE);

      getfillpattern(patternbuffer);

      getfillsettings(&fstype);

 

Back to Turbo C Graphics Index



Source Code


 

#include <graphics.h>

#include <stdio.h>

#include <math.h>

#include <conio.h>

#include <dos.h>

#include <stdlib.h>

 

int InitGraphics()

{

      int grd, grm;

      int gresult;

      // Detect the graphics driver and mode

      detectgraph(&grd,&grm);

      // initialize the graphics mode with initgraph

      initgraph(&grd, &grm, "");

 

      gresult = graphresult();

      if(gresult != grOk)

      {

            printf(grapherrormsg(gresult));

            getch();

            return -1;

      }

 

      // set the background color

      setbkcolor(RED);

      // set the foreground color

      setcolor(WHITE);

      // draw a white color border with rectangle

      rectangle(0,0,getmaxx(),getmaxy());

      return 1;

}

 

void main()

{

      int i, x, y, w;

      int left,top,right,bottom;

      int margin, width, height;

      int cx, cy;

      char msg[512];

      unsigned char patternbuffer[8];

      unsigned char upattern_solid[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

      unsigned char upattern[8] = {  0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 };

      struct fillsettingstype fstype;

      struct linesettingstype lstype;

 

      if(InitGraphics() == -1)

            return;

 

      margin = 100; // 100 pixel margin

      left = margin;

      top = margin;

      bottom = getmaxy() - margin;

      right = getmaxx() - margin;

 

      // draw a bar with user defined solid fill on blue color

      // use rectangle to have a white color border

      setfillstyle(SOLID_FILL, WHITE);

      setfillpattern(upattern_solid, BLUE);

      getfillpattern(patternbuffer);

      getfillsettings(&fstype);

      sprintf(msg, "pattern id: %d color: %d buffer: %d", fstype.pattern, fstype.color, patternbuffer[0]);

      bar(left, top, right, bottom);

      rectangle(left, top, right, bottom);

 

      //another inner rectangle drawn with the use of moveto and lineto functions

      setcolor(YELLOW);

      moveto(left + 5, top + 5);

      lineto(right - 5, top + 5);

      lineto(right - 5, bottom - 5);

      lineto(left + 5, bottom - 5);

      lineto(left + 5, top + 5);

 

      //another inner square drawn with the use of moverel and linerel functions

      setcolor(WHITE);

      moveto(left, top);

      moverel(20, 20);

      linerel(50, 0);

      linerel(0, 50);

      linerel(-50, 0);

      linerel(0, -50);

 

      // draw two lines by crossing above square

      // SOLID_LINE, DOTTED_LINE, CENTER_LINE, DASHED_LINE, USERBIT_LINE

      setlinestyle(DASHED_LINE, 1, 1);

      line(left + 20, top + 20, left + 70, top + 70);

      setlinestyle(DOTTED_LINE, 1, 3);

      line(left + 70, top + 20, left + 20, top + 70);

      getlinesettings(&lstype);

 

      // find the text width and text height

      width = textwidth(msg);

      height = textheight(msg);

      // calculate the right and bottom with margin and getmaxx and getmaxy function

 

      // change the text foreground color to yellow

      setcolor(YELLOW);

      cx = (right + left - width) / 2;

      cy = (bottom + top - height) / 2;

      settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);

      outtextxy(cx,cy,msg);

 

      setcolor(WHITE);

      moveto(100, 100);

      sprintf(msg, "linestyle: %d thickness: %d getx : %d, gety: %d", lstype.linestyle, lstype.thickness, getx(), gety());

      width = textwidth(msg);

      cx = (right + left - width) / 2;

      cy += textheight(msg) + 10;;

 

      // use moveto and outtext function instead of outtextxy

      moveto(cx, cy);

      outtext(msg);

 

      getch();

      closegraph();

}

 

Output