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