expand the name
[AROS-Contrib.git] / fish / 304_lines / lines.c
blob076f6575be9954e9c976fd214d5d5f2b011fc84c
1 /*
2 * lines.c : Line pattern generator experimental interface
4 * this stuf adapted from mackie.c on fish disk 131.
5 * by Joel Swank 11-13-88
13 * The maximum number of lines on the screen at once.
16 #include <aros/oldprograms.h>
17 #include <exec/types.h>
18 #include <intuition/intuition.h>
20 #include <string.h>
21 #include <stdlib.h>
23 #define MAXLINES (100)
25 #define XSIZE 670
26 #define YSIZE 440
27 #define MAXHELP 20
29 #define INTUITION_REV 1L
32 * The global variables.
34 struct GfxBase *GfxBase;
35 struct RastPort *rastport ;
36 int screenheight, screenwidth ;
37 struct Screen *scr;
38 struct Window *Wind = NULL;
39 struct MsgPort *CreatePort() ;
40 struct IntuitionBase *IntuitionBase ;
41 struct IntuiMessage *msg;
43 void eraseold();
46 * definition of the main window
49 static struct NewWindow New_Window = {
50 0, 2, /* Take all except the */
51 XSIZE, YSIZE, /* top two lines */
52 -1, -1, /* Default pens */
53 CLOSEWINDOW+VANILLAKEY, /* Inputs acceppeted */
54 WINDOWCLOSE /* Fairly standard window */
55 | BACKDROP | SIMPLE_REFRESH | BORDERLESS ,
56 NULL, /* no gadgets */
57 (struct Image *) NULL,
58 (UBYTE *) "h for Help", /* title */
59 (struct Screen *) NULL, /* filled at startup */
60 (struct BitMap *) NULL,
61 0, 0, 0, 0, /* no change sizes, doesn't matter */
62 CUSTOMSCREEN
63 } ;
68 static struct NewScreen newscreen = {
69 0, 0,
70 XSIZE, YSIZE+2,
72 0, 1,
73 HIRES | LACE | SCREENQUIET,
74 CUSTOMSCREEN,
75 NULL,
76 NULL,
77 NULL,
78 NULL } ;
80 char *HelpText[40] = {
81 " Lines - line pattern generator",
82 " by Joel Swank 11/13/88",
83 " ",
84 " Control from keyboard as follows:",
85 " ",
86 " space Start/Stop generation",
87 " o Do one generation",
88 " f Freeze Thaw color rotation",
89 " h Help Screen",
90 " x,q Exit Program",
91 " Escape Exit Program",
92 " ",
93 " Return to continue",
94 NULL };
96 void done();
97 void clr_grf();
98 void startlines();
99 void advancelines();
100 void drawnew();
101 void colors();
102 void do_help();
105 * MainLine
107 int main(int argc, char **argv) {
108 long i ;
109 USHORT keycode, stop, freeze;
111 screenheight = YSIZE;
112 screenwidth = XSIZE;
114 if ((IntuitionBase = (struct IntuitionBase *)
115 OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
116 done(21);
118 GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
119 if (GfxBase == NULL) done(22);
121 if ( (scr = OpenScreen(&newscreen)) == NULL) done(26);
122 New_Window.Screen = scr;
124 if ((Wind = (struct Window *) OpenWindow(&New_Window)) == NULL)
125 done(25);
126 rastport = Wind->RPort;
128 SetRGB4(&(scr->ViewPort), 0L, 0L, 0L, 0L) ;
129 clr_grf();
131 startlines() ;
132 for (i=0; i<MAXLINES; i++) {
133 advancelines() ;
134 drawnew() ;
136 colors() ;
138 stop = FALSE;
139 freeze = FALSE;
140 while (1) {
142 while(NULL != (msg = (struct IntuiMessage *) GetMsg(Wind->UserPort))) {
143 switch(msg->Class) {
144 case VANILLAKEY:
145 keycode = msg->Code;
146 ReplyMsg((struct Message *)msg);
147 switch (keycode)
149 case ' ': /* start/stop */
150 if (stop == TRUE) stop = FALSE;
151 else stop = TRUE;
152 break;
153 case 'h': /* Help */
154 /* wait for this message */
155 if (!stop) Wait( 1L << Wind->UserPort->mp_SigBit);
156 do_help();
157 /* wait on next mesg */
158 Wait( 1L << Wind->UserPort->mp_SigBit);
159 clr_grf();
160 break;
161 case 'o': /* one iteration */
162 stop = TRUE;
163 goto once;
164 case 'f': /* freeze/thaw color changing */
165 if (freeze == TRUE) freeze = FALSE;
166 else freeze = TRUE;
167 break;
168 case 'q': /* Exit */
169 case 'x':
170 case '\033':
171 done(0);
172 break;
174 continue;
175 case CLOSEWINDOW:
176 ReplyMsg((struct Message *)msg);
177 done(0);
179 ReplyMsg((struct Message *)msg);
181 if (stop == TRUE)
183 Wait( 1L << Wind->UserPort->mp_SigBit); /* wait on mesg */
184 continue;
187 once:
188 eraseold() ;
189 advancelines() ;
190 drawnew() ;
191 eraseold() ;
192 advancelines() ;
193 drawnew() ;
194 eraseold() ;
195 advancelines() ;
196 drawnew() ;
197 eraseold() ;
198 advancelines() ;
199 drawnew() ;
200 eraseold() ;
201 advancelines() ;
202 drawnew() ;
203 eraseold() ;
204 advancelines() ;
205 drawnew() ;
206 if (!freeze) colors() ;
210 * End of MainLine
215 * done - just clean up that which is open, and then leave.
217 void done(how)
218 int how;
220 if (Wind) CloseWindow(Wind) ;
221 if (scr) CloseScreen(scr) ;
222 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase) ;
223 if (GfxBase) CloseLibrary((struct Library *)GfxBase) ;
225 //OpenWorkBench() ; /* As requested */
226 exit(how) ;
230 * This routine returns a random value from 0 to n-1.
232 int randm(i)
233 int i ;
235 static long seed ;
236 long rval ;
238 if (seed == 0)
239 seed = 323214521 + scr->MouseX +
240 scr->MouseY ;
241 seed = seed * 123213 + 121 ;
242 rval = (seed >> 5) & 65535 ;
243 return ((i * rval) >> 16) ;
246 * This routine sets x and y values to a random number.
248 static long x, y ;
249 void randomxy() {
250 x = randm(screenwidth) ;
251 y = randm(screenheight) ;
254 * Main routines are always fun.
256 short x1store[MAXLINES], y1store[MAXLINES] ;
257 short x2store[MAXLINES], y2store[MAXLINES] ;
258 short ptr ;
259 short dx1, dy1, dx2, dy2 ;
260 short ox1, oy1, ox2, oy2 ;
261 short nx1, ny1, nx2, ny2 ;
262 short dr, dg, db ;
263 short or, og, ob ;
264 short nr, ng, nb ;
266 * Initialize things for the first lines.
268 void startlines() {
269 ptr = 0 ;
270 if (dx1 == 0) {
271 ox1 = randm(screenwidth) ;
272 ox2 = randm(screenwidth) ;
273 oy1 = randm(screenheight) ;
274 oy2 = randm(screenheight) ;
275 dx1 = 3 ;
276 dx2 = 4 ;
277 dy1 = 1 ;
278 dy2 = 6 ;
279 nr = 53 ;
280 ng = 33 ;
281 nb = 35 ;
282 dr = -3 ;
283 dg = 5 ;
284 db = 7 ;
286 SetRGB4(&(scr->ViewPort), 0L, 0L, 0L, 0L) ;
287 SetRGB4(&(scr->ViewPort), 1L, (long)(nr >> 3),
288 (long)(ng >> 3), (long)(nb >> 3)) ;
291 * Advance the number by the delta, and check the boundaries.
293 void adv(o, d, n, w)
294 short *o, *d, *n ;
295 long w ;
297 *n = *o + *d ;
298 if (*n < 0) {
299 *n = 0 ;
300 *d = randm(6) + 1 ;
301 } else if (*n >= w) {
302 *n = w - 1 ;
303 *d = - randm(6) - 1 ;
307 * Advance the two points which make up the lines.
309 void advancelines() {
310 adv(&ox1, &dx1, &nx1, screenwidth) ;
311 adv(&ox2, &dx2, &nx2, screenwidth) ;
312 adv(&oy1, &dy1, &ny1, screenheight) ;
313 adv(&oy2, &dy2, &ny2, screenheight) ;
316 * Draw a new set of lines.
318 void drawnew() {
319 x1store[ptr] = ox1 = nx1 ;
320 x2store[ptr] = ox2 = nx2 ;
321 y1store[ptr] = oy1 = ny1 ;
322 y2store[ptr] = oy2 = ny2 ;
323 Move(rastport, (long)ox1, (long)oy1) ;
324 Draw(rastport, (long)ox2, (long)oy2) ;
325 Draw(rastport, (long)(screenwidth-ox1-1), (long)(screenheight-oy1-1)) ;
326 Draw(rastport, (long)(screenwidth-ox2-1), (long)(screenheight-oy2-1)) ;
327 Draw(rastport, (long)ox1, (long)oy1) ;
328 ptr++ ;
329 if (ptr == MAXLINES)
330 ptr = 0 ;
333 * Erase the old line.
335 void eraseold() {
336 short oldpen ;
338 oldpen = rastport->FgPen ;
339 SetAPen(rastport, 0L) ;
340 Move(rastport, (long)x1store[ptr], (long)y1store[ptr]) ;
341 Draw(rastport, (long)x2store[ptr], (long)y2store[ptr]) ;
342 Draw(rastport, (long)(screenwidth-x1store[ptr]-1),
343 (long)(screenheight-y1store[ptr]-1)) ;
344 Draw(rastport, (long)(screenwidth-x2store[ptr]-1),
345 (long)(screenheight-y2store[ptr]-1)) ;
346 Draw(rastport, (long)x1store[ptr], (long)y1store[ptr]) ;
347 SetAPen(rastport, (long)oldpen) ;
350 * This routine mucks with the colors.
352 void colors() {
353 or = nr ;
354 og = ng ;
355 ob = nb ;
356 adv(&or, &dr, &nr, 128) ;
357 adv(&og, &dg, &ng, 128) ;
358 adv(&ob, &db, &nb, 128) ;
359 SetRGB4(&(scr->ViewPort), 1L, (long)(nr >> 3),
360 (long)(ng >> 3), (long)(nb >> 3)) ;
364 * do_help - display help text
367 void do_help()
369 int i;
370 clr_grf();
372 for (i=0; i<MAXHELP; i++) /* dump the whole help text array */
374 if (!HelpText[i]) break;
375 Move(rastport,50L,(long) (i+1)*9+20);
376 Text(rastport,HelpText[i], (long) strlen(HelpText[i]));
382 void clr_grf()
384 SetAPen(rastport, 0L) ;
385 Forbid() ;
386 RectFill(rastport, 0L, 0L, (long)screenwidth-1, (long)screenheight-1) ;
387 Permit() ;
388 SetAPen(rastport, 1L) ;