]> git.sur5r.net Git - cc65/blob - samples/tgidemo.c
Switched Apple II output format to AppleSingle.
[cc65] / samples / tgidemo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <cc65.h>
4 #include <conio.h>
5 #include <ctype.h>
6 #include <modload.h>
7 #include <tgi.h>
8
9
10
11 #ifndef DYN_DRV
12 #  define DYN_DRV       1
13 #endif
14
15 #define COLOR_BACK      TGI_COLOR_BLACK
16 #define COLOR_FORE      TGI_COLOR_WHITE
17
18
19 /*****************************************************************************/
20 /*                                   Data                                    */
21 /*****************************************************************************/
22
23
24
25 /* Driver stuff */
26 static unsigned MaxX;
27 static unsigned MaxY;
28 static unsigned AspectRatio;
29
30
31
32 /*****************************************************************************/
33 /*                                   Code                                    */
34 /*****************************************************************************/
35
36
37
38 static void CheckError (const char* S)
39 {
40     unsigned char Error = tgi_geterror ();
41     if (Error != TGI_ERR_OK) {
42         printf ("%s: %d\n", S, Error);
43         if (doesclrscrafterexit ()) {
44             cgetc ();
45         }
46         exit (EXIT_FAILURE);
47     }
48 }
49
50
51
52 static void DoWarning (void)
53 /* Warn the user that the TGI driver is needed for this program */
54 {
55     printf ("Warning: This program needs the TGI\n"
56             "driver on disk! Press 'y' if you have\n"
57             "it - any other key exits.\n");
58     if (tolower (cgetc ()) != 'y') {
59         exit (EXIT_SUCCESS);
60     }
61     printf ("Ok. Please wait patiently...\n");
62 }
63
64
65
66 static void DoCircles (void)
67 {
68     static const unsigned char Palette[2] = { TGI_COLOR_WHITE, TGI_COLOR_ORANGE };
69     unsigned char I;
70     unsigned char Color = COLOR_FORE;
71     unsigned X = MaxX / 2;
72     unsigned Y = MaxY / 2;
73
74     tgi_setpalette (Palette);
75     while (!kbhit ()) {
76         tgi_setcolor (COLOR_FORE);
77         tgi_line (0, 0, MaxX, MaxY);
78         tgi_line (0, MaxY, MaxX, 0);
79         tgi_setcolor (Color);
80         for (I = 10; I < 240; I += 10) {
81             tgi_ellipse (X, Y, I, tgi_imulround (I, AspectRatio));
82         }
83         Color = Color == COLOR_FORE ? COLOR_BACK : COLOR_FORE;
84     }
85
86     cgetc ();
87     tgi_clear ();
88 }
89
90
91
92 static void DoCheckerboard (void)
93 {
94     static const unsigned char Palette[2] = { TGI_COLOR_WHITE, TGI_COLOR_BLACK };
95     unsigned X, Y;
96     unsigned char Color;
97
98     tgi_setpalette (Palette);
99     Color = COLOR_BACK;
100     while (1) {
101         for (Y = 0; Y <= MaxY; Y += 10) {
102             for (X = 0; X <= MaxX; X += 10) {
103                 tgi_setcolor (Color);
104                 tgi_bar (X, Y, X+9, Y+9);
105                 Color = Color == COLOR_FORE ? COLOR_BACK : COLOR_FORE;
106                 if (kbhit ()) {
107                     cgetc ();
108                     tgi_clear ();
109                     return;
110                 }
111             }
112             Color = Color == COLOR_FORE ? COLOR_BACK : COLOR_FORE;
113         }
114         Color = Color == COLOR_FORE ? COLOR_BACK : COLOR_FORE;
115     }
116 }
117
118
119
120 static void DoDiagram (void)
121 {
122     static const unsigned char Palette[2] = { TGI_COLOR_WHITE, TGI_COLOR_BLACK };
123     int XOrigin, YOrigin;
124     int Amp;
125     int X, Y;
126     unsigned I;
127
128     tgi_setpalette (Palette);
129     tgi_setcolor (COLOR_FORE);
130
131     /* Determine zero and aplitude */
132     YOrigin = MaxY / 2;
133     XOrigin = 10;
134     Amp     = (MaxY - 19) / 2;
135
136     /* Y axis */
137     tgi_line (XOrigin, 10, XOrigin, MaxY-10);
138     tgi_line (XOrigin-2, 12, XOrigin, 10);
139     tgi_lineto (XOrigin+2, 12);
140
141     /* X axis */
142     tgi_line (XOrigin, YOrigin, MaxX-10, YOrigin);
143     tgi_line (MaxX-12, YOrigin-2, MaxX-10, YOrigin);
144     tgi_lineto (MaxX-12, YOrigin+2);
145
146     /* Sine */
147     tgi_gotoxy (XOrigin, YOrigin);
148     for (I = 0; I <= 360; I += 5) {
149
150         /* Calculate the next points */
151         X = (int) (((long) (MaxX - 19) * I) / 360);
152         Y = (int) (((long) Amp * -cc65_sin (I)) / 256);
153
154         /* Draw the line */
155         tgi_lineto (XOrigin + X, YOrigin + Y);
156     }
157
158     cgetc ();
159     tgi_clear ();
160 }
161
162
163
164 static void DoLines (void)
165 {
166     static const unsigned char Palette[2] = { TGI_COLOR_WHITE, TGI_COLOR_BLACK };
167     unsigned X;
168
169     tgi_setpalette (Palette);
170     tgi_setcolor (COLOR_FORE);
171
172     for (X = 0; X <= MaxY; X += 10) {
173         tgi_line (0, 0, MaxY, X);
174         tgi_line (0, 0, X, MaxY);
175         tgi_line (MaxY, MaxY, 0, MaxY-X);
176         tgi_line (MaxY, MaxY, MaxY-X, 0);
177     }
178
179     cgetc ();
180     tgi_clear ();
181 }
182
183
184
185 int main (void)
186 {
187     unsigned char Border;
188
189 #if DYN_DRV
190     /* Warn the user that the tgi driver is needed */
191     DoWarning ();
192
193     /* Load and initialize the driver */
194     tgi_load_driver (tgi_stddrv);
195     CheckError ("tgi_load_driver");
196 #else
197     /* Install the driver */
198     tgi_install (tgi_static_stddrv);
199     CheckError ("tgi_install");
200 #endif
201
202     tgi_init ();
203     CheckError ("tgi_init");
204     tgi_clear ();
205
206     /* Get stuff from the driver */
207     MaxX = tgi_getmaxx ();
208     MaxY = tgi_getmaxy ();
209     AspectRatio = tgi_getaspectratio ();
210
211     /* Set the palette, set the border color */
212     Border = bordercolor (COLOR_BLACK);
213
214     /* Do graphics stuff */
215     DoCircles ();
216     DoCheckerboard ();
217     DoDiagram ();
218     DoLines ();
219
220 #if DYN_DRV
221     /* Unload the driver */
222     tgi_unload ();
223 #else
224     /* Uninstall the driver */
225     tgi_uninstall ();
226 #endif
227
228     /* Reset the border */
229     (void) bordercolor (Border);
230
231     /* Done */
232     printf ("Done\n");
233     return EXIT_SUCCESS;
234 }