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