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

  1. /* saver_lines.c                         */
  2. /* by Joel Mathew Hegberg                */
  3. /* a simple ssaver screen-saver program  */
  4. /* which runs on any type screen.        */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include "saver.h"
  9.  
  10. int orgwin, win, twin;
  11.  
  12. int gotosleep = FALSE;
  13. int sigcode = 0;
  14.  
  15. int x1, y1;
  16. int x2, y2;
  17. int dx1, dy1;
  18. int dx2, dy2;
  19. int mx1, my1;
  20. int mx2, my2;
  21.  
  22. int counter;
  23. int color, maxcolors;
  24.  
  25. sighandler(signal)
  26. int signal;
  27. {
  28.     switch (signal) {
  29.     case SLEEP_SIG:
  30.         gotosleep = TRUE;
  31.         break;
  32.     case WAKE_SIG:
  33.         gotosleep = FALSE;
  34.         break;
  35.     case QUIT_SIG:
  36.     default:
  37.         gotosleep = FALSE;
  38.         sigcode = signal;
  39.         if (_gs_currscr(STDOUT,&twin) == -1)
  40.             exit(0);
  41.         if (twin == win) {
  42.             _ss_select(STDOUT, orgwin);
  43.         }
  44.     }
  45. }
  46.  
  47. main()
  48. {
  49.     int evID;                    /* event ID */
  50.  
  51.     sigmask(1);                    /* mask signals */
  52.     intercept(sighandler);
  53.  
  54.     if ((evID = _ev_link(EV_NAME)) == -1)
  55.         exit(1);
  56.  
  57.     DWSet(STDOUT, 0, 0, 0, 80, 26, 0, 0, 1);
  58.  
  59.     if (_gs_currscr(STDOUT,&orgwin) == -1)
  60.         exit(0);
  61.  
  62.     CurOff(STDOUT);
  63.     BColor(STDOUT, 0);
  64.     _gs_wdev(STDOUT, &win);        /* get our window device number */
  65.     _ss_select(STDOUT, win);    /* now select it! */
  66.  
  67.     init_saver();
  68.  
  69.     sigmask(0);                    /* unmask signals */
  70.     _ev_set(evID, 0, 0x8000);    /* wake up everybody */
  71.  
  72.     /* we unlink from the event since we are through with it */
  73.     _ev_unlink(evID);
  74.     
  75.     while (!sigcode)
  76.     {
  77.         if (gotosleep)
  78.             sleep(0);
  79.         else
  80.             do_lines();
  81.     }
  82. }
  83.  
  84. init_saver()
  85. {
  86.     /* # of colors available on screen */
  87.     maxcolors = Get_Num_Colors(STDOUT);
  88.  
  89.     /* line starting coordinates */
  90.     x1 = 50;
  91.     x2 = 400;
  92.     y1 = 100;
  93.     y2 = 200;
  94.  
  95.     /* line movement variables */
  96.     dx1 = 2;
  97.     dx2 = -3;
  98.     dy1 = -1;
  99.     dy2 = 2;
  100.  
  101.     /* multipliers */
  102.     mx1 = 1;
  103.     my1 = 1;
  104.     mx2 = 3;
  105.     my2 = 2;
  106.  
  107.     counter = 0;
  108.     color = 1;
  109. }
  110.  
  111. do_lines()
  112. {    
  113.     FColor(STDOUT,color);    /* set line color */
  114.     SetDPtr(STDOUT,x1,y1);    /* start coordinate */
  115.     Line(STDOUT,x2,y2);        /* draw the line */
  116.         
  117.     /* next, move the line according to directionals */
  118.     x1 += dx1 * mx1;
  119.     x2 += dx2 * mx2;
  120.     y1 += dy1 * my1;
  121.     y2 += dy2 * my2;
  122.         
  123.     /* now, check if any directionals need to be reversed */
  124.     if (x1 < 0 || x1 > 639)
  125.     {
  126.         x1 -= dx1 * mx1;
  127.         dx1 =- dx1;
  128.         if (++mx1 > 5)
  129.             mx1 = 1;
  130.     }
  131.     if (x2 < 0 || x2 > 639)
  132.     {
  133.         x2 -= dx2 * mx2;
  134.         dx2 =- dx2;
  135.         if (++mx2 > 5)
  136.             mx2 = 1;
  137.     }
  138.     if (y1 < 0 || y1 > 207)
  139.     {
  140.         y1 -= dy1 * my1;
  141.         dy1 =- dy1;
  142.         if (++my1 > 5)
  143.             my1 = 1;
  144.     }
  145.     if (y2 < 0 || y2 > 207)
  146.     {
  147.         y2 -= dy2 * my2;
  148.         dy2 =- dy2;
  149.         if (++my2 > 5)
  150.             my2 = 1;
  151.     }
  152.     
  153.     if (++counter > 500)    /* after 500 lines, clear the screen */
  154.     {
  155.         Clear(STDOUT);
  156.         counter = 0;
  157.     }
  158.         
  159.     if (++color >= maxcolors)
  160.         color = 1;    /* select next color */
  161.  
  162.     tsleep(2);
  163. }
  164.  
  165.