home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / GRAPHICS / ssaver.lzh / SRC / saver_slider.c < prev    next >
C/C++ Source or Header  |  1995-04-17  |  7KB  |  368 lines

  1. /* saver_slider.c - puzzle screensaver for ssaver */
  2. #define MYRAND
  3. #define srand(x)    rrand(-time(0));
  4.  
  5. #include <stdio.h>
  6. #include <modes.h>
  7. #include <types.h>
  8. #include <wind.h>
  9.  
  10. #include "saver.h"
  11.  
  12. int  orgwin;
  13. int  win;
  14. int  twin;
  15.  
  16. char pal[768];
  17.  
  18. int  gotosleep = FALSE;
  19. int  sigcode = 0;    /* storage to keep received signal(s) */
  20. int  sighandler();    /* this will use a signal handler function */
  21.     
  22. extern char *_gs_scadd();
  23. extern int errno;
  24.  
  25. #define GRID_X 20
  26. #define GRID_Y 13
  27.  
  28. int  hole_x, hole_y;
  29. int  pid;
  30.  
  31. main()
  32. {
  33.     int evID;                    /* event ID */
  34.  
  35.     sigmask(1);                    /* mask signals */
  36.     
  37.     intercept(sighandler);        /* install signal handler */
  38.  
  39.     if ((evID = _ev_link(EV_NAME)) == -1)
  40.         exit(1);
  41.  
  42.     DWSet(STDOUT, 0xFF, 0, 0, 1, 1, 0, 0, 0);
  43.     CurOff(STDOUT);
  44.     BackWin(STDOUT);
  45.  
  46.     if (_gs_currscr(STDOUT,&orgwin) == -1) {
  47.         _ev_set(evID, 0, 0x8000);    /* wake up everybody */
  48.  
  49.         /* we unlink from the event since we are through with it */
  50.         _ev_unlink(evID);
  51.         exit(0);
  52.     }
  53.  
  54.     copy_screen(orgwin);
  55.  
  56.     init_saver();
  57.     
  58.     sigmask(0);                    /* unmask signals */
  59.     _ev_set(evID, 0, 0x8000);    /* wake up everybody */
  60.  
  61.     /* we unlink from the event since we are through with it */
  62.     _ev_unlink(evID);
  63.  
  64.     while(!sigcode)
  65.     {
  66.         if (gotosleep)
  67.             sleep(0);
  68.         else
  69.             slider();
  70.     }
  71.     cleanup();
  72. }
  73.  
  74. copy_screen(devnum)
  75. int devnum;
  76. {
  77.     SCINFO sc;
  78.     char devname[32];
  79.     int  devpath;
  80.     int  i;
  81.     int  lines_to_copy, bytes_to_copy;
  82.     int  bytes_per_scanline, total_scan_lines;
  83.     int  max_x_pixel_coord, bits_per_pixel;
  84.     int  max_colors, pixels_per_byte;
  85.     char *scrn, *this_scrn;
  86.  
  87.  
  88.     if (devnum == 0)
  89.         sprintf(devname,"/term");
  90.     else
  91.         sprintf(devname,"/w%d",devnum);
  92.  
  93.     /* get info on the parent device screen */
  94.     _gs_scinfo(STDOUT,&sc);
  95.  
  96.     if ((devpath = open(devname,1)) == -1)
  97.         exit (errno);
  98.  
  99.     if (_gs_palette(devpath,0,pal,256) == -1)
  100.         exit (errno);
  101.     
  102.     close (devpath);
  103.     
  104.     /* Now, let's get the information...           */
  105.     scrn = sc.sd_address;
  106.     bytes_per_scanline = sc.sd_phy_width;
  107.     total_scan_lines = sc.sd_max_y;
  108.     max_colors = 1 << sc.sd_bitsperpixel;
  109.  
  110.     _ss_select(STDOUT, devnum);            /* now select it! */
  111.  
  112.     if (max_colors == 16)
  113.     {
  114.         DWEnd(STDOUT);
  115.         DWSet(STDOUT, 0, 0, 0, 80, 26, 0, 0, 0);
  116.     }
  117.     else
  118.     {
  119.         DWEnd(STDOUT);
  120.         DWSet(STDOUT, 3, 0, 0, 40, 26, 0, 0, 0);
  121.     }
  122.     CurOff(STDOUT);
  123.     BColor(STDOUT, 0);
  124.     ScaleSw(STDOUT,1);
  125.  
  126.     this_scrn = _gs_scadd(STDOUT);
  127.     lines_to_copy = (total_scan_lines > 208) ? 208 : total_scan_lines;
  128.     bytes_to_copy = (sc.sd_phy_width > 320) ? 320 : sc.sd_phy_width; 
  129.     
  130.     /* warning BIG-TIME CHEAT!!! */
  131.     for (i = 0; i <= lines_to_copy; i++)
  132.     {
  133.         memcpy(&this_scrn[i * 320],&scrn[i * bytes_per_scanline],bytes_to_copy);
  134.     }
  135.     _ss_palette(STDOUT,0,pal,256);
  136.  
  137.     _gs_wdev(STDOUT, &win);        /* get our window device number */
  138.     _ss_select(STDOUT, win);    /* now select it! */
  139. }
  140.  
  141.  
  142. init_saver()
  143. {
  144.     int  i, j;
  145.  
  146.     pid = getpid();    /* get process-id for unique group value */
  147.  
  148.     ScaleSw(STDOUT,1);
  149.  
  150.     srand(0);
  151.  
  152.     /* init the Draw pointer */
  153.     SetDPtr(STDOUT,0,0);
  154.     FColor(STDOUT,0);
  155.  
  156.     /* Lets break up the screen */
  157.     for (i = 1; i < 21; i++)
  158.     {
  159.         SetDPtr(STDOUT,(i << 5) - 1,0);
  160.         RLine(STDOUT,0,207);
  161.     }
  162.     for (i = 1; i < 14; i++)
  163.     {
  164.         SetDPtr(STDOUT,0,(i << 4) - 1);
  165.         RLine(STDOUT,639,0);
  166.     }
  167.  
  168.     /* reset the draw pointer */
  169.     SetDPtr(STDOUT,0,0);
  170.  
  171.     /* in case someone left buffer hanging around in our group */
  172.     KilBuf(STDOUT,pid,0);        
  173.  
  174.     /* Init out 2x(y) buffer */
  175.     GetBlk(STDOUT,pid,1,0,0,32,16);
  176.  
  177.     /* pick our hole */
  178.     hole_x = rrand(GRID_X) - 1;
  179.     hole_y = rrand(GRID_Y) - 1;
  180.  
  181. #if 0
  182.     /* clear the hole */
  183.     SetDPtr(STDOUT,hole_x << 5,hole_y << 4);
  184.     RBar(STDOUT,31,15);
  185. #endif
  186. }
  187.  
  188. slider()
  189. {
  190.      static int last = 0;
  191.      int  i, j;
  192.     int  way;
  193.     int  dx, dy;
  194.     int  w, h, x, y;
  195.  
  196.     do {
  197.         way = rrand(4) - 1;
  198.  
  199.         /* which way to move the hole */
  200.         switch(way) {
  201.         case 0: /* up */
  202.             dx = 0;
  203.             dy = -1;
  204.             break;
  205.                 
  206.         case 1: /* down */
  207.             dx = 0;
  208.             dy = 1;
  209.             break;
  210.             
  211.         case 2: /* left */
  212.             dx = -1;
  213.             dy = 0;
  214.             break;
  215.             
  216.         case 3: /* right */
  217.             dx = 1;
  218.             dy = 0;
  219.             break;
  220.                 
  221.         }
  222.     } while (way == last ||
  223.          hole_x + dx >= GRID_X || hole_x + dx < 0 ||
  224.          hole_y + dy >= GRID_Y || hole_y + dy < 0);
  225.  
  226.     last = (way == 1 ? 0 : way == 0 ? 1 : way == 2 ? 3 : 2);
  227.         
  228.     switch (way) {
  229.     case 0:
  230.         h = rrand(hole_y);
  231.         x = hole_x << 5;
  232.         y = hole_y << 4;
  233.         hole_y -= h;
  234.         for (j = 0; j < 16; j += 2)
  235.         {
  236.             for (i = 0; i < h; i++)
  237.             {
  238.                 GetBlk(STDOUT,pid,1,x,y - ((i + 1) << 4) + j,32,16);
  239.                 PutBlk(STDOUT,pid,1,x,(y - ((i + 1) << 4) + j) + 2);
  240.             }
  241.             SetDPtr(STDOUT,hole_x << 5,(hole_y << 4));
  242.             RBar(STDOUT,31,1+j);
  243.             tsleep(2);
  244.             if (sigcode)
  245.                 break;
  246.         }
  247.         break;
  248.     case 1:
  249.         h = rrand(GRID_Y - hole_y - 1);
  250.         x = hole_x << 5;
  251.         y = hole_y << 4;
  252.         hole_y += h;
  253.         for (j = 0; j < 16; j += 2)
  254.         {
  255.             for (i = 0; i < h; i++)
  256.             {
  257.                 GetBlk(STDOUT,pid,1,x,y + ((i + 1) << 4) - j,32,16);
  258.                 PutBlk(STDOUT,pid,1,x,(y + ((i + 1) << 4) - j) - 2);
  259.             }
  260.             SetDPtr(STDOUT,hole_x << 5,(hole_y << 4) + (14 - j));
  261.             RBar(STDOUT,31,1+j);
  262.             tsleep(2);
  263.             if (sigcode)
  264.                 break;
  265.         }
  266.         break;
  267.     case 2:
  268.         w = rrand(hole_x);
  269.         x = hole_x << 5;
  270.         y = hole_y << 4;
  271.         hole_x -= w;
  272.         for (j = 0; j < 32; j += 4)
  273.         {
  274.             for (i = 0; i < w; i++)
  275.             {
  276.                 GetBlk(STDOUT,pid,1, x - ((i + 1) << 5) + j,y,32,16);
  277.                 PutBlk(STDOUT,pid,1,(x - ((i + 1) << 5) + j) + 4,y);
  278.             }
  279.             SetDPtr(STDOUT,hole_x << 5,(hole_y << 4));
  280.             RBar(STDOUT,3+j,15);
  281.             tsleep(2);
  282.             if (sigcode)
  283.                 break;
  284.         }
  285.         break;
  286.     case 3:
  287.         w = rrand(GRID_X - hole_x - 1);
  288.         x = hole_x << 5;
  289.         y = hole_y << 4;
  290.         hole_x += w;
  291.         for (j = 0; j < 32; j += 4)
  292.         {
  293.             for (i = 0; i < w; i++)
  294.             {
  295.                 GetBlk(STDOUT,pid,1, x + ((i + 1) << 5) - j,y,32,16);
  296.                 PutBlk(STDOUT,pid,1,(x + ((i + 1) << 5) - j) - 4,y);
  297.             }
  298.             SetDPtr(STDOUT,(hole_x << 5) + (28 - j),hole_y << 4);
  299.             RBar(STDOUT,3+j,15);
  300.             tsleep(2);
  301.             if (sigcode)
  302.                 break;
  303.         }
  304.         break;
  305.     }
  306.     /* Give us a little rest ;-) */
  307.     if (!sigcode)
  308.         tsleep(0x80000100);
  309. }
  310.  
  311. cleanup()
  312. {
  313.     KilBuf(STDOUT,pid,0);        
  314. }
  315.  
  316. sighandler(signal)
  317. int signal;
  318. {
  319.     switch(signal) {
  320.     case SLEEP_SIG:
  321.         gotosleep = TRUE;
  322.         break;
  323.     case WAKE_SIG:
  324.         gotosleep = FALSE;
  325.         break;
  326.     case QUIT_SIG:
  327.     default:
  328.         gotosleep = FALSE;
  329.         sigcode = signal;
  330.         if (_gs_currscr(STDOUT,&twin) == -1)
  331.             exit(0);
  332.         if (twin == win) {
  333.             _ss_select(STDOUT, orgwin);
  334.         }
  335.     }
  336. }
  337.  
  338. #ifdef MYRAND
  339. static int rndval=10000;
  340.  
  341. /* rrand( val ) : return a pseudo-random value between 1 and val.
  342. **               IF val is -1, it is used as a seed.
  343. **               IF val is negative the better the seed.
  344. */
  345. rrand( val )
  346. int val;
  347. {
  348.     int bit0, bit1, bit14, num, i;
  349.  
  350.     /* see if -1 */
  351.     if ( val < 0 ) {
  352.         rndval = -val;
  353.     }
  354.  
  355.     for ( i = 0; i < 10; i++ )
  356.     {
  357.         bit0 = rndval & 1;
  358.         bit1 = (rndval & 2) >> 1;
  359.         bit14 = bit0 ^ bit1;
  360.         rndval >>=1;
  361.         rndval |= (bit14 << 14);
  362.     }
  363.     num = rndval % val;
  364.     return( num ? num : val );
  365. }
  366. #endif
  367.  
  368.