]> git.sur5r.net Git - cc65/blob - samples/mousedemo.c
2ac1dc38dd7500ed06f744262c4008e937dc5e3e
[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 <stdlib.h>
11 #include <string.h>
12 #include <mouse.h>
13 #include <conio.h>
14 #include <dbg.h>
15
16
17
18 #if defined(__C64__) || defined(__C128__) || defined(__CBM510__)
19
20 /* Address of data for sprite 0 */
21 #if defined(__C64__)
22 #  define SPRITE0_DATA    0x0340
23 #  define SPRITE0_PTR     0x07F8
24 #elif defined(__C128__)
25 #  define SPRITE0_DATA    0x0E00
26 #  define SPRITE0_PTR     0x07F8
27 #elif defined(__CBM510__)
28 #  define SPRITE0_DATA    0xF400
29 #  define SPRITE0_PTR     0xF3F8
30 #endif
31
32 /* The mouse sprite (an arrow) */
33 static const unsigned char MouseSprite[64] = {
34     0x00, 0x00, 0x00,
35     0x00, 0x00, 0x00,
36     0x00, 0x00, 0x00,
37     0x0F, 0xE0, 0x00,
38     0x0F, 0xC0, 0x00,
39     0x0F, 0x80, 0x00,
40     0x0F, 0xC0, 0x00,
41     0x0D, 0xE0, 0x00,
42     0x08, 0xF0, 0x00,
43     0x00, 0x78, 0x00,
44     0x00, 0x3C, 0x00,
45     0x00, 0x1E, 0x00,
46     0x00, 0x0F, 0x00,
47     0x00, 0x07, 0x80,
48     0x00, 0x03, 0x80,
49     0x00, 0x00, 0x00,
50     0x00, 0x00, 0x00,
51     0x00, 0x00, 0x00,
52     0x00, 0x00, 0x00,
53     0x00, 0x00, 0x00,
54     0x00, 0x00, 0x00,
55     0x00
56 };
57
58 #endif  /* __C64__ or __C128__ */
59
60
61
62 static void ShowState (unsigned char Invisible)
63 /* Display cursor visibility */
64 {
65     gotoxy (0, 6);
66     cclear (40);
67     gotoxy (0, 6);
68     cprintf ("Mouse cursor %svisible", Invisible? "in" : "");
69 }
70
71
72
73 int main (void)
74 {
75     struct mouse_info info;
76     unsigned char Invisible;
77     unsigned char Done;
78 #if defined(__ATARI__)
79     unsigned char type;
80 #endif
81
82     /* Initialize the debugger */
83     DbgInit (0);
84
85     /* Clear the screen, set white on black */
86     bordercolor (COLOR_BLACK);
87     bgcolor (COLOR_BLACK);
88     textcolor (COLOR_GRAY3);
89     cursor (0);
90     clrscr ();
91
92 #if defined(__C64__) || defined(__C128__) || defined(__CBM510__)
93     /* Copy the sprite data */
94     memcpy ((void*) SPRITE0_DATA, MouseSprite, sizeof (MouseSprite));
95
96     /* Set the VIC sprite pointer */
97     *(unsigned char*)SPRITE0_PTR = SPRITE0_DATA / 64;
98
99     /* Set the color of sprite 0 */
100 #ifdef __CBM510__
101     pokebsys ((unsigned) &VIC.spr0_color, COLOR_WHITE);
102 #else
103     VIC.spr0_color = COLOR_WHITE;
104 #endif
105
106     /* Initialize the mouse */
107     mouse_init (MOUSE_CBM1351);
108
109 #elif defined(__ATARI__)
110
111     do {
112         cputs("\r\n");
113         cputs("0 = trak-ball\r\n");
114         cputs("1 = ST mouse\r\n");
115         cputs("2 = Amiga mouse\r\n");
116         cputs("Enter type (0-2): ");
117         type = cgetc();
118         cputc(type);
119     } while (type < '0' || type > '2');
120     type -= '0';
121
122     /* Initialize the mouse */
123     mouse_init (type);
124     *(unsigned char *)0x2c0 = 15;  /* set mouse cursor color (PM0) */
125     clrscr ();
126
127 #endif
128
129     /* Print a help line */
130     revers (1);
131     cputsxy (0, 0, "d: debug   h: hide   q: quit   s: show  ");
132     revers (0);
133
134     /* Test loop */
135     Done = 0;
136     ShowState (Invisible = 1);
137     while (!Done) {
138         if (kbhit()) {
139             switch (cgetc()) {
140                 case 'd':
141                     BREAK();
142                     break;
143                 case 'h':
144                     ShowState (++Invisible);
145                     mouse_hide ();
146                     break;
147                 case 's':
148                     if (Invisible) {
149                         ShowState (--Invisible);
150                         mouse_show ();
151                     }
152                     break;
153                 case 'q':
154                     Done = 1;
155                     break;
156             }
157         }
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     }
171
172     mouse_done ();
173     clrscr ();
174     cputs ("Goodbye!\r\n");
175
176     return EXIT_SUCCESS;
177 }
178
179
180