Chapter 2. Getting Started with the Allegro Game Library

This chapter introduces you to the Allegro game library and shows you how to configure your favorite C++ compiler to use Allegro. In the previous edition, this material was all provided in the appendix, but a few things have changed in the last few years. For one thing, the latest version of Allegro at the time of this writing is 4.2, which is a significant upgrade over the previous version, which was 4.0.3. Work has even begun on 4.3, so the volunteer Allegro team is always keeping busy!

There was a time when installing Allegro was a very difficult process for the inexperienced C++ programmer. Previously, it was necessary to compile the Allegro library's source code before using it. The extremely talented contributors to Allegro have made things so much easier with the latest version, and with the pre-compiled support files. Everything you need to know to get up and running with Allegro and the most popular compilers is fully explained in the following pages. Unlike some programming books that try to offer standalone chapters as a series of independent tutorials, the chapters in this book should be read sequentially because each chapter builds on the one before it. This chapter in particular is critical in that respect because it explains how to set up the development tools used in the rest of the book.

Here is a breakdown of the major topics in this chapter:

Allegro's Versatility Explained

Allegro is one of the few game libraries that truly support most C++ compilers on many systems. But with that wide net of support comes a bit of complexity that I hope to clear up for you in this chapter. First, let's talk about the Windows platform, and then we'll figure out our options with some other platforms.

Allegro is useful for more than just games. It is a full-featured multimedia library as well, and it can be used to create any type of graphical program. I can imagine dozens of uses for Allegro outside the realm of games (such as graphing mathematical functions). You could also use Allegro to port classic games (for which the source code is available) to other computer systems. I have had a lot of fun porting old graphics programs and games to Allegro because it is so easy to use and yet so powerful at runtime.

For instance, Relic Entertainment released the source code to Homeworld in September, 2003, to great acclaim in the game development community. You can download the Homeworld source code by going to http://www.relic.com/rdn. You will need to sign up for an account with the Relic Developer's Network (which is free) to download the source code, an 18 MB zip file. Homeworld was written for DirectX and OpenGL.

The source code for many other commercial games has been released in the last few years, such as the code for Quake III. John Carmack from id Software seems to have started this trend by originally releasing the Doom source code a few years after the game's release, and following that with the code for most of id's games through the years. Why? Because he shares the opinion of many in the game industry that software should not be patented, that education and lifelong learning should be encouraged. Carmack is also a cross-platform developer.

How about another good example? Microsoft recently let loose on the source code and complete resources for MechCommander 2. This was one of my favorite games, so I must admit that I've enjoyed perusing the source code to this game. It is available for download from several sources, so if you Google it, you should find it.

Tip

Please read the first section covering Visual C++ 6.0, which goes into detail on how the Allegro library is used and linked into a C++ project. I do not go into that same depth for the rest of the compiler configurations to save space, but want you to have that information at the same time.

I usually keep the Allegro manual open while writing code (it is located on the CD-ROM in Allegro 4.2manualindex.html). After you have programmed for a while without an online help feature, your coding skill will improve dramatically. It is amazing how very little some programmers really know about their choice programming language because they rely so heavily upon case-sensitive help (and in the case of Microsoft's editors, the IntelliSense feature)! I don't suggest that you memorize the standard C and C++ libraries (although that wouldn't hurt). This might sound ridiculous at first, but it makes sense: When you have to make a little extra effort to look up some information, you are more likely to remember it and not need to look it up again. To an expert programmer, features of convenience usually only get in the way.

Configuring Your Favorite C++ Compiler

I know you are looking forward to jumping into some great source code and working on some real games. I feel the same way! But before you can do that, I have to explain how to configure the development tools used in this book. Regardless of whether you are a newcomer to programming or a seasoned expert looking for an entertaining diversion, you will find this information valuable because it is important to get set up properly before you delve into the advanced programming chapters to come!

Allegro programs can be compiled by many different compilers, which is what sets this game library apart from most others. Consider DirectX, for example. First of all, DirectX is a Windows-only software development kit (SDK) for games. Secondly, it is very difficult to compile a DirectX program with any compiler other than Microsoft Visual C++. DirectX had support for Borland C++ Builder for a time, but it was never fully supported or very easy to set up. Consider some of the other choices you have for cross-platform game development.

You could use OpenGL, but it only handles graphics, not input, sound, or any other component. Some OpenGL games use DirectX components such as DirectInput (for keyboard, mouse, and joystick support) along with Direct-Sound and DirectMusic. Some games use OpenGL with SDL (Simple Direct-Media Layer) to gain access to input, sound, and other resources. There are many professional and hobby game engines to choose from, and most of them are fine-tuned for a particular platform.

Overview of the Compiler Configurations

I want to give you a quick overview of the steps needed to configure a compiler to use the Allegro library, so that the more detailed instructions that follow will be a little clearer for you.

The first step is to find the appropriate zip archive containing the pre-compiled version of Allegro along with supporting files for your compiler. These Allegro packages are found on the CD-ROM in Allegro 4.2.

The second step is to copy the bin and include folders out of the Allegro support package into your compiler's folder on the hard drive.

The third step is to copy the DLL files to your WindowsSystem32 folder so an Allegro program will run from anywhere on your system without complaint. You must remember to distribute the appropriate DLL with your game if you share it with others.

Now I will explain in detail how to configure each specific compiler so you will be able to get up to speed with Allegro as quickly and easily as possible.

Windows Platform

There are many compiler choices on the Windows platform, from the offerings by Microsoft and Borland to the free Dev-C++ compiler. Let's take a look at each one in detail and learn how to configure each of these compilers so you can build Allegro code. This is what you might call a “turn-key” tutorial, since it will explain exactly how to get set up depending on your compiler choice.

Microsoft Visual C++ 6.0

For many years this was the compiler of choice for game developers, even after Visual Studio .NET came out in 2002. Many of you probably still have this compiler installed on your systems for compatibility with old projects. (The previous edition of this book focused on this compiler quite a bit.) I am now putting Visual C++ 6.0 out to pasture for a long-overdue retirement, and will not be using it. Why? Because Microsoft is giving away a free compiler now, the Express Edition of Visual C++ 2005. Given the availability and feature set of this new (and, unbelievably, free) compiler, I recommend you avail yourself of it. However, I will at least give you the information you will need to configure Allegro if you are using 6.0. After all, the latest versions of Visual C++ have heavy system requirements. If you're using an older PC with limited RAM, then 6.0 is a good choice.

Installing Allegro into Visual C++ 6.0

Before you can start using Allegro, you must install the Allegro support files in your compiler's folder. Let's start with where to find the Allegro support files. On the CD-ROM in the Allegro 4.2 folder is a file called allegro-msvc6-4.2.0.zip. This zip file has been opened and is located in the Allegro 4.2 folder already. Inside this folder you will find three sub-folders:

  • bin

  • lib

  • include

The lib and include folders need to be copied into the Visual C++ 6.0 installation folder on your hard drive. This is usually located at C:Program FilesMicrosoft Visual StudioVC98. Just copy both folders into this location, and then you will be able to create Allegro projects very easily. (When warned that the folders already exist, go ahead and proceed, nothing will be overwritten.)

Note

The pre-compiled Allegro packages for the various compilers were put together by members of the Allegro fan site, www.allegro.cc, to whom I (and many others) am grateful. These pre-compiled versions of Allegro make installation a cinch. Before these support packages were put together, we had to build the Allegro source code and install it the hard way!

If you are familiar with the second edition of this book, then you may recall how challenging that was. Now the whole process has been significantly simplified. No more compiling the Allegro library! I would like to thank the members of www.allegro.cc for their hard work, which has made the latest version of Allegro much easier to use.

If you are using another operating system, you may have to build the Allegro sources, but in the case of Mac and Linux, the systems are usually pre-configured with the compiler so the process is much easier than it ever was on Windows. Essentially, imagine that Microsoft packaged Visual C++ along with Windows—that is essentially the case with Mac and Linux systems, only the compiler is called GCC.

The third folder, bin, contains the Allegro library DLLs, which must be distributed with your Allegro programs. The DLLs contain the compiled Allegro library. When your program includes the allegro.h header file, it refers to function names stored in the library file (such as alleg.lib). This library file does not actually contain the Allegro functions, it just includes a link to the DLL file (such as alleg42.dll).

So, let's summarize. You write a C++ program that includes the allegro.h header file. You add alleg.lib to your project's linker settings so that it is compiled within your executable file. That code points to an external DLL called alleg42.dll. When your game first loads up, it also loads the DLL into memory. Any Allegro function that your game calls jumps over to the DLL code in memory and executes.

This is called the dynamic link method. There is also a way to compile the entire Allegro library so that it is linked into your executable file, and then there is no need for the DLL—this is called the static link method. You can see the statically linked library files inside the lib folder that you just copied into Visual C++ 6.0. If you look inside, you'll see six library files:

alleg.lib

Release library (dynamic link)

alld.lib

Debug library (dynamic link)

allp.lib

Profiling library (dynamic link)

alleg_s.lib

Release library (static link)

alld_s.lib

Debug library (static link)

allp_s.lib

Profiling library (static link)

Don't let these files confuse you, because their names tell us what they are all about, and you just need it to be explained to you. The standard library file is called alleg.lib. This is used in a normal “Release” type build of your game. All C++ compilers support multiple configurations, so you can use Debug mode when developing and testing the code, and then switch to Release mode for when your game is ready to go. The debug configuration produces slower code than release mode, because it includes variable names and line numbers and other things that make debugging possible. The release version of an executable leaves all of that stuff out so the code will run faster.

The last step is to copy the DLL files out of the bin folder to C:Windows System32. On some systems, this might be C:WINNTSystem32. You will need to copy these three files:

alleg42.dll

alld42.dll

allp42.dll

Copying these files to your system folder will make it possible to run any Allegro program from anywhere on your hard drive without having to deal with the error message that the DLL is not found. The alternative is to always copy the appropriate DLL file to your project folder. You just have to remember that the DLL has to be distributed with your game too! So, keep a copy of the DLL file you're using handy for when you want to send your game to a friend.

Try not to be confused by all the different files we've looked at here. You really only deal with one type of file at a time, depending on the configuration of your compiler. If you are in the middle of developing a game, you'll want to use Debug mode, but when the game is finished and ready for release, you can switch to Release build and recompile your project. If you want to keep things simple, you can just use alleg.lib all the time. This is the release version of the library. Odds are, you will never need to debug into the Allegro library, so I see nothing wrong with just using alleg.lib all the time (thus ignoring alld.lib). It is certainly faster to use the release version of the library.

Creating a Test Project for Dynamic Linking

Here is how you can configure a Visual C++ 6.0 project to use the Allegro library. First, fire up Visual C++ 6.0, if you don't have it open yet. Next, create a new Win32 Application type project (see Figure 2.1) by selecting File, New. Give the project any name you want.

The New Project dialog box in Visual C++ 6.0.

Figure 2.1. The New Project dialog box in Visual C++ 6.0.

Next, open the Project menu and select Settings to bring up the Project Settings dialog (see Figure 2.2). Click on the Link tab at the top and look for the Object/Library Modules text field. Clear the entire field and type in alleg.lib in place of the other library files, as shown in the figure. All you need for a dynamically linked Allegro program is the alleg.lib library file.

The Project Settings dialog box in Visual C++ 6.0.

Figure 2.2. The Project Settings dialog box in Visual C++ 6.0.

Now you need to make sure the linker can find Allegro. Add a new source code file to the project and type in the following code. This program does very little, but it verifies that Allegro has been linked to your program.

#include <allegro.h>
int main(void) {
    allegro_init();
    set_gfx_mode(GFX_SAFE, 640, 480, 0, 0);
    install_keyboard();
    textout_ex(screen, font, "Hello World!", 1, 1, 10, -1);
    textout_ex(screen, font, "Press ESCape to quit.", 1, 12, 11, -1);
    while(!key[KEY_ESC]);
    allegro_exit();
    return 0;
}
END_OF_MAIN()

If all goes as expected, the compilation output window should show “0 error(s), 0 warning(s)” (see Figure 2.3), and upon running the program, you should see a message box with the phrase “Welcome To Allegro!” If there are any errors, be sure to check for typos.

Compiling an Allegro program with Visual C++ 6.0.

Figure 2.3. Compiling an Allegro program with Visual C++ 6.0.

Creating a Test Project for Static Linking

To statically link the Allegro library with your program's executable, you must have the DirectX SDK (Software Development Kit) installed. The DirectX SDK is installed to the lib and include folder in your compiler's folder, just like the Allegro files. When you link dynamically, Allegro's functions only need the DirectX runtime, so there's no extra files needed. But when you compile the static library, it must make calls into DirectX for drawing surfaces and sprites and playing sound effects and so on.

Although DirectX 9 is available at the time of this writing, you really only need DirectX 8 to compile statically linked code with Allegro. But, you will only need to install the DirectX SDK (which includes the header and library files) if you want to compile the static version of Allegro. If you have not already installed it, refer to the CD-ROM folder called DirectX. If you are using the dynamic version of Allegro (which is most likely the case), you will not need the DirectX SDK. This is an option for advanced users. All you need for normal dynamically linked Allegro programs is the normal DirectX runtime that is most likely already installed on your system. As far as other operating systems are concerned, there are no special libraries or SDKs needed.

Now let's look at how to configure a Visual C++ 6.0 project to build a program using the static Allegro library. This type of project needs a lot more library files than the single alleg.lib file you configured before. Open up the Project Properties and look in the linker object/library modules text box, and add the following entries. Be sure to separate each one with a space.

alleg_s.lib
gdi32.lib
winmm.lib
ole32.lib
dxguid.lib
dinput.lib
ddraw.lib
dsound.lib

The DirectX-specific library files are dxguid.lib, dinput.lib, ddraw.lib, and dsound.lib. The Windows version of Allegro specifically calls on various DirectX functions to get things done when you call on Allegro-specific functions. On other platforms, like Mac and Linux, there are different libraries used. DirectX is only required on the Windows platform.

Tip

I recommend you install an older version of the DirectX SDK when using Visual C++ 6.0, because modern versions of DirectX (such as the June 2006 release) are mostly geared for the latest compiler and have .NET Framework dependencies. In addition, I believe Microsoft has dropped support for 6.0 in the latest versions of DirectX. I have provided the DirectX SDK 8.0 on the CD-ROM for use with Allegro, in case you want to go the static route. If you already have DirectX SDK 9.0 on your system, be sure to install 8.0 in a different location.

There is one more option that needs to be set. You need to tell Allegro to use the static library by including the following line at the top of any program that will use the static library:

#define ALLEGRO_STATICLINK

I have provided a static link project template for Visual C++ 6.0 in the sourcesmsvc60chapter02 folder on the CD-ROM for your use. I won't be going over the steps to create a static project for every compiler, but I have provided examples for you on the CD-ROM.

Microsoft Visual C++ 7.0–7.1 (2002–2003)

The differences between Visual Studio 7.0 (.NET 2002) and Visual Studio 7.1 (.NET 2003) are very minor. Microsoft made an update to the .NET Framework, upgrading it from 1.0 to 1.1, and made a few minor changes to the IDE. The two versions are almost identical otherwise, so you may apply the following tutorial to either version. I'll cover them both together.

You will need to install the pre-compiled Allegro zip file for Visual C++ 7.0 or 7.1 to your hard drive in the appropriate folder where your compiler is located. The default install location is C:Program FilesMicrosoft Visual Studio .NETVC7 (for both versions). Locate the zip file called allegro-msvc70-4.2.0.zip and extract it to a temporary location on your hard drive (for 7.1, use allegro-msvc71-4.2.0.zip). Optionally, I have already opened the zip file for you: Look on the CD-ROM under Allegro 4.2Visual C++ 7.0 (2002) or Allegro 4.2Visual C++ 7.1 (2003). In each of these folders you will find three sub-folders:

  • bin

  • lib

  • include

Copy the entire contents of lib and include into C:Program FilesMicrosoft Visual Studio .NETVC7. That install location for Visual Studio will already contain lib and include folders, so by doing this you will be adding the Allegro support files to Visual C++.

The third folder, bin, contains the DLL files required by an Allegro program. You can copy them to your WindowsSystem32 folder (WINNTSystem32 on some systems) or just keep the DLL handy in each of your project folders for distribution with the program's executable.

Creating a Test Project

Now let's try out the installation by creating a test project, and then see if we can build some Allegro code to verify it is working. Open up Visual C++ and create a new Win32 Project. To do this, open the File menu and choose New, Project, as shown in Figure 2.4. Choose a folder where the new project will be located and enter a name for your new project. I have called this program TestAllegro but you may give it any name you want.

Creating a new Win32 Project in Visual C++ .NET.

Figure 2.4. Creating a new Win32 Project in Visual C++ .NET.

Next, you'll see the Application Wizard dialog box come up, as shown in Figure 2.5. Click the Application Settings option on the left (below the Overview tab). We don't want a project full of template files, so just check the Empty Project checkbox. Make sure under the Application type list you have Windows application selected. Click Finish, and the project will then be created, as shown in Figure 2.6. But this is an empty project, because you chose that option.

The Application Wizard in Visual C++ .NET.

Figure 2.5. The Application Wizard in Visual C++ .NET.

The new project has been created in Visual C++ .NET.

Figure 2.6. The new project has been created in Visual C++ .NET.

Now let's add a source code file to the project. Open the Project menu, and select Add New Item as shown in Figure 2.7. This will bring up the Add New Item dialog box. In the list of templates on the right, choose the C++ File (.cpp) template. Down below, in the Name field, name this new file main.cpp, as shown in Figure 2.8.

Adding a new source code file from the Project menu.

Figure 2.7. Adding a new source code file from the Project menu.

The Add New Item dialog box.

Figure 2.8. The Add New Item dialog box.

You will now see that the new file has been added to the project. The source code file, main.cpp, is currently empty, as you can see in Figure 2.9. Let's type some code into this file. The following code listing might not make much sense to you since we haven't studied any Allegro code yet, so bear with me at this point and I will explain the code very soon. All we want to do right now is get the compiler configured properly.

The new main.cpp source code file has been added to the project.

Figure 2.9. The new main.cpp source code file has been added to the project.

#include <allegro.h>
int main(void) {
    allegro_init();
    set_gfx_mode(GFX_SAFE, 640, 480, 0, 0);
    install_keyboard();
    textout_ex(screen, font, "Hello World!", 1, 1, 10, -1);
    textout_ex(screen, font, "Press ESCape to quit.", 1, 12, 11, -1);
    while(!key[KEY_ESC]);
    allegro_exit();
    return 0;
}
END_OF_MAIN()

Configuring the Project

Now you can configure the project so that all of those Allegro functions will actually compile and run. If you just try to run the project right now, you'll get compiler or linker errors. Let's open the project's Properties dialog box so you can configure it. Open the Project menu, as shown in Figure 2.10, and select the TestAllegro Properties option at the bottom of the menu.

The Project menu.

Figure 2.10. The Project menu.

This will bring up the property pages for your project. Select the C/C++ item on the tree to the left. Click on the Code Generation item under C/C++ options, as shown in Figure 2.11. Then click on the Runtime Library option. Using the drop-down list, change the runtime library to Multi-threaded Debug DLL as shown. The reason why this change is needed is that Visual C++ creates a single-threaded project by default, and Allegro is a multi-threaded library. So, in order to use Allegro, we have to make sure the compiler will output multi-threaded code.

The Code Generation property page.

Figure 2.11. The Code Generation property page.

Now we have to make another change while we're in this Properties dialog. The next item down in the tree below C/C++ is called Linker. Open up this property page. Then click on the Input item in the Linker list to bring up the linker dependencies page shown in Figure 2.12. This is where you tell the compiler to use the Allegro library file. In the Additional Dependencies textbox, type in alleg.lib.

The Linker property page.

Figure 2.12. The Linker property page.

Note

If you are an experienced C++ programmer, you may want to configure the project for Debug and Release mode. In these tutorials, I have been using only the release build of the Allegro library, alleg.lib. The reason for this is an issue of compatibility with Visual C++ runtime files. There are some configurations that generate an error when you try to use the debug library (alld.lib) due to a missing runtime file. Due to that potential issue, I am recommending the use of the release file (alleg.lib), especially for beginners who I would like to isolate from potential trouble spots as much as possible.

Your project is now configured. Go ahead and build the code to verify that it is working properly. Also, make sure the Allegro DLL files are available, either in the Windows system32 folder or in your project folder. If the configuration was set up properly, you should see the window shown in Figure 2.13 appear on the screen. If you get any errors, go back and double-check the steps in this tutorial to see if you have made a typo or missed a step. Since there are really only a few steps involved, you should be able to narrow down any problem.

The TestAllegro program.

Figure 2.13. The TestAllegro program.

Microsoft Visual C++ 8.0 (2005)

Configuring a project to use Allegro for Visual C++ 8.0 (2005) is similar to the process for 2003, but there are a few differences in some of the dialog boxes, so I'll go over those changes. You will need to install the pre-compiled Allegro zip file for Visual C++ 8.0 to your hard drive, in the appropriate folder where your compiler is located. The default install location is C:Program FilesMicrosoft Visual Studio 8VC.

The good news is, it's very easy to configure this compiler to use Allegro, using the pre-built support files! You will very quickly learn to create a new project and configure it for Allegro in a matter of minutes. So, let's see what's involved.

Locate the zip file called allegro-msvc80-4.2.0.zip from the CD-ROM in Allegro 4.2, and extract it to a temporary location on your hard drive. To save some time, I have also opened the zip file for you: Look on the CD-ROM under Allegro 4.2Visual C++ 8.0 (2005). In this folder you will find three sub-folders:

  • bin

  • lib

  • include

Copy the entire contents of lib and include into C:Program FilesMicrosoft Visual Studio 8VC. That install location for Visual Studio will already contain lib and include folders, so by doing this you will be adding the Allegro support files to Visual C++.

The third folder, bin, contains the DLL files required by an Allegro program. You can copy them to your WindowsSystem32 folder (WINNTSystem32 on some systems) or just keep the DLL handy in each of your project folders for distribution with the program's executable.

Creating a Test Project

Now let's try out the installation by creating a test project, and then see if we can build some Allegro code to verify it is working. Open up Visual C++ 2005 and create a new Empty Project. To do this, open the File menu and choose New, Project, as shown in Figure 2.14. Choose a folder where the new project will be located and enter a name for your new project. I have called this program TestAllegro but you may give it any name you want. Note that I have selected Empty Project from the list of project templates in Figure 2.15. This figure is from the Express Edition, and will look a little different if you are using the Professional edition.

Opening the New Project dialog from the File menu.

Figure 2.14. Opening the New Project dialog from the File menu.

The New Project dialog.

Figure 2.15. The New Project dialog.

This process will be a little different than the process for Visual C++ 2003, because there is no Application Wizard this time. Instead, the new project is simply created and ready for you to add a new source code file (see Figure 2.16).

A new project has been created in Visual C++ 2005.

Figure 2.16. A new project has been created in Visual C++ 2005.

Now let's add a source code file to the project. Open the Project menu, and select Add New Item to bring up the Add New Item dialog box. In the list of templates on the right, choose the C++ File (.cpp) template. Down below, in the Name field, name this new file main.cpp, as shown in Figure 2.17.

Adding a new source code file to the empty project.

Figure 2.17. Adding a new source code file to the empty project.

You will now see that the new file has been added to the project. The source code file, main.cpp, is currently empty. Let's type some code into this file. The following code listing might not make much sense to you since we haven't studied any Allegro code yet, so bear with me at this point and I will explain the code very soon. All we want to do right now is get the compiler configured properly.

#include <allegro.h>
int main(void) {
    allegro_init();
    set_gfx_mode(GFX_SAFE, 640, 480, 0, 0);
    install_keyboard();
    textout_ex(screen, font, "Hello World!", 1, 1, 10, -1);
    textout_ex(screen, font, "Press ESCape to quit.", 1, 12, 11, -1);
    while(! key[KEY_ESC]);
    allegro_exit();
    return 0;
}
END_OF_MAIN()

When you have typed the code in, the IDE should look like Figure 2.18. If you have any experience with C++, you are probably mystified by that unusual function called at the end of the main function, called END_OF_MAIN(). Note also that there is no semicolon after this line. That's not actually a function call at all, it's a macro used by Allegro to identify the end of the main function. In normal Windows programming, the main function is called winmain() instead of just plain main(). Allegro doesn't like this, because C++ programs in all other platforms just use main(). Windows is kind of an oddball in this respect, which is why END_OF_MAIN() is needed. You'll get used to it as a trademark of an Allegro program soon enough.

The Allegro program is almost ready to be tested.

Figure 2.18. The Allegro program is almost ready to be tested.

Configuring the Project

Now you can configure the project so that all of those Allegro functions will actually compile and run. If you just try to run the project right now, you'll get compiler or linker errors. Let's open up the project's Properties dialog box so you can configure it. You'll find that this is even easier than the configuration for Visual C++ 2003, if you read that section of the chapter. Open the Project menu and select the Properties option at the bottom of the menu.

This will bring up the property pages for your project. We don't need to set the code generation to multi-threaded, which was necessary with 2003, because 2005 uses this option by default! See, I told you things were easier with this compiler. Now we have to simply add one small item to the properties for this project, and then it will compile the Allegro code. Look at the list of Configuration Properties on the left. Open up the property page called Linker. Then click on the Input item in the Linker list to bring up the linker dependencies page shown in Figure 2.19.

Adding the Allegro library as a linker input option.

Figure 2.19. Adding the Allegro library as a linker input option.

This is where you tell the compiler to use the Allegro library file. In the Additional Dependencies textbox, type in “alleg.lib”.

Note

If you are an experienced C++ programmer, you may want to configure the project for Debug and Release mode. In these tutorials, I have been using only the release build of the Allegro library, alleg.lib. The reason for this is an issue of compatibility with Visual C++ runtime files. There are some configurations that generate an error when you try to use the debug library (alld.lib), due to a missing runtime file. Due to that potential issue, I am recommending the use of the release file (alleg.lib), especially for beginners who I would like to isolate from potential trouble spots as much as possible.

This issue is common when using Visual C++ 2005 Express Edition, but most other editions don't have this issue. It may also be related to the .NET Framework 2.0. Since there are so many configuration possibilities, I defer to the easiest solution. But you may go ahead and configure debug with alld.lib and release with alleg.lib if you want.

Your project is now configured. Go ahead and build the code to verify that it is working properly by pressing Ctrl+F5 (pressing just F5 runs the program with debugging, which is slower, but you may do that if you want). Also, make sure the Allegro DLL files are available, either in the Windows system32 folder or in your project folder.

If the configuration was set up properly, you should see the window come up with the message “Hello World!” on it (shown previously in Figure 2.13). If you get any errors, go back and double-check the steps in this tutorial to see if you have made a typo or missed a step. I have performed this new project configuration dozens of times, and it really is a cinch, so if there is an error, it's always due to a missing library file. Did you copy all of the lib and include files as explained earlier? That is really all there is to it, and this compiler works great for compiling Allegro programs!

Bloodshed Dev-C++ 5.0

Dev-C++ is an open-source C++ compiler with an integrated development environment (IDE). Dev-C++ uses the free GCC (GNU Compiler Collection) compiler, which is—ironically—the same compiler used on the Linux and Mac platforms. Dev-C++ and GCC are both distributed under the GNU General Public License, which means they are freely redistributable as long as the source code is provided for the tools themselves and any derivative works. In case you were wondering, GNU stands for “GNU is Not UNIX.” This is something of an inside joke in the open-source community, in that the name is recursive. Hilarious.

Dev-C++ was developed by Bloodshed Software (http://www.bloodshed.net), and the primary website for Dev-C++ is http://www.bloodshed.net/dev/devcpp.html. The version of Dev-C++ included on the CD-ROM is 4.9.9.2. Dev-C++ projects have been included with all of the source code in the book and made available to you on the CD-ROM. So if you want to use this awesome free compiler, you have the installer and all the source code projects ready to go, along with the pre-built Allegro library (see Figure 2.20).

Dev-C++ is a terrific free C++ compiler that builds Allegro code.

Figure 2.20. Dev-C++ is a terrific free C++ compiler that builds Allegro code.

Note

Bloodshed Software also has a very interesting product called Dev-Pascal that uses the same IDE as Dev-C++ but features syntax highlighting for the Pascal language (including support for Delphi) and makes use of the GNU Pascal compiler. I sure would have enjoyed this product back in the day, when I was a Turbo Pascal fan!

Dev-C++ is a fully capable Windows compiler with support for all the usual Windows libraries (kernel32, user32, gdi32, and so on). In the previous edition of this book, we had to compile the Allegro source code because distribution of 4.0.3 was not pre-packaged like it is now (via the helpful Allegro fans). Previously, we had to install the Dev-C++ MinGW UNIX environment and the MinGW version of DirectX 8 to compile Allegro. Suffice it to say, that was an incredibly difficult process that led to a lot of frustration. But those problems are all behind us because we now have a pre-built version of Allegro that's ready to go for Dev-C++!

MinGW (bundled with Dev-C++) also includes public domain implementations of the entire Windows 32-bit API. This means that someone actually wrote compatible versions of the entire Windows API for use with tools such as MinGW32 to make GCC compatible with Windows. This provides you with all the power of Visual C++ in a very tight, small package. The real benefit to Visual C++ is the comprehensive documentation in the form of MSDN, the dialog editor/form designer, and other value-added features. If you aren't using the .NET Framework, then Dev-C++ is just as capable as Visual C++.

Tip

The only real documentation you will need is the Allegro manual, which is provided on the CD-ROM in the Allegro 4.2manual folder. Open the index.html file to get to the Allegro documentation. You will want to keep this HTML help file open all the time while you're writing code.

I won't debate the fact that Microsoft produces an exemplary and untouchable C++ compiler and IDE for Windows in the form of Visual C++. What I find most convenient about Dev-C++ is the very small footprint in memory (only about 12 MB), the small install file, and the simple installer.

Now allow me to explain how to set up Allegro for this compiler for either dynamic or static link. You must first install Allegro. The default install location for Dev-C++ is in C:Dev-Cpp, so I'll assume you've installed Dev-C++ to that location. Look on the CD-ROM under Allegro 4.2 for the file called allegromingw-4.2.0.zip, which contains the Allegro support files for Dev-C++. I have opened this file for you and it is located in Allegro 4.2Dev-C++ 5.0 (MinGW). Inside this folder are three sub-folders:

  • lib

  • include

  • bin

The lib and include folders contain the Allegro support files needed to compile your Allegro code with Dev-C++. Copy both of these folders into C:Dev-C++, and don't worry about any overwrite warning messages that Windows gives you.

Creating a Test Project

First, let's fire up Dev-C++ and create a new project. Open the File menu and select New, Project, as shown in Figure 2.21. Select Empty Project and give it an appropriate name. I have called this project Test_DevCpp, which is what it is called on the CD-ROM. Dev-C++ will ask you where you would like to save the new project. Choose a folder on your hard drive where you would like to save the new project.

Opening the New Project dialog in Dev-C++.

Figure 2.21. Opening the New Project dialog in Dev-C++.

Now that the new project has been created, you need to add a new source code file to the project. Open the Project menu and select New File (see Figure 2.22).

The New Project dialog in Dev-C++.

Figure 2.22. The New Project dialog in Dev-C++.

Dev-C++ will immediately open a new file in the code editor. Open the File menu and choose Save to save the new source code file as main.cpp. Now type in the source code that follows for this test program:

#include <allegro.h>
int main(void) {
    allegro_init();
    set_gfx_mode(GFX_SAFE, 640, 480, 0, 0);
    install_keyboard();
    textout_ex(screen, font, "Hello World!", 1, 1, 10, -1);
    textout_ex(screen, font, "Press ESCape to quit.", 1, 12, 11, -1);
    while(!key[KEY_ESC]);
    allegro_exit();
    return 0;
}
END_OF_MAIN()

When you have typed in the code, the Dev-C++ IDE will look like Figure 2.23.

Typing in the source code for a test program in Dev-C++.

Figure 2.23. Typing in the source code for a test program in Dev-C++.

Configuring the Project

Now let's configure the project so it will compile Allegro code. If you try to compile the program right now, it will generate errors because you haven't told the compiler where to find the Allegro library file. So, let's do that. It's very easy, but quite different from the other compilers we've looked at so far. If you open the Project menu, you'll find an option called Project Options. This brings up the Project Options dialog box, shown in Figure 2.24 .

Changing the project options in Dev-C++.

Figure 2.24. Changing the project options in Dev-C++.

The first thing you should do in the Project Options dialog is change the project type. When you create an empty project, Dev-C++ defaults to Win32 Console. Change it to Win32 GUI. This will get rid of a pesky pop-up window every time you run an Allegro program from Dev-C++. Setting it to Win32 GUI will cause just a single window to appear when you run your code.

Now there's just one thing we need to add to configure the project to support the Allegro library. In the Project Options dialog, click on the Parameters tab on the top. There are three types of parameters you can set in this tab: Compiler, C++ Compiler, and Linker. In the Linker field, add “–lalleg” as shown in Figure 2.25. Click OK to close the dialog.

Adding the Allegro library to the linker options.

Figure 2.25. Adding the Allegro library to the linker options.

The project is now ready to be compiled and run. In Dev-C++, you use the Execute menu to build and run your programs. You can press Ctrl+F9 to compile a program. If you've made extensive changes to your header (.h) files and need to do a rebuild, that option is Ctrl+F11. Usually, the easiest option is F9, which performs a Compile & Run. The program output is shown in Figure 2.26.

The Hello World program is running!

Figure 2.26. The Hello World program is running!

Configuring the Project for Static Build

Now I want to explain how to set up Dev-C++ for the static library version of Allegro. This version will let you build a program that has no dependency on the Allegro DLL. Let's just assume we're working with the Test_DevCpp program that you just created. I'll show you how to convert that project so that it compiles with the static Allegro library.

First, you have to install the MinGW version of DirectX inside the Dev-C++ folder. On the CD-ROM, look in the softwareDev-C++ 5.0DirectX Files folder. In this folder you will find lib and include. Copy both of these folders into C:Dev-C++. That will give you the library files needed to build the statically linked version of Allegro (which calls on DirectX to “do its stuff”).

Note

The –lalleg option in the linker options is a switch that tells the linker to include the library file named liballeg.a. The compiler just assumes that “lib” is at the front of the file, and also assumes there is an extension of .a at the end of the file. So, when you specify “–lalleg” the compiler looks for a file called “liballeg.a”.

Next, we can configure the project for the static library. Open the Project Options dialog box and click on the Parameters tab. As you'll recall, all you have to do to create a dynamically linked Allegro program is add—lalleg to the Linker box. To configure the project to use the static library, you have to insert quite a few more files to the Linker Options box and another option in the Compiler Options box.

In the first box, labeled Compiler, type in the static library option –ALLEGRO_ STATICLINK. Be sure to include the minus sign at the beginning, because this is a compiler option that is literally passed to GCC at the command line.

Next, in the Linker box, remove “–lalleg” and enter the following files instead:

-lalleg_s
-lgdi32
-lwinmm
-lole32
-ldxguid
-ldinput
-lddraw
-ldsound

Figure 2.27 shows the Project Options dialog box with the settings needed for a statically linked Allegro program.

Configuring Dev-C++ for a statically linked Allegro program.

Figure 2.27. Configuring Dev-C++ for a statically linked Allegro program.

That's all there is to configuring the static library. Since you just modified the previous project, you should still have the source code ready to compile. Go ahead and build the project by pressing F9, and the program should look just like it did before. The difference is, this new executable has no dependencies! That's right, you don't need the DLL file for this type of build.

If you have any problems compiling the program at this point, it is most likely due to missing DirectX files, missing Allegro include files, or missing Allegro library files. You do not need alleg42.dll to run the static version.

Borland C++ 5.5 / C++ Builder

The good new is, yes, you can use Borland C++ to compile the Allegro code in this book. But I will not be formally supporting Borland with this book, because this is a niche compiler. I won't be going into detail on how to configure Borland C++ 5.5 or C++Builder, but I have provided the BCC version of Allegro on the CD-ROM in Allegro 4.2Borland C++ 5.5 (BCC32). If you are an experienced Borland C++ programmer, you will be able to use these pre-packaged Allegro files and the source code in the book with your favorite compiler. This is simply a “bang for the buck” issue, because very few people are using this compiler. I know, the Borland fans deserve as much support as they can get, and that is why I'm talking about Borland C++ here.

Basically, you can use the command-line version of Borland C++ 5.5 to build programs using the Allegro library, or you can configure C++Builder 4.0 or later. There is a help file distributed with Allegro called bcc32.html that explains how to configure Borland's compilers for use with Allegro. That help file is available on the CD-ROM in Allegro 4.2manualuild. But you can't actually compile the Allegro source code using Borland C++ because it uses a different library file format than Visual C++. Borland released a tool called IMPLIB that makes it possible, but I recommend just using the pre-packaged version. Since there is a pre-packaged version of Allegro ready to go with Borland, there's no need for you to build the Allegro library yourself.

Linux Platform

The Linux operating system is a good choice for writing games with Allegro because the GCC compiler is always installed with the operating system, and you can type in programs with a simple text editor and get them to run with very little effort. However, Linux is not for the faint at heart, so if you are a beginner trying to get up to speed with Linux, you might have to pick up a book on using Linux. Since most Linux programmers use their own preferred text editor and the command-line GCC compiler to build their code, I will not be going to all the trouble of creating project files for KDevelop. From what I've heard regarding the previous edition of this book, no one used them. But, good-natured person that I am, I will at least show you how to configure KDevelop so that it will use the Allegro library.

If you haven't installed Allegro yet, open up the linux.html help file located on the CD-ROM in Allegro 4.2manualuild. This file explains how to build and install Allegro 4.2 on a variety of Linux systems. When that is done, you can proceed.

First fire up KDevelop, and then create a new project by opening the Project menu and selecting New. Choose a C terminal program (as shown in Figure 2.28).

Creating a new C terminal program in KDevelop.

Figure 2.28. Creating a new C terminal program in KDevelop.

In the ApplicationWizard dialog box that appears, I recommend disabling the following three unneeded options:

  • GNU-Standard-Files (INSTALL,README,COPYING . . .)

  • User-Documentation

  • Ism-File - Linux Software Map

However, you should keep the Generate Sources and Headers option selected, as shown in Figure 2.29.

Setting parameters in KDevelop's ApplicationWizard.

Figure 2.29. Setting parameters in KDevelop's ApplicationWizard.

You can skip the Version Control System Support dialog box. In the next two dialog boxes, turn off Headertemplate for .h-files and Headertemplate for .c-files, which clog up the source code. Finally, you will come to a Processes dialog box in the ApplicationWizard. Click on Create to build the new project, and ignore any obscure errors that appear regarding missing files. When you are finished, click on the Exit button. KDevelop will create a new project for you, as shown in Figure 2.30.

The new C terminal program, ready to run in KDevelop.

Figure 2.30. The new C terminal program, ready to run in KDevelop.

Now you can set up the project for Allegro. Open the Project menu and select Options to bring up the Project Options dialog box. Click on the Linker Options icon on the left. Select the X11 and kdeui library check boxes, and type the following in the Additional Libraries text box:

-L/usr/local/lib
-L/usr/X11R6/lib
-lalleg
-lpthread
-lXxf86dga
-lXxf86vm
-ldl

Note the uppercase L in the first two linker options; these tell the linker to include every library file found in the supplied folder name, if required by the smart linker (see Figure 2.31).

The Project Options dialog box for compiling an Allegro program with KDevelop.

Figure 2.31. The Project Options dialog box for compiling an Allegro program with KDevelop.

Returning to the editor window, you can type in a program that actually demonstrates that the Allegro library is indeed working as expected. Here is a short program that will do just that.

#include <allegro.h>
int main(void) {
    char version[80];
    allegro_init();
    sprintf(version, "Allegro library version = %s", allegro_id);
    allegro_message(version);
    allegro_exit();
    return 0;
}
END_OF_MAIN()

If the project has been configured correctly and if Allegro was installed correctly, the program should compile and run with an output like the one shown in Figure 2.32. I have tested the code on Fedore Core, and assume it will run without incident on other popular Linux distributions as well, such as Debian.

Testing the Allegro program compiled with KDevelop.

Figure 2.32. Testing the Allegro program compiled with KDevelop.

Mac Platform

Allegro does compile and is fully supported on the Mac OS platform, including the latest versions of MacOS X. To install Allegro 4.2 on your Mac, you will need to perform a few more steps than are required for Windows users. If you look in the CD-ROM in Allegro 4.2manualuild, there is a file called macosx.html that contains a complete tutorial on configuring Allegro to build with Apple Developer Tools (December 2002 or newer) or using the XCode environment. There is no pre-packaged support for the Mac, so you will have to follow the instructions in this help file to configure your Mac to build Allegro and then configure a project for it. The process is very straightforward and fully explained.

The Linux version of the Allegro sources can be compiled on your Mac OS X system using GCC. The Allegro library is located on the CD-ROM under Allegro 4.2Sources. The file is called allegro-4.2.0.tar.gz.

Basically, these steps are exactly what we had to do in the previous edition of this book on most platforms, and the instructions in the macosx.html help file are almost exactly the same as those for Linux. Compiling the Allegro source code is basically the same process here as it was for Linux because UNIX is at the core of Mac OS X, and most of the steps are identical. I will simply defer you to the help file to get Allegro set up on your Mac, because I am not a Mac user and would not want to make any false assumptions about using it. Once you have Allegro configured, all of the source code in the book will build on your Mac system with precisely the same results as any other system.

Taking Allegro for a Spin

Now that you have a solid grasp of how to configure your favorite compiler for Allegro, let's take Allegro for a spin and get a little more familiar with it. I will give you the source code and tell you what the output of this program should look like, and then your task is to create the project (with whatever compiler you're using) and get it configured so that it will compile and run. The GetInfo program is interesting, in my opinion, because it works on every system, but produces a totally different output on those systems. You may be running Windows XP on a Pentium IV, or Debian Linux on a Pentium III, or even MacOS X on a PowerPC system, and this program will display the details of your system in any of these cases. The geek in me enjoys statistical-type information and specifications like this, and I suspect you do to, so let's take a look at this program.

Introducing Some of Allegro's Features

I'm going to gradually introduce you to Allegro's functions in each chapter, and we'll get started right now because this GetInfo program has a lot of interesting stuff in it. The first function that you need to know is allegro_init, which has this syntax:

int allegro_init();

This function is required because it initializes the Allegro library. If you do not call this function, the program will probably crash (at worst) or simply not work (at best). In addition to initializing the library, allegro_init also fills a number of global string and number variables that you can use to display information about Allegro. One such variable is a string called allegro_id; it is declared like this:

extern char allegro_id[];

You can use allegro_id to display the version number for the Allegro library you have installed. That is a good way to check whether Allegro has been installed correctly, so you should write some code to display allegro_id. Referring to the GetInfo project you just created, type in the following code:

#include <stdlib.h>
#include <allegro.h>
int main() {
    allegro_init();
    printf("Allegro version = %s
", allegro_id);
    printf("
Press any key...
");
    system("pause");
    allegro_exit();
    return 0;
}
END_OF_MAIN()

This program doesn't go into graphics mode; it just prints some information to the console, which is why I included the stdlib.h file and used the system ("pause") function (so the console window would not immediately close). You can't use Allegro's keyboard functions in console mode, because Allegro taps into the keyboard through the graphical window. Remember, that END_OF_MAIN() function at the bottom of the source listing is actually a macro that is used by Allegro and helps with the multi-platform nature of the library. This is odd, but the macro simply must follow the main function in every program that uses Allegro.

Running the GetInfo Program

If you haven't already, compile and run the program to see the version number of Allegro displayed on the screen. If you have problems running the program, aside from syntax errors due to typos, you might want to double-check that you have the correct path to the Allegro library file. For any errors, refer to the section earlier in this chapter for your configuration and double-check your project settings. This is a good test to perform, because it not only tells you if your program is interfacing with Allegro, but it makes sure the correct version of Allegro is installed.

Adding to the GetInfo Program

Now ou can add some more functionality to the GetInfo program to explore more of the functions available with Allegro. Rather than going over each step individually, I'm just going to throw it all out here for you to examine. I think you will appreciate all of the different systems that Allegro recognizes, and hopefully your system will be correctly displayed.

Mine wasn't, interestingly enough! I have several systems that I test code on, but my main PC is a Pentium IV chip with HyperThreading, which is sort of a hybrid before the Pentium D (dual core) and the Core Solo and Core Duo chips were introduced. My system came up with processor family 15 (extended) and model number 14, which was not defined in Allegro. So I just added a new line to the appropriate switch statement to print out my processor. Feel free to do this if you know exactly what type of processor your system has. These details are not as important as they seem at first, because Allegro programs really only need a vague idea about the hardware.

Before giving you the code for GetInfo, let me show you the output from my system, in Figure 2.33.

The GetInfo program displays details about a computer system.

Figure 2.33. The GetInfo program displays details about a computer system.

#include <allegro.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
  //initialize Allegro
     allegro_init();

     set_gfx_mode(GFX_SAFE, 640, 480, 0, 0);
     install_keyboard();

     //display version info
        textprintf_ex(screen, font, 0, 0, 15, -1,
        "Allegro version: %s", allegro_id);

     int year = ALLEGRO_DATE / 10000;
     int month = (ALLEGRO_DATE / 100) % 100;
     int day = ALLEGRO_DATE % 100;
     textprintf_ex(screen,font,0,20,15,-1,
         "Revision Date:     %d/%d/%d", month, day, year);

     //display operating system
     char os[20];
     switch (os_type) {
         case   OSTYPE_UNKNOWN:   strcpy(os,"Unknown/DOS");      break;
         case   OSTYPE_WIN3:     strcpy(os,"Windows 3.1");       break;
         case   OSTYPE_WIN95:    strcpy(os,"Windows 95");        break;
         case   OSTYPE_WIN98:    strcpy(os,"Windows 98");        break;
         case   OSTYPE_WINME:    strcpy(os,"Windows ME");        break;
         case   OSTYPE_WINNT:    strcpy(os,"Windows NT");        break;
         case   OSTYPE_WIN2000:   strcpy(os,"Windows 2000");     break;
         case   OSTYPE_WINXP:    strcpy(os,"Windows XP");        break;
         case   OSTYPE_OS2:      strcpy(os,"OS/2");              break;
         case   OSTYPE_WARP:     strcpy(os,"OS/2 Warp 3");       break;
         case   OSTYPE_DOSEMU:     strcpy(os,"Linux DOSEMU");    break;
         case   OSTYPE_OPENDOS:   strcpy(os,"Caldera OpenDOS");     break;
         case   OSTYPE_LINUX:    strcpy(os,"Linux");             break;
         case   OSTYPE_SUNOS:    strcpy(os,"SunOS/Solaris");     break;
         case   OSTYPE_FREEBSD:   strcpy(os,"FreeBSD");          break;
         case   OSTYPE_NETBSD:     strcpy(os,"NetBSD");          break;
         case   OSTYPE_IRIX:     strcpy(os,"IRIX");              break;
         case   OSTYPE_DARWIN:     strcpy(os,"Darwin");          break;
         case   OSTYPE_QNX:      strcpy(os,"QNX");               break;
         case   OSTYPE_UNIX:     strcpy(os,"Unix");              break;
         case   OSTYPE_BEOS:     strcpy(os,"BeOS");              break;
         case   OSTYPE_MACOS:    strcpy(os,"MacOS");             break;
         case   OSTYPE_MACOSX:     strcpy(os,"MacOS X");         break;
     }
     //display version tacked onto end of O/S name
     textprintf_ex(screen,font,0,40,15,-1,
          "Operating System: %s %i.%i", os, os_version, os_revision);
     if (os_multitasking == 0)
         textout_ex(screen,font,"Multitask:     No",0,60,15,-1);
     else
         textout_ex(screen,font,"Multitask:     Yes",0,60,15,-1);

       //display system info
     int width, height;
     if (get_desktop_resolution(&width, &height) != 0)
           textout_ex(screen,font,"Resolution: Unknown",0,80,15,-1);
     else
       textprintf_ex(screen,font,0,80,15,-1,
           "Resolution:        %i x %i", width, height);

     textprintf_ex(screen,font,0,100,15,-1,
         "Color Depth:      %i bits", desktop_color_depth());

     //display CPU information
     textprintf_ex(screen,font,0,120,15,-1,
         "CPU Vendor:       %s", cpu_vendor);

     //for each cpu family, check the specific model number
     char family[40], model[40];
     switch (cpu_family) {
         case CPU_FAMILY_I386:
            strcpy(family, "386");
            sprintf(model, "%i", cpu_model);
            break;

       case CPU_FAMILY_I486:
         strcpy(family, "486");
         switch (cpu_model) {
             case CPU_MODEL_I486DX:
                 strcpy(model,"486 DX");
                 break;
             case CPU_MODEL_I486DX50:
                 strcpy(model,"486 DX/50");
                 break;
             case CPU_MODEL_I486SX:
                 strcpy(model,"486 SX");
                 break;
             case CPU_MODEL_I487SX:
                 strcpy(model,"487 SX");
                 break;
             case CPU_MODEL_I486SL:
                 strcpy(model,"486 SL");
                 break;
             case CPU_MODEL_I486SX2:
                 strcpy(model,"486 SX/2");
                 break;
             case CPU_MODEL_I486DX2:
                 strcpy(model,"486 DX/2");
                 break;
             case CPU_MODEL_I486DX4:
             strcpy(model,"486 DX/4");
             break;
   }
   break;

case CPU_FAMILY_I586:
    strcpy(family, "586");
    switch (cpu_model) {
        case CPU_MODEL_K5:
            strcpy(model,"AMD K5");
            break;
        case CPU_MODEL_PENTIUM:
            strcpy(model,"Pentium");
            break;
        case CPU_MODEL_PENTIUMP54C:
            strcpy(model,"Pentium Pro");
            break;
        case CPU_MODEL_PENTIUMOVERDRIVE:
            strcpy(model,"Pentium Overdrive");
            break;
        case CPU_MODEL_PENTIUMOVERDRIVEDX4:
            strcpy(model,"Pentium Overdrive DX/4");
            break;
        case CPU_MODEL_K6:
            strcpy(model,"AMD K6");
            break;
        case CPU_MODEL_CYRIX:
            strcpy(model,"Cyrix");
            break;
        case CPU_MODEL_UNKNOWN:
            sprintf(model,"%i",cpu_model);
            break;
       default:
          sprintf(model,"%i",cpu_model);
          break;
   }
   break;


case CPU_FAMILY_I686:
    strcpy(family, "686");
    switch (cpu_model) {
        case CPU_MODEL_PENTIUMPROA:
            strcpy(model,"Pentium Pro/A");
            break;
        case CPU_MODEL_PENTIUMPRO:
            strcpy(model,"Pentium Pro");
            break;
        case CPU_MODEL_PENTIUMIIKLAMATH:
            strcpy(model,"Pentium II/Klamath");
            break;
        case CPU_MODEL_PENTIUMII:
            strcpy(model,"Pentium II");
            break;
        case CPU_MODEL_CELERON:
            strcpy(model,"Celeron");
            break;
        case CPU_MODEL_PENTIUMIIIKATMAI:
            strcpy(model,"Pentium III/Katmai");
            break;
        case CPU_MODEL_PENTIUMIIICOPPERMINE:
            strcpy(model,"Pentium III/Coppermine");
            break;
        case CPU_MODEL_PENTIUMIIIMOBILE:
            strcpy(model,"Pentium III/Mobile");
            break;
        case CPU_MODEL_ATHLON:
            strcpy(model,"Athlon");
            break;
       default:
            sprintf(model,"%i",cpu_model);
            break;
   }
   break;

case CPU_FAMILY_ITANIUM:
    strcpy(family, "Itanium");
    sprintf(model, "%i", cpu_model);
    break;

case CPU_FAMILY_EXTENDED:
    sprintf(family,"%i (unknown)", cpu_family);
    switch (cpu_model) {
        case CPU_MODEL_PENTIUMIV:
            strcpy(model,"Pentium IV");
            break;
        case CPU_MODEL_XEON:
            strcpy(model,"Pentium IV Xeon");
            break;
        case CPU_MODEL_ATHLON64:
            strcpy(model,"AMD Athlon 64");
            break;
        case CPU_MODEL_OPTERON:
            strcpy(model,"AMD Opteron");
            break;
        case 14:
            strcpy(model,"Pentium IV HyperThreading");
            break;
        default:
            sprintf(model,"%i (unknown)", cpu_model);
            break;
     }
     break;


 case CPU_FAMILY_POWERPC:
    strcpy(family, "PowerPC");
     switch(cpu_model) {
         case CPU_MODEL_POWERPC_601:
            strcpy(model,"601");
            break;
         case CPU_MODEL_POWERPC_602:
            strcpy(model,"602");
            break;
         case CPU_MODEL_POWERPC_603:
            strcpy(model,"603");
            break;
        case CPU_MODEL_POWERPC_603e:
            strcpy(model,"603e");
               break;
        case CPU_MODEL_POWERPC_603ev:
               strcpy(model,"603ev");
               break;
        case CPU_MODEL_POWERPC_604:
               strcpy(model,"604");
               break;
        case CPU_MODEL_POWERPC_604e:
               strcpy(model,"604e");
               break;
        case CPU_MODEL_POWERPC_620:
               strcpy(model,"620");
               break;
        case CPU_MODEL_POWERPC_750:
               strcpy(model,"750");
               break;
        case CPU_MODEL_POWERPC_7400:
               strcpy(model,"7400");
               break;
        case CPU_MODEL_POWERPC_7450:
               strcpy(model,"7450");
               break;
        default:
              sprintf(model,"%i",cpu_model);
              break;
      break;
      }
 case CPU_FAMILY_UNKNOWN:
       sprintf(family, "%i", cpu_family);
       sprintf(model,  "%i", cpu_model);
       break;
 }

 textprintf_ex(screen,font,0,140,15,-1,
     "CPU Family:         %s", family);
 textprintf_ex(screen,font,0,160,15,-1,
     "CPU Model:          %s", model);


 //display processor capabilities
 char caps[40];
 strcpy(caps,"");
 if ((cpu_capabilities & CPU_ID)==CPU_ID)
    strcat(caps,"CPU_ID ");
 if ((cpu_capabilities & CPU_FPU)==CPU_FPU)
    strcat(caps,"FPU ");
 if ((cpu_capabilities & CPU_MMX)==CPU_MMX)
    strcat(caps,"MMX ");
 if ((cpu_capabilities & CPU_MMXPLUS)==CPU_MMXPLUS)
    strcat(caps,"MMX+ ");
 if ((cpu_capabilities & CPU_SSE)==CPU_SSE)
    strcat(caps,"SSE ");
 if ((cpu_capabilities & CPU_SSE2)==CPU_SSE2)
    strcat(caps,"SSE2 ");
 if ((cpu_capabilities & CPU_SSE3)==CPU_SSE3)
    strcat(caps,"SSE3 ");
 if ((cpu_capabilities & CPU_3DNOW)==CPU_3DNOW)
    strcat(caps,"3DNOW ");
 if ((cpu_capabilities & CPU_ENH3DNOW)==CPU_ENH3DNOW)
    strcat(caps,"ENH_3DNOW ");
 if ((cpu_capabilities & CPU_CMOV)==CPU_CMOV)
    strcat(caps,"CMOV ");
 if ((cpu_capabilities & CPU_AMD64)==CPU_AMD64)
    strcat(caps,"AMD64 ");
 if ((cpu_capabilities & CPU_IA64)==CPU_IA64)
    strcat(caps,"IA64 ");

 textprintf_ex(screen,font,0,180,15,-1,
    "CPU Capabilities: %s", caps);

 while(!keypressed());
 allegro_exit();
    return 0;
}
END_OF_MAIN()

Summary

That sums up the introduction and configuration of Allegro. I hope that by this time you are familiar with the IDE and have a good understanding of how the library works, as well as what Allegro is capable of (with a little effort on your part). This chapter has given you few tools for building a game as of yet, but it was necessary along that path. Installing and configuring the dev tools is always a daunting task for those who are new to programming, and even experienced programmers get lost when trying to get up and running with a new IDE, compiler, and game library. Not only did you learn to configure a new IDE and open-source compiler, you also got started writing programs using an open-source game library.

This chapter was particularly daunting because I wanted to get all of these cross-platform details out of the way right here and now, before moving on. Now that the logistics are out of the way, we can focus on learning Allegro and writing a few sample programs in the following chapters. I won't be discussing the different compilers from this point forward, so that is why this chapter is so thorough. Any time you have a problem compiling one of the programs to come in future chapters, just refer back here for a rundown on configuration. This will allow us to delve into the Allegro library without being concerned with the differences among the compilers.

Chapter Quiz

You can find the answers to this chapter quiz in Appendix A, “Chapter Quiz Answers.”

1.

What version of Allegro are we using here?

  1. 1.0

  2. 2.4

  3. 4.2

  4. 5.3

2.

GNU is an acronym for which of the following phrases?

  1. GNU is Not UNIX

  2. Great Northern University

  3. Central Processing Unit

  4. None of the above

3.

What is the primary website for Dev-C++?

4.

What is the name of the compiler used by Dev-Pascal?

  1. GNU Pascal

  2. Turbo Pascal

  3. Object Pascal

  4. Microsoft Pascal

5.

What version of Dev-C++ are we using in this book?

  1. 3.0

  2. 4.0

  3. 5.0

  4. 6.0

6.

Which version of Visual C++ are we focusing primarily on in this book?

  1. 6.0

  2. 7.0 (2002)

  3. 7.1 (2003)

  4. 8.0 (2005)

7.

What distinctive feature of Dev-C++ sets it apart from commercial development tools?

  1. Dev-C++ is open-source

  2. Dev-C++ is free

  3. Dev-C++ is multi-platform

  4. All of the above

8.

What is the name of the game programming library featured in this chapter?

  1. DirectX

  2. Gnome

  3. GTK+

  4. Allegro

9.

What function must be called before you use the Allegro library?

  1. main()

  2. byte_me()

  3. allegro_init()

  4. lets_get_started()

10.

What statement must be included at the end of main() in an Allegro program?

  1. END_OF_THE_WORLD()

  2. END_OF_MAIN()

  3. END_OF_FREEDOM()

  4. AH_DONUTS()

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.162.37