]> git.sur5r.net Git - cc65/blob - samples/mousedemo.c
New optimization step
[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 #define max(a,b)  (((a) > (b)) ? (a) : (b))\r
61 #define min(a,b)  (((a) < (b)) ? (a) : (b))\r
62
63
64
65 static void CheckError (const char* S, unsigned char Error)
66 {
67     if (Error != MOUSE_ERR_OK) {
68         printf ("%s: %s(%d)\n", S, mouse_geterrormsg (Error), Error);
69         exit (EXIT_FAILURE);
70     }
71 }
72
73
74
75 static void DoWarning (void)
76 /* Warn the user that a mouse driver is needed for this program */
77 {
78     printf ("Warning: This program needs the mouse\n"
79             "driver with the name\n"
80             "    %s\n"
81             "on disk! Press 'y' if you have it or\n"
82             "any other key to exit.\n", mouse_stddrv);
83     if (tolower (cgetc ()) != 'y') {
84         exit (EXIT_SUCCESS);
85     }
86     printf ("Ok. Please wait patiently...\n");
87 }
88
89
90
91 static void ShowState (unsigned char Jailed, unsigned char Invisible)
92 /* Display jail and cursor state */
93 {
94     gotoxy (0, 6);
95     cclear (40);
96     gotoxy (0, 6);
97     cprintf ("Mouse cursor %svisible%s", Invisible? "in" : "", Jailed? ", jailed" : "");
98 }
99
100
101
102 int main (void)
103 {
104     struct mouse_info info;
105     struct mouse_box full_box;
106     struct mouse_box small_box;
107     unsigned char Invisible;
108     unsigned char Done;
109     unsigned char Jailed;
110
111     /* Initialize the debugger */
112     DbgInit (0);
113
114     /* Output a warning about the driver that is needed */
115     DoWarning ();
116
117     /* Clear the screen, set white on black */
118     (void) bordercolor (COLOR_BLACK);
119     (void) bgcolor (COLOR_BLACK);
120     (void) textcolor (COLOR_GRAY3);
121     cursor (0);
122     clrscr ();
123
124 #if defined(__C64__) || defined(__C128__) || defined(__CBM510__)
125     /* Copy the sprite data */
126     memcpy ((void*) SPRITE0_DATA, MouseSprite, sizeof (MouseSprite));
127
128     /* Set the VIC sprite pointer */
129     *(unsigned char*)SPRITE0_PTR = SPRITE0_DATA / 64;
130
131     /* Set the color of sprite 0 */
132 #ifdef __CBM510__
133     pokebsys ((unsigned) &VIC.spr0_color, COLOR_WHITE);
134 #else
135     VIC.spr0_color = COLOR_WHITE;
136 #endif
137
138 #endif
139
140     /* Load and install the mouse driver */
141     CheckError ("mouse_load_driver",
142                 mouse_load_driver (&mouse_def_callbacks, mouse_stddrv));
143
144     /* Get the initial mouse bounding box */
145     mouse_getbox (&full_box);
146
147     /* Print a help line */
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 }
222
223
224