home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Add-Ons / After Dark / Joe Judge / Misc ƒ / utils.c < prev    next >
Encoding:
Text File  |  1994-11-26  |  5.0 KB  |  289 lines  |  [TEXT/KAHL]

  1.  
  2. // utils
  3. #include <Picker.h>
  4. #include <GestaltEqu.h>
  5. #include <utils.h>
  6.  
  7. // this is from the Think C reference code example ...
  8. // fixed by me later on
  9. unsigned short RangedRdm( unsigned short min, unsigned short max )
  10. /* assume that min is less than max */
  11. {
  12.     // uh ... not this isn't quite right - it's between 0 and 65535, not 65536
  13.     unsigned    qdRdm;    /* treat return value as 0-65536 */
  14.     long    range, t;
  15.     unsigned short temp;
  16.     
  17.     if (min == max) 
  18.         return min;
  19.         
  20.         
  21.     // just to be safe, I'll put this here
  22.     if (min > max) {
  23.         //DebugStr("\pMin greater then Max in RangedRdm");
  24.         temp = min;
  25.         min = max;
  26.         max = temp;
  27.     }
  28.     
  29.     qdRdm = Random();
  30.     range = max - min;
  31.     // max - min gives us the the difference between max and min ... that is 
  32.     // not inclusive. It gives us { min <= range < max }
  33.     // so we never see that max number!!
  34.     range++;
  35.     t = ((long)qdRdm * range) / 65536;     /* now 0 <= t <= range */
  36.     return( t+min );
  37. }
  38.  
  39.  
  40. // this tests if a key is currently held down
  41. short isPressed(unsigned short k )
  42. // k =  any keyboard scan code, 0-127
  43. {
  44.     unsigned char km[16];
  45.  
  46.     GetKeys( (long *) km);
  47.     return ( ( km[k>>3] >> (k & 7) ) & 1);
  48. }
  49.  
  50.  
  51. Boolean 
  52. WNEAvailable(void) {
  53.     SysEnvRec    theSysEnv;
  54.     Boolean    wneAvail = FALSE;
  55.  
  56.     SysEnvirons( 1, &theSysEnv );
  57.     if ( theSysEnv.machineType >= 1 ) {
  58.         if ( NGetTrapAddress( 0xA860, 1 ) != 0xA89F )
  59.         return  TRUE;
  60.     } 
  61.     return FALSE;
  62. }
  63.  
  64.  
  65. Boolean
  66. HasColorQD(void) {
  67.     OSErr    err;
  68.     Boolean answer = true;
  69.     long    gestaltResult;
  70.  
  71.     err = Gestalt(gestaltQuickdrawVersion, &gestaltResult);
  72.  
  73.     answer = (err == noErr) && (gestaltResult >= gestalt8BitQD);
  74.     return answer;
  75. }
  76.  
  77.  
  78. Boolean HasNewGWorld(void)
  79. {
  80.     OSErr err;
  81.     long    gestaltResult;
  82.  
  83.     err = Gestalt(gestaltQuickdrawVersion, &gestaltResult);
  84.  
  85.     return (err == noErr) && (((gestaltResult > gestaltOriginalQD) &&
  86.             (gestaltResult < gestalt8BitQD)) || (gestaltResult >= gestalt32BitQD));
  87. }
  88.  
  89.  
  90.  
  91. void
  92. RandomIndexedColor(short screenDepth)  {
  93. short numColors;
  94. RGBColor rgb;
  95.  
  96.     numColors = 1 << screenDepth;
  97.     Index2Color( RangedRdm(1,numColors), &rgb );
  98.     RGBForeColor( &rgb);
  99.  
  100. }
  101.  
  102.  
  103.  
  104. void
  105. SetIndexedColor(int whichColor)  {
  106. RGBColor rgb;
  107.  
  108.     if (HasColorQD()) {
  109.         Index2Color( whichColor, &rgb );
  110.         RGBForeColor( &rgb);
  111.     } else ForeColor(whiteColor);
  112. }
  113.  
  114.  
  115. void
  116. PickBrightRGB( RGBColor *rgb) {
  117. HSVColor hColor;
  118.     hColor.hue = RangedRdm(0, 65535);
  119.     hColor.saturation = (SmallFract)RangedRdm( 49150, 65535);
  120.     hColor.value = (SmallFract) RangedRdm( 49150, 65535);
  121.     HSV2RGB( &hColor, rgb);
  122. }
  123.  
  124. void
  125. PickDarkRGB( RGBColor *rgb) {
  126. HSVColor hColor;
  127.     hColor.hue = RangedRdm(0, 65535);
  128.     hColor.saturation = (SmallFract)RangedRdm( 49150, 65535);
  129.     hColor.value = (SmallFract) RangedRdm( 32767/2, 32767);
  130.     HSV2RGB( &hColor, rgb);
  131. }
  132.  
  133.  
  134. void 
  135. SetRandomDarkRGB(void) {
  136. RGBColor rColor;
  137.  
  138.     if (!HasColorQD()) {
  139.         ForeColor(blackColor);
  140.     } else {
  141.         PickDarkRGB( &rColor);
  142.         RGBForeColor( &rColor);
  143.     }
  144. }
  145.  
  146. void
  147. SetRandomBrightRGB(void) {
  148. RGBColor rColor;
  149.  
  150.     if (!HasColorQD()) {
  151.         ForeColor(whiteColor);
  152.     } else {
  153.         PickBrightRGB( &rColor);
  154.         RGBForeColor( &rColor);
  155.     }
  156. }
  157.  
  158.  
  159. #define theLower(a, b) ( (a) < (b) ? (a) : (b) )
  160. #define abs(a)    ((a) < 0 ? -(a) : (a))
  161.  
  162. void
  163. SetColorDiff( 
  164.     RGBColor *rgbTheColor, 
  165.         RGBColor *a, RGBColor *b, 
  166.             double percent) {
  167. HSVColor  ha, hb, hc;
  168.  
  169.     RGB2HSV( a, &ha);
  170.     RGB2HSV( b, &hb);
  171.     
  172.     hc.hue = theLower(ha.hue, hb.hue) +
  173.         (percent * abs( ha.hue - hb.hue) );
  174.  
  175.     hc.saturation = 65535;
  176.     hc.value = 65535;
  177.     
  178.     HSV2RGB( &hc, rgbTheColor);
  179.     
  180. }
  181.  
  182. #if 0
  183.     rgbTheColor->red = theLower(a->red, b->red) +
  184.         (percent * abs( a->red - b->red ) );
  185.     
  186.     rgbTheColor->blue = theLower(a->blue, b->blue) +
  187.         (percent * abs( a->blue - b->blue ) );
  188.     
  189.     rgbTheColor->green = theLower(a->green, b->green) +
  190.         (percent * abs( a->green - b->green ) );
  191. #endif
  192.  
  193.  
  194.  
  195.  
  196. Boolean 
  197. HasSoundInputDevice(void) {
  198. OSErr retCode;
  199. long feature;
  200.  
  201.     retCode = Gestalt(gestaltSoundAttr, &feature);
  202.     if (retCode != noErr) 
  203.         return FALSE;
  204.     else
  205.         return (feature & (1 << gestaltHasSoundInputDevice));
  206. }
  207.  
  208.  
  209.  
  210. // this assumes the port is set to the dialog box?
  211. void
  212. FlashDItem(DialogPtr dlg, int item)
  213. {
  214. long waste;
  215. short        type;
  216. Handle    theHandle;
  217. Rect     iRect;
  218.  
  219.     GetDItem(dlg, item, &type, &theHandle, &iRect);
  220.  
  221.     HiliteControl( (ControlHandle)theHandle, inButton);
  222.     Delay(2, &waste);
  223.     HiliteControl( (ControlHandle)theHandle, 0);
  224.     
  225. }
  226.  
  227.  
  228. void
  229. pstrcat(char *s1, char *s2)
  230. {
  231.     register char *p;
  232.     register short len, i;
  233.     
  234.     if (*s1+*s2<=255) {
  235.         p = *s1 + s1 + 1;
  236.         *s1 += (len = *s2++);
  237.     }
  238.     else {
  239.         *s1 = 255;
  240.         p = s1 + 256 - (len = *s2++);
  241.     }
  242.     for (i=len; i; --i) *p++ = *s2++;
  243. }
  244.  
  245. void
  246. pstrcpy( char *s1, char *s2 )
  247. {
  248.     register int length;
  249.     
  250.     for (length=*s2; length>=0; --length) *s1++=*s2++;
  251. }
  252.  
  253.  
  254. // has the system 7 time manager?
  255. Boolean HasTimeMgr(void)
  256. {
  257.     OSErr    err;
  258.     Boolean isSystemGood = true;
  259.     long    gestaltResult;
  260.  
  261.     err = Gestalt(gestaltTimeMgrVersion, &gestaltResult);
  262.  
  263.     isSystemGood = (err == noErr) && (gestaltResult >= gestaltStandardTimeMgr);
  264.  
  265.     return isSystemGood;
  266. }
  267.  
  268. long
  269. PickDarkIndexColor(void) {
  270. RGBColor rColor;
  271.  
  272.     PickDarkRGB( &rColor);
  273.     return Color2Index( &rColor);
  274.     
  275. }
  276.  
  277.  
  278. long
  279. PickBrightIndexColor(void) {
  280. RGBColor rColor;
  281.  
  282.     PickBrightRGB( &rColor);
  283.     return Color2Index( &rColor);
  284.     
  285. }
  286.  
  287.  
  288.  
  289.