]> git.sur5r.net Git - cc65/blob - samples/mousedemo.c
Update the mouse demo to run with the new mouse API that uses loadable
[cc65] / samples / mousedemo.c
1 /*
2  * Demo program for mouse usage. Will work for the C64/C128/CBM510/Atari
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 <dbg.h>
16
17
18
19 #if defined(__C64__) || defined(__C128__) || defined(__CBM510__)
20
21 /* Address of data for sprite 0 */
22 #if defined(__C64__)
23 #  define SPRITE0_DATA    0x0340
24 #  define SPRITE0_PTR     0x07F8
25 #  define DRIVER          "c64-1351.mou"
26 #elif defined(__C128__)
27 #  define SPRITE0_DATA    0x0E00
28 #  define SPRITE0_PTR     0x07F8
29 #  define DRIVER          "c128-1351.mou"
30 #elif defined(__CBM510__)
31 #  define SPRITE0_DATA    0xF400
32 #  define SPRITE0_PTR     0xF3F8
33 #  define DRIVER          ""            /* Currently unavailable */
34 #endif
35
36 /* The mouse sprite (an arrow) */
37 static const unsigned char MouseSprite[64] = {
38     0x00, 0x00, 0x00,
39     0x00, 0x00, 0x00,
40     0x00, 0x00, 0x00,
41     0x0F, 0xE0, 0x00,
42     0x0F, 0xC0, 0x00,
43     0x0F, 0x80, 0x00,
44     0x0F, 0xC0, 0x00,
45     0x0D, 0xE0, 0x00,
46     0x08, 0xF0, 0x00,
47     0x00, 0x78, 0x00,
48     0x00, 0x3C, 0x00,
49     0x00, 0x1E, 0x00,
50     0x00, 0x0F, 0x00,
51     0x00, 0x07, 0x80,
52     0x00, 0x03, 0x80,
53     0x00, 0x00, 0x00,
54     0x00, 0x00, 0x00,
55     0x00, 0x00, 0x00,
56     0x00, 0x00, 0x00,
57     0x00, 0x00, 0x00,
58     0x00, 0x00, 0x00,
59     0x00
60 };
61
62 #endif  /* __C64__ or __C128__ */
63
64
65
66 static void CheckError (const char* S, unsigned char Error)
67 {
68     if (Error != MOUSE_ERR_OK) {
69         printf ("%s: %s(%d)\n", S, mouse_geterrormsg (Error), Error);
70         exit (EXIT_FAILURE);
71     }
72 }
73
74
75
76 static void DoWarning (void)
77 /* Warn the user that a mouse driver is needed for this program */
78 {
79     printf ("Warning: This program needs the mouse\n"
80             "driver with the name\n"
81             "    %s\n"
82             "on disk! Press 'y' if you have it or\n"
83             "any other key to exit.\n", DRIVER);
84     if (cgetc () != 'y') {
85         exit (EXIT_SUCCESS);
86     }
87     printf ("Ok. Please wait patiently...\n");
88 }
89
90
91
92 static void ShowState (unsigned char Invisible)
93 /* Display cursor visibility */
94 {
95     gotoxy (0, 6);
96     cclear (40);
97     gotoxy (0, 6);
98     cprintf ("Mouse cursor %svisible", Invisible? "in" : "");
99 }
100
101
102
103 int main (void)
104 {
105     struct mouse_info info;
106     unsigned char Invisible;
107     unsigned char Done;
108
109     /* Initialize the debugger */
110     DbgInit (0);
111
112     /* Output a warning about the driver that is needed */
113     DoWarning ();
114
115     /* Clear the screen, set white on black */
116     bordercolor (COLOR_BLACK);
117     bgcolor (COLOR_BLACK);
118     textcolor (COLOR_GRAY3);
119     cursor (0);
120     clrscr ();
121
122 #if defined(__C64__) || defined(__C128__) || defined(__CBM510__)
123     /* Copy the sprite data */
124     memcpy ((void*) SPRITE0_DATA, MouseSprite, sizeof (MouseSprite));
125
126     /* Set the VIC sprite pointer */
127     *(unsigned char*)SPRITE0_PTR = SPRITE0_DATA / 64;
128
129     /* Set the color of sprite 0 */
130 #ifdef __CBM510__
131     pokebsys ((unsigned) &VIC.spr0_color, COLOR_WHITE);
132 #else
133     VIC.spr0_color = COLOR_WHITE;
134 #endif
135
136 #endif
137
138     /* Load and install the mouse driver */
139     CheckError ("mouse_load_driver", mouse_load_driver (&mouse_def_callbacks, DRIVER));
140
141     /* Print a help line */
142     revers (1);
143     cputsxy (0, 0, "d: debug   h: hide   q: quit   s: show  ");
144     revers (0);
145
146     /* Test loop */
147     Done = 0;
148     ShowState (Invisible = 1);
149     while (!Done) {
150         if (kbhit()) {
151             switch (cgetc()) {
152                 case 'd':
153                     BREAK();
154                     break;
155                 case 'h':
156                     ShowState (++Invisible);
157                     mouse_hide ();
158                     break;
159                 case 's':
160                     if (Invisible) {
161                         ShowState (--Invisible);
162                         mouse_show ();
163                     }
164                     break;
165                 case 'q':
166                     Done = 1;
167                     break;
168             }
169         }
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     }
183
184     /* Uninstall and unload the mouse driver */
185     CheckError ("mouse_unload", mouse_unload ());
186
187     /* Say goodbye */
188     clrscr ();
189     cputs ("Goodbye!\r\n");
190
191     return EXIT_SUCCESS;
192 }
193
194
195