c-cpp-setup

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:

Important Notes Before Starting

  1. 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
  2. One-Click Run Setup: To run graphics programs with Code Runner (▶️ button):
    1. Open VSCode Settings (File → Preferences → Settings)
    2. Search for “code-runner.executorMap”
    3. Click “Edit in settings.json”
    4. 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:

Step 1: Copy Required Files

First, we need to copy the library files to your MinGW installation:

  1. 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)
  2. Copy header files to MinGW include directory:
    • Copy graphics.h and winbgim.h
    • Paste them in: C:\MinGW\include
  3. 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:

  1. 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;
}
  1. 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:
        test_graphics
        
  2. 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:

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

  1. 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
  2. Window not appearing
    • Verify compiler flags are included exactly as shown
    • Check if initwindow() values are reasonable
    • Make sure you have getch() before closegraph()
  3. Shapes not showing
    • Verify coordinates are within window bounds
    • Check if colors are set correctly
    • Ensure window is initialized before drawing

Still Having Problems?

  1. Double-check MinGW installation
  2. Try reinstalling the library files
  3. Make sure you’re using the exact compiler flags shown
  4. Restart your computer if changes don’t take effect

What’s Next?

Once your graphics library is working:

  1. Try modifying the example programs
  2. Experiment with different shapes and colors
  3. Create simple animations using loops
  4. Build interactive graphics programs

Important Notes


Back to: Home