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_rain.c < prev    next >
C/C++ Source or Header  |  1995-11-02  |  4KB  |  236 lines

  1. /* saver_rain.c                                   */
  2. /* A screen-saver program for use with SSaver,    */
  3. /* by Boisy G. Pitre                              */
  4. /* This program works on a 256-color screen       */
  5.  
  6. #include <stdio.h>
  7. #include <modes.h>
  8. #include <types.h>
  9. #include <wind.h>
  10.  
  11. #include "saver.h"
  12.  
  13. int  orgwin;
  14. int  win;
  15. int  twin;
  16.  
  17. int  pid;
  18. int  lines_to_copy;
  19. int  bytes_to_copy;
  20. char pal[768];
  21.  
  22. int  gotosleep = FALSE;
  23.  
  24. int  sigcode = 0;    /* storage to keep received signal(s) */
  25. int  sighandler();    /* this will use a signal handler function */
  26.     
  27. extern char *_gs_scadd();
  28. extern int errno;
  29.  
  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.         /* we unlink from the event since we are through with it */
  49.         _ev_unlink(evID);
  50.         exit(0);
  51.     }
  52.  
  53.     copy_screen(orgwin);
  54.     
  55.     init_saver();
  56.     
  57.     sigmask(0);                    /* unmask signals */
  58.     _ev_set(evID, 0, 0x8000);    /* wake up everybody */
  59.  
  60.     /* we unlink from the event since we are through with it */
  61.     _ev_unlink(evID);
  62.  
  63.     while(!sigcode)
  64.     {
  65.         if (gotosleep)
  66.             sleep(0);
  67.         else
  68.             rain();
  69.     }
  70.     cleanup();
  71. }
  72.  
  73. copy_screen(devnum)
  74. int devnum;
  75. {
  76.     SCINFO sc;
  77.     char devname[32];
  78.     int  devpath;
  79.     int  i;
  80.     int  bytes_per_scanline, total_scan_lines;
  81.     int  max_x_pixel_coord, bits_per_pixel;
  82.     int  max_colors, pixels_per_byte;
  83.     char *scrn, *this_scrn;
  84.  
  85.  
  86.     if (devnum == 0)
  87.         sprintf(devname,"/term");
  88.     else
  89.         sprintf(devname,"/w%d",devnum);
  90.  
  91.     /* get info on the parent device screen */
  92.     _gs_scinfo(STDOUT,&sc);
  93.  
  94.     if ((devpath = open(devname,1)) == -1)
  95.         exit (errno);
  96.  
  97.     if (_gs_palette(devpath,0,pal,256) == -1)
  98.         exit (errno);
  99.     
  100.     close (devpath);
  101.     
  102.     /* Now, let's get the information...           */
  103.     scrn = sc.sd_address;
  104.     bytes_per_scanline = sc.sd_phy_width;
  105.     total_scan_lines = sc.sd_max_y;
  106.     max_colors = 1 << sc.sd_bitsperpixel;
  107.  
  108.     _ss_select(STDOUT, devnum);            /* now select it! */
  109.  
  110.     if (max_colors == 16)
  111.     {
  112.         DWEnd(STDOUT);
  113.         DWSet(STDOUT, 0, 0, 0, 80, 26, 0, 0, 0);
  114.     }
  115.     else
  116.     {
  117.         DWEnd(STDOUT);
  118.         DWSet(STDOUT, 3, 0, 0, 40, 26, 0, 0, 0);
  119.     }
  120.     CurOff(STDOUT);
  121.     BColor(STDOUT, 0);
  122.     ScaleSw(STDOUT,1);
  123.  
  124.     this_scrn = _gs_scadd(STDOUT);
  125.     lines_to_copy = (total_scan_lines > 208) ? 208 : total_scan_lines;
  126.     bytes_to_copy = (sc.sd_phy_width > 320) ? 320 : sc.sd_phy_width; 
  127.     
  128.     /* warning BIG-TIME CHEAT!!! */
  129.     for (i = 0; i <= lines_to_copy; i++)
  130.     {
  131.         memcpy(&this_scrn[i * 320],&scrn[i * bytes_per_scanline],bytes_to_copy);
  132.     }
  133.     _ss_palette(STDOUT,0,pal,256);
  134.  
  135.     _gs_wdev(STDOUT, &win);        /* get our window device number */
  136.     _ss_select(STDOUT, win);    /* now select it! */
  137. }
  138.  
  139.  
  140. init_saver()
  141. {
  142.     pid = getpid();    /* get process-id for unique group value */
  143.  
  144.     rrand(-time(0));
  145.     
  146.     /* in case someone left buffer hanging around in our group */
  147.     KilBuf(STDOUT,pid,0);        
  148.  
  149.     /* Init out 2x(y) buffer */
  150.     GetBlk(STDOUT,pid,1,0,0,2,lines_to_copy - 1);
  151.  
  152.     /* init the Draw pointer */
  153.     SetDPtr(STDOUT,0,0);
  154. }
  155.  
  156. rain()
  157. {
  158.     int  i, x;
  159.  
  160.     /* move some lines down */
  161.     for (i = 0; i < bytes_to_copy/24; i++)
  162.     {
  163.         /* get a random x value */
  164.         x = rrand(bytes_to_copy) - 1;
  165.  
  166.         /* Get 8-bits by Max_y */
  167.         GetBlk(STDOUT,pid,1,x << 1,0,2,lines_to_copy - 1);
  168.  
  169.         /* Put it down 1 scanline */
  170.         PutBlk(STDOUT,pid,1,x << 1,1);
  171.  
  172.         /* Black out top pixels */
  173.         RPoint(STDOUT,x << 1,0);
  174.         RPoint(STDOUT,(x << 1) + 1,0);
  175.     }
  176.     /* Give us a little rest ;-) */
  177.     tsleep(2);
  178. }
  179.  
  180. cleanup()
  181. {
  182.     KilBuf(STDOUT,pid,0);        
  183. }
  184.  
  185. sighandler(signal)
  186. int signal;
  187. {
  188.     switch(signal) {
  189.     case SLEEP_SIG:
  190.         gotosleep = TRUE;
  191.         break;
  192.     case WAKE_SIG:
  193.         gotosleep = FALSE;
  194.         break;
  195.     case QUIT_SIG:
  196.     default:
  197.         gotosleep = FALSE;
  198.         sigcode = signal;
  199.         if (_gs_currscr(STDOUT,&twin) == -1)
  200.             exit(0);
  201.         if (twin == win) {
  202.             _ss_select(STDOUT, orgwin);
  203.         }
  204.     }
  205. }
  206.  
  207. static int rndval=10000;
  208.  
  209. /* rrand( val ) : return a pseudo-random value between 1 and val.
  210. **               IF val is -1, it is used as a seed.
  211. **               IF val is negative the better the seed.
  212. */
  213. rrand( val )
  214. int val;
  215. {
  216.     int bit0, bit1, bit14, num, i;
  217.  
  218.     /* see if -1 */
  219.     if ( val < 0 ) {
  220.         rndval = -val;
  221.     }
  222.  
  223.     for ( i = 0; i < 10; i++ )
  224.     {
  225.         bit0 = rndval & 1;
  226.         bit1 = (rndval & 2) >> 1;
  227.         bit14 = bit0 ^ bit1;
  228.         rndval >>=1;
  229.         rndval |= (bit14 << 14);
  230.     }
  231.     num = rndval % val;
  232.     return( num ? num : val );
  233. }
  234.  
  235.  
  236.