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