Topic : Allegro Tutorial
Author : Gillius
Page : << Previous 2  
Go to page :


draw directly to the screen. Use a technique like double buffering where you draw everything to a buffer then draw the buffer to the screen to eliminate flicker and increase speed. Dirty rectangles is also a good variation of double buffering which draws only changed areas to the screen.




Sample Code
This sample program was made and tested under both MSVC and DJGPP using the latest Allegro WIP as of 7/9/00. It displays Hello World!!! in the middle of the screen in yellow with a blue background then waits for a keypress. Many other examples can be found in Allegro's examples directory.

If you have seen Allegro 3.1 you may notice some differences from the code you are used to seeing. The function allegro_message works like printf but works under environments like Windows in the form of a pop-up box, since they have no default text output. In GUI environments the set_window_title function sets the window's title.

Each hicolor and truecolor depth is tested since different cards support different depths. For example my Voodoo3 card supports 24 but not 32 in VESA but 32 and not 24 under Windows. 15 and 16 bit color depths have roughly the same amount of colors and 24 and 32 bit color depths have the same amount of colors. The makecol function takes red, green, and blue components to form a color. See the Allegro documentation for detailed explanations of all of these functions.

#include <allegro.h>

const int scrx = 640;
const int scry = 480;

int main(int argc, char* argv[]) {
  if (allegro_init()) {
    allegro_message("Cannot initalize Allegro.\n");
    return 1;
  }

  //Set the window title when in a GUI environment
  set_window_title("Hello World");

  if (install_keyboard()) {
    allegro_message("Cannot initalize keyboard input.\n");
    return 1;
  }

  //set graphics mode, trying all acceptable depths
  set_color_depth(32);
  if (set_gfx_mode(GFX_AUTODETECT, scrx, scry, 0, 0)) {
    set_color_depth(24);
    if (set_gfx_mode(GFX_AUTODETECT, scrx, scry, 0, 0)) {
      set_color_depth(16);
      if (set_gfx_mode(GFX_AUTODETECT, scrx, scry, 0, 0)) {
        set_color_depth(15);
        if (set_gfx_mode(GFX_AUTODETECT, scrx, scry, 0, 0)) {
          allegro_message("Video Error: %s.\n", allegro_error);
          return 1;
        }
      }
    }
  }

  //set text background color to bright blue
  text_mode(makecol(0, 0, 255));

  //prints yellow "Hello World!!!" in middle of screen
  textout_centre(screen, font, "Hello World!!!", scrx/2,
                 scry/2, makecol(255, 255, 0));

  //Wait for a key to be pressed
  while (!keypressed()) {}

  return 0;
  //Allegro will automatically deinitalize itself on exit
}
END_OF_MAIN()






Using Microsoft Visual C++ with Allegro
I wrote these instructions using MSVC 6.0, and probably work for other MSVC versions too. This section assumes you have never seen MSVC before.

The first thing is to get Allegro installed. Follow the instructions in the Allegro package (at the time of writing, the readme.vc file).

Once Allegro is setup you will want to try to create new Allegro programs or port your Allegro 3.x programs to the new WIP, and to do this you will want to create a new project.

In the file menu, select new, then click projects tab, select Win32 application, and in the project name box type in the name for your new project. If you wish you may change the location of the created directory in the Location box. Press the OK button.

Note for those porting their old projects: it is not necessary for the source code to be in that same directory -- for example if you are working on a project that you want both DOS and Windows executables and you started it in DJGPP, you could leave your source code in the DJGPP directory so both DJGPP and MSVC share the same code, and what you change in MSVC changes in the DJGPP version and vice versa.

The next window will ask what kind of application to you want. Select "An empty project" then press the finish button.

If you do not have any source files to add (from a previous Allegro project you wish to port to Windows), select the new option from the file menu. Select the files tab. From here you can create C++ source and header files, as well as other files which you will probably not create while using Allegro. To create a file select the appropiate file from the list on the left, type in a file name, and press OK. By default the file will be added to your newly created project

If you are porting a program and already have the source code, you can add your code in the Project menu. Select Add To Project > Files, then locate your source files and add them. Remember you can drag a box and use the shift to select multiple files just like other Windows file dialogs. You should add all of your C/C++ source files and your headers as well. Although adding the headers is not required, adding them makes it easy to click on the file in the project to edit it. You can add any other file type as well, for example a readme.txt file or a Word document, for quick editing.

The left pane has the class view, which when you add your C++ files you can get a chart of all your classes and their data members and methods which you can double click to see/edit, a very valuable feature when using C++. You can click the tabs below to see files, resources, and a help window. Click on the FileView tab to see the source you added. Use the above tree to navigate the files in your project -- double click to edit.

If you are porting your program from Allegro 3.x, there are a few changes you need to make, which are mentioned in the Allegro documentation. Once you make these changes your program will compile under any compiler supported by Allegro.

Once you have your source code in MSVC, you will want to set up your project to compile for Allegro. In the project menu select settings. In the Settings For box make sure Win32 Debug is selected. Make sure your project's name is highlighted in the tree box below by clicking on it, then click on the Link tab. In the Object/library modules textbox add "alld.lib" to the end of the list (do not type the quotes). Lastly, change the Settings For box to Win32 Release and add "alleg.lib" to the end of the Object/library modules list and press OK.

To compile your program press the build button on the toolbar (shortcut F7). The message window at the bottom will show any errors and warnings -- double click on a line to be shown the error to be fixed. After all errors are fixed press the build again until the EXE builds (It will show 0 errors and warnings if it worked). To run your program press the the execute button which looks like an exclaimation point (shortcut Shift-F5) to run the program. If you want to run the program in debugging mode press the button next to it, the "Go" button (shortcut F5). Common debugging functions are shown on the debugging menu.

NOTE: The MSVC debugger cannot be used if your program is running in full-screen DirectX mode. Change your graphics driver to GFX_DIRECTX_WIN or GFX_DIRECTX_OVL if your card supports it, else use GFX_GDI which is unfortunately much slower. You obviously will also need to have your desktop in a higher resolution than your game if you intend to see other windows.

After your program is complete and you want to distribute it, you will want to compile the program in release mode. Note that release mode only works on the Professional and Enterprise editions of MSVC; MSVC standard does not have an optimizer and only runs in debugging mode. In the Build menu pick "Set Active Configuration" and pick the release mode for your project and rebuild your project as before. You will find the release and debug EXE files in the Release and Debug directories in your main project's directory.

Page : << Previous 2