]> git.sur5r.net Git - cc65/blob - libsrc/c64/pencalib.c
Merge pull request #646 from polluks/patch-8
[cc65] / libsrc / c64 / pencalib.c
1 /*
2 ** Calibrate lightpen drivers to the current video hardware.
3 **
4 ** 2013-07-25, Greg King
5 **
6 */
7
8
9 #include <conio.h>
10 #include <mouse.h>
11 #include <pen.h>
12
13
14 #define COMMAND1 "Adjust by clicking on line."
15 #define COMMAND2 "Finish by clicking off bar."
16
17
18 /*
19 ** There is a delay between when the VIC sends its signal, and when the display
20 ** shows that signal.  There is another delay between the display and when
21 ** the lightpen says that it saw that signal. Each display and pen is different.
22 ** Therefore, the driver must be calibrated to them.  A white bar is painted on
23 ** the screen; and, a line is drawn down the middle of it.  When the user clicks
24 ** on that line, the difference between its position and where the VIC thinks
25 ** that the pen is pointing becomes an offset that is subtracted from what the
26 ** VIC sees.
27 */
28 void __fastcall__ pen_calibrate (unsigned char *XOffset)
29 {
30     unsigned char oldBg = bgcolor (COLOR_BLUE);
31     unsigned char oldText = textcolor (COLOR_GRAY3);
32     unsigned char oldRev = revers (1);
33     unsigned char sprite0Color = VIC.spr_color[0];
34     unsigned char width, width2, height, height4, height8;
35     struct mouse_info info;
36
37     screensize (&width, &height);
38     width2 = width / 2;
39     height4 = height / 4;
40     height8 = height4 * 8;
41
42     /* Draw a bar and line. */
43
44     clrscr ();
45     cclearxy (0, height4, height4 * width);
46     cvlinexy (width2, height4 + 1, height4 - 2);
47     revers (0);
48
49     /* Print instructions. */
50
51     cputsxy (width2 - (sizeof COMMAND1) / 2, height / 2 + 1, COMMAND1);
52     cputsxy (width2 - (sizeof COMMAND2) / 2, height / 2 + 3, COMMAND2);
53
54     VIC.spr_color[0] = COLOR_GRAY2;
55     mouse_show ();
56     mouse_move (width2 * 8, height8 / 2);
57
58     for (;;) {
59         /* Wait for the main button to be released. */
60
61         do ; while ((mouse_buttons () & MOUSE_BTN_LEFT));
62
63         /* Wait for the main button to be pressed. */
64
65         do {
66             mouse_info (&info);
67         } while (!(info.buttons & MOUSE_BTN_LEFT));
68
69         /* Find out if the pen is on or off the bar. */
70
71         if (info.pos.y < height8 || info.pos.y >= height8 * 2) {
72             break;
73         }
74
75         /* On the bar; adjust the offset. */
76         /* Characters are eight pixels wide.
77         ** The VIC-II sees every other pixel;
78         ** so, we use half of the difference.
79         */
80
81         *XOffset += (info.pos.x - (width2 * 8 + 8/2)) / 2;
82     }
83
84     /* Off the bar; wait for the main button to be released. */
85
86     do ; while ((mouse_buttons () & MOUSE_BTN_LEFT));
87
88     mouse_hide ();
89     VIC.spr_color[0] = sprite0Color;
90     revers (oldRev);
91     textcolor (oldText);
92     bgcolor (oldBg);
93     clrscr ();
94 }