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_stars.c < prev    next >
C/C++ Source or Header  |  1995-11-04  |  4KB  |  175 lines

  1. /* ssaver_stars.c                                 */
  2. /* A screen-saver program for use with SSaver.    */
  3. /* Interface to K-Windows/SSaver by Boisy G. Pitre */
  4. /* Star Field Simulation by Blair Leduc           */
  5.  
  6. /* Define if your machine isn't that fast */
  7. /*#define DONT_PAUSE*/
  8.  
  9. #include <modes.h>
  10. #include <types.h>
  11.  
  12. #include "saver.h"
  13.  
  14. int orgwin, win, twin;
  15.  
  16. int sighandler();    /* this will use a signal handler function */
  17. int sigcode = 0;    /* storage to keep received signal(s) */
  18.  
  19. int gotosleep = FALSE;
  20. unsigned char *scrad=(unsigned char *)0;
  21.  
  22. extern int errno;
  23.  
  24. void draw_stars();
  25. void init_stars();
  26.  
  27. main()
  28. {
  29.   int i;
  30.   int evID;                    /* event ID */
  31.  
  32.   sigmask(1);                    /* mask signals */
  33.   
  34.   intercept(sighandler);        /* install signal handler */
  35.  
  36.   if ((evID = _ev_link(EV_NAME)) == -1)
  37.     exit(1);
  38.   
  39.   /* WARNING: If you chang the screen type, adjust the */
  40.   /* screen dimension defines below  */
  41.   DWSet(STDOUT, 8, 0, 0, 48, 30, 0, 255, 255);
  42.   if (_gs_currscr(STDOUT,&orgwin) == -1)
  43.     exit(0);
  44.  
  45.   CurOff(STDOUT);
  46.   BColor(STDOUT, 255);
  47.   Border(STDOUT, 255);
  48.   Clear(STDOUT);
  49.   for (i=0; i<256; i++) {
  50.     Palette(STDOUT, i, 255-i, 255-i, 255-i);
  51.   }
  52.  
  53.   init_stars();
  54.  
  55.   _gs_wdev(STDOUT, &win);                 /* get our window device number */
  56.   _ss_select(STDOUT, win);            /* now select it! */
  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.   scrad=(unsigned char *)_gs_scadd(STDOUT);
  65.  
  66.   while(!sigcode)
  67.   {
  68.     if (gotosleep)
  69.       sleep(0);
  70.     else
  71.       /* Do our thing */
  72.       draw_stars();
  73. #ifndef DONT_PAUSE
  74.       tsleep(5);
  75. #endif
  76.   }
  77. }
  78.  
  79. sighandler(signal)
  80. int signal;
  81. {
  82.     switch (signal) {
  83.     case SLEEP_SIG:
  84.         gotosleep = TRUE;
  85.         break;
  86.     case WAKE_SIG:
  87.         gotosleep = FALSE;
  88.         break;
  89.     case QUIT_SIG:
  90.     default:
  91.         gotosleep = FALSE;
  92.         sigcode=signal;
  93.         if (_gs_currscr(STDOUT,&twin) == -1)
  94.             exit(0);
  95.         if (twin == win) {
  96.             _ss_select(STDOUT, orgwin);
  97.         }
  98.     }
  99. }
  100.  
  101. /*
  102.  
  103.     Star field program -- written by Blair Leduc
  104.             
  105. */
  106.  
  107. #include <time.h>
  108.  
  109. void init_stars();
  110. void draw_stars();
  111. unsigned int rand();
  112.  
  113. /* Screen dimensions */
  114. #define HEIGHT 240
  115. #define HEIGHT2 120
  116. #define WIDTH 384
  117. #define WIDTH2 192
  118.  
  119. /* Star parameters */
  120. #define MAXZPOS 4096
  121. #define MINZPOS 2
  122. #define MAXSTARS 300
  123.  
  124. #define WARPSPEED 60
  125.  
  126. /* Randomization */
  127. #define RAND()   (rand()%4096-2047)     /* Pick a number in -2047 to 2048 */
  128. #define ZRAND()  (rand()%MAXZPOS+1)     /* Don't pick zero (very bad) */
  129.  
  130. typedef struct {
  131.   int x, y, z;                            /* Star's position in 3D */
  132.   unsigned char *pos;                   /* Star's position on the 2D screen */
  133. } STAR;
  134.  
  135. STAR Stars[MAXSTARS];
  136.  
  137. void init_stars()
  138. {
  139.   int i, x, y, z;
  140.  
  141.   srand(time(0));
  142.   for (i=0; i<MAXSTARS; i++) {
  143.     Stars[i].pos=0;
  144.     do {
  145.       Stars[i].x=RAND();
  146.       Stars[i].y=RAND();
  147.       Stars[i].z=ZRAND();
  148.       x=(Stars[i].x<<8)/Stars[i].z+WIDTH2;
  149.       y=(Stars[i].y<<8)/Stars[i].z+HEIGHT2;
  150.     } while (x<0 || y<0 || x>=WIDTH || y>=HEIGHT || Stars[i].z<MINZPOS);
  151.   }
  152. }
  153.  
  154.  
  155. void draw_stars()
  156. {
  157.   int i,x,y;
  158.   
  159.   for(i=0; i<MAXSTARS; i++) {
  160.     if (Stars[i].pos)
  161.       *(Stars[i].pos)=255;
  162.     x=(Stars[i].x<<8)/Stars[i].z+WIDTH2;
  163.     y=(Stars[i].y<<8)/Stars[i].z+HEIGHT2;
  164.     if (x<0 || y<0 || x>=WIDTH || y>=HEIGHT || Stars[i].z<MINZPOS) {
  165.       Stars[i].z=MAXZPOS;
  166.       x=(Stars[i].x<<8)/Stars[i].z+WIDTH2;
  167.       y=(Stars[i].y<<8)/Stars[i].z+HEIGHT2;
  168.     }
  169.     *(Stars[i].pos=scrad+y*WIDTH+x)=(Stars[i].z>>4)-1;
  170.     Stars[i].z-=WARPSPEED;  /* Move star forward and */
  171.     if (Stars[i].z<=0)      /* don't let star hit zero */
  172.       Stars[i].z=1;
  173.   }
  174. }
  175.