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 >
Wrap
C/C++ Source or Header
|
1995-11-04
|
4KB
|
175 lines
/* ssaver_stars.c */
/* A screen-saver program for use with SSaver. */
/* Interface to K-Windows/SSaver by Boisy G. Pitre */
/* Star Field Simulation by Blair Leduc */
/* Define if your machine isn't that fast */
/*#define DONT_PAUSE*/
#include <modes.h>
#include <types.h>
#include "saver.h"
int orgwin, win, twin;
int sighandler(); /* this will use a signal handler function */
int sigcode = 0; /* storage to keep received signal(s) */
int gotosleep = FALSE;
unsigned char *scrad=(unsigned char *)0;
extern int errno;
void draw_stars();
void init_stars();
main()
{
int i;
int evID; /* event ID */
sigmask(1); /* mask signals */
intercept(sighandler); /* install signal handler */
if ((evID = _ev_link(EV_NAME)) == -1)
exit(1);
/* WARNING: If you chang the screen type, adjust the */
/* screen dimension defines below */
DWSet(STDOUT, 8, 0, 0, 48, 30, 0, 255, 255);
if (_gs_currscr(STDOUT,&orgwin) == -1)
exit(0);
CurOff(STDOUT);
BColor(STDOUT, 255);
Border(STDOUT, 255);
Clear(STDOUT);
for (i=0; i<256; i++) {
Palette(STDOUT, i, 255-i, 255-i, 255-i);
}
init_stars();
_gs_wdev(STDOUT, &win); /* get our window device number */
_ss_select(STDOUT, win); /* now select it! */
sigmask(0); /* unmask signals */
_ev_set(evID, 0, 0x8000); /* wake up everybody */
/* we unlink from the event since we are through with it */
_ev_unlink(evID);
scrad=(unsigned char *)_gs_scadd(STDOUT);
while(!sigcode)
{
if (gotosleep)
sleep(0);
else
/* Do our thing */
draw_stars();
#ifndef DONT_PAUSE
tsleep(5);
#endif
}
}
sighandler(signal)
int signal;
{
switch (signal) {
case SLEEP_SIG:
gotosleep = TRUE;
break;
case WAKE_SIG:
gotosleep = FALSE;
break;
case QUIT_SIG:
default:
gotosleep = FALSE;
sigcode=signal;
if (_gs_currscr(STDOUT,&twin) == -1)
exit(0);
if (twin == win) {
_ss_select(STDOUT, orgwin);
}
}
}
/*
Star field program -- written by Blair Leduc
*/
#include <time.h>
void init_stars();
void draw_stars();
unsigned int rand();
/* Screen dimensions */
#define HEIGHT 240
#define HEIGHT2 120
#define WIDTH 384
#define WIDTH2 192
/* Star parameters */
#define MAXZPOS 4096
#define MINZPOS 2
#define MAXSTARS 300
#define WARPSPEED 60
/* Randomization */
#define RAND() (rand()%4096-2047) /* Pick a number in -2047 to 2048 */
#define ZRAND() (rand()%MAXZPOS+1) /* Don't pick zero (very bad) */
typedef struct {
int x, y, z; /* Star's position in 3D */
unsigned char *pos; /* Star's position on the 2D screen */
} STAR;
STAR Stars[MAXSTARS];
void init_stars()
{
int i, x, y, z;
srand(time(0));
for (i=0; i<MAXSTARS; i++) {
Stars[i].pos=0;
do {
Stars[i].x=RAND();
Stars[i].y=RAND();
Stars[i].z=ZRAND();
x=(Stars[i].x<<8)/Stars[i].z+WIDTH2;
y=(Stars[i].y<<8)/Stars[i].z+HEIGHT2;
} while (x<0 || y<0 || x>=WIDTH || y>=HEIGHT || Stars[i].z<MINZPOS);
}
}
void draw_stars()
{
int i,x,y;
for(i=0; i<MAXSTARS; i++) {
if (Stars[i].pos)
*(Stars[i].pos)=255;
x=(Stars[i].x<<8)/Stars[i].z+WIDTH2;
y=(Stars[i].y<<8)/Stars[i].z+HEIGHT2;
if (x<0 || y<0 || x>=WIDTH || y>=HEIGHT || Stars[i].z<MINZPOS) {
Stars[i].z=MAXZPOS;
x=(Stars[i].x<<8)/Stars[i].z+WIDTH2;
y=(Stars[i].y<<8)/Stars[i].z+HEIGHT2;
}
*(Stars[i].pos=scrad+y*WIDTH+x)=(Stars[i].z>>4)-1;
Stars[i].z-=WARPSPEED; /* Move star forward and */
if (Stars[i].z<=0) /* don't let star hit zero */
Stars[i].z=1;
}
}