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