Home Docs
Graphics.h Documentation
Compiler

Drivers and Modes

Drivers & Modes in graphics.h

This page explains graphics driver and mode basics in simple terms. Each topic has a short snippet, and one full program is provided at the end.

On This Page

DETECT Driver

DETECT asks graphics.h to auto-detect the best available graphics driver and mode.

int gd = DETECT, gm;

initgraph()

Initializes graphics mode. This must be called before drawing functions.

initgraph(&gd, &gm, "");

getmaxx() / getmaxy()

Returns maximum valid X and Y screen coordinates in current mode.

int maxX = getmaxx();
int maxY = getmaxy();

closegraph()

Shuts down graphics mode and restores text mode.

closegraph();

Full Program

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

int main() {
    int gd = DETECT, gm;
    char msg[80];

    initgraph(&gd, &gm, "");

    sprintf(msg, "Mode size: %d x %d", getmaxx(), getmaxy());
    setcolor(WHITE);
    outtextxy(20, 20, "Graphics initialized using DETECT");
    outtextxy(20, 45, msg);
    rectangle(60, 80, 260, 220);

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

Click Run (DOS) to test this program.