WinBGIm is still widely used in academics to introduce the subject of Computer Graphics. However, times have changed since the library received its last update almost 18 years ago.

Let’s see how to use the library the with modern tooling.

Assumptions made:

  • You have followed along the guide to create a C/C++ developement environment to the dot and have MinGW-w64 utils on the PATH

Steps

  1. The original library was written in the 32-bit era. Download a 64-bit rebuild of the same from here.
    1. Download the graphics.h and libbgi.a files.
  2. Choose a folder you’ll write winbgim related code in and copy these files to a sub-directory named winbgim inside it.
    1. Ideally, the winbgim sub-dir should further be divided into lib and include directories to store the libbgi.a and graphics.h respectively. But it’s fine for now since this is for learning purposes.
    2. Maintaining a separate copy of external dependencies is called vendoring and it enables smoother version management and separation of concerns in bigger codebases.
  3. Launch Command Prompt as Admin and type in the following command:
    1
    
    mklink C:\mingw64\bin\make.exe C:\mingw64\bin\mingw32-make.exe
    
    1. The command creates a symbolic link which is like creating a copy of the file whose contents are always up-to-date with the contents of the original one.
  4. Create a new file named Makefile inside the code folder with the following contents:
    1
    2
    3
    4
    5
    
    CC = g++
    CFLAGS = -W -Wall -pedantic
    CPPFLAGS = -I ./winbgim
    LDFLAGS = -L ./winbgim
    LDLIBS = -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
    
  5. Fire up a new terminal (integrated or otherwise) in the same directory as this file and the code. Issue the following command to compile your programs:
    1
    
    make <your-code-filename-without-extension>
    
    1. The compiled executable can be found in the same directory with the same name as the source file.

Make sure your code file’s extension is .cpp to avoid the error cannot open source file "sstream" (dependency of "graphics.h") if you are using VS Code.

Breaking Down Steps

In steps 1 and 2, we download and place the WinBGIm library files in our working folder.

mingw32-make is the mingw variant of GNU Make. Since its name is too long to type and since it’s known as make on *—Nix systems anyways, we create a shortened symbolic link to it in the step 3.

Lastly we add a Makefile i.e. instructions to build/compile our programs in step 4.

What are Makefiles

A C program of decent size requires multiple libraries to carry out its functions. This requirement is satisfied by linking-in the libraries with the executable. It is analogous to imports in Python or Java.

There’s one caveat though. What libraries to link in, their location and in what order to link them has to be specified every time the compiler is invoked. Besides, any non-standard header files used also have to be made known to the compiler. This is where Makefiles and build-automation tools come into the pitcure. They help us avoid the manual drudgery of managing this. GNU Make is one of them and here is its documentation.

I promise a blog post explainging build automation and Makefiles sometime in the future.

Happy Hacking ;)