C Graphics Programming Tutorial


C Programming language

C Graphics Programming

This C Graphics tutorials is for those who want to learn fundamentals of Graphics programming, without any prior knowledge of graphics. This tutorials contains many fundamental graphics program like drawing of various geometrical shapes, use of mathematical function in drawing

curves, coloring an object with different colors, patterns and simple animation programs. This tutorial will provide you an overview of computer graphics and it's fundamentals.

Graphics programming in C using Turbo C provides a nostalgic journey for those who started their programming journey in the DOS era. Turbo C was a popular integrated development environment (IDE) that included a graphics library for simple graphics programming. In this tutorial, we'll explore the basics of C graphics programming using Turbo C, covering topics such as setting up the environment, drawing shapes, handling input, and creating animations.


Setting Up Turbo C for Graphics Programming

Turbo C is a DOS-based IDE, and setting it up for graphics programming involves a few specific steps. Here's a guide to help you get started:
  • Download Turbo C: You can find Turbo C online and download it from various sources that distribute legacy software.

  • Install Turbo C: Once downloaded, follow the installation instructions to set up Turbo C on your system. Make sure to install it in a directory without spaces in the path, as older DOS-based programs might have issues with spaces.

  • Configure Graphics Library: Turbo C includes a graphics library that you need to configure. Open Turbo C and go to the "Options" menu. Select "Linker" and add the library file "graphics.lib" to the list of library files.

  • Include Header Files: In your C program, include the necessary header files
    #include <graphics.h>
    #include <conio.h>
      
With Turbo C set up, you're ready to start graphics programming.

The first step in any graphics program is to initialize the graphics drivers on the computer using initgraph method of graphics.h library.

void initgraph(int *graphicsDriver, int *graphicsMode, char *driverDirectoryPath);

It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode. It also resets or initializes all graphics settings like color, palette, current position etc, to their default values. Below is the description of input parameters of initgraph function.

  • graphicsDriver : It is a pointer to an integer specifying the graphics driver to be used. It tells the compiler that what graphics driver to use or to automatically detect the drive. In all our programs we will use DETECT macro of graphics.h library that instruct compiler for auto detection of graphics driver.
  • graphicsMode : It is a pointer to an integer that specifies the graphics mode to be used. If *graphdriver is set to DETECT, then initgraph sets *graphmode to the highest resolution available for the detected driver.
  • driverDirectoryPath : It specifies the directory path where graphics driver files (BGI files) are located. If directory path is not provided, then it will seach for driver files in current working directory directory. In all our sample graphics programs, you have to change path of BGI directory accordingly where you turbo C compiler is installed.

Colors in C Graphics Programming

There are 16 colors declared in C Graphics. We use colors to set the current drawing color, change the color of background, change the color of text, to color a closed shape etc. To specify a color, we can either use color constants like setcolor(RED), or their corresponding integer codes like setcolor(4). Below is the color code in increasing order.

COLOR MACRO INTEGER VALUE
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

At the end of our graphics program, we have to unloads the graphics drivers and sets the screen back to text mode by calling closegraph function. Here is our first C Graphics program to draw a straight line on screen.

/* C graphics program to draw a line */
#include<graphics.h>
#include<conio.h>
int main() {
    int gd = DETECT, gm;
    /* initialization of graphic mode */
    initgraph(&gd, &gm, "C:\\TC\\BGI"); 
    line(100,100,200, 200);
    getch();
    closegraph();
    return 0;
}

In this program initgraph function auto detects an appropriate graphics driver and sets graphics mode maximum possible screen resolution. Then line function draws a straight line from coordinate (100, 100) to (200, 200). Then we added a call to getch function to avoid instant termination of program as it waits for user to press any key. At last, we unloads the graphics drivers and sets the screen back to text mode by calling closegraph function.


Drawing Basic Shapes

Turbo C provides functions for drawing lines, circles, rectangles, and other shapes. Here's an example:

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

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

    // Draw a line
    line(100, 100, 200, 200);

    // Draw a rectangle
    rectangle(250, 150, 350, 250);

    // Draw a circle
    circle(500, 200, 50);

    getch();
    closegraph();
    return 0;
}  
  
In this example, we use the line, rectangle, and circle functions to draw basic shapes on the graphics window.

C Graphics Programs


Conclusion

This tutorial has introduced you to C graphics programming using Turbo C, a classic IDE from the DOS era. You've learned how to set up Turbo C for graphics programming, draw basic shapes, handle user input, and create simple animations. While Turbo C might be considered outdated for modern development, exploring its graphics capabilities offers a nostalgic and educational experience. As you continue to delve into graphics programming, you can explore more advanced techniques and consider transitioning to modern graphics libraries for contemporary development. Happy coding!