When I ask engineering students to switch from TurboC++ to gcc on GNU/Linux, they always tell me about their graphics lab. Even though they can do all the other lab exercises on gcc-on-GNU/Linux, they hesitate when it comes to graphics. The tried and trusted graphics.h and conio.h seems to be the problem. For getting over it the students have to be given an alternative that is as easily usable and has at least a similar API. Now an opportunity for this came when Mar Baselios College of Engineering here in Trivandrum decided to move all of their computer labs to FOSS. They asked the community here to help .
We ( == me, Anoop Jacob Thomas, Prasad S R and Sooraj Kenoth ) looked at alternatives to TurboC++’s graphics functions and decided that since SDL is very popular and seems to be widely used, it would be a good choice. It was then that Syam Krishan C R , an ISRO engineer, suggested allegro. So I checked it out. Here is what I found
- Its API is very similar to the primitives available in TurboC++’s graphics.h. You can modify working TurboC++ code to use Allegro very easily.
- Its crossplatform – even if you are on Windows, you could use this.
Here are the steps I had to take to use Allegro on my GNU/Linux machine:
- I had to install allegro libraries. I use Arch Linux, so I used pacman to install it. In Debian based systems the following command should work: sudo apt-get install liballegro-dev
- Checkout what the whole thing is about – a very very minimal intro can be found here : http://www.cppgameprogramming.com/cgi/nav.cgi?page=allegprimitive
- When its time to compile and link the code , use something like ‘ g++ main.cpp -o main.out `allegro-config –shared` ‘
Here, main.cpp is your code. allegro-config is a utility that gets installed when you install Allegro. It gives you the options that you have to pass to the compiler when linking programs that use Allegro. Instead of running this program , getting the output and using it as parameter to g++ , we put the program in backticks (`) so when the shell see the above command, it will first execute and hen replace the command in the backticks with the output.
I used Netbeans as my C++ IDE. When you use Netbeans, you have to do two things.
- To enable code completion, you have to show netbeans the path to the allegro header files. Go to: Tools > Options > C/C++ > Code Assistance tab > C++ Compiler tab . Click on Add an browse to /usr/include/allegro. Click Ok.
- When building a project in Netbeans C++ IDE , you have to tell it how to link the allegro libraries. For this, right click on your project’s name in the project window, select properties and click on ‘Linker’ under the Build category. Under the heading libraries, click on the button with the text ‘…’. This opens up another window. Click on Add Option, select ‘Other option’ and put `allegro-config –shared` in the text area. Remember to include the backticks. Thats all you need to do.
I tried to port some TurboC++ code that Anoop Jacob Thomas gave me. Click here to get it. Extract it. The folder ( GraphicsDemo) is a Netbeans project. If you have Netbeans C++ ide installed, you can open the project from inside Netbeans by clicking File > Open Project and then browsing to it. Otherwise, open a terminal, change into the extracted directory and type ‘make’ (without quotes). This should produce the executable dist/Debug/GNU-Linux-x86/graphicsdemo . Just change into that directory and do a ./graphicsdemo to run the program.
Inside the archive, there is a file called Transformations_OriginalCode.txt. This is the original TurboC++ code. The three files you have to look for are : main.cpp, CommonFunctions.h and Transformations.cpp (which is the modified code). The code is pretty self explanatory. I did only minimal changes to the code. In fact I modified only the drawPolygon() method and removed the myclrscr() method from the code completely.
Do point out any errors you might notice.