]> git.sur5r.net Git - cc65/blob - samples/tgidemo.c
Calculate time in seconds, not ticks. Implement pager output for primes.
[cc65] / samples / tgidemo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <conio.h>
4 #include <ctype.h>
5 #include <modload.h>
6 #include <tgi.h>
7
8
9
10 #if defined(__APPLE2__) || defined(__APPLE2ENH__)
11 #  define TGI_MODE      TGI_MODE_280_192_8
12 #else
13 #  define TGI_MODE      TGI_MODE_320_200_2
14 #endif
15 #define COLOR_BACK      COLOR_BLACK
16 #define COLOR_FORE      COLOR_WHITE
17
18
19 /*****************************************************************************/
20 /*                                   Data                                    */
21 /*****************************************************************************/
22
23
24
25 static const unsigned char SinusTable[] = {
26     0x64,0x63,0x61,0x5F,0x5D,0x5B,0x59,0x57,0x55,0x54,
27     0x52,0x50,0x4E,0x4C,0x4A,0x49,0x47,0x45,0x43,0x42,
28     0x40,0x3E,0x3C,0x3B,0x39,0x38,0x36,0x34,0x33,0x31,
29     0x30,0x2E,0x2D,0x2B,0x2A,0x28,0x27,0x26,0x24,0x23,
30     0x22,0x20,0x1F,0x1E,0x1D,0x1C,0x1B,0x1A,0x19,0x18,
31     0x17,0x16,0x15,0x14,0x13,0x12,0x12,0x11,0x10,0x10,
32     0x0F,0x0E,0x0E,0x0D,0x0D,0x0C,0x0C,0x0C,0x0B,0x0B,
33     0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,
34     0x0B,0x0B,0x0B,0x0C,0x0C,0x0C,0x0D,0x0D,0x0E,0x0E,
35     0x0F,0x10,0x10,0x11,0x12,0x12,0x13,0x14,0x15,0x16,
36     0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,
37     0x22,0x23,0x24,0x26,0x27,0x28,0x2A,0x2B,0x2D,0x2E,
38     0x30,0x31,0x33,0x34,0x36,0x38,0x39,0x3B,0x3C,0x3E,
39     0x40,0x42,0x43,0x45,0x47,0x49,0x4A,0x4C,0x4E,0x50,
40     0x52,0x54,0x55,0x57,0x59,0x5B,0x5D,0x5F,0x61,0x63,
41     0x64,0x65,0x67,0x69,0x6B,0x6D,0x6F,0x71,0x73,0x74,
42     0x76,0x78,0x7A,0x7C,0x7E,0x7F,0x81,0x83,0x85,0x86,
43     0x88,0x8A,0x8C,0x8D,0x8F,0x91,0x92,0x94,0x95,0x97,
44     0x98,0x9A,0x9B,0x9D,0x9E,0xA0,0xA1,0xA2,0xA4,0xA5,
45     0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,
46     0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB6,0xB7,0xB8,0xB8,
47     0xB9,0xBA,0xBA,0xBB,0xBB,0xBC,0xBC,0xBC,0xBD,0xBD,
48     0xBD,0xBD,0xBD,0xBD,0xBD,0xBD,0xBD,0xBD,0xBD,0xBD,
49     0xBD,0xBD,0xBD,0xBC,0xBC,0xBC,0xBB,0xBB,0xBA,0xBA,
50     0xB9,0xB8,0xB8,0xB7,0xB6,0xB6,0xB5,0xB4,0xB3,0xB2,
51     0xB1,0xB0,0xAF,0xAE,0xAD,0xAC,0xAB,0xAA,0xA9,0xA8,
52     0xA6,0xA5,0xA4,0xA2,0xA1,0xA0,0x9E,0x9D,0x9B,0x9A,
53     0x98,0x97,0x95,0x94,0x92,0x91,0x8F,0x8D,0x8C,0x8A,
54     0x88,0x86,0x85,0x83,0x81,0x7F,0x7E,0x7C,0x7A,0x78,
55     0x76,0x74,0x73,0x71,0x6F,0x6D,0x6B,0x69,0x67,0x65
56 };
57
58
59
60 /* Driver stuff */
61 static unsigned MaxX;
62 static unsigned MaxY;
63
64
65
66 /*****************************************************************************/
67 /*                                   Code                                    */
68 /*****************************************************************************/
69
70
71
72 static void CheckError (const char* S)
73 {
74     unsigned char Error = tgi_geterror ();
75     if (Error != TGI_ERR_OK) {
76         printf ("%s: %d\n", S, Error);
77         exit (EXIT_FAILURE);
78     }
79 }
80
81
82
83 static void DoWarning (void)
84 /* Warn the user that the TGI driver is needed for this program */
85 {
86     printf ("Warning: This program needs the TGI\n"
87             "driver on disk! Press 'y' if you have\n"
88             "it - any other key exits.\n");
89     if (tolower (cgetc ()) != 'y') {
90         exit (EXIT_SUCCESS);
91     }
92     printf ("Ok. Please wait patiently...\n");
93 }
94
95
96
97 static void DoCircles (void)
98 {
99     static const unsigned char Palette[2] = { COLOR_WHITE, COLOR_ORANGE };
100     unsigned char I;
101     unsigned char Color = COLOR_FORE;
102     unsigned X = MaxX / 2;
103     unsigned Y = MaxY / 2;
104
105     tgi_setpalette (Palette);
106     while (!kbhit ()) {
107         tgi_setcolor (COLOR_FORE);
108         tgi_line (0, 0, MaxX, MaxY);
109         tgi_line (0, MaxY, MaxX, 0);
110         tgi_setcolor (Color);
111         for (I = 10; I < 240; I += 10) {
112             tgi_circle (X, Y, I);
113         }
114         Color = Color == COLOR_FORE ? COLOR_BACK : COLOR_FORE;
115     }
116
117     cgetc ();
118     tgi_clear ();
119 }
120
121
122
123 static void DoCheckerboard (void)
124 {
125     static const unsigned char Palette[2] = { COLOR_WHITE, COLOR_BLACK };
126     unsigned X, Y;
127     unsigned char Color;
128
129     tgi_setpalette (Palette);
130     Color = COLOR_BACK;
131     while (1) {
132         for (Y = 0; Y <= MaxY; Y += 10) {
133             for (X = 0; X <= MaxX; X += 10) {
134                 tgi_setcolor (Color);
135                 tgi_bar (X, Y, X+9, Y+9);
136                 Color = Color == COLOR_FORE ? COLOR_BACK : COLOR_FORE;
137                 if (kbhit ()) {
138                     cgetc ();
139                     tgi_clear ();
140                     return;
141                 }
142             }
143             Color = Color == COLOR_FORE ? COLOR_BACK : COLOR_FORE;
144         }
145         Color = Color == COLOR_FORE ? COLOR_BACK : COLOR_FORE;
146     }
147 }
148
149
150
151 static void DoDiagram (void)
152 {
153     static const unsigned char Palette[2] = { COLOR_WHITE, COLOR_BLACK };
154     unsigned X, I;
155
156     tgi_setpalette (Palette);
157     tgi_setcolor (COLOR_FORE);
158     tgi_line (10, 10, 10, MaxY-10);
159     tgi_lineto (MaxX-10, MaxY-10);
160     tgi_line (8, 12, 10, 10);
161     tgi_lineto (12, 12);
162     tgi_line (MaxX-12, MaxY-12, MaxX-10, MaxY-10);
163     tgi_lineto (MaxX-12, MaxY-8);
164     for (I = 0, X = 10; X < MaxX-10; ++X) {
165         tgi_setpixel (X, SinusTable[I]);
166         if (++I >= sizeof (SinusTable)) {
167             I = 0;
168         }
169     }
170
171     cgetc ();
172     tgi_clear ();
173 }
174
175
176
177 static void DoLines (void)
178 {
179     static const unsigned char Palette[2] = { COLOR_WHITE, COLOR_BLACK };
180     unsigned X;
181
182     tgi_setpalette (Palette);
183     tgi_setcolor (COLOR_FORE);
184
185     for (X = 0; X <= MaxY; X += 10) {
186         tgi_line (0, 0, MaxY, X);
187         tgi_line (0, 0, X, MaxY);
188         tgi_line (MaxY, MaxY, 0, MaxY-X);
189         tgi_line (MaxY, MaxY, MaxY-X, 0);
190     }
191
192     cgetc ();
193     tgi_clear ();
194 }
195
196
197
198 int main (void)
199 {
200     unsigned char Border;
201
202     /* Warn the user that the tgi driver is needed */
203     DoWarning ();
204
205     /* Load and initialize the driver */
206     tgi_load (TGI_MODE);
207     CheckError ("tgi_load");
208     tgi_init ();
209     CheckError ("tgi_init");
210
211     /* Get stuff from the driver */
212     MaxX = tgi_getmaxx ();
213     MaxY = tgi_getmaxy ();
214
215     /* Set the palette, set the border color */
216     Border = bordercolor (COLOR_BLACK);
217
218     /* Do graphics stuff */
219     DoCircles ();
220     DoCheckerboard ();
221     DoDiagram ();
222     DoLines ();
223
224     /* Unload the driver */
225     tgi_unload ();
226
227     /* Reset the border */
228     (void) bordercolor (Border);
229
230     /* Done */
231     printf ("Done\n");
232     return EXIT_SUCCESS;
233 }