Fill settings function

setfillstyle() in graphics.h

void far setfillstyle(int pattern, int color);

The setfillstyle() function configures the active fill pattern and fill color for all subsequent filled BGI drawing operations. When you call solid shape functions—such as bar(), bar3d(), fillellipse(), fillpoly(), sector(), pieslice(), or floodfill()—the BGI rendering engine checks the state configured by setfillstyle() to determine how to texture and paint the internal pixels of the shape boundaries.

BGI manages fills by tiling an 8x8 pixel bitmap pattern across the target viewport. The library provides 12 built-in fill patterns, ranging from simple solid fills and hatch grids to dotted textures and slash patterns. The color argument determines the foreground color of the pattern, while pixels mapped as background in the pattern draw using the active background color (or remain transparent depending on transparency modes). By combining different patterns and colors, programmers can simulate textures like brick walls, crosshatching, shading, and gradients within retro graphics constraints.

Parameters

NameTypeDescription
patternintAn integer color pattern index or one of the pre-defined BGI fill pattern constants. Valid ranges are 0 through 12.
colorintThe fill color index (typically 0 to 15) or a standard BGI color name constant (e.g. RED, YELLOW).

Both parameters are integer values. The pattern constants are defined in graphics.h as enumerations. Below is the complete catalog of BGI fill patterns available for use.

BGI Fill Pattern Table

IndexConstant NameDescription
0EMPTY_FILLFills with the current background color (solid erase).
1SOLID_FILLFills with a solid, uniform color (most commonly used).
2LINE_FILLFills with horizontal parallel lines.
3LTSLASH_FILLFills with light slanted lines (slanted top-left to bottom-right).
4SLASH_FILLFills with thick slanted lines (slanted top-left to bottom-right).
5BKSLASH_FILLFills with thick back-slanted lines (top-right to bottom-left).
6LTBKSLASH_FILLFills with light back-slanted lines (top-right to bottom-left).
7HATCH_FILLFills with a horizontal/vertical grid crosshatch pattern.
8XHATCH_FILLFills with a diagonal crosshatch pattern.
9INTERLEAVE_FILLFills with interleaved parallel line segments.
10WIDE_DOT_FILLFills with widely spaced sparse dots.
11CLOSE_DOT_FILLFills with closely spaced dense dots.
12USER_FILLFills with a custom 8x8 pattern defined using setfillpattern().

Return Value

setfillstyle() returns void. If you need to retrieve the current fill settings, call the function getfillsettings(), which populates a structure with the active pattern index and color value.

Code Examples

All 12 Fill Patterns Demo

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

int main() {
    int gd = DETECT, gm;
    int i, x = 30, y = 50;
    char text[15];
    initgraph(&gd, &gm, "");

    for(i = 1; i <= 11; i++) {
        // Apply fill pattern i with color light blue (9)
        setfillstyle(i, 9);
        
        // Draw a filled bar
        bar(x, y, x + 80, y + 80);
        
        // Outline the bar in white
        setcolor(WHITE);
        rectangle(x, y, x + 80, y + 80);

        // Label the pattern index
        sprintf(text, "Pattern: %d", i);
        outtextxy(x, y + 90, text);

        x += 120;
        if(x > 500) {
            x = 30;
            y += 150;
        }
    }

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

This program loops from index 1 (SOLID_FILL) through index 11 (CLOSE_DOT_FILL). For each index, it configures `setfillstyle()`, draws a filled bar, and outlines it with a white rectangle. This visual grid acts as a useful reference index sheet for fill styles.

A simple textured house sketch

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

int main() {
    int gd = DETECT, gm;
    int points[] = {150, 200, 250, 100, 350, 200, 150, 200};
    initgraph(&gd, &gm, "");

    // 1. Draw solid yellow sky background
    setfillstyle(SOLID_FILL, DARKGRAY);
    bar(0, 0, 640, 480);

    // 2. Draw hatch house walls
    setfillstyle(HATCH_FILL, CYAN);
    bar(170, 200, 330, 380);

    // 3. Draw roof using slash fill
    setfillstyle(SLASH_FILL, RED);
    fillpoly(4, points);

    // 4. Draw door using solid fill
    setfillstyle(SOLID_FILL, BROWN);
    bar(230, 280, 270, 380);

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

This code shows how you can combine different pattern and color configurations to create a basic textured shape composite. By changing the fill style before each call, each segment gets painted with a distinct texture.

Common Mistakes

Confusing argument order

The syntax requires (pattern, color). A common mistake is reversing the arguments to setfillstyle(color, pattern). Reversing these values will lead to invalid configuration codes or apply the wrong colors and textures (for example, setting the pattern to color index 14 and the color to pattern index 1).

Forgetting that USER_FILL requires setup

If you call setfillstyle(USER_FILL, RED) without first defining a custom pattern bitmap using setfillpattern(), BGI will execute undefined behavior, which often renders as a messy garbled pattern or crashes the graphics driver. Always set the custom pattern first before referencing USER_FILL.

Expecting outline colors to change

setfillstyle() determines how the *interior* pixels of shape boundaries are painted. It does not change the outline border drawing color, which is governed by setcolor(). For functions like floodfill(x, y, border_color), make sure you configure setcolor() for the boundary and setfillstyle() for the fill itself.

Try it online

Test patterns instantly. Copy-paste these codes into the browser-based editor panel to execute C BGI graphics programs directly in the browser.