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