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