Show exmouse.c syntax highlighted
/*
* Example program for the Allegro library, by Shawn Hargreaves.
*
* This program demonstrates how to get mouse input.
*/
#include <stdio.h>
#include "allegro.h"
int main()
{
int mickeyx = 0;
int mickeyy = 0;
AL_BITMAP *custom_cursor;
char msg[80];
int c = 0;
allegro_init();
al_install_keyboard();
al_install_mouse();
al_install_timer();
if (al_set_gfx_mode(AL_GFX_SAFE, 320, 200, 0, 0) != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Unable to set any graphic mode\n%s\n", al_error);
return 1;
}
al_set_palette(al_desktop_palette);
al_text_mode(al_make_color(255, 255, 255));
al_clear_to_color(al_screen, al_make_color(255, 255, 255));
al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W/2, 8, al_make_color(0, 0, 0),
"Driver: %s", mouse_driver->name);
do {
/* On most platforms (eg. DOS) things will still work correctly
* without this call, but it is a good idea to include it in any
* programs that you want to be portable, because on some platforms
* you may not be able to get any mouse input without it.
*/
al_poll_mouse();
al_acquire_screen();
/* the mouse position is stored in the variables al_mouse_x and al_mouse_y */
sprintf(msg, "al_mouse_x = %-5d", al_mouse_x);
al_put_text(al_screen, al_font_8x8, msg, 16, 48, al_make_color(0, 0, 0));
sprintf(msg, "al_mouse_y = %-5d", al_mouse_y);
al_put_text(al_screen, al_font_8x8, msg, 16, 64, al_make_color(0, 0, 0));
/* or you can use this function to measure the speed of movement.
* Note that we only call it every fourth time round the loop:
* there's no need for that other than to slow the numbers down
* a bit so that you will have time to read them...
*/
c++;
if ((c & 3) == 0)
al_get_mouse_mickeys(&mickeyx, &mickeyy);
sprintf(msg, "mickey_x = %-7d", mickeyx);
al_put_text(al_screen, al_font_8x8, msg, 16, 88, al_make_color(0, 0, 0));
sprintf(msg, "mickey_y = %-7d", mickeyy);
al_put_text(al_screen, al_font_8x8, msg, 16, 104, al_make_color(0, 0, 0));
/* the mouse button state is stored in the variable al_mouse_b */
if (al_mouse_b & 1)
al_put_text(al_screen, al_font_8x8, "left button is pressed ", 16, 128, al_make_color(0, 0, 0));
else
al_put_text(al_screen, al_font_8x8, "left button not pressed", 16, 128, al_make_color(0, 0, 0));
if (al_mouse_b & 2)
al_put_text(al_screen, al_font_8x8, "right button is pressed ", 16, 144, al_make_color(0, 0, 0));
else
al_put_text(al_screen, al_font_8x8, "right button not pressed", 16, 144, al_make_color(0, 0, 0));
if (al_mouse_b & 4)
al_put_text(al_screen, al_font_8x8, "middle button is pressed ", 16, 160, al_make_color(0, 0, 0));
else
al_put_text(al_screen, al_font_8x8, "middle button not pressed", 16, 160, al_make_color(0, 0, 0));
/* the wheel position is stored in the variable al_mouse_z */
sprintf(msg, "al_mouse_z = %-5d", al_mouse_z);
al_put_text(al_screen, al_font_8x8, msg, 16, 184, al_make_color(0, 0, 0));
al_release_screen();
al_vsync();
} while (!al_key_pressed());
al_clear_keybuf();
/* To display a mouse pointer, call al_show_mouse(). There are several
* things you should be aware of before you do this, though. For one,
* it won't work unless you call al_install_timer() first. For another,
* you must never draw anything onto the al_screen while the mouse
* pointer is visible. So before you draw anything, be sure to turn
* the mouse off with al_show_mouse(NULL), and turn it back on again when
* you are done.
*/
al_clear_to_color(al_screen, al_make_color(255, 255, 255));
al_put_text_centre(al_screen, al_font_8x8, "Press a al_key to change cursor",
AL_SCREEN_W/2, AL_SCREEN_H/2, al_make_color(0, 0, 0));
al_show_mouse(al_screen);
al_read_key();
al_show_mouse(NULL);
/* create a custom mouse cursor bitmap... */
custom_cursor = al_create_bitmap(32, 32);
al_clear_to_color(custom_cursor, al_bitmap_mask_color(al_screen));
for (c=0; c<8; c++)
al_draw_circle(custom_cursor, 16, 16, c*2, al_palette_color[c]);
/* select the custom cursor and set the focus point to the middle of it */
al_set_mouse_sprite(custom_cursor);
al_set_mouse_sprite_focus(16, 16);
al_clear_to_color(al_screen, al_make_color(255, 255, 255));
al_put_text_centre(al_screen, al_font_8x8, "Press a al_key to quit", AL_SCREEN_W/2,
AL_SCREEN_H/2, al_make_color(0, 0, 0));
al_show_mouse(al_screen);
al_read_key();
al_show_mouse(NULL);
al_destroy_bitmap(custom_cursor);
return 0;
}
AL_END_OF_MAIN();
See more files for this project here