]> git.sur5r.net Git - cc65/blob - samples/mousedemo.c
Adjusted comment.
[cc65] / samples / mousedemo.c
1 /*
2  * Demo program for mouse usage. Will work for the C64/C128/CBM510/Atari/Apple2
3  *
4  * Ullrich von Bassewitz, 13.09.2001
5  *
6  */
7
8
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <mouse.h>
13 #include <conio.h>
14 #include <ctype.h>
15 #include <dbg.h>
16
17
18
19 #if defined(__C64__) || defined(__C128__)
20
21 /* Address of data for sprite 0 */
22 #if defined(__C64__)
23 #  define SPRITE0_DATA  0x0340
24 #  define SPRITE0_PTR   0x07F8
25 #elif defined(__C128__)
26 #  define SPRITE0_DATA  0x0E00
27 #  define SPRITE0_PTR   0x07F8
28 #endif
29
30 /* The mouse sprite (an arrow) */
31 static const unsigned char MouseSprite[64] = {
32     0x00, 0x00, 0x00,
33     0x00, 0x00, 0x00,
34     0x00, 0x00, 0x00,
35     0x0F, 0xE0, 0x00,
36     0x0F, 0xC0, 0x00,
37     0x0F, 0x80, 0x00,
38     0x0F, 0xC0, 0x00,
39     0x0D, 0xE0, 0x00,
40     0x08, 0xF0, 0x00,
41     0x00, 0x78, 0x00,
42     0x00, 0x3C, 0x00,
43     0x00, 0x1E, 0x00,
44     0x00, 0x0F, 0x00,
45     0x00, 0x07, 0x80,
46     0x00, 0x03, 0x80,
47     0x00, 0x00, 0x00,
48     0x00, 0x00, 0x00,
49     0x00, 0x00, 0x00,
50     0x00, 0x00, 0x00,
51     0x00, 0x00, 0x00,
52     0x00, 0x00, 0x00,
53     0x00
54 };
55
56 #endif  /* __C64__ or __C128__ */
57
58
59 /* Dynamically loaded driver by default */
60 #ifndef DYN_DRV
61 #  define DYN_DRV       1
62 #endif
63
64
65 #define max(a,b)  (((a) > (b)) ? (a) : (b))
66 #define min(a,b)  (((a) < (b)) ? (a) : (b))
67
68
69
70 static void CheckError (const char* S, unsigned char Error)
71 {
72     if (Error != MOUSE_ERR_OK) {
73         cprintf ("%s: %s(%d)\r\n", S, mouse_geterrormsg (Error), Error);
74         exit (EXIT_FAILURE);
75     }
76 }
77
78
79
80 static void DoWarning (void)
81 /* Warn the user that a mouse driver is needed for this program */
82 {
83     cprintf ("Warning: This program needs the mouse\r\n"
84              "driver with the name\r\n"
85              "    %s\r\n"
86              "on disk! Press 'y' if you have it or\r\n"
87              "any other key to exit.\r\n", mouse_stddrv);
88     if (tolower (cgetc ()) != 'y') {
89         exit (EXIT_SUCCESS);
90     }
91     cprintf ("Ok. Please wait patiently...\r\n");
92 }
93
94
95
96 static void ShowState (unsigned char Jailed, unsigned char Invisible)
97 /* Display jail and cursor state */
98 {
99     gotoxy (0, 6);
100     cclear (40);
101     gotoxy (0, 6);
102     cprintf ("Mouse cursor %svisible%s", Invisible? "in" : "", Jailed? ", jailed" : "");
103 }
104
105
106
107 int main (void)
108 {
109     struct mouse_info info;
110     struct mouse_box full_box;
111     struct mouse_box small_box;
112     unsigned char Invisible;
113     unsigned char Done;
114     unsigned char Jailed;
115
116     /* Initialize the debugger */
117     DbgInit (0);
118
119     /* Clear the screen, set white on black */
120     (void) bordercolor (COLOR_BLACK);
121     (void) bgcolor (COLOR_BLACK);
122     (void) textcolor (COLOR_GRAY3);
123     cursor (0);
124     clrscr ();
125
126 #if defined(__C64__) || defined(__C128__) || defined(__CBM510__)
127     /* Copy the sprite data */
128     memcpy ((void*) SPRITE0_DATA, MouseSprite, sizeof (MouseSprite));
129
130     /* Set the VIC sprite pointer */
131     *(unsigned char*)SPRITE0_PTR = SPRITE0_DATA / 64;
132
133     /* Set the color of sprite 0 */
134 #ifdef __CBM510__
135     pokebsys ((unsigned) &VIC.spr0_color, COLOR_WHITE);
136 #else
137     VIC.spr0_color = COLOR_WHITE;
138 #endif
139
140 #endif
141
142 #if DYN_DRV
143     /* Output a warning about the driver that is needed */
144     DoWarning ();
145
146     /* Load and install the mouse driver */
147     CheckError ("mouse_load_driver",
148                 mouse_load_driver (&mouse_def_callbacks, mouse_stddrv));
149 #else
150     /* Install the mouse driver */
151     CheckError ("mouse_install",
152                 mouse_install (&mouse_def_callbacks, mouse_static_stddrv));
153 #endif
154
155     /* Get the initial mouse bounding box */
156     mouse_getbox (&full_box);
157
158     /* Print a help line */
159     clrscr ();
160     revers (1);
161     cputsxy (0, 0, "d)ebug  h)ide   q)uit   s)how   j)ail   ");
162     revers (0);
163
164     /* Test loop */
165     Done = 0;
166     Jailed = 0;
167     Invisible = 1;
168     ShowState (Jailed, Invisible);
169     while (!Done) {
170
171         /* Get the current mouse coordinates and button states and print them */
172         mouse_info (&info);
173         gotoxy (0, 2);
174         cprintf ("X  = %3d", info.pos.x);
175         gotoxy (0, 3);
176         cprintf ("Y  = %3d", info.pos.y);
177         gotoxy (0, 4);
178         cprintf ("LB = %c", (info.buttons & MOUSE_BTN_LEFT)? '1' : '0');
179         gotoxy (0, 5);
180         cprintf ("RB = %c", (info.buttons & MOUSE_BTN_RIGHT)? '1' : '0');
181
182         /* Handle user input */
183         if (kbhit ()) {
184             switch (tolower (cgetc ())) {
185
186                 case 'd':
187                     BREAK();
188                     break;
189
190                 case 'h':
191                     ShowState (Jailed, ++Invisible);
192                     mouse_hide ();
193                     break;
194
195                 case 'j':
196                     if (Jailed) {
197                         Jailed = 0;
198                         mouse_setbox (&full_box);
199                     } else {
200                         Jailed = 1;
201                         small_box.minx = max (info.pos.x - 10, full_box.minx);
202                         small_box.miny = max (info.pos.y - 10, full_box.miny);
203                         small_box.maxx = min (info.pos.x + 10, full_box.maxx);
204                         small_box.maxy = min (info.pos.y + 10, full_box.maxy);
205                         mouse_setbox (&small_box);
206                     }
207                     ShowState (Jailed, Invisible);
208                     break;
209
210                 case 's':
211                     if (Invisible) {
212                         ShowState (Jailed, --Invisible);
213                         mouse_show ();
214                     }
215                     break;
216
217                 case 'q':
218                     Done = 1;
219                     break;
220             }
221         }
222
223     }
224
225 #if DYN_DRV
226     /* Uninstall and unload the mouse driver */
227     CheckError ("mouse_unload", mouse_unload ());
228 #else
229     /* Uninstall the mouse driver */
230     CheckError ("mouse_uninstall", mouse_uninstall ());
231 #endif
232
233     /* Say goodbye */
234     clrscr ();
235     cputs ("Goodbye!\r\n");
236
237     return EXIT_SUCCESS;
238 }