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

  1. /* install.c  allows user to pick the type of monitor and addaptor in use */
  2. /* and the color of the foreground, background and emphasized letters. */
  3. /* writes values to a file specified on command line, default: install.dat */
  4. /* link with screenf.lib */
  5.  
  6. /* MIDI Sequencing In C, Jim Conger, M&T Books, 1989 */
  7.  
  8. /* #define TURBOC 1   Define if using TURBOC, leave out for Microsoft */
  9.  
  10. #include <stdio.h>
  11. #include <conio.h>
  12.  
  13. #include "standard.h"
  14. #include "screenf.h"
  15. #include "install.h"
  16.  
  17. /* function declarations */
  18.  
  19. void init_colors(void);
  20. void put_to_file(void *addr, int size, FILE *stream);
  21. void get_from_file(void *addr, int size, FILE *stream);
  22. void set_text_attrib(int mode);
  23. void set_graph_attrib(int mode);
  24. int load_video_data(char *filename);
  25.  
  26. /* globals - these variables will normally be globals in a program */
  27.  
  28. int  g_text_mode, g_graph_mode, g_dots_h, g_dots_v, g_let_dots_v,
  29.     g_let_dots_h, g_norm_attrib, g_emph_attrib, g_cursor_attrib,
  30.     g_text_char_h, g_text_char_v, g_graph_char_h, g_graph_char_v,
  31.     g_text_colors, g_graph_colors;
  32.  
  33.  
  34. void
  35. main(argc, argv)
  36. int argc;
  37. char *argv[];
  38. {
  39.     int ans, pick, lastpick, color_let, let_bak, color_emph, emph_bak, 
  40.         color_cursor, cursor_bak;
  41.     char nbuf[10], filename[15];
  42.     FILE *outfile;
  43.     struct strchain *chain;
  44.  
  45.     g_text_mode = 3;                    /* default values (CGA assumed) */
  46.     g_graph_mode = 6;
  47.     set_text_attrib(g_text_mode);       /* initialize variables */
  48.     set_graph_attrib(g_graph_mode);
  49.     g_norm_attrib = 7;
  50.     g_emph_attrib = 0x0F;
  51.     g_cursor_attrib = 0x70;
  52.     
  53.     clearscreen(BWC);
  54.     chain = inpchain("install.scr", SCRNWIDE + 1);
  55.     if (chain == NULL){
  56.         fputs("\nCould not open file install.scr - not on default disk?",
  57.             stdout);
  58.         exit();
  59.     }
  60.     
  61.     if (argc > 1)       /* get output file from command line or use default */
  62.         strcpy(filename, argv[1]);
  63.     else
  64.         strcpy(filename, "install.dat");
  65.     
  66.     ans = load_video_data(filename);
  67.     if (!ans)
  68.         writerr("First time installation - no install file.", 15, BWC, BWC);
  69.  
  70.     color_let = g_norm_attrib & 0x0F;
  71.     let_bak = (g_norm_attrib & 0xF0) >> 4;
  72.     color_cursor = g_cursor_attrib & 0x0F;
  73.     cursor_bak = (g_cursor_attrib & 0xF0) >> 4;
  74.     color_emph = g_emph_attrib & 0x0F;
  75.     emph_bak = (g_emph_attrib & 0xF0) >> 4;
  76.     
  77.     pick = 0;
  78.     while (1){
  79.         g_norm_attrib = (let_bak << 4) + color_let;
  80.         g_cursor_attrib = (cursor_bak << 4) + color_cursor;
  81.         g_emph_attrib = (emph_bak << 4) + color_emph;
  82.         clearscreen(g_norm_attrib);
  83.         dispchain(chain, 1, g_norm_attrib);
  84.         init_colors();
  85.                                         /* display example lines for attrib */
  86.         
  87.         writeword("Normal letters will look like:", 21, 20, BWC);
  88.         writeword("These letters.", 57, 20, g_norm_attrib);
  89.         writeword("Cursor letters will look like:", 21, 21, BWC);
  90.         writeword("These letters.", 57, 21, g_cursor_attrib);
  91.         writeword("Emphasized letters will look like:", 21, 22, BWC);
  92.         writeword("These letters.", 57, 22, g_emph_attrib);
  93.  
  94.         write_int(g_text_mode, 7, 6, BRBWC);    /* put numeric values on */
  95.         write_int(g_graph_mode, 7, 9, BRBWC);   /* screen by selection */
  96.         write_int(color_let, 17, 17, BRBWC);    /* menu item */
  97.         write_int(let_bak, 17, 18, BRBWC);
  98.         write_int(color_cursor, 46, 17, BRBWC);
  99.         write_int(cursor_bak, 46, 18, BRBWC);
  100.         write_int(color_emph, 75, 17, BRBWC);
  101.         write_int(emph_bak, 75, 18, BRBWC);
  102.  
  103.         /* allow cursor movement to select a menu item, pick is item chosen */
  104.  
  105.         pick = movescrn(g_text_mode, scrndata, pick, NPARAM - 1, 
  106.             g_norm_attrib, g_cursor_attrib);
  107.         switch (pick){
  108.         case (0):
  109.             getint(SCRNTALL - 1, "Enter text mode number ->", &g_text_mode, 
  110.                 2, 7, BWC, BRBWC);
  111.             set_text_attrib(g_text_mode);
  112.             break;
  113.         case (1):
  114.             getint(SCRNTALL - 1, "Enter graphics mode number ->", 
  115.                 &g_graph_mode, 6, 18, BWC, BRBWC);
  116.             set_graph_attrib(g_graph_mode);
  117.             break;
  118.         case (2):
  119.             getint(SCRNTALL - 1, "Enter normal letter attribute number ->", 
  120.                 &color_let, 0, 15, BWC, BRBWC); 
  121.             break;
  122.         case (3):
  123.             getint(SCRNTALL - 1, "Enter cursor letter attribute number ->", 
  124.                 &color_cursor, 0, 15, BWC, BRBWC);
  125.             break;
  126.         case (4):
  127.             getint(SCRNTALL - 1, "Enter emphasized letter attribute number ->",                &color_emph, 0, 15, BWC, BRBWC);
  128.             break;
  129.         case (5):
  130.             getint(SCRNTALL - 1,
  131.                 "Enter normal letter background attribute number ->",
  132.                 &let_bak, 0, 15, BWC, BRBWC);
  133.             break;
  134.         case (6):
  135.             getint(SCRNTALL - 1,
  136.                 "Enter cursor background attribute number ->",
  137.                 &cursor_bak, 0, 15, BWC, BRBWC);
  138.             break;
  139.         case (7):
  140.             getint(SCRNTALL - 1,
  141.                 "Enter emphasized background attribute number ->",
  142.                 &emph_bak, 0, 15, BWC, BRBWC);
  143.             break;
  144.         case (8):               /* save */
  145.             outfile = fopen(filename, "w");
  146.             if (outfile == NULL){
  147.                 fputs("\nCould not open output file ", stderr);
  148.                 fputs(filename, stderr);
  149.                 fputc('\n', stderr);
  150.                 clearscreen(BWC);
  151.                 exit();
  152.             }
  153.             else{
  154.                 put_to_file(&g_text_mode, 1, outfile);
  155.                 put_to_file(&g_graph_mode, 1, outfile);
  156.                 put_to_file(&g_dots_v, 1, outfile);
  157.                 put_to_file(&g_dots_h, 1, outfile);
  158.                 put_to_file(&g_norm_attrib, 1, outfile);
  159.                 put_to_file(&g_cursor_attrib, 1, outfile);
  160.                 put_to_file(&g_emph_attrib, 1, outfile);
  161.                 put_to_file(&g_let_dots_v, 1, outfile);
  162.                 put_to_file(&g_let_dots_h, 1, outfile);
  163.                 put_to_file(&g_text_char_h, 1, outfile);
  164.                 put_to_file(&g_text_char_v, 1, outfile);
  165.                 put_to_file(&g_graph_char_h, 1, outfile);
  166.                 put_to_file(&g_graph_char_v, 1, outfile);
  167.                 put_to_file(&g_text_colors, 1, outfile);
  168.                 put_to_file(&g_graph_colors, 1, outfile);
  169.                 fclose(outfile);
  170.             }
  171.             clearscreen(BWC);
  172.             exit();
  173.         case (-2):                  /* ESC */
  174.         case (9):                   /* quit */
  175.             pick = 0;
  176.             writeword("Don't forget to save data. Quit ? (Y/N)->",
  177.                 1, SCRNTALL, BWC);
  178.             ans = getche();
  179.             if(toupper(ans) != 'Y'){
  180.                 break;
  181.             }
  182.             else{
  183.                 clearscreen(BWC);
  184.                 exit();
  185.             }
  186.         default:
  187.             writerr("Use arrow keys to move cursor, ret to select.",
  188.                 SCRNTALL - 1, BWC, BRBWC);
  189.             break;
  190.         }
  191.     }
  192. }
  193.  
  194.  
  195.  
  196. void
  197. init_colors()       /* show each attribute as a number */
  198. {
  199.     int i, x;
  200.     char str[3];
  201.  
  202.     for (i = 0; i < 16; i++){
  203.         itoa(i, str, 10);
  204.         x = 24 + (3 * i);
  205.         writeword(str, x, 13, BWC);
  206.         writeword(str, x, 14, i);
  207.         writeword(str, x, 15, i << 4);
  208.     }   
  209. }
  210.  
  211.  
  212.  
  213. void
  214. set_text_attrib(mode)       /* set globals based on text mode selected */
  215. int mode;
  216. {
  217.     switch(mode){
  218.     case(2):
  219.     case(3):
  220.         g_text_char_h = 80;
  221.         g_text_char_v = 25;
  222.         g_text_colors = 16;
  223.         break;
  224.     case(7):
  225.         g_text_char_h = 80;
  226.         g_text_char_v = 25;
  227.         g_text_colors = 4;
  228.         break;
  229.     default:
  230.         g_text_mode = 3;
  231.         writerr("Text screen must be mode 2, 3, or 7.", 
  232.             SCRNTALL, BWC, BRBWC);
  233.         break;
  234.     }
  235. }
  236.  
  237.  
  238.  
  239. void
  240. set_graph_attrib(mode)      /* set globals based on graphics mode selected */
  241. int mode;
  242. {
  243.     char nbuf[10];
  244.     
  245.     switch(mode){
  246.     case(4):
  247.     case(5):
  248.         g_dots_h = 320;
  249.         g_dots_v = 200;
  250.         g_let_dots_v = 8;
  251.         g_let_dots_h = 8;
  252.         g_graph_char_h = 40;
  253.         g_graph_char_v = 25;
  254.         g_graph_colors = 4;
  255.         break;
  256.     case(6):
  257.         g_dots_h = 640;
  258.         g_dots_v = 200;
  259.         g_let_dots_v = 8;
  260.         g_let_dots_h = 8;
  261.         g_graph_char_h = 80;
  262.         g_graph_char_v = 25;
  263.         g_graph_colors = 2;
  264.         break;
  265.     case(13):
  266.         g_dots_h = 320;
  267.         g_dots_v = 200;
  268.         g_let_dots_v = 8;
  269.         g_let_dots_h = 8;
  270.         g_graph_char_h = 40;
  271.         g_graph_char_v = 25;
  272.         g_graph_colors = 16;
  273.         break;
  274.     case(14):
  275.         g_dots_h = 640;
  276.         g_dots_v = 200;
  277.         g_let_dots_v = 8;
  278.         g_let_dots_h = 8;
  279.         g_graph_char_h = 80;
  280.         g_graph_char_v = 25;
  281.         g_graph_colors = 4;
  282.         break;
  283.     case(15):           /* can be either Hercules or EGA */
  284.         writeword("Hercules mono or EGA ? (H/E) ->", 0, SCRNTALL - 1,
  285.             BWC);
  286.         gets(nbuf);
  287.         if (toupper(*nbuf) == 'H'){
  288.             g_dots_h = 720;
  289.             g_dots_v = 348;
  290.             g_let_dots_v = 14;
  291.             g_let_dots_h = 9;
  292.             g_graph_char_h = 80;
  293.             g_graph_char_v = 25;
  294.             g_graph_colors = 2;
  295.         }
  296.         else if (toupper(*nbuf) == 'E'){
  297.             g_dots_h = 640;
  298.             g_dots_v = 350;
  299.             g_let_dots_v = 14;
  300.             g_let_dots_h = 8;
  301.             g_graph_char_h = 80;
  302.             g_graph_char_v = 25;
  303.             g_graph_colors = 4;
  304.         }
  305.         else{
  306.             writerr("Must be either E or H.", SCRNTALL - 1, BWC, BRBWC);
  307.         }
  308.         break;                  
  309.     case(16):
  310.         g_dots_h = 640;
  311.         g_dots_v = 350;
  312.         g_let_dots_v = 14;
  313.         g_let_dots_h = 8;
  314.         g_graph_char_h = 80;
  315.         g_graph_char_v = 25;
  316.         g_graph_colors = 16;
  317.         break;
  318.     case(17):               /* VGA modes */
  319.         g_dots_h = 640;
  320.         g_dots_v = 480;
  321.         g_let_dots_v = 16;
  322.         g_let_dots_h = 8;
  323.         g_graph_char_h = 80;
  324.         g_graph_char_v = 30;
  325.         g_graph_colors = 2;
  326.         break;
  327.     case(18):
  328.         g_dots_h = 640;
  329.         g_dots_v = 480;
  330.         g_let_dots_v = 16;
  331.         g_let_dots_h = 8;
  332.         g_graph_char_h = 80;
  333.         g_graph_char_v = 30;
  334.         g_graph_colors = 16;
  335.         break;
  336.     case(19):
  337.         g_dots_h = 320;
  338.         g_dots_v = 200;
  339.         g_let_dots_v = 8;
  340.         g_let_dots_h = 8;
  341.         g_graph_char_h = 40;
  342.         g_graph_char_v = 25;
  343.         g_graph_colors = 256;
  344.         break;
  345.     default:
  346.         writerr("Graphics screen must have a valid mode number.",
  347.             SCRNTALL - 1, BWC, BRBWC);
  348.         break;
  349.     }
  350. }
  351.  
  352.  
  353.  
  354. /* this function will also appear in the body of a program that uses */
  355. /* install.c to load it's video parameter data from a file.  Usually */
  356. /* the file is install.dat */
  357.  
  358. int
  359. load_video_data(filename)   /* load visible part of video data to global */
  360. char *filename;             /* data defined in video.h */
  361. {
  362.     int temp;
  363.     FILE *infile;
  364.  
  365.     infile = fopen(filename, "r");
  366.     
  367.     if (infile == NULL)
  368.         return(0);
  369.     
  370.     get_from_file(&g_text_mode, 1, infile);
  371.     get_from_file(&g_graph_mode, 1, infile);
  372.     get_from_file(&g_dots_v, 1, infile);
  373.     get_from_file(&g_dots_h, 1, infile);
  374.     get_from_file(&g_norm_attrib, 1, infile);
  375.     get_from_file(&g_cursor_attrib, 1, infile);
  376.     get_from_file(&g_emph_attrib, 1, infile);
  377.     get_from_file(&g_let_dots_v, 1, infile);
  378.     get_from_file(&g_let_dots_h, 1, infile);
  379.     get_from_file(&g_text_char_h, 1, infile);
  380.     get_from_file(&g_text_char_v, 1, infile);
  381.     get_from_file(&g_graph_char_h, 1, infile);
  382.     get_from_file(&g_graph_char_v, 1, infile);
  383.     get_from_file(&g_text_colors, 1, infile);
  384.     get_from_file(&g_graph_colors, 1, infile);
  385.  
  386.     fclose(infile);
  387.     return(1);
  388. }
  389.  
  390.  
  391.  
  392. void
  393. put_to_file(addr, size, stream)        /* put near int data to stream */
  394. void *addr;
  395. int size;
  396. FILE *stream;
  397. {
  398.     int i, end;
  399.     char *caddr;
  400.     
  401.     caddr = (char *)addr;
  402.     end = size * sizeof(int);
  403.     
  404.     for(i = 0; i < end; i++){
  405.         fputc(*caddr++, stream);
  406.     }
  407. }
  408.  
  409.  
  410.  
  411. void
  412. get_from_file(addr, size, stream)  /* get int data from stream, put into */
  413. void *addr;                        /* near memory */
  414. int size;
  415. FILE *stream;
  416. {
  417.     int i, end;
  418.     char *caddr;
  419.     
  420.     caddr = (char *)addr;
  421.     end = size * sizeof(int);
  422.     
  423.     for(i = 0; i < end; i++){
  424.         *caddr++ = fgetc(stream);
  425.     }
  426. }
  427.