/* Background Graphics lecture To use a background pattern and fill you must first draw your shapes, a rectangle around the whole screen, and then use floodfill to fill the area. See below: */ #include #include #include #include int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int maxx, maxy; /* initialize graphics, local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } maxx = getmaxx(); maxy = getmaxy(); /* select drawing color */ setcolor(getmaxcolor()); //getmaxcolor will return white. Other values //are: RED, GREEN, BLUE, etc.... /* select fill color */ setfillstyle(SOLID_FILL, BLUE); /* draw a border around the screen */ rectangle(0, 0, maxx, maxy); /* draw some circles */ circle(maxx / 3, maxy /2, 50); circle(maxx / 2, 20, 100); circle(maxx-20, maxy-50, 75); circle(20, maxy-20, 25); /* wait for a key */ getch(); /* fill in bounded region */ floodfill(2, 2, WHITE); //the first two paramaters are coordinates //to begin the fill. If you want the fill outside an object make sure //the coordinates are outside. The reverse is also true. The fill //will start from the coordinates and spread outward until it runs into //the color specified by the third paramater. /* clean up */ getch(); closegraph(); return 0; } //Note... this WILL compile and run as a demo. /* Another usefull procedure is drawpoly. This routine requires that you define an arry of type int, and place the coordinates, one per postion into the array. See the demo program's roof section for an example of this. ************** /* House demo. This program will draw a house and a background picture. */ #include #include #include #include int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int maxx, maxy; int mypoints[6]; /* initialize graphics, local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } maxx = getmaxx(); maxy = getmaxy(); /* select drawing color */ setcolor(GREEN); /* select fill color */ setfillstyle(SOLID_FILL, GREEN); /* draw a border around the screen */ rectangle(0, 0, maxx, maxy); //draw the hills setcolor(GREEN); ellipse(maxx / 6, maxy / 2, 0, 180, maxx / 6, 20); ellipse(maxx / 6 * 3, maxy / 2, 0, 180, maxx / 5, 30); ellipse(maxx / 6 * 5, maxy / 2, 0, 180, maxx / 5, 20); //fillhills floodfill(maxx -2, maxy - 2, GREEN); //fillsky setfillstyle( SOLID_FILL, BLUE); floodfill(2, 2, GREEN); //redraw border setcolor(WHITE); rectangle(0,0,maxx, maxy); //drawsun setcolor(YELLOW); circle( maxx - 75, 75, 50 ); setfillstyle( SOLID_FILL, YELLOW); floodfill( maxx - 73, 75, YELLOW); //drawhouse1 setcolor(WHITE); rectangle( maxx / 2 - 30, 400, maxx / 2 + 30, 460); setfillstyle(SOLID_FILL, WHITE); floodfill(maxx / 2 - 20, 420, WHITE); //drawroof1 setcolor(RED); setfillstyle( SOLID_FILL, RED); mypoints[0] = maxx / 2 - 30; mypoints[1] = 400; mypoints[2] = maxx / 2; mypoints[3] = 340; mypoints[4] = maxx / 2 + 30; mypoints[5] = 400; fillpoly(3, mypoints); //drawdoor setcolor(BROWN); setfillstyle( SOLID_FILL, BROWN); rectangle( maxx /2 -10, 430, maxx /2 +10, 460); floodfill( maxx /2 -8, 432, BROWN); /* clean up */ getch(); closegraph(); return 0; }