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