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

  1. /* saver_plasma.c                                 */
  2. /* A screen-saver program for use with SSaver,    */
  3. /* by Mike Haaland, James Jones                   */
  4. /* This program works on a 256-color screen       */
  5.  
  6. #include <modes.h>
  7. #include <types.h>
  8.  
  9. #include "saver.h"
  10.  
  11. int orgwin, win, twin;
  12.  
  13. int sighandler();    /* this will use a signal handler function */
  14. int sigcode = 0;    /* storage to keep received signal(s) */
  15.  
  16. int gotosleep = FALSE;
  17.  
  18. extern int errno;
  19.  
  20. void rotate_colors();
  21.  
  22. main()
  23. {
  24.     int evID;                    /* event ID */
  25.  
  26.     sigmask(1);                    /* mask signals */
  27.     
  28.     intercept(sighandler);        /* install signal handler */
  29.  
  30.     if ((evID = _ev_link(EV_NAME)) == -1)
  31.         exit(1);
  32.  
  33.     DWSet(STDOUT, 3, 0, 0, 40, 26, 0, 0, 0);
  34.     if (_gs_currscr(STDOUT,&orgwin) == -1)
  35.         exit(0);
  36.  
  37.     CurOff(STDOUT);
  38.     BColor(STDOUT, 0);
  39.     _gs_wdev(STDOUT, &win);            /* get our window device number */
  40.     _ss_select(STDOUT, win);            /* now select it! */
  41.  
  42.     sigmask(0);                    /* unmask signals */
  43.     _ev_set(evID, 0, 0x8000);    /* wake up everybody */
  44.  
  45.     /* we unlink from the event since we are through with it */
  46.     _ev_unlink(evID);
  47.  
  48.     init_plasma();
  49.     
  50.     while(!sigcode)
  51.     {
  52.         if (gotosleep)
  53.             sleep(0);
  54.         else
  55.             /* Do our thing */
  56.             rotate_colors();
  57.     }
  58. }
  59.  
  60. sighandler(signal)
  61. int signal;
  62. {
  63.     switch (signal) {
  64.     case SLEEP_SIG:
  65.         gotosleep = TRUE;
  66.         break;
  67.     case WAKE_SIG:
  68.         gotosleep = FALSE;
  69.         break;
  70.     case QUIT_SIG:
  71.     default:
  72.         gotosleep = FALSE;
  73.         sigcode=signal;
  74.         if (_gs_currscr(STDOUT,&twin) == -1)
  75.             exit(0);
  76.         if (twin == win) {
  77.             _ss_select(STDOUT, orgwin);
  78.         }
  79.     }
  80. }
  81.  
  82.  
  83. #include <stdio.h>
  84. #include <time.h>
  85.  
  86. /* If your library has no rand() function */
  87. #define randomize()     srand(time(0))
  88. #define random(x)     ( rand() % (x + 1) )
  89.  
  90. unsigned char *scrn_mem;
  91.  
  92. #define FINDSCRN        scrn_mem = (unsigned char *)_gs_scadd(STDOUT)
  93.  
  94. #define abs(x)          mabs(x)
  95.  
  96. void plot256();
  97. void setMode();
  98. void setVGApalette();
  99. void set_color();
  100. void initPalette();
  101. void subdivide();
  102. void rotate_colors();
  103. void setVGAreg();
  104. short readPixel();
  105.  
  106. short Xlow = 0, Ylow = 0, Xres = 319, Yres = 199, scale_factor = 1;
  107. short interval = 24, sm_indx = 25;
  108. unsigned char Pal_Array[256][3];
  109.  
  110. mabs(value)
  111. int value;
  112. {
  113.     return( (value < 0) ? -value : value);
  114. }
  115.  
  116. init_plasma()
  117. {
  118.     char *ptr, *strchr();
  119.  
  120.     FINDSCRN;
  121.  
  122.     initPalette();
  123.     randomize();
  124.     plot256(Xlow,Ylow,random(255) + 1);
  125.     plot256(Xres,Ylow,random(255) + 1);
  126.     plot256(Xres,Yres,random(255) + 1);
  127.     plot256(Xlow,Yres,random(255) + 1);
  128.     subdivide(Xlow,Ylow,Xres,Yres);
  129. }
  130.  
  131. /* plot256() - Function to plot point to VGA 256 Color Screen */
  132. void plot256(x, y, color)
  133. int x, y, color;
  134. {
  135.     unsigned char *scrn_byte;
  136.     
  137.     scrn_byte = scrn_mem + (y * 320) + x;
  138.  
  139.     *scrn_byte = (unsigned char)color & 0xff;
  140. }
  141.  
  142.  
  143. /* setVGAreg() = Function to set individual VGA color register */
  144. void setVGAreg( reg_no, red, green, blue)
  145. int reg_no, red, green, blue;
  146. {
  147.     short r, g, b;
  148.  
  149.     r = (red * 255) / 63;
  150.     g = (green * 255) / 63;
  151.     b = (blue * 255) / 63;
  152.     
  153.     Palette(STDOUT,red,green,blue);
  154. }
  155.  
  156. /* setVGApalette() = Function to set all 256 color registers */
  157. void setVGApalette(buffer)
  158. unsigned char *buffer;
  159. {
  160.     unsigned char buf[768];
  161.     register short count;
  162.  
  163.     for (count = 0; count < 768; count++ )
  164.         buf[count] = (*buffer++ * 255) / 63;
  165.     _ss_palette(STDOUT,0,buf,256);
  166. }
  167.  
  168. /* subdivide() = Divides up a display section and fills with color. */
  169. void subdivide(x1, y1, x2, y2)
  170. int x1, y1, x2, y2;
  171. {
  172.     short sm_indx;
  173.     short x, y, color;
  174.  
  175.     if (sigcode)
  176.         return;
  177.  
  178.     while (gotosleep)
  179.         sleep(0);
  180.  
  181.     if ( ((x2 - x1) < 2) && ((y2 - y1) < 2) )
  182.         return;
  183.     x = (x1 + x2) >> 1;
  184.     y = (y1 + y2) >> 1;
  185.     set_color(x1,y1,x,y1,x2,y1);
  186.     set_color(x2,y1,x2,y,x2,y2);
  187.     set_color(x1,y2,x,y2,x2,y2);
  188.     set_color(x1,y1,x1,y,x1,y2);
  189.     color = (readPixel(x1,y1) + readPixel(x2,y1) + readPixel(x2,y2)
  190.         + readPixel(x1,y2) + 2) >> 2; /* was >> 1 */
  191.     plot256 (x, y, color);
  192.     subdivide(x1,y1,x,y);
  193.     subdivide(x,y1,x2,y);
  194.     subdivide(x,y,x2,y2);
  195.     subdivide(x1,y,x,y2);
  196. }
  197.  
  198. /* set_color() = Picks the new color for the pixel */
  199. void set_color( xa, ya, x, y, xb, yb)
  200. int xa, ya, x, y, xb, yb;
  201. {
  202.     long color;
  203.  
  204.     color = abs(xa - xb) + abs(ya - yb);
  205.  
  206.     if (color == 0)
  207.         return;
  208.  
  209.     color = ( (rand() % (color * 2)) - color);
  210.     color +=  (readPixel(xa,ya) + readPixel(xb,yb) + 1) >> 1;
  211.  
  212.     if (color < 1)
  213.         color = 1;
  214.     if (color > 255)
  215.         color = 255;
  216.     if (readPixel(x,y) == 0)
  217.         plot256(x,y,color);
  218. }
  219.  
  220. /* initPalette() = Sets the colors of the VGA Palette */
  221. void initPalette()
  222. {
  223.  
  224.     short max_color = 63;
  225.     short sm_indx;
  226.  
  227.     Pal_Array[0][0] = 0;
  228.     Pal_Array[0][1] = 0;
  229.     Pal_Array[0][2] = 0;
  230.  
  231.     for (sm_indx=1; sm_indx<86; sm_indx++)
  232.     {
  233.         Pal_Array[sm_indx][0]     = 0;
  234.         Pal_Array[sm_indx][1]     = (sm_indx * max_color) / 85;
  235.         Pal_Array[sm_indx][2]     = ((86 - sm_indx) * max_color) / 85;
  236.         Pal_Array[sm_indx+85][0]  = (sm_indx * max_color) / 85;
  237.         Pal_Array[sm_indx+85][1]  = ((86 - sm_indx) * max_color) / 85;
  238.         Pal_Array[sm_indx+85][2]  = 0;
  239.         Pal_Array[sm_indx+170][0] = ((86 - sm_indx) * max_color) / 85;
  240.         Pal_Array[sm_indx+170][1] = 0;
  241.         Pal_Array[sm_indx+170][2] = (sm_indx * max_color) / 85;
  242.     }
  243.     setVGApalette(Pal_Array[0]);
  244. }
  245.  
  246. /* readPixel = Read a Pixel from the Screen */
  247. short readPixel( x, y)
  248. int x, y;
  249. {
  250.     unsigned char *scrn_byte;
  251.     
  252.     scrn_byte = scrn_mem + (y * 320) + x;
  253.  
  254.     return ((short) *scrn_byte);
  255. }
  256.  
  257. /* rotateColors() = Rotates the VGA Colors */
  258. void rotate_colors()
  259. {
  260.     short i;
  261.     short old_red, old_grn, old_blu;
  262.     short new_red, new_grn, new_blu;
  263.  
  264.     old_red = Pal_Array[255][0];
  265.     old_grn = Pal_Array[255][1];
  266.     old_blu = Pal_Array[255][2];
  267.  
  268.     new_red = Pal_Array[1][0];
  269.     new_grn = Pal_Array[1][1];
  270.     new_blu = Pal_Array[1][2];
  271.  
  272.     for (i=1; i<255; i++)
  273.     {
  274.         Pal_Array[i][0] = Pal_Array[i+1][0];
  275.         Pal_Array[i][1] = Pal_Array[i+1][1];
  276.         Pal_Array[i][2] = Pal_Array[i+1][2];
  277.     }
  278.     Pal_Array[255][0] = new_red;
  279.     Pal_Array[255][1] = new_grn;
  280.     Pal_Array[255][2] = new_blu;
  281.  
  282.     setVGApalette(Pal_Array[0]);
  283.     tsleep(2);
  284. }
  285.  
  286. #if 0
  287. /* rotateColors() = Rotates the VGA Colors and changed the hue */
  288. void do_rotate_colors()
  289. {
  290.     short i, j, k;
  291.     short old_red, old_grn, old_blu;
  292.     short new_red, new_grn, new_blu;
  293.     short last_step=32;
  294.     char ch;
  295.  
  296.     while (!sigcode)
  297.     {
  298.         old_red = Pal_Array[255][0];
  299.         old_grn = Pal_Array[255][1];
  300.         old_blu = Pal_Array[255][2];
  301.  
  302.         new_red = rand() % 63;
  303.         new_grn = rand() % 63;
  304.         new_blu = rand() % 63;
  305.  
  306.         for (j=1; j<last_step; j++)
  307.         {
  308.             for (i=1; i<255; i++)
  309.             {
  310.                 Pal_Array[i][0] = Pal_Array[i+1][0];
  311.                 Pal_Array[i][1] = Pal_Array[i+1][1];
  312.                 Pal_Array[i][2] = Pal_Array[i+1][2];
  313.             }
  314.  
  315.             Pal_Array[255][0] = old_red + ((new_red-old_red)*j)/last_step;
  316.             Pal_Array[255][1] = old_grn + ((new_grn-old_grn)*j)/last_step;
  317.             Pal_Array[255][2] = old_blu + ((new_blu-old_blu)*j)/last_step;
  318.  
  319.             setVGApalette(Pal_Array[0]);
  320.         }
  321.     }
  322. }
  323. #endif
  324.  
  325. static long seed = 1L;
  326.  
  327. int rand()
  328. {
  329.     seed = (1103515245L * seed + 12345) & 0xFFFFFFFF;
  330.     return((short) (seed  & 077777));
  331. }
  332.  
  333. int srand (new_seed)
  334. long new_seed;
  335. {
  336.     seed = new_seed & 0xFFFFFFFF;
  337. }
  338.  
  339.  
  340.