home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / mt / buldmenu.c < prev    next >
C/C++ Source or Header  |  1989-01-12  |  7KB  |  189 lines

  1. /* buldmenu.c  makes up a struct selement array from screen file */
  2. /*  assumes that each screen element which should be a cursor choice */
  3. /*  is surrounded by a pair of { } chars.  Close elements can use just { */
  4. /*  jlc 6/88  rev 1 */
  5. /*  9/88 converted to Microsoft graphics library row, col #'s */
  6.  
  7. /* #define TURBOC 1   Define if using TURBOC, leave out for Microsoft */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. #ifdef TURBOC
  13.     #include <alloc.h>
  14. #else
  15.     #include <malloc.h>
  16. #endif
  17.  
  18. #include "standard.h"
  19. #include "screenf.h"
  20.  
  21. #define MAXEL           500     /* max number of screen elements */
  22. #define CONTENT_WIDE    15      /* char width of content, struct selement */
  23. #define SCRN_PITCH      8       /* int ratio of row vs col dist on screen */
  24. #define MINY_DIST       0       /* minimum significant vertical distance */
  25. #define MINX_DIST       2       /* minimum significant horizontal distance */
  26. /* (less and the items are considered to be on the same line vertically) */
  27.  
  28. /* the only function's prototype */
  29.  
  30. void write_instructions(void);
  31.  
  32.  
  33. void                
  34. main(argc, argv)
  35. int argc;
  36. char *argv[];
  37. {
  38.     int *x, *y, i, j, numel, row, col, right_dis, left_dis, up_dis, 
  39.         down_dis, close_up, close_down, close_left, close_right, xdist, 
  40.         ydist, delx, dely;
  41.     char c, *sp, nbuf[10], lbuf[SCRNWIDE], *str[MAXEL];
  42.     FILE *infile, *outfile;
  43.  
  44.     if (argc < 2){          /* if no input file - put up help info. */
  45.         write_instructions();
  46.         exit();
  47.     }
  48.     else if (argc < 3){     /* default output is stdout */
  49.         outfile = stdout;
  50.     }
  51.     else{
  52.         outfile = fopen(argv[2], "w");
  53.         if (outfile == NULL){
  54.             fputs("\nCould not open output file.", stdout);
  55.             write_instructions();
  56.             exit();
  57.         }
  58.     }
  59.  
  60.     infile = fopen(argv[1], "r");
  61.     if (infile == NULL){
  62.         fputs("\nCould not open input file.", stdout);
  63.         write_instructions();
  64.         exit();
  65.     }
  66.             
  67.     
  68.     x = (int *) malloc(MAXEL * sizeof(int)); /* allocate memory for arrays */
  69.     y = (int *) malloc(MAXEL * sizeof(int));
  70.     for (i = 0; i < MAXEL; i++){
  71.         str[i] = (char *) malloc(CONTENT_WIDE * sizeof(char));
  72.     }
  73.     
  74.     numel = 0;
  75.     col = row = 1;
  76.     while (1){              /* main loop, runs through every char in file */
  77.         c = fgetc(infile);
  78.         switch(c){
  79.         case('\t'):         /* expand tabs to blanks */
  80.             while(!(++col % TABSPACE))
  81.                 ;
  82.             break;          
  83.         case('{'):          /* start of a menu item */
  84.             col++;
  85.             x[numel] = col;
  86.             y[numel] = row;
  87.             sp = str[numel];
  88.             i = 0;
  89.             do {            /* read chars until }, { or out of space */
  90.                 c = fgetc(infile);
  91.                 if (c != '}' && c != '{')
  92.                     *sp++ = c;
  93.                 col++;
  94.             } while (i++ < CONTENT_WIDE && c != '}' && c != '{' && c != EOF);
  95.             *sp = '\0';
  96.             numel++;
  97.             break;
  98.         case('\n'):         /* new line */
  99.             col = 1;
  100.             row++;
  101.             break;      
  102.         case(EOF):          /* end of file, so compute neighbors distance */
  103.             fclose(infile);
  104.             fputs("\n#define NPARAM  ", outfile);
  105.             itoa(numel, nbuf, 10);
  106.             fputs(nbuf, outfile);
  107.             fputs("\n\nstruct selement _______[NPARAM] = {\n", outfile);
  108.             for (i = 0; i < numel; i++){
  109.                 left_dis = right_dis = up_dis = down_dis = 32000;
  110.                                         /* default nearest is same spot */
  111.                 close_left = close_right = close_up = close_down = i;
  112.                                         /* find nearest neighbors */
  113.                 for (j = 0; j < numel; j++){
  114.                     delx = x[j] - x[i];
  115.                     dely = y[j] - y[i];
  116.                         /* note the factor for screen not square */
  117.                     xdist = abs(delx) + (SCRN_PITCH * abs(dely));
  118.                     ydist = abs(delx) + abs(dely);
  119.                     if (delx > MINX_DIST){              /* on right side ? */
  120.                         if (xdist < right_dis){
  121.                             right_dis = xdist;
  122.                             close_right = j;
  123.                         }
  124.                     }
  125.                     else if (delx < (-1) * MINX_DIST){  /* on left side ? */
  126.                         if (xdist < left_dis){
  127.                             left_dis = xdist;
  128.                             close_left = j;
  129.                         }
  130.                     }
  131.                     if (dely > MINY_DIST){              /* below ? */
  132.                         if (ydist < down_dis){
  133.                             down_dis = ydist;
  134.                             close_down = j;
  135.                         }
  136.                     }
  137.                     else if (dely < (-1) * MINY_DIST){  /* above ? */
  138.                         if (ydist < up_dis){
  139.                             up_dis = ydist;
  140.                             close_up = j;
  141.                         }
  142.                     }
  143.                 }
  144.                 strcpy(lbuf, "\t{ ");   /* write the structure def. line */
  145.                 itoa(x[i], nbuf, 10);
  146.                 strcat(lbuf, nbuf);
  147.                 strcat(lbuf, ", ");
  148.                 itoa(y[i], nbuf, 10);
  149.                 strcat(lbuf, nbuf);
  150.                 strcat(lbuf, ", \"");
  151.                 strcat(lbuf, str[i]);
  152.                 strcat(lbuf, "\", ");
  153.                 itoa(close_up, nbuf, 10);
  154.                 strcat(lbuf, nbuf);
  155.                 strcat(lbuf, ", ");
  156.                 itoa(close_down, nbuf, 10);
  157.                 strcat(lbuf, nbuf);
  158.                 strcat(lbuf, ", ");
  159.                 itoa(close_left, nbuf, 10);
  160.                 strcat(lbuf, nbuf);
  161.                 strcat(lbuf, ", ");
  162.                 itoa(close_right, nbuf, 10);
  163.                 strcat(lbuf, nbuf);
  164.                 strcat(lbuf, ", ");
  165.                 itoa(i, nbuf, 10);
  166.                 strcat(lbuf, nbuf);
  167.                 strcat(lbuf, " },\n");
  168.                 fputs(lbuf, outfile);                   
  169.             }
  170.             fputs("};\n", outfile);
  171.             exit();
  172.         default:        /* any other char - just move past it */
  173.             col++;
  174.             break;
  175.         }
  176.     }
  177. }
  178.  
  179.  
  180. void
  181. write_instructions()
  182. {
  183.         fputs("\nBULDMENU - builds a cursor control file for use by writscrn.c", stdout);
  184.         fputs("\nfunction movescrn().  Normally the output file is added to the", stdout);
  185.         fputs("\nprogram's header file as a struct of type selement.", stdout);
  186.         fputs("\nEach element surrounded by { } characters becomes a menu item.", stdout);
  187.         fputs("\n\nUsage: buldmenu inputfile outputfile", stdout);
  188. }
  189.