C++ Game Programming
Basic C++ Lessons
This set of lessons will give you a simple introduction to programming in the C++ language.
Allegro Lessons
This set of lessons will demonstrate how to use the Allegro graphics library to create video games.
Game Source Code
Here you will find source code to several games so that you can see the code in action.
Recommended Books
Here is a list of books which should help you with C++

Allegro Mouse
Here we will look into how the mouse is used with Allegro. Here is some example code:

#include <allegro.h>

BITMAP* buffer;

int cursor_x = 20;
int cursor_y = 20;

void getMouseInfo(){

     if(mouse_b & 1){

                  cursor_x = mouse_x;
                  cursor_y = mouse_y;

     }

}

void updateScreen(){

     circlefill ( buffer, cursor_x, cursor_y, 20, makecol( 0, 0, 255));
     draw_sprite( screen, buffer, 0, 0);

}

int main(){
    
    allegro_init();
    install_mouse();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
    
    buffer = create_bitmap( 640, 480);
    
    show_mouse(buffer);
    
    while( !key[KEY_ESC]){
    
           getMouseInfo();
           updateScreen();
    }

    return 0;
    
}
END_OF_MAIN();


This program will draw a blue circle at the current mouse position everytime the left mouse button is pressed.

First of all, to get the mouse to appear, show_mouse(buffer) is used. As you might expect, function displays the mouse cursor on the bitmap specified. In order to make the mouse dissear once again, just do a show_mouse(NULL).

In order to check for the button states, Allegro defines a variable called mouse_b. It sets different bits in the variable to 1, depending on what button was pressed. Here is how to check for the different buttons:

	Left button: mouse_b & 1
	Right button: mouse_b & 2
	Wheel: mouse_b & 3
Checking mouse position is much easier. Allegro stores them in mouse_x and mouse_y. The current wheel position is stored in mouse_z. All of these are plain old normal ints and can be treated like any other variable you've declared (no funny '&'s or anything like that needed).

You may have noticed a black rectangle on some of the circles with the last example. Mostly when you try to move the mouse fast. Wmz, one of the sites viewers, submited the following code which shows one way of eliminating this problem:


#include <allegro.h>

BITMAP* buffer;
int cursor_x = 20;
int cursor_y = 20;
int getMouseInfo(){
     if(mouse_b & 1){
                  cursor_x = mouse_x;
                  cursor_y = mouse_y;
      return 1;
     }
  return 0;
}
void updateScreen(){
 
     show_mouse(NULL);
     circlefill ( buffer, cursor_x, cursor_y, 5, makecol( 0, 0, 255));
     draw_sprite( screen, buffer, 0, 0);  
}
int main(){
    
    allegro_init();
    install_mouse();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
    
    buffer = create_bitmap( 640, 480);     

    show_mouse(screen);        
 
    while( !key[KEY_ESC])
 {
  int switcher=1;
  while(getMouseInfo()) 
  { 
   updateScreen();
   if(getMouseInfo()==0) switcher=0;
  }
  if(switcher==0) show_mouse(screen);
    }
    
 return 0; 
}
END_OF_MAIN();

Here Wmz used a variable called switcher to determine wether or not the mouse cursor should be displayed. If something needs to be drawn to the screen, updateScreen() will first deactivate the mouse so that it does not interrupt the process, once the update is finished, the while loop will continue and redraw the mouse cursor directly to the screen.
Google