]> git.sur5r.net Git - cc65/blob - samples/mousedemo.c
The _printf routine does not return anything.
[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 Invisible)
88 /* Display cursor visibility */
89 {
90     gotoxy (0, 6);
91     cclear (40);
92     gotoxy (0, 6);
93     cprintf ("Mouse cursor %svisible", Invisible? "in" : "");
94 }
95
96
97
98 int main (void)
99 {
100     struct mouse_info info;
101     unsigned char Invisible;
102     unsigned char Done;
103
104     /* Initialize the debugger */
105     DbgInit (0);
106
107     /* Output a warning about the driver that is needed */
108     DoWarning ();
109
110     /* Clear the screen, set white on black */
111     (void) bordercolor (COLOR_BLACK);
112     (void) bgcolor (COLOR_BLACK);
113     (void) textcolor (COLOR_GRAY3);
114     cursor (0);
115     clrscr ();
116
117 #if defined(__C64__) || defined(__C128__) || defined(__CBM510__)
118     /* Copy the sprite data */
119     memcpy ((void*) SPRITE0_DATA, MouseSprite, sizeof (MouseSprite));
120
121     /* Set the VIC sprite pointer */
122     *(unsigned char*)SPRITE0_PTR = SPRITE0_DATA / 64;
123
124     /* Set the color of sprite 0 */
125 #ifdef __CBM510__
126     pokebsys ((unsigned) &VIC.spr0_color, COLOR_WHITE);
127 #else
128     VIC.spr0_color = COLOR_WHITE;
129 #endif
130
131 #endif
132
133     /* Load and install the mouse driver */
134     CheckError ("mouse_load_driver",
135                 mouse_load_driver (&mouse_def_callbacks, mouse_stddrv));
136
137     /* Print a help line */
138     revers (1);
139     cputsxy (0, 0, "d: debug   h: hide   q: quit   s: show  ");
140     revers (0);
141
142     /* Test loop */
143     Done = 0;
144     ShowState (Invisible = 1);
145     while (!Done) {
146         if (kbhit ()) {
147             switch (tolower (cgetc ())) {
148                 case 'd':
149                     BREAK();
150                     break;
151                 case 'h':
152                     ShowState (++Invisible);
153                     mouse_hide ();
154                     break;
155                 case 's':
156                     if (Invisible) {
157                         ShowState (--Invisible);
158                         mouse_show ();
159                     }
160                     break;
161                 case 'q':
162                     Done = 1;
163                     break;
164             }
165         }
166
167         /* Get the current mouse coordinates and button states and print them */
168         mouse_info (&info);
169         gotoxy (0, 2);
170         cprintf ("X  = %3d", info.pos.x);
171         gotoxy (0, 3);
172         cprintf ("Y  = %3d", info.pos.y);
173         gotoxy (0, 4);
174         cprintf ("LB = %c", (info.buttons & MOUSE_BTN_LEFT)? '1' : '0');
175         gotoxy (0, 5);
176         cprintf ("RB = %c", (info.buttons & MOUSE_BTN_RIGHT)? '1' : '0');
177
178     }
179
180     /* Uninstall and unload the mouse driver */
181     CheckError ("mouse_unload", mouse_unload ());
182
183     /* Say goodbye */
184     clrscr ();
185     cputs ("Goodbye!\r\n");
186
187     return EXIT_SUCCESS;
188 }
189
190
191