forgotten commit. disabled until egl is adapted.
[AROS-Contrib.git] / fish / tree / tree.c
blob05ef8f8364f3a2887c3906ad4a49d5975b976691
1 /************************************************************************
2 * Version 1.00 TREE.C - Draw a Recursive Tree 08-May-86 *
3 * Commodore Amiga Only Module TREE.C *
4 *************************************************************************
5 * Copyright (c) 1986, Robert S. French *
6 * --------------------------------------------------------------------- *
7 * This program has been placed in the public domain. A limited *
8 * license is hereby granted for the unlimited use and distribution of *
9 * this program, provided it is not used for commercial or profit- *
10 * making purposes. Thank you. *
11 *************************************************************************
12 * Author information: | Disclaimer: *
13 * | *
14 * Name: Robert S. French | The author takes no responsibil- *
15 * USnail: 2740 Frankfort Avenue | ity for damages incurred during *
16 * Louisville, KY 40206 | the use of this program. *
17 * Phone: (502) 897-5096 \-----------------------------------*
18 * ARPA: French#Robert%d@LLL-MFE UUCP: ihnp4!ptsfa!well!french *
19 *************************************************************************
20 * Please send any comments, suggestions, or bugs to one of the above *
21 * addresses. *
22 *************************************************************************
23 * Acknowledgements *
24 * ================ *
25 * *
26 * Original version presented for IBM BASIC by William K. Balthrop in *
27 * Home Computer Magazine, Vol. 5, No. 6. *
28 * *
29 * Random number generator from the Encyclopedia of Computer Science *
30 * and Engineering, second edition. *
31 ************************************************************************/
33 /* Necessary includes */
35 #include <aros/oldprograms.h>
37 #include <exec/types.h>
38 #include <exec/libraries.h>
39 #include <devices/keymap.h>
40 #include <graphics/copper.h>
42 #include <graphics/display.h>
44 #include <graphics/gfxbase.h>
45 #include <graphics/text.h>
46 #include <graphics/view.h>
47 #include <graphics/gels.h>
48 #include <graphics/regions.h>
49 #include <hardware/blit.h>
50 #include <intuition/intuition.h>
51 #include <intuition/intuitionbase.h>
52 #include <stdio.h>
54 #define abort myabort
56 void exit(int);
58 /* Function Prototypes */
59 void chk_done();
60 void draw_ground();
61 long my_rand();
62 void init_rand();
63 void drawbranch(int x,int y,int ex,int ey,int d,int l);
64 void branch(int xs,int ys,int len,int dir,int rl);
65 void abort(char *s);
67 /* Library Pointers */
69 struct IntuitionBase *IntuitionBase = 0;
70 struct GfxBase *GfxBase = 0;
72 /* Initial Graphics Conditions */
74 struct NewScreen newscr = {
75 0, /* Left */
76 0, /* Top */
77 640, /* Width */
78 200, /* Height */
79 4, /* Depth */
80 0, /* DPen */
81 1, /* BPen */
82 HIRES, /* View Modes */
83 CUSTOMSCREEN, /* Type */
84 0, /* Font */
85 "Recursive Tree (1.00) by Robert French - Right mouse button to exit", /* Title */
86 0, /* Gadgets */
87 0 /* Bitmap */
90 struct NewWindow newwin = {
91 0, /* Left */
92 0, /* Top */
93 640, /* Width */
94 200, /* Height */
95 -1, /* DPen */
96 -1, /* BPen */
97 MOUSEBUTTONS, /* IDCMP */
98 BACKDROP | BORDERLESS | ACTIVATE | RMBTRAP, /* Flags */
99 0, /* Gadgets */
100 0, /* Check */
101 0, /* Title */
102 0, /* Screen */
103 0, /* Bitmap */
104 0, /* MinWidth */
105 0, /* MinHeight */
106 0, /* MaxWidth */
107 0, /* MaxHeight */
108 CUSTOMSCREEN /* Screen type */
111 UWORD colors[16] = {
112 0x000, 0xfff, 0xf00, 0xff0, 0x0d0, 0x0b0, 0x090, 0xa84,
113 0x973, 0x970, 0x960, 0x860, 0x850, 0x750, 0x740, 0x640
116 struct Screen *scr = 0;
117 struct Window *win = 0;
118 struct RastPort *rp;
119 struct ViewPort *vp;
121 /* Random Number Definitions */
123 long seeds[17];
124 int seedp1,seedp2,seedp3;
126 /* Direction Definitions */
128 struct s_dir {
129 int x;
130 int y;
131 } offset[8] = {
132 { 0, -1 },
133 { 2, -1 },
134 { 2, 0 },
135 { 2, 1 },
136 { 0, 1 },
137 { -2, 1 },
138 { -2, 0 },
139 { -2, -1 }
140 }, boff[4] = {
141 { -1, 0 },
142 { -1, -1 },
143 { 0, -1 },
144 { 1, -1 }
147 int main()
149 int i;
151 if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)) == 0)
152 abort("Can't open intuition.library");
154 if ((GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)) == 0)
155 abort("Can't open graphics.library");
157 if ((scr = (struct Screen *)OpenScreen(&newscr)) == 0)
158 abort("Can't open screen");
160 newwin.Screen = scr;
162 if ((win = (struct Window *)OpenWindow(&newwin)) == 0)
163 abort("Can't open window");
165 rp = win->RPort;
166 vp = &scr->ViewPort;
168 LoadRGB4(vp,colors,16);
169 SetDrMd(rp,JAM1);
170 init_rand();
172 for (;;) {
173 SetAPen(rp,0);
174 RectFill(rp,0,10,639,199);
176 draw_ground();
178 branch(320,120,14,0,1); /* Draw first right branch */
179 branch(320,120,14,0,-1); /* Draw first left branch */
181 for (i=0;i<10;i++) {
182 Delay(60);
183 chk_done();
186 abort("");
189 void abort(s)
190 char *s;
192 if (win)
193 CloseWindow(win);
194 if (scr)
195 CloseScreen(scr);
196 if (IntuitionBase)
197 CloseLibrary((struct Library *)IntuitionBase);
198 if (GfxBase)
199 CloseLibrary((struct Library *)GfxBase);
200 printf("%s\n",s);
201 exit(0);
204 void branch(xs,ys,len,dir,rl)
205 int xs,ys,len,dir,rl;
207 int r,xe,ye;
209 chk_done();
211 if (len > 11)
212 len--;
213 else {
214 r = my_rand();
215 switch (r) {
216 case 12:
217 len /= 2;
218 break;
219 case 1:
220 len *= 7;
221 len /= 10;
222 break;
223 case 2:
224 case 3:
225 len *= 8;
226 len /= 10;
227 break;
228 case 4:
229 len *= 9;
230 len /= 10;
231 break;
232 default:
233 len--;
234 break;
238 dir = (dir+rl) & 7;
239 xe = xs+len*offset[dir].x;
240 ye = ys+len*offset[dir].y;
241 r = my_rand();
242 switch (r) {
243 case 0:
244 xe--;
245 break;
246 case 1:
247 xe++;
248 break;
249 case 2:
250 ye--;
251 break;
252 case 3:
253 ye++;
254 break;
256 drawbranch(xs,ys,xe,ye,dir,len);
257 if (len < 1)
258 return;
259 branch(xe,ye,len,dir,1); /* Draw right branch */
260 branch(xe,ye,len,dir,-1); /* Draw left branch */
263 void drawbranch(x,y,ex,ey,d,l)
264 int x,y,ex,ey,d,l;
266 SetAPen(rp,l+2);
268 if (l > 9) {
269 Move(rp,x+boff[d&3].x,y+boff[d&3].y);
270 Draw(rp,ex+boff[d&3].x,ey+boff[d&3].y);
272 if (l > 6) {
273 Move(rp,x-boff[d&3].x,y-boff[d&3].y);
274 Draw(rp,ex-boff[d&3].x,ey-boff[d&3].y);
276 Move(rp,x,y);
277 Draw(rp,ex,ey);
280 void init_rand()
282 LONG sec,mic,x;
283 int i;
285 CurrentTime(&sec,&mic);
287 x = sec*(mic | 1);
289 for (i=0;i<17;i++) {
290 seeds[i] = x;
291 x = x*3+mic;
294 seedp1 = 4;
295 seedp2 = 16;
296 seedp3 = 0;
299 long my_rand()
301 long result;
303 result = seeds[seedp1]+seeds[seedp2];
304 seeds[seedp3] = result;
305 if (++seedp1 > 16)
306 seedp1 = 0;
307 if (++seedp2 > 16)
308 seedp2 = 0;
309 if (++seedp3 > 16)
310 seedp3 = 0;
312 return (result & 31);
315 void draw_ground()
317 int i,j,x,y,c;
319 SetAPen(rp,5);
320 RectFill(rp,0,194,639,199);
321 SetAPen(rp,14);
322 x = 28;
323 y = 199;
324 for (c=0;c<13;c++) {
325 for (j=0;j<2;j++) {
326 for (i=0;i<c;i++,y--) {
327 if (y < 118)
328 return;
329 Move(rp,318-x,y);
330 Draw(rp,322+x,y);
332 x--;
337 void chk_done()
339 struct IntuiMessage *message;
340 ULONG class;
341 USHORT code;
343 while ((message = (struct IntuiMessage *)GetMsg(win->UserPort))) {
344 class = message->Class;
345 code = message->Code;
346 ReplyMsg((struct Message *)message);
347 if (class == MOUSEBUTTONS && code == MENUDOWN)
348 abort("");