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