development-curl is a virtual target
[AROS-Contrib.git] / Misc / DDemo7 / DDemo7.c
blobdb1e2edeecb31da522baa61119059966bdee8c6e
1 /* Dreier in C*/
2 /* Abgeleitet aus dem Basic-Programm Dreier */
3 /* P. Kittel Ffm 23.7.87, 14.6.94 */
4 /* Grafik-Demo-Programm mit schnellem Flächenfüllen (Dreiecke) und
5 Farbzyklus-Animation */
6 /* Version mit 64 Farben */
7 /* Version für OS 3.0 und AA-Chips */
8 /* Version mit zusätzlichem Disable() und Enable() */
9 /* Version mit doppeltem TmpRas und LoadRGB4 statt SetRGB4 */
10 /* Version für automatische NTSC/PAL-Erkennung */
11 /* Bei Aufruf vom CLI aus kann eine Zahl mitangegeben werden, die
12 die Laufzeit bestimmt, eine Einheit entspricht etwa 1/2 Sekunde */
13 /* Compiliert mit Lattice C-Compiler 6.2 */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <intuition/intuition.h>
18 #include <utility/tagitem.h>
19 #include <graphics/gfxmacros.h>
20 #include <graphics/gfxbase.h>
21 #include <graphics/text.h>
22 #include <math.h>
24 #include <proto/exec.h>
25 #include <proto/dos.h>
26 //#include <clib/graphics_protos.h>
27 //#include <pragmas/graphics_pragmas.h>
28 //#include <clib/intuition_protos.h>
29 //#include <pragmas/intuition_pragmas.h>
30 #include <proto/graphics.h>
31 #include <proto/intuition.h>
32 #include <aros/oldprograms.h>
34 long __stack=20480;
36 struct IntuitionBase *IntuitionBase; /* Pointer aus exec */
37 struct GfxBase *GfxBase;
38 struct ViewPort *vp;
39 #define INTUITION_REV 39
40 #define GRAPHICS_REV 39
42 struct TextFont *font;
43 struct TextAttr MyFont =
45 "topaz.font",
46 TOPAZ_SIXTY,
47 FS_NORMAL,
48 FPF_ROMFONT,
51 struct NewScreen NewScreen =
55 570, /* Nicht volle Breite, da Grafik kreisförmig. */
56 /* Aber breiter als Window, damit Vorder/Hintergrund- */
57 /* Gadget und ein Stück Titelbalken des Screens frei */
58 /* sind zum Betätigen bzw. Ziehen mit der Maus. */
59 256, /* Höhe für PAL-Format vorbelegt */
61 0, 1,
62 HIRES,
63 CUSTOMSCREEN,
64 &MyFont,
65 " Commodore - AMIGA Peter-Demos DDemo7 ",
66 NULL,
67 NULL,
70 int main(argc,argv)
71 int argc;
72 char *argv[];
74 struct Screen *Screen;
75 struct NewWindow NewWindow;
76 struct Window *Window;
77 struct RastPort *rp;
78 UWORD areabuffer[20];
79 struct TmpRas mytmpras;
80 struct AreaInfo myareainfo;
81 PLANEPTR myplane;
82 struct IntuiMessage *msg;
83 ULONG colormap[770],rl,gl,bl;
84 int i,i3,x1,y1,x2,y2,x3,y3,x9,y9,nn,nnm1,ffi,co,fpal,z,zd,zm;
85 int cc,c2,c6,cd;
86 double fs,ff,sif,y0,ye,z1,z2,s2,z3,a,m,fr,fg,fb,ar,ag,ab,cf,nn1,nnf;
87 double pr[256],pg[256],pb[256];
89 nn=63; /* Anzahl Bitplanes, Farben */
90 nn1=(double)(nn-1); nnm1=(nn-1)*3;
91 nnf=(double)(0xffffffff);
93 x9=500; y9=184+53+2; fpal=1; /* Vorbelegung für PAL */
94 y0= 58.0; ye=119.0; /* Anfangskoordinaten */
95 x1=250; y1=140; z1=0.0;
96 x2= 90; y2=y1; z2=0.0; z3=0.0;
98 fs=0.0; ff=4000.0; sif=273.0+sin(0.15); /* Farben */
99 c2=15*256; c6=15*16; co=2;
100 cf=1.0; cc=0; fr=0.0; fg=0.0; fb=0.0;
102 IntuitionBase = (struct IntuitionBase *)
103 OpenLibrary ("intuition.library",INTUITION_REV);
104 if (IntuitionBase == NULL) exit (10);
106 GfxBase = (struct GfxBase *)
107 OpenLibrary("graphics.library",GRAPHICS_REV);
108 if(GfxBase == NULL)
109 { CloseLibrary((struct Library *)IntuitionBase);
110 exit (10);
113 /* PAL / NTSC ? */
114 if (fpal > 0)
115 { if (!((GfxBase->DisplayFlags) & PAL)) fpal=0; }
117 if (fpal == 0)
118 { NewScreen.Height=200; /* NTSC! */
119 y9-=55; y1=105; y0=45.0; ye=93.0; /* NTSC-Koordinaten */
122 colormap[0]=2L<<16; /* 2 Farben */
123 colormap[1]=0x77777777; /* Hintergrund in grau */
124 colormap[2]=0x77777777;
125 colormap[3]=0x77777777;
126 colormap[4]=0xFFFFFFFF; /* Rahmen in weiß */
127 colormap[5]=0xFFFFFFFF;
128 colormap[6]=0xFFFFFFFF;
129 colormap[7]=0; /* Tabellenende */
130 if((Screen = (struct Screen *) OpenScreenTags(&NewScreen,
131 SA_Colors32 ,colormap,
132 SA_Interleaved,TRUE,
133 TAG_END ,0L )) == NULL)
134 { CloseLibrary((struct Library *)GfxBase);
135 CloseLibrary((struct Library *)IntuitionBase);
136 exit (10);
139 NewWindow.LeftEdge = 0;
140 NewWindow.TopEdge = 16;
141 NewWindow.Width = x9; /* 9 Pixel für Randabstand */
142 NewWindow.Height = y9; /* 16 Pixel für Rand und Titelleiste */
143 NewWindow.DetailPen = 0;
144 NewWindow.BlockPen = 1;
145 NewWindow.Title = NULL;
146 NewWindow.Flags =
147 WFLG_CLOSEGADGET|WFLG_BORDERLESS|WFLG_BACKDROP;
148 NewWindow.IDCMPFlags = CLOSEWINDOW; /* Hier Abbruchmöglichkeit */
149 NewWindow.Type = CUSTOMSCREEN;
150 NewWindow.FirstGadget = NULL;
151 NewWindow.CheckMark = NULL;
152 NewWindow.Screen = Screen;
153 NewWindow.BitMap = NULL;
154 NewWindow.MinWidth = x9;
155 NewWindow.MinHeight = y9;
156 NewWindow.MaxWidth = x9;
157 NewWindow.MaxHeight = y9;
158 if(( Window = ( struct Window *) OpenWindow( &NewWindow )) == NULL )
159 { CloseScreen (Screen);
160 CloseLibrary((struct Library *)IntuitionBase);
161 CloseLibrary((struct Library *)GfxBase);
162 exit(10);
165 rp=Window->RPort;
166 vp=&Screen->ViewPort;
167 font=(struct TextFont *)OpenFont(&MyFont);
168 SetFont(rp,font);
170 /* Palette mit Graukeil vorbelegen */
171 bl=0xffffffff/nn;
172 for (i=2; i<=nn; i++)
173 { rl=bl*i;
174 pr[i]=0.0; pg[i]=0.0; pb[i]=0.0;
175 colormap[3*i]=rl; colormap[3*i+1]=rl; colormap[3*i+2]=rl;
177 colormap[0]=(62L<<16)+2; /* 62 Farben (alle bis auf 0 und 1) */
178 colormap[187]=0; /* Tabellenende */
180 /* Für Flächenfüllen benötigt */
181 InitArea(&myareainfo,areabuffer,8);
182 rp->AreaInfo=&myareainfo;
183 myplane =(PLANEPTR)AllocRaster(640,256);
184 if (myplane==NULL)
185 { CloseWindow (Window);
186 CloseScreen (Screen);
187 CloseLibrary((struct Library *)IntuitionBase);
188 CloseLibrary((struct Library *)GfxBase);
189 exit(10);
191 rp->TmpRas =(struct TmpRas *)&mytmpras;
192 InitTmpRas(&mytmpras,myplane,RASSIZE(640,256));
194 SetAPen(rp,1); /* Reklametext in weiß */
195 Move(rp, 30, 8); Text(rp,"Schnelle" , 8);
196 Move(rp, 30, 17); Text(rp,"Grafik" , 6);
197 SetAPen(rp,nn); /* Dieser Text macht Farbzyklus mit */
198 Move(rp, 30,y9-12); Text(rp,"16,8 Mio" , 8);
199 Move(rp, 30,y9- 3); Text(rp,"Farbtöne" , 8);
200 SetAPen(rp,1); /* Wieder weiß */
201 Move(rp,x9-13*10, 8); Text(rp,"Flächenfüllen",13);
202 Move(rp,x9-11*10, 17); Text(rp,"mit Blitter" ,11);
203 Move(rp,x9- 5*10,y9-12); Text(rp,"Bitte" , 5);
204 Move(rp,x9-12*10,y9- 3); Text(rp,"vergleichen!" ,12);
206 zm = 9;
207 zd = 0; /* Schrittweite 0 für unbegrenzte Laufzeit */
208 /* Interpretation der CLI-Parameter */
209 if (argc > 1) { zm=atoi(argv[argc-1]); zd=1; } /* Laufzeit begrenzt */
210 z = 0;
212 while (z < zm) /* Schleife über vorgegebene Anzahl Durchgänge */
214 if ((msg = (struct IntuiMessage *)GetMsg(Window->UserPort)) != NULL)
215 { ReplyMsg((struct Message *)msg); z=zm+1; } /* Abbruch! */
217 /* Nächste Dreieckskoordinaten */
218 z1+=0.01; if (z1 > 6.28) z1-=6.28;
219 z2+=0.03*sin(z1); if (z2 > 6.28) z2-=6.28;
220 s2 =sin(z2); /* Modulation des Radius */
221 /* Modulierter Winkelfortschritt */
222 z3+=z1*s2/4; if (z3 > 6.28) z3-=6.28;
224 /* Dreieckspunkt 1 immer fest in Mitte */
225 x3=x2; /* Alter Punkt 2 wird Punkt 3 */
226 y3=y2;
227 /* Punkt 2 neu ermitteln */
228 x2=(int)(120.0*(1.0+s2)*cos(z3)+(double)x1);
229 y2=(int)( y0*(1.0+s2)*sin(z3)+ ye);
231 co++; /* Farbzähler */
232 if (co > nn)
234 co=2; z+=zd; /* Nächster Durchlauf durch die Palette */
236 /* Nächsten Farbton bestimmen */
237 fs+=0.1; if (fs > 7.0) fs-=7.0;
238 /* Linearer plus Modulationsanteil */
239 ff+=sif*(1.0+cos(fs)*1.02);
240 if (ff > 4095.49) ff-=4095.49; /* Farbnummer */
241 ffi=(int)ff;
242 ar=fr; ag=fg; ab=fb;
243 /* Einzelne Farbkomponenten */
244 fr=(ffi & 15)/15.0;
245 fg=(ffi & c6)/((double)c6);
246 fb=(ffi & c2)/((double)c2);
247 pr[nn]=fr; pg[nn]=fg; pb[nn]=fb;
249 /* cf entscheidet bei Interpolation über Extremwert 1 oder 0 */
250 if (cc++ > 20) { cc=0; cf=-cf; } /* Alle 20 wechseln */
252 /* Rotanteil interpolieren */
253 if ((cf*(fr+ar)) > cf)
254 { m=(2.0-fr-ar)/nn1; a=ar-m; /* Extremwert 1 */
255 for (i=2; i<nn; i++)
256 { pr[i]=a+((double)i)*m; if (pr[i] > 1.0) pr[i]=2.0-pr[i]; }
257 } else
258 { m=( -fr-ar)/nn1; a=ar-m; /* Extremwert 0 */
259 for (i=2; i<nn; i++)
260 { pr[i]=a+((double)i)*m; if (pr[i] < 0.0) pr[i]= -pr[i]; }
263 /* Grünanteil interpolieren */
264 if ((cf*(fg+ag)) > cf)
265 { m=(2.0-fg-ag)/nn1; a=ag-m;
266 for (i=2; i<nn; i++)
267 { pg[i]=a+((double)i)*m; if (pg[i] > 1.0) pg[i]=2.0-pg[i]; }
268 } else
269 { m=( -fg-ag)/nn1; a=ag-m;
270 for (i=2; i<nn; i++)
271 { pg[i]=a+((double)i)*m; if (pg[i] < 0.0) pg[i]= -pg[i]; }
274 /* Blauanteil interpolieren */
275 if ((cf*(fb+ab)) > cf)
276 { m=(2.0-fb-ab)/nn1; a=ab-m;
277 for (i=2; i<nn; i++)
278 { pb[i]=a+((double)i)*m; if (pb[i] > 1.0) pb[i]=2.0-pb[i]; }
279 } else
280 { m=( -fb-ab)/nn1; a=ab-m;
281 for (i=2; i<nn; i++)
282 { pb[i]=a+((double)i)*m; if (pb[i] < 0.0) pb[i]= -pb[i]; }
286 /* Palette Farben 2 bis nn zyklisch umbelegen */
287 i3=nnm1;
288 rl=colormap[i3-2]; gl=colormap[i3-1]; bl=colormap[i3];
289 while (i3>3) { colormap[i3]=colormap[i3-3]; i3--; }
290 colormap[1]=rl; colormap[2]=gl; colormap[3]=bl;
292 /* Nur einen neuen Farbton zur Zeit neu anzeigen */
293 cd=2*co-2; if (cd > nn) cd=cd-nn+1;
294 i3=3*cd-5;
295 colormap[i3 ]=(ULONG)(pr[co]*nnf);
296 colormap[i3+1]=(ULONG)(pg[co]*nnf);
297 colormap[i3+2]=(ULONG)(pb[co]*nnf);
298 LoadRGB32(vp,colormap);
300 /* Dreieck malen */
301 SetAPen (rp,co);
302 AreaMove(rp,x1,y1);
303 AreaDraw(rp,x2,y2);
304 AreaDraw(rp,x3,y3);
305 WaitTOF();
306 AreaEnd (rp);
309 /* Sauber aufräumen */
311 FreeRaster (myplane,640,256);
312 CloseWindow (Window);
313 CloseScreen (Screen);
314 CloseLibrary((struct Library *)IntuitionBase);
315 CloseLibrary((struct Library *)GfxBase);
317 return 0;