Graphics Library (graphics.h) Setup Guide
This guide will help you set up the graphics.h library for C/C++ programming on Windows. The graphics library allows you to create windows and draw shapes in your programs.
Prerequisites
Before starting, make sure you have:
- MinGW installed (follow Windows Setup Guide)
- VSCode with C/C++ extension (follow VSCode Setup Guide)
- Code Runner extension installed in VSCode
- Basic C programming knowledge
- About 10-15 minutes to complete setup
Important Notes Before Starting
- C++ Files Required:
- You MUST save your graphics programs with
.cpp
extension
- The graphics library will NOT work with
.c
files
- This is due to the dependency libraries used by graphics.h
- Don’t worry - you can still write C-style code in .cpp files
- One-Click Run Setup:
To run graphics programs with Code Runner (▶️ button):
- Open VSCode Settings (File → Preferences → Settings)
- Search for “code-runner.executorMap”
- Click “Edit in settings.json”
- Add/update the “cpp” entry:
"code-runner.executorMap": {
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32 && $fileNameWithoutExt"
}
Now you can run graphics programs with just one click on the ▶️ button!
What We’re Installing and Why
graphics.h = A graphics library that lets you:
- Create windows in your C programs
- Draw shapes, lines, and text
- Make simple graphical applications
- Learn basic computer graphics concepts
Step 1: Copy Required Files
First, we need to copy the library files to your MinGW installation:
- Get the library files:
- Go to the
resources/graphics
folder in this repository
- You should find these files:
graphics.h
(header file)
winbgim.h
(additional header file)
libbgi.a
(library file)
- Copy header files to MinGW include directory:
- Copy
graphics.h
and winbgim.h
- Paste them in:
C:\MinGW\include
- Copy library file to MinGW lib directory:
- Copy
libbgi.a
- Paste it in:
C:\MinGW\lib
Step 2: Test Your Installation
Let’s verify everything is working with a simple test program:
- Create a test file:
- Open VSCode
- Create a new file called
test_graphics.cpp
(must use .cpp extension!)
- Copy this test code:
#include <graphics.h>
int main() {
// Create a window
initwindow(800, 600, "My First Graphics");
// Draw a circle
circle(400, 300, 100);
// Wait for user input
getch();
closegraph();
return 0;
}
- Run the program:
- Method 1 - Code Runner (Recommended):
- Make sure you’ve configured Code Runner as shown above
- Click the ▶️ button in the top-right corner or press Ctrl+Alt+N
- Method 2 - Terminal:
- Open terminal in VSCode
- Use this command to compile:
g++ test_graphics.cpp -o test_graphics -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
- Run the program:
- Success check:
- You should see a window appear
- The window should show a circle
- Clicking the window should close it
Common Graphics Functions
Here are some basic functions you can use:
Window Management
initwindow(width, height); // Create window
closegraph(); // Close window
cleardevice(); // Clear screen
Basic Shapes
// Point
putpixel(x, y, color);
// Line
line(x1, y1, x2, y2);
// Rectangle
rectangle(left, top, right, bottom);
// Circle
circle(x, y, radius);
// Text
outtextxy(x, y, "Your text here");
Colors
Available colors:
- BLACK (0)
- BLUE (1)
- GREEN (2)
- CYAN (3)
- RED (4)
- MAGENTA (5)
- BROWN (6)
- LIGHTGRAY (7)
- DARKGRAY (8)
- LIGHTBLUE (9)
- LIGHTGREEN (10)
- LIGHTCYAN (11)
- LIGHTRED (12)
- LIGHTMAGENTA (13)
- YELLOW (14)
- WHITE (15)
Set colors:
setcolor(COLOR); // Set drawing color
setbkcolor(COLOR); // Set background color
Example Program
Here’s a complete program that draws a house:
#include <graphics.h>
int main() {
// Create window
initwindow(800, 600, "House Drawing");
// Set color to blue
setcolor(BLUE);
// Draw house body
rectangle(300, 200, 500, 400);
// Draw roof
line(300, 200, 400, 100);
line(400, 100, 500, 200);
// Draw door
rectangle(375, 300, 425, 400);
// Wait for key press
getch();
closegraph();
return 0;
}
Troubleshooting
Common Issues
- Compiler errors about missing graphics.h
- Check if you copied files to correct MinGW folders
- Verify file permissions
- Make sure paths are exactly:
C:\MinGW\include
and C:\MinGW\lib
- Window not appearing
- Verify compiler flags are included exactly as shown
- Check if initwindow() values are reasonable
- Make sure you have getch() before closegraph()
- Shapes not showing
- Verify coordinates are within window bounds
- Check if colors are set correctly
- Ensure window is initialized before drawing
Still Having Problems?
- Double-check MinGW installation
- Try reinstalling the library files
- Make sure you’re using the exact compiler flags shown
- Restart your computer if changes don’t take effect
What’s Next?
Once your graphics library is working:
- Try modifying the example programs
- Experiment with different shapes and colors
- Create simple animations using loops
- Build interactive graphics programs
Important Notes
- The graphics library is Windows-only
- It’s included primarily for educational purposes
- Modern applications typically use more advanced graphics libraries
- Perfect for learning basic computer graphics concepts
Back to: Home