File: exmouse.c

package info (click to toggle)
allegro4 2%3A4.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 17,052 kB
  • ctags: 12,972
  • sloc: ansic: 109,525; asm: 16,672; cpp: 3,221; sh: 1,761; makefile: 556; pascal: 105; perl: 73
file content (135 lines) | stat: -rw-r--r-- 4,284 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 *    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;
   BITMAP *custom_cursor;
   char msg[80];
   int c = 0;

   allegro_init();
   install_keyboard(); 
   install_mouse();
   install_timer();
   if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
      return 1;
   }
   set_palette(desktop_palette);
   text_mode(makecol(255, 255, 255));
   clear_to_color(screen, makecol(255, 255, 255));

   textprintf_centre(screen, font, SCREEN_W/2, 8, makecol(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.
       */ 
      poll_mouse();

      acquire_screen();

      /* the mouse position is stored in the variables mouse_x and mouse_y */
      sprintf(msg, "mouse_x = %-5d", mouse_x);
      textout(screen, font, msg, 16, 48, makecol(0, 0, 0));

      sprintf(msg, "mouse_y = %-5d", mouse_y);
      textout(screen, font, msg, 16, 64, makecol(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)
	 get_mouse_mickeys(&mickeyx, &mickeyy);

      sprintf(msg, "mickey_x = %-7d", mickeyx);
      textout(screen, font, msg, 16, 88, makecol(0, 0, 0));

      sprintf(msg, "mickey_y = %-7d", mickeyy);
      textout(screen, font, msg, 16, 104, makecol(0, 0, 0));

      /* the mouse button state is stored in the variable mouse_b */
      if (mouse_b & 1)
	 textout(screen, font, "left button is pressed ", 16, 128, makecol(0, 0, 0));
      else
	 textout(screen, font, "left button not pressed", 16, 128, makecol(0, 0, 0));

      if (mouse_b & 2)
	 textout(screen, font, "right button is pressed ", 16, 144, makecol(0, 0, 0));
      else
	 textout(screen, font, "right button not pressed", 16, 144, makecol(0, 0, 0));

      if (mouse_b & 4)
	 textout(screen, font, "middle button is pressed ", 16, 160, makecol(0, 0, 0));
      else
	 textout(screen, font, "middle button not pressed", 16, 160, makecol(0, 0, 0));

      /* the wheel position is stored in the variable mouse_z */
      sprintf(msg, "mouse_z = %-5d", mouse_z);
      textout(screen, font, msg, 16, 184, makecol(0, 0, 0));

      release_screen();

      vsync();

   } while (!keypressed());

   clear_keybuf();

   /*  To display a mouse pointer, call 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 install_timer() first. For another,
    *  you must never draw anything onto the screen while the mouse
    *  pointer is visible. So before you draw anything, be sure to turn 
    *  the mouse off with show_mouse(NULL), and turn it back on again when
    *  you are done.
    */
   clear_to_color(screen, makecol(255, 255, 255));
   textout_centre(screen, font, "Press a key to change cursor",
		  SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0));
   show_mouse(screen);
   readkey();
   show_mouse(NULL);

   /* create a custom mouse cursor bitmap... */
   custom_cursor = create_bitmap(32, 32);
   clear_to_color(custom_cursor, bitmap_mask_color(screen));
   for (c=0; c<8; c++)
      circle(custom_cursor, 16, 16, c*2, palette_color[c]);

   /* select the custom cursor and set the focus point to the middle of it */
   set_mouse_sprite(custom_cursor);
   set_mouse_sprite_focus(16, 16);

   clear_to_color(screen, makecol(255, 255, 255));
   textout_centre(screen, font, "Press a key to quit", SCREEN_W/2,
		  SCREEN_H/2, makecol(0, 0, 0));
   show_mouse(screen);
   readkey();
   show_mouse(NULL);

   destroy_bitmap(custom_cursor);

   return 0;
}

END_OF_MAIN();