home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / vifs / vifs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-22  |  5.4 KB  |  259 lines

  1. static char *header = "\n\
  2.                                                                             \n\
  3. /*                                                                          \n\
  4.  *    vifs.c  --  View an Iterated Function System.                       \n\
  5.  *    Theory from M. Barnsley 'Fractals Everywhere' (ISBN 0-12-079062-9). \n\
  6.  *                                                                          \n\
  7.  *    4 june 1989  Olle Olsson  (olle@ergodata.se).                       \n\
  8.  */                                                                         \n\
  9.                                                                             \n\
  10.                                                                             \n\
  11. ";
  12.  
  13. #include <conio.h>
  14. #include <dos.h>
  15. #include <process.h>
  16. #include <string.h>
  17. #include "ifs.h"
  18. #include "gmtc.h"
  19. #include "getkey.h"
  20.  
  21. /* max number of transforms */
  22. #define NTRANSF 50
  23.  
  24. /* max group index */
  25. #define MAXGROUP (NTRANSF/2)
  26.  
  27. /* a mouse graphics cursor shape */
  28. struct gm_gcurs gcu =
  29.     {
  30.     7, 7,            /* horizontal and vertical hot spot */
  31.         {        /* screen mask */
  32.         0   /* initialized by the program */
  33.         },
  34.         {        /* cursor mask */
  35.         0x00, 0x00, 0x00, 0x00,
  36.         0x10, 0x10, 0x10, 0xfe,
  37.         0x10, 0x10, 0x10, 0x00,
  38.         0x00, 0x00, 0x00, 0x00
  39.         }
  40.     };
  41.  
  42. static void usage( void );
  43.  
  44. static void usage( void )
  45. {
  46. /* this is probably going to clear the screen */
  47. sleep( 1 );
  48. endshow();
  49.  
  50. printf( "Usage:\n" );
  51. printf( "\tvifs [options] ifs_data_file\n" );
  52. printf( "Options are:\n" );
  53. printf( "\t-e\t\tedit mode (mouse required)\n" );
  54. printf( "\n" );
  55. }
  56.  
  57. /* the transformations */
  58. static transform trf[NTRANSF];    /* (could have been a linked list...) */
  59.  
  60. /* the colors (palette) */
  61. static rgb colors[MAXCOLORS + 1];
  62.  
  63. void main( argc, argv )
  64. int argc;
  65. char *argv[];
  66. {
  67. FILE *inf;            /* data file */
  68. ifsdes dd;            /* ifs descriptor */
  69. int edmode;            /* show/iterate flag */
  70. int trace;            /* trace flag */
  71. int mouse;            /* mouse flag */
  72. int i, c;            /* tmp */
  73. int running;            /* flag */
  74. char datafile[100];        /* data file name */
  75. char *ap, **p;            /* argument pointers */
  76. struct gm_status gs;        /* for getkey() */
  77.  
  78. /* advertise */
  79. printf( header );
  80. sleep( 1 );
  81.  
  82. /* initialize */
  83. dd.tp = trf;
  84. dd.maxsize = NTRANSF;
  85. dd.colors = colors;
  86. dd.clrsize = sizeof (colors) / sizeof (colors[0]);
  87. dd.textcolor = dd.clrsize - 1;
  88. dd.prrdens = 0;
  89. dd.maxgroup = MAXGROUP;
  90.  
  91. /* set up */
  92. initshow();
  93.  
  94. /* is there a mouse ? (gm_init() in graphics mode!) */
  95. if (!(mouse = gm_init()))
  96.     {
  97.     printf( "The mouse has escaped, get another one!\n\n" );
  98.     /*sleep( 2 );*/
  99.     }
  100. else
  101.     {
  102.     /* change the mouse cursor */
  103.     /* first initialize the screen mask */
  104.     for (i = 0; i < sizeof (gcu.gm_cumask) / sizeof (gcu.gm_cumask[0]); ++i)
  105.         gcu.gm_scmask[i] = ~gcu.gm_cumask[i];
  106.  
  107.     gm_defg( &gcu );
  108.     }
  109.  
  110. /* restore to text mode for error exits */
  111. /*restorecrtmode(); this confuses the mouse driver */
  112.  
  113.  
  114. /* read arguments */
  115. datafile[0] = '\0';
  116. trace = edmode = 0;
  117. for (p = argv + 1, c = 1; c < argc; ++c)
  118.     {
  119.     if (trace) printf( "%s:\n", *p );
  120.     ap = *p++;
  121.  
  122.     if (*ap == '-')    for (i = 1; ap[i]; ++i) switch (ap[i])
  123.         {
  124. /*        case 'u':
  125.             if (++c >= argc)
  126.                 error(
  127.              "-u: upper limit for transformation count missing");
  128.  
  129.             if ((maxsize = atoi( *p++ ) ) < 2)
  130.                 error( "upper limit %d is too low", maxsize );
  131.  
  132.             continue;
  133. */
  134.  
  135.         case 't':
  136.             trace++;
  137.             continue;
  138.  
  139.         case 'e':
  140.             if (mouse) edmode++;
  141.             else error( "edit isn't possible without a mouse" );
  142.             continue;
  143.  
  144.         default:
  145.             usage();
  146.             error( "don't understand flag '-%c'", ap[i] );
  147.         }
  148.     else if (!datafile[0])
  149.         {
  150.         strcpy( datafile, ap );
  151.         }
  152.     else
  153.         {
  154.         usage();
  155.         error( "too many data files specified" );
  156.         }
  157.     }
  158.  
  159. /* open the data file */
  160. if (!datafile[0])
  161.     error( "No IFS data file specified" );
  162.  
  163. if ((inf = fopen( datafile, "r" )) == NULL)
  164.     error( "Can't open input file '%s'", datafile );
  165.  
  166. /* read the description */
  167. rdescr( inf, &dd, trace );
  168. fclose( inf );
  169.  
  170. /* set the text color */
  171. dd.colors[dd.textcolor].r = dd.colors[dd.textcolor].g =
  172. dd.colors[dd.textcolor].b = 2;
  173.  
  174. if (trace) printf( "(imeasm:%d)\n", dd.im );
  175.  
  176. if (trace)
  177.     {
  178.     printf( "(trace, esc to quit)\n" );
  179.     if (getch() == '\033') error( "trace exit" );
  180.     cleardevice();
  181.     }
  182.  
  183. for (running = 1; running;)
  184.     {
  185.     if (edmode)
  186.         {
  187.         /* edit mode */
  188.         if (!(running = edtrans( &dd, trace )))
  189.             break;
  190.         }
  191.     else running = 0;
  192.  
  193.     /*setgraphmode( grmode ); this confuses the mouse driver,
  194.      * do cleardevice() instead */
  195.     cleardevice();
  196.  
  197.     /* set up the palette */
  198.     palsetup( &dd );
  199.  
  200.     /* show the fractal */
  201.     if (mouse) wbup();
  202.     ifsshow( &dd, mouse ? gotkey : kbhit, 0, putpixel, getpixel );
  203.  
  204.     /* ready, beep */
  205.     beep();
  206.  
  207.     /* if ifsshow() was stopped: clear keyboard  (no echo) */
  208.     if (mouse) while (gotkey())
  209.         {
  210.         wbup();
  211.         (void) getkey( &gs );
  212.         }
  213.     else while (kbhit())
  214.         (void) getch();
  215.  
  216.     /* wait for a (possibly second) key */
  217.     if (mouse)
  218.         {
  219.         wbup();
  220.         (void) getkey( &gs );
  221.         }
  222.     else (void) getch();
  223.     }
  224.  
  225. endshow();
  226.  
  227. exit( 0 );
  228. }
  229.  
  230. void beep()
  231. {
  232. printf( "\007" );
  233. }
  234.  
  235. void error( s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 )
  236. char *s;
  237. int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
  238. {
  239. /* this is probably going to clear the screen */
  240. sleep( 1 );
  241. endshow();
  242.  
  243. fprintf( stderr, "\nError:" );
  244. fprintf( stderr, s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 );
  245. fprintf( stderr, "\n" );
  246.  
  247. exit( 2 );
  248. }
  249.  
  250. void warning( s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 )
  251. char *s;
  252. int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
  253. {
  254. fprintf( stderr, "\nWarning:" );
  255. fprintf( stderr, s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 );
  256. fprintf( stderr, "\n" );
  257. }
  258.  
  259.