home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / acksrc.zip / ACKUTIL.C < prev    next >
Text File  |  1993-06-15  |  9KB  |  350 lines

  1. /*           ACK-3D ( Animation Construction Kit 3D )              */
  2. /* Utility routines   */
  3. /* Author: Lary Myers */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <dos.h>
  8. #include <mem.h>
  9. #include <alloc.h>
  10. #include <io.h>
  11. #include <fcntl.h>
  12. #include <time.h>
  13. #include <string.h>
  14. #include <sys\stat.h>
  15. #include "ack3d.h"
  16. #include "ackext.h"
  17.  
  18.  
  19. /****************************************************************************
  20. **                                       **
  21. ****************************************************************************/
  22. void Beep(void)
  23. {
  24. sound(440);
  25. delay(20);
  26. nosound();
  27. }
  28.  
  29. /****************************************************************************
  30. **                                       **
  31. ****************************************************************************/
  32. void WrapUp(void)
  33. {
  34.  
  35. if (dfp != NULL)
  36.     fclose(dfp);
  37.  
  38. textmode(3);
  39. setvect(KEYBD,oldvec);
  40.  
  41. }
  42.  
  43. /****************************************************************************
  44. ** Allocates memory for the various trig tables and reads in the pre-       **
  45. ** calculated values from "trig.dat".                       **
  46. **                                       **
  47. **                                       **
  48. ****************************************************************************/
  49. int GetTables(void)
  50. {
  51.     int        handle,len;
  52.     int        ca,na;
  53.  
  54. LongTanTable = (long far *)malloc(sizeof(long) * INT_ANGLE_360);
  55. LongInvTanTable = (long far *)malloc(sizeof(long) * INT_ANGLE_360);
  56. CosTable = (long far *)malloc(sizeof(long) * INT_ANGLE_360);
  57. SinTable = (long far *)malloc(sizeof(long) * INT_ANGLE_360);
  58. InvSinTable = (long far *)malloc(sizeof(long) * INT_ANGLE_360);
  59. InvCosTable = (long far *)malloc(sizeof(long) * INT_ANGLE_360);
  60. LongCosTable = (long far *)malloc(sizeof(long) * INT_ANGLE_360);
  61. xNextTable = (long far *)malloc(sizeof(long) * INT_ANGLE_360);
  62. yNextTable = (long far *)malloc(sizeof(long) * INT_ANGLE_360);
  63. ViewCosTable = (long far *)malloc(sizeof(long) * VIEW_WIDTH);
  64.  
  65. if (LongTanTable == NULL ||
  66.     LongInvTanTable == NULL ||
  67.     CosTable == NULL ||
  68.     SinTable == NULL ||
  69.     InvSinTable == NULL ||
  70.     InvCosTable == NULL ||
  71.     LongCosTable == NULL ||
  72.     xNextTable == NULL ||
  73.     yNextTable == NULL ||
  74.     ViewCosTable == NULL)
  75.     {
  76.     printf("Unable to get initialization memory!\n");
  77.     return(-1);
  78.     }
  79.  
  80. len = sizeof(long) * INT_ANGLE_360;
  81. handle = open("trig.dat",O_RDWR|O_BINARY);
  82. if (handle < 1)
  83.     {
  84.     printf("Unable to locate file TRIG.DAT\n");
  85.     return(-1);
  86.     }
  87.  
  88. read(handle,SinTable,len);
  89. read(handle,CosTable,len);
  90. read(handle,LongTanTable,len);
  91. read(handle,LongInvTanTable,len);
  92. read(handle,InvCosTable,len);
  93. read(handle,InvSinTable,len);
  94. read(handle,LongCosTable,len);
  95.  
  96. close(handle);
  97.  
  98. ca = INT_ANGLE_30;
  99. na = -1;
  100.  
  101. for (len = 0; len < VIEW_WIDTH; len++)
  102.     {
  103.     ViewCosTable[len] = LongCosTable[ca];
  104.     ca += na;
  105.     if (ca <= 0)
  106.     {
  107.     ca = -ca;
  108.     na = -na;
  109.     }
  110.     }
  111.  
  112. for (len = 0; len < INT_ANGLE_360; len++)
  113.     {
  114.     yNextTable[len] = (long)GRID_SIZE * LongTanTable[len];
  115.     xNextTable[len] = (long)GRID_SIZE * LongInvTanTable[len];
  116.     }
  117.  
  118. return(0);
  119. }
  120.  
  121. /****************************************************************************
  122. ** Checks the scancode from the INT 9 interrupt and sets the appropriate   **
  123. ** item in the keyboard structure.                       **
  124. ****************************************************************************/
  125. void keyCheck(void)
  126. {
  127.   if(scanCode==RIGHT_ARROW_PRESSED)keyBoard.rightArrow=1;
  128.   if(scanCode==RIGHT_ARROW_RELEASED)keyBoard.rightArrow=0;
  129.   if(scanCode==UP_ARROW_PRESSED)keyBoard.upArrow=1;
  130.   if(scanCode==UP_ARROW_RELEASED)keyBoard.upArrow=0;
  131.   if(scanCode==LEFT_ARROW_PRESSED)keyBoard.leftArrow=1;
  132.   if(scanCode==LEFT_ARROW_RELEASED)keyBoard.leftArrow=0;
  133.   if(scanCode==DOWN_ARROW_PRESSED)keyBoard.downArrow=1;
  134.   if(scanCode==DOWN_ARROW_RELEASED)keyBoard.downArrow=0;
  135.   if(scanCode==CONTROL_PRESSED)keyBoard.control=1;
  136.   if(scanCode==CONTROL_RELEASED)keyBoard.control=0;
  137.   if(scanCode==ESCAPE)keyBoard.escape=1;
  138.   if(scanCode==PLUS_PRESSED)keyBoard.plus = 1;
  139.   if(scanCode==PLUS_RELEASED)keyBoard.plus = 0;
  140.   if(scanCode==MINUS_PRESSED)keyBoard.minus = 1;
  141.   if(scanCode==MINUS_RELEASED)keyBoard.minus = 0;
  142.   if(scanCode==LETTER_A_PRESSED)keyBoard.letA = 1;
  143.   if(scanCode==LETTER_A_RELEASED)keyBoard.letA = 0;
  144.   if(scanCode==LETTER_S_PRESSED)keyBoard.letS = 1;
  145.   if(scanCode==LETTER_S_RELEASED)keyBoard.letS = 0;
  146.  
  147. #if 0
  148.   if(scanCode==HOME_PRESSED)keyBoard.home=1;
  149.   if(scanCode==HOME_RELEASED)keyBoard.home=0;
  150.   if(scanCode==END_PRESSED)keyBoard.end=1;
  151.   if(scanCode==END_RELEASED)keyBoard.end=0;
  152.   if(scanCode==PGUP_PRESSED)keyBoard.pgup=1;
  153.   if(scanCode==PGUP_RELEASED)keyBoard.pgup=0;
  154.   if(scanCode==PGDN_PRESSED)keyBoard.pgdn=1;
  155.   if(scanCode==PGDN_RELEASED)keyBoard.pgdn=0;
  156. #endif
  157.  
  158. }
  159.  
  160.  
  161. /****************************************************************************
  162. ** Keyboard interrupt 9                               **
  163. ****************************************************************************/
  164. void interrupt myInt(void)
  165. {
  166.   register char x;
  167.  
  168.   scanCode = inp(0x60); // read keyboard data port
  169.   x = inp(0x61);
  170.   outp(0x61, (x | 0x80));
  171.   outp(0x61, x);
  172.   outp(0x20, 0x20);
  173.   keyCheck();
  174. }
  175.  
  176. /****************************************************************************
  177. **                                       **
  178. ****************************************************************************/
  179. void keyBoardInit(void)
  180. {
  181.   keyBoard.rightArrow=0;
  182.   keyBoard.upArrow=0;
  183.   keyBoard.leftArrow=0;
  184.   keyBoard.downArrow=0;
  185.   keyBoard.control=0;
  186.   keyBoard.escape=0;
  187.   numLockKeyStatus=0;
  188. }
  189.  
  190. /****************************************************************************
  191. ** Return the square root of a long value                   **
  192. ****************************************************************************/
  193. long long_sqrt(long v)
  194. {
  195.     int        i;
  196.     unsigned    long result,tmp;
  197.     unsigned    long low,high;
  198.  
  199. if (v <= 1L) return((unsigned)v);
  200.  
  201. low = v;
  202. high = 0L;
  203. result = 0;
  204.  
  205. for (i = 0; i < 16; i++)
  206.     {
  207.     result += result;
  208.     high = (high << 2) | ((low >>30) & 0x3);
  209.     low <<= 2;
  210.  
  211.     tmp = result + result + 1;
  212.     if (high >= tmp)
  213.     {
  214.     result++;
  215.     high -= tmp;
  216.     }
  217.     }
  218.  
  219. if (v - (result * result) >= (result - 1))
  220.     result++;
  221.  
  222. return(result);
  223. }
  224.  
  225.  
  226. /****************************************************************************
  227. **                                       **
  228. ****************************************************************************/
  229. char *GetExtent(char *s)
  230. {
  231.     char    *e;
  232.  
  233. e = strchr(s,'.');
  234. if (e == NULL)
  235.     return(s);
  236. e++;
  237.  
  238. return(e);
  239. }
  240.  
  241. /****************************************************************************
  242. **                                       **
  243. ****************************************************************************/
  244. char *StripEndOfLine(char *s)
  245. {
  246.     int        len;
  247.     char    ch;
  248.  
  249. len = strlen(s);
  250.  
  251. while (--len >= 0)
  252.     {
  253.     ch = s[len];
  254.     if (ch != ' ' && ch != ';' && ch != '\t' && ch != 13 && ch != 10)
  255.     break;
  256.  
  257.     s[len] = '\0';
  258.     }
  259.  
  260. return(s);
  261. }
  262.  
  263. /****************************************************************************
  264. **                                       **
  265. ****************************************************************************/
  266. char *SkipSpaces(char *s)
  267. {
  268.  
  269. while (*s == ' ' || *s == '\t' || *s == ',')
  270.     strcpy(s,&s[1]);
  271.  
  272. return(s);
  273. }
  274.  
  275. /****************************************************************************
  276. **                                       **
  277. ****************************************************************************/
  278. char *AddExtent(char *s,char *ext)
  279. {
  280. if (strchr(s,'.') == NULL)
  281.     strcat(s,ext);
  282.  
  283. return(s);
  284. }
  285.  
  286. /****************************************************************************
  287. **                                       **
  288. ****************************************************************************/
  289. char *CopyToComma(char *dest,char *src)
  290. {
  291.     char    ch;
  292.  
  293. while (*src)
  294.     {
  295.     ch = *src++;
  296.     if (ch == ' ' || ch == '\t' || ch == ',')
  297.     break;
  298.  
  299.     *dest++ = ch;
  300.     }
  301.  
  302. *dest = '\0';
  303.  
  304. return(src);
  305. }
  306.  
  307. /****************************************************************************
  308. **                                       **
  309. ****************************************************************************/
  310. void CheckMouse(MOUSE *m)
  311. {
  312.     int        dx,dy;
  313.     int        x,y,buttons;
  314.  
  315.  
  316. if (HaveMouse)
  317.     {
  318.     mouse_read_cursor(&buttons,&y,&x);
  319.     dx = x - 160;
  320.     dy = y - 120;
  321.     m->mButtons = buttons;
  322.     mouse_set_cursor(120,160);
  323.  
  324.     if (abs(dy) > 10 && abs(dx) < 32)
  325.     dx >>= 2;
  326.  
  327.     m->mdx = dx;
  328.     m->mdy = dy;
  329.  
  330.     }
  331.  
  332. }
  333.  
  334. /****************************************************************************
  335. **                                       **
  336. ****************************************************************************/
  337. unsigned char CheckSpecialCodes(int MapPosn)
  338. {
  339.     int        i;
  340.  
  341. for (i = 0; i < TotalSpecial; i++)
  342.     {
  343.     if (SpecialCodes[i].mPos == MapPosn)
  344.     return(SpecialCodes[i].mCode);
  345.     }
  346.  
  347. return(0);
  348. }
  349.  
  350.