In this article we can put away our coding fingers for a moment and focus on an important feature of C++. It is portable and extendable!
This portability of code is an extremely important feature of C++ - but it requires that we write clean, well maintained classes or functions, that are self contained and not dependant on external variables.
A collection of such types of classes can be put together to form libraries which often form the basis of many different types of programs and API's. When we use these collection of classes and functions elsewhere in totally different programs we effectively add extra commands and features to C++, extending it to suit our needs.
You have already used libraries without realizing it. When you compile your programs to run in Windows, Visual Studio automatically includes many specific libraries to allow you to draw to the screen, get input from the keyboard and many other important features. Many other standard functions are contained in the Standard C++ Library which itself contains the STL, the Standard Template Library as well as the original Standard C Library. The Standard C++ library contains many useful functions and you should familiarize yourself with them because at some point you will make use of them. A good source of information can be found at http://www.cplusplus.com/reference/ .
Over time, as you become more competent you will find yourself writing your own stock of routines that you find work well in one game and you may want to use again. You may also find that a routine in the STL is too slow or too memory hungry for you, so you may choose to write an alternative faster/smaller version. Once you have such a collection of routines/classes you can produce your own libraries.
Including a Library
There are confusingly 2 main separate steps to include a library. Lets first of all make sure that Visual studio knows where to look for the libraries.
Load up Visual Studio. Select Tools->Options and look for the Projects and Solutions section (it should be expanded) like this:
As you can see you have a range of directories for you executables...we seldomly need to touch this. Click on the Show directories for drop down though and you get this:
You can now include directories for Lib files and you may also need to include dirs for include files. As you can see, the standard Windows libraries directories are already listed to allow you to compile the standard types of programs supported by Visual Studio. You don't need to enter anything yet, just be aware of these menus.
Now that we know how to tell Visual Studio where to find a library, we still have to tell our program to use that library. The simplest way is to use the project property menu which is located in Project->[XXXX]Properties where [XXXX] is the name of your project. You can also access this menu with alt-F7.
* Note you will need to have a project in VS to see this menu
As you can see there is an option for Additional Dependencies. Clicking on that gives you a [...] button which when you click it gives you an input box like this:
You can see some of the libs that are already included by default. You can enter the name of your own/new library in there. And when the linker next links your project, the library will be included. Any calls you make to routines in that library will now be resolved and you can make use of these functions.
** Remember though: this only tells the linker the name of the file xxx.lib usually...you must also remember to include the directory in the Projects and Solutions menu.
Re-inventing the wheel... Or not
The whole point of libraries, is that over the years many people have come up against the same standard problems, and have found ways to resolve them. These ways have been improved until finally the best overall solutions have been discovered.
These solutions are published in the form of libraries, which then prevent you from having to develop your own solutions to problems already solved.
Also, libraries can hide difficult tasks and provide simple function calls to simplify your work. A classic case is the way Graphics and Audio are handled on PC. To handle the hardware directly is extremely challenging and complex, so Libraries like DirectX and OpenGL for graphics and FMod for sound, provide simple ways to put items on screen or make sound, so you don't have to learn complex hardware coding.
Why is this fastrack titled research when all we've talked about is libraries? The answer is very simple: there are lots of libraries out there, and it's up to you to find the best ones to suit your needs. Some libraries are obvious, like DirectX and OpenGL, but others are not. Your development as a programmer requires you to keep yourself aware of all the different libraries out there and be aware of what solutions the offer to specific problems.
Your task for today:
- Review your earlier tank projects, and locate a suitable sound library on the internet you can download.
- Incorporate this library into your tank project and generate some sounds at suitable places (collision and rumble sounds). Don't worry about trying to find accurate sounds, most libraries come with some examples of their use.
- Once you've done that, provide examples of the following types of libraries and experiment with their examples (if supplied)...Google is your friend.