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