]> git.sur5r.net Git - cc65/blob - samples/mousedemo.c
Consistently use conio (instead of mixing with stdio).
[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 #define max(a,b)  (((a) > (b)) ? (a) : (b))
60 #define min(a,b)  (((a) < (b)) ? (a) : (b))
61
62
63
64 static void CheckError (const char* S, unsigned char Error)
65 {
66     if (Error != MOUSE_ERR_OK) {
67         cprintf ("%s: %s(%d)\r\n", S, mouse_geterrormsg (Error), Error);
68         exit (EXIT_FAILURE);
69     }
70 }
71
72
73
74 static void DoWarning (void)
75 /* Warn the user that a mouse driver is needed for this program */
76 {
77     cprintf ("Warning: This program needs the mouse\r\n"
78              "driver with the name\r\n"
79              "    %s\r\n"
80              "on disk! Press 'y' if you have it or\r\n"
81              "any other key to exit.\r\n", mouse_stddrv);
82     if (tolower (cgetc ()) != 'y') {
83         exit (EXIT_SUCCESS);
84     }
85     cprintf ("Ok. Please wait patiently...\r\n");
86 }
87
88
89
90 static void ShowState (unsigned char Jailed, unsigned char Invisible)
91 /* Display jail and cursor state */
92 {
93     gotoxy (0, 6);
94     cclear (40);
95     gotoxy (0, 6);
96     cprintf ("Mouse cursor %svisible%s", Invisible? "in" : "", Jailed? ", jailed" : "");
97 }
98
99
100
101 int main (void)
102 {
103     struct mouse_info info;
104     struct mouse_box full_box;
105     struct mouse_box small_box;
106     unsigned char Invisible;
107     unsigned char Done;
108     unsigned char Jailed;
109
110     /* Initialize the debugger */
111     DbgInit (0);
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     /* Output a warning about the driver that is needed */
137     DoWarning ();
138
139     /* Load and install the mouse driver */
140     CheckError ("mouse_load_driver",
141                 mouse_load_driver (&mouse_def_callbacks, mouse_stddrv));
142
143     /* Get the initial mouse bounding box */
144     mouse_getbox (&full_box);
145
146     /* Print a help line */
147     clrscr ();
148     revers (1);
149     cputsxy (0, 0, "d)ebug  h)ide   q)uit   s)how   j)ail   ");
150     revers (0);
151
152     /* Test loop */
153     Done = 0;
154     Jailed = 0;
155     Invisible = 1;
156     ShowState (Jailed, Invisible);
157     while (!Done) {
158
159         /* Get the current mouse coordinates and button states and print them */
160         mouse_info (&info);
161         gotoxy (0, 2);
162         cprintf ("X  = %3d", info.pos.x);
163         gotoxy (0, 3);
164         cprintf ("Y  = %3d", info.pos.y);
165         gotoxy (0, 4);
166         cprintf ("LB = %c", (info.buttons & MOUSE_BTN_LEFT)? '1' : '0');
167         gotoxy (0, 5);
168         cprintf ("RB = %c", (info.buttons & MOUSE_BTN_RIGHT)? '1' : '0');
169
170         /* Handle user input */
171         if (kbhit ()) {
172             switch (tolower (cgetc ())) {
173
174                 case 'd':
175                     BREAK();
176                     break;
177
178                 case 'h':
179                     ShowState (Jailed, ++Invisible);
180                     mouse_hide ();
181                     break;
182
183                 case 'j':
184                     if (Jailed) {
185                         Jailed = 0;
186                         mouse_setbox (&full_box);
187                     } else {
188                         Jailed = 1;
189                         small_box.minx = max (info.pos.x - 10, full_box.minx);
190                         small_box.miny = max (info.pos.y - 10, full_box.miny);
191                         small_box.maxx = min (info.pos.x + 10, full_box.maxx);
192                         small_box.maxy = min (info.pos.y + 10, full_box.maxy);
193                         mouse_setbox (&small_box);
194                     }
195                     ShowState (Jailed, Invisible);
196                     break;
197
198                 case 's':
199                     if (Invisible) {
200                         ShowState (Jailed, --Invisible);
201                         mouse_show ();
202                     }
203                     break;
204
205                 case 'q':
206                     Done = 1;
207                     break;
208             }
209         }
210
211     }
212
213     /* Uninstall and unload the mouse driver */
214     CheckError ("mouse_unload", mouse_unload ());
215
216     /* Say goodbye */
217     clrscr ();
218     cputs ("Goodbye!\r\n");
219
220     return EXIT_SUCCESS;
221 }