home *** CD-ROM | disk | FTP | other *** search
/ PC Format (Spanish) 4 / GOLF_CHLNGR.iso / setup.new < prev    next >
Text File  |  1995-04-12  |  12KB  |  555 lines

  1. //╔══════════════════════════════════════════════════════════════════════════╗
  2. //║ UPDATE LOG: SETUP.C                                                      ║
  3. //║                                                                          ║
  4. //║ date         update by       description                                 ║
  5. //║ ────         ─────────       ───────────                                 ║
  6. //║                                                                          ║
  7. //║                                                                          ║
  8. //║                                                                          ║
  9. //║                                                                          ║
  10. //╚══════════════════════════════════════════════════════════════════════════╝
  11.  
  12. #include <dos.h>
  13. #include <malloc.h>
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <string.h>
  18. #include "basics.h"
  19.  
  20. static int dispinfo_membase = -1;
  21. static int display_mode = -1;
  22. static uchar dispinfo[64];
  23. extern char *block_addr;
  24. extern char *map_addr;
  25.  
  26. extern int    test_cnt;
  27. extern int    chk_cnt;
  28.  
  29. unsigned int    joy_xmin    = 1024;
  30. unsigned int    joy_xmax    = 0;
  31. unsigned int    joy_ymin    = 1024;
  32. unsigned int    joy_ymax    = 0;
  33. int    joy1_x    = 0;
  34. int    joy1_y    = 0;
  35.  
  36.  
  37. static short int scrsetup_data2[8] = {
  38.     0x0d06, 0x3e07, 0x4109, 0xea10,
  39.     0xac11, 0xbf12,
  40.     0xe715, 0x0616
  41.     };
  42.  
  43. /****************************************************************************/
  44.  
  45. void
  46. getout(int error)
  47. {
  48.     int n;
  49.  
  50.     remove_midi();
  51.  
  52.     sync();
  53.  
  54.     init_midi();
  55.     init_digi(0);
  56.     sync();
  57.     remove_digi(0);
  58.     sync();
  59.     remove_midi();
  60.  
  61.  
  62.     handler(REMOVE, H_TOD);
  63.     reset_tod();                    // reset the DOS clock to normal speed
  64.     handler(REMOVE, H_KEYBOARD);
  65.     handler(REMOVE, H_CRITICAL);
  66.     handler(REMOVE, H_CTRL_C);
  67.  
  68.  
  69.     if (display_mode >= 0)
  70.         set_display(display_mode);
  71.  
  72.     if (error)
  73.     {                    // print error if there was one
  74.  
  75.         printf("Error: %s\n", error_txt[(-error)-1]);
  76.         if (n = errno)
  77.         {
  78.             if (n > MAX_DOSERROR)
  79.                 n = 0;
  80.             printf("DOS error: %s\n", dos_error[n]);
  81.         }
  82.  
  83.         switch (error)
  84.         {
  85.             case E_OPEN:
  86.                 printf("\tFile: %s\n", last_fname);
  87.                 break;
  88.             case E_MEMORY:
  89.             case E_DOSMEM:
  90.             case E_MEMLUMPS:
  91.                 printf("Tried to allocate %d bytes\n", last_memsize);
  92.                 printf("%d other memory chunk%s allocated:-\n",
  93.                     memlumps, memlumps==1 ? "" : "s");
  94.                 for (n = 0; n < memlumps; n++)
  95.                     printf("\tSize:\t%d\tAddr: %d\n", memdata[n][1], memdata[n][0]);
  96.                 break;
  97.             case E_IO:
  98.                 printf("\tFile: %s\n",
  99.                     file_names[last_handle]);
  100.                 break;
  101.             case E_CREATE:
  102.                 printf("\tFile: %s\n",
  103.                     last_fname);
  104.                 break;
  105.             case E_TOOMANY:
  106.                 printf("\tFiles:-\n");
  107.                 for (n = 0; n < MAX_FILES; n++)
  108.                     if (file_names[n])
  109.                         printf("\t\t%s\n", file_names[n]);
  110.                 break;
  111.                 break;
  112.         }
  113.         printf("\n");
  114.     }
  115.  
  116.     for (n = 0; n < MAX_FILES; n++)
  117.         if (file_names[n])
  118.         {
  119.             printf("Closing file %s\n", file_names[n]);
  120.             close(n);
  121.         }
  122.  
  123.     freemem();                    // free all memory allocated
  124.  
  125.     reset_clock();
  126.  
  127.     exit(0);                        // goodbye
  128. }
  129.  
  130. /****************************************************************************/
  131.  
  132. void
  133. save_display()
  134. {
  135.     union REGS regs;
  136.     uint size;
  137.  
  138.     dispinfo_membase = memlumps;
  139.  
  140.     regs.w.ax = 0x1c00;
  141.     regs.w.cx = 7;
  142.     int386(0x10, ®s, ®s);
  143.     size = (uint) (regs.w.bx << 6);
  144.     if ((regs.x.ebx = (uint) getmem(size)) == (uint) NULL)
  145.         getout(E_MEMORY);
  146.     regs.w.ax = 0x1c01;
  147.     regs.w.cx = 7;
  148.     int386(0x10, ®s, ®s);
  149. }
  150.  
  151. /****************************************************************************/
  152.  
  153. void
  154. restore_display()
  155. {
  156.     union REGS regs;
  157.  
  158.     regs.w.ax = 0x1c02;
  159.     regs.w.cx = 7;
  160.     regs.x.ebx = (uint) memdata[dispinfo_membase][0];
  161.     int386(0x10, ®s, ®s);
  162. }
  163.  
  164. /****************************************************************************/
  165.  
  166. void
  167. get_dispmode()
  168. {
  169.     union REGS regs;
  170.  
  171.     regs.h.ah = 0x0f;
  172.     int386(0x10, ®s, ®s);
  173.     display_mode = regs.h.al;
  174. }
  175.  
  176. /****************************************************************************/
  177.  
  178. void
  179. set_display(int mode)
  180. {
  181.     union REGS regs;
  182.  
  183.     regs.w.ax = mode;
  184.     int386(0x10, ®s, ®s);
  185. }
  186.  
  187. /****************************************************************************/
  188.  
  189. void
  190. get_dispinfo()
  191. {
  192.     union REGS regs;
  193.  
  194.     regs.w.ax = 0x1b00;
  195.     regs.w.bx = 0;
  196.     regs.x.edi = (uint) dispinfo;
  197.     int386(0x10, ®s, ®s);
  198. }
  199.  
  200. /****************************************************************************/
  201.  
  202. void
  203. set_palette()
  204. {
  205.     fade_to_pal(60, game_palette);
  206. }
  207.  
  208. /****************************************************************************/
  209.  
  210. void
  211. wipe_palette()
  212. {
  213.     fade_to_col(1, 0);
  214. }
  215.  
  216. /****************************************************************************/
  217.  
  218.  
  219. /*        // the setup for strange screen mode goes like this:-
  220.  
  221.         ; sequencer register
  222. set no odd/even, set extended mem
  223. do synchrous reset
  224.  
  225.         ; misc. output register
  226. set 480 lines, set no odd/even, set 640 columns, enable display ram, color IO
  227.  
  228.         ; sequencer register
  229. clear synchrous reset
  230.  
  231.         ; CRT controller register
  232. clear write protect on regs 0-7
  233. vertical total = 0d
  234. overflow = 3e
  235. max. scan line = 41
  236. vert. retrace start = ea
  237. vert. retrace end = ac
  238. vert. display end = df
  239. vert. display end = df
  240. clear doubleword mode and count by 4
  241. vert. blank start = e7
  242. vert. blank end = 06
  243. enable retrace, set byte mode and address wrap, clear CGA&Hercules compatible
  244.  
  245.         ; sequencer register
  246. enable write to plane 1
  247.  
  248. */
  249.  
  250. void ext_screen()
  251. {
  252.     int n;
  253.  
  254.     outp(0x3d4,0x10);
  255.     outp(0x3d5, (inp(0x3d5))|128);
  256.  
  257.     outp(0x3c2, 0xe3);
  258.     outp(0x3d4, 0x11);
  259.     outp(0x3d5, inp(0x3d5)&0x7f);
  260.     for (n=0; n<8; n++)
  261.         outpw(0x3d4, scrsetup_data2[n]);
  262.  
  263.     outp(0x3d4, 0x11);
  264.     outp(0x3d5, inp(0x3d5)&0x7f);
  265.     outp(0x3d4, 0);
  266.     outp(0x3d5, 95);
  267.     outp(0x3d4, 1);
  268.     outp(0x3d5, 79-8);
  269.     outp(0x3d4, 2);
  270.     outp(0x3d5, 80-8);
  271.     outp(0x3d4, 3);
  272.     outp(0x3d5, 130+0);
  273.     outp(0x3d4, 4);
  274.     outp(0x3d5, 84-4);
  275.     outp(0x3d4, 5);
  276.     outp(0x3d5, 128+0);
  277.     outp(0x3d4, 0x13);
  278.     outp(0x3d5, 36);
  279.  
  280.     inp(0x3da);                    /* initialize attribute controller    */
  281. }
  282.  
  283. /****************************************************************************/
  284.  
  285. /*void calibrate_joysticks(void)
  286. {
  287.     int left, right, up, down;
  288.     int joy;
  289.     int jx, jy;
  290.     int time = 0;
  291.     sword *ptr = &joy1_lscale;
  292.     sword *ptr2 = &joy1_lthresh;
  293.     int n, totx, toty;
  294.  
  295.     joy_timeout = 10000;            // large timeout value
  296.     joy_mask = 15;                    // read both joysticks
  297.     joy_check = 1;                    // indicate joystick calibration in progress
  298.  
  299.     for (joy=0; joy<2; joy++)
  300.     {         // do both joysticks
  301.  
  302.         keydata->keymap[K_ESC] = 0;     // debounce escape key
  303.  
  304.         left = 20000;                // impossible start values
  305.         right = 0;
  306.         up = 20000;
  307.         down = 0;
  308.  
  309.         for (;;)
  310.         {
  311.             sync();
  312.             read_joy();
  313.  
  314. #if DEBUGGING==1
  315.             keydata->last_key = get_key();
  316.             clear_keyb();
  317. #endif
  318.  
  319.             if (joy==0)
  320.             {            // get current joystick x and y counts
  321.                 jx = joy1_xc;
  322.                 jy = joy1_yc;
  323.             }
  324.             else
  325.             {
  326.                 jx = joy2_xc;
  327.                 jy = joy2_yc;
  328.             }
  329.  
  330.             if (jx==0)            // if reader can't read joystick, quit loop
  331.                 break;
  332.  
  333.             ifkey(K_ESC)            // quit on escape key
  334.                 break;
  335.  
  336.             if (jx < left)            // detemine any new min or max values
  337.                 left = jx;
  338.             if (jx > right)
  339.                 right = jx;
  340.             if (jy < up)
  341.                 up = jy;
  342.             if (jy > down)
  343.                 down = jy;
  344.  
  345.             copysource = backscr;     // copy screen over
  346.             copydest = (char *)MCGA_RAM;
  347.             dump_screen();
  348.  
  349.             copydest = backscr;        // clear back screen
  350.             clr_screen();
  351.  
  352.             print(0, 0, "Joystick Calibration");
  353.             print(0, 20, "Move joystick %d to all extents, then release to centre", joy+1);
  354.             print(0, 32, "Left:%d\nUp:%d\nRight:%d\nDown:%d",
  355.                 left, up, right, down);
  356.             print(0, 80, "Press Esc when finished");
  357.         }
  358.  
  359.         if (jx)
  360.         {                    // if joystick is present
  361.  
  362.             n = right > down ? right : down; // set n to largest count
  363.             if (n > time)
  364.                 time = n;            // set timeout value;
  365.             totx = 0;
  366.             toty = 0;
  367.             for (n = 1; n <= 256; n++) {    // read centre counts lots of times
  368.                 read_joy();
  369.                 if (joy)
  370.                 {
  371.                     totx += joy2_xc;
  372.                     toty += joy2_yc;
  373.                 }
  374.                 else
  375.                 {
  376.                     totx += joy1_xc;
  377.                     toty += joy1_yc;
  378.                 }
  379.             }
  380.             jx = totx >> 8;        // calculate average centre counts;
  381.             jy = toty >> 8;
  382.  
  383.             n = jx-left;
  384.             *ptr++ = (16*1024)/n;    // set left scaling value
  385.             *ptr2++ = -n/6;        // set left threshold
  386.  
  387.             n = right-jx;
  388.             *ptr2++ = n/4;            // set right threshold
  389.             n -= n>>3;
  390.             *ptr++ = (16*1024)/n;    // set right scaling value
  391.  
  392.             n = jy-up;
  393.             *ptr++ = (16*1024)/n;    // set up scaling value
  394.             *ptr2++ = -n/6;        // set up threshold
  395.  
  396.             n = down-jy;
  397.             *ptr2++ = n/5;            // set down threshold
  398.             n -= n>>3;
  399.             *ptr++ = (16*1024)/n;    // set down scaling value
  400.  
  401.             *ptr++ = jx;            // set x-centre
  402.             *ptr++ = jy;            // set y-centre
  403.  
  404.             ptr = &joy2_lscale;        // point to joy2 scaling data
  405.             ptr2 = &joy2_lthresh;    // point to joy2 threshold data
  406.         }
  407.     }
  408.     joy_check = 0;                        // finished joystick calibration
  409.     joy_timeout = time+(time>>3);        // set maximum timeout value needed
  410.  
  411.     copydest = (char *)MCGA_RAM;
  412.     clr_screen();
  413.  
  414. } */
  415.  
  416. /****************************************************************************/
  417.  
  418. void init_vbl(void)    // synchronises the dos clock with the vertical retrace
  419. {
  420.     if(scrxsize == 320)
  421.         {
  422.         toddata->clk_splits[0] = (calc_vbltimer()-400);
  423.         }
  424.     else
  425.         {
  426.         toddata->clk_splits[0] = (calc_vbltimer()-300);
  427.         }
  428.  
  429.     toddata->no_splits = 1;
  430.     init_tod();
  431. }
  432.  
  433. /****************************************************************************/
  434.  
  435. int setup(void)
  436. {
  437.     int    i;
  438.  
  439.     get_dispmode();                // remember current state of display
  440.     get_dispinfo();
  441.     set_display(0x13);                // MCGA mode please
  442.  
  443.     if(scrxsize == 288)
  444.         {
  445.         ext_screen();                    // set strange screen mode
  446.         }
  447.  
  448.     txt_screen = (char *)MCGA_RAM;    // set vars for text print routine
  449.     txt_linewid = scrxsize;
  450.     txt_linewid8 = scrxsize*8;
  451.     txt_col = 1;
  452.  
  453.  
  454.     handler(INSTALL, H_CTRL_C);        // program break interrup -> iret
  455.     handler(INSTALL, H_CRITICAL);        // critical error handler
  456.     handler(INSTALL, H_KEYBOARD);        // keyboard handler
  457.     handler(INSTALL, H_TOD);            // dos clock handler (pretend vbl)
  458.  
  459.     init_vbl();                    // synchronise with vbl
  460.  
  461.     sync();
  462.     sync();
  463.     chk_cnt = test_cnt * 4;        //setup fall through counter
  464.  
  465.     if ((backscr = (char *) getmem((uint) 65536)) == (char *) NULL)
  466.         getout(E_MEMORY);
  467.  
  468.     copydest = backscr;
  469.     clr_screen();
  470.  
  471.     game_palette[1][0] = 0;
  472.     game_palette[1][1] = 63;
  473.     game_palette[1][2] = 63;
  474.  
  475.     set_palette();
  476.  
  477.     txt_screen = backscr;
  478.     txt_col = 1;
  479.  
  480. /*    calibrate_joysticks();*/
  481.  
  482. //    mouse_buttons = init_mouse();
  483.     if (!(mouse_buttons = init_mouse()))
  484.         return E_NOMOUSE;                    // return error if no mouse installed
  485.  
  486.     return 0;                        // return no error
  487. }
  488.  
  489. /****************************************************************************/
  490.  
  491. void
  492. _waitvbl(int i)
  493. {
  494.     while(i > 0)
  495.         {
  496.         sync();
  497.         i--;
  498.         }
  499.     return;
  500. }
  501.  
  502. /****************************************************************************/
  503.  
  504. void
  505. read_joy(void)
  506. {
  507.     int    x_scale, y_scale, x_size, y_size;
  508.  
  509.     if(joy_flag != NULL)
  510.         {
  511.  
  512.         if(joy_x < joy_xmin)                    //if calibrated wrong,
  513.             joy_xmin = joy_x;                    //re-calib during game
  514.         if(joy_x > joy_xmax)
  515.             joy_xmax = joy_x;
  516.         if(joy_y < joy_ymin)
  517.             joy_ymin = joy_y;
  518.         if(joy_y > joy_ymax)
  519.             joy_ymax = joy_y;
  520.  
  521.         x_size = joy_xmax - joy_xmin;
  522.         y_size = joy_ymax - joy_ymin;
  523.  
  524.         x_scale = x_size >> 5;                    // div 16
  525.         y_scale = y_size >> 5;
  526.  
  527.         if(x_scale != NULL)
  528.             joy1_x = joy_x / x_scale;
  529.  
  530.         if(y_scale != NULL)
  531.             joy1_y = joy_y / y_scale;
  532.  
  533.         joy1_x -= 16;                            // -16 to +16
  534.         joy1_y -= 16;
  535.  
  536.  
  537.         if(joy1_x < -16)                        //fix boundaries
  538.             joy1_x = -16;
  539.         if(joy1_x > 16)
  540.             joy1_x = 16;
  541.         if(joy1_y < -16)
  542.             joy1_y = -16;
  543.         if(joy1_y > 16)
  544.             joy1_y = 16;
  545.  
  546.         }
  547.  
  548.     joy_flag = 1;                    //turn joy back on if got turned off
  549.                                         //ie not in or pulled out of port
  550.     return;
  551. }
  552.  
  553. /****************************************************************************/
  554.  
  555.