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