In older compilers for C and C++ programs, graphics construction was taught via graphics.h header file. In India, should be astonishing for most, but almost all schools and colleges here still use the TURBO C++ compiler installed via third party software on a Windows operating system to impart the knowledge, which leaves us with libraries that we often find out to be compatible with only the given environment. I love and prefer to use a GNU/Linux distribution for writing my programs. This instilled in me the inquisitivity to try out displaying graphics via the graphics.h header file in my system dual-booted with Ubuntu 16.04 LTS. This did not work, that is, it won't compile due to lack in recognition of the header file. Hence, I searched for a work around that could let me practice my Computer Graphics course on my Ubuntu installed system which is descripted below.
Graphics.h
It is a header file for C and C++ programs to add graphics via the TURBOC compiler. The functions written in this header file can be explored here.
However, problem arises in attempt to display graphics using graphics.h header file in Ubuntu. This is because of the graphics.h header file is not supported by g++ compiler.
To accomplish this, one must run the following commands in shell.
- To download the required packages and library files.
cat packages.txt | xargs sudo apt-get install
OR
xargs -a packages.txt sudo apt-get install
xargs reads items from the standard input and executes the command as many times as the items, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines.
-
Download libgraph-1.0.1.tar.gz or the suitable version from here
-
Extract libgraph-1.0.1.tar.gz into your Home folder
tar xf ~/libgraph-1.0.1.tar.gz
- Navigate into the folder
$ cd libgraph-1.0.1
- Run the following commands in shell. The next command is worth executing only and only if the previous commands successfully executes.
$ ./configure
$ sudo make
$ sudo make install
Now, when writing the graphics code in C or C++, initialize graphics files in Ubuntu as:
int gd,gm=VGAMAX; gd=DETECT;
initgraph(&gd,&gm,NULL);
In Windows, NULL is replaced by path/to/the/BGI file which is written, for instance, as:
int gd,gm=VGAMAX; gd=DETECT;
initgraph(&gd,&gm,"C://TC/BGI");
The path/to/the/BGI shall differ for each system and hence, the path unique to the system must be entered.
Now to compile the code with gcc compiler and then run it, execute the following commands in shell. We are taking example of smile.c graphics file
$ gcc smile.c -o smile -lgraph
$ ./smile
Link to repository
Some basic computer graphics code written in C++ for Computer Graphics Practicum is available here if you wish to check out or test this implementation!