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 >
Wrap
C/C++ Source or Header
|
1995-11-02
|
4KB
|
236 lines
/* saver_rain.c */
/* A screen-saver program for use with SSaver, */
/* by Boisy G. Pitre */
/* This program works on a 256-color screen */
#include <stdio.h>
#include <modes.h>
#include <types.h>
#include <wind.h>
#include "saver.h"
int orgwin;
int win;
int twin;
int pid;
int lines_to_copy;
int bytes_to_copy;
char pal[768];
int gotosleep = FALSE;
int sigcode = 0; /* storage to keep received signal(s) */
int sighandler(); /* this will use a signal handler function */
extern char *_gs_scadd();
extern int errno;
main()
{
int evID; /* event ID */
sigmask(1); /* mask signals */
intercept(sighandler); /* install signal handler */
if ((evID = _ev_link(EV_NAME)) == -1)
exit(1);
DWSet(STDOUT, 0xFF, 0, 0, 1, 1, 0, 0, 0);
CurOff(STDOUT);
BackWin(STDOUT);
if (_gs_currscr(STDOUT,&orgwin) == -1) {
_ev_set(evID, 0, 0x8000); /* wake up everybody */
/* we unlink from the event since we are through with it */
_ev_unlink(evID);
exit(0);
}
copy_screen(orgwin);
init_saver();
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);
while(!sigcode)
{
if (gotosleep)
sleep(0);
else
rain();
}
cleanup();
}
copy_screen(devnum)
int devnum;
{
SCINFO sc;
char devname[32];
int devpath;
int i;
int bytes_per_scanline, total_scan_lines;
int max_x_pixel_coord, bits_per_pixel;
int max_colors, pixels_per_byte;
char *scrn, *this_scrn;
if (devnum == 0)
sprintf(devname,"/term");
else
sprintf(devname,"/w%d",devnum);
/* get info on the parent device screen */
_gs_scinfo(STDOUT,&sc);
if ((devpath = open(devname,1)) == -1)
exit (errno);
if (_gs_palette(devpath,0,pal,256) == -1)
exit (errno);
close (devpath);
/* Now, let's get the information... */
scrn = sc.sd_address;
bytes_per_scanline = sc.sd_phy_width;
total_scan_lines = sc.sd_max_y;
max_colors = 1 << sc.sd_bitsperpixel;
_ss_select(STDOUT, devnum); /* now select it! */
if (max_colors == 16)
{
DWEnd(STDOUT);
DWSet(STDOUT, 0, 0, 0, 80, 26, 0, 0, 0);
}
else
{
DWEnd(STDOUT);
DWSet(STDOUT, 3, 0, 0, 40, 26, 0, 0, 0);
}
CurOff(STDOUT);
BColor(STDOUT, 0);
ScaleSw(STDOUT,1);
this_scrn = _gs_scadd(STDOUT);
lines_to_copy = (total_scan_lines > 208) ? 208 : total_scan_lines;
bytes_to_copy = (sc.sd_phy_width > 320) ? 320 : sc.sd_phy_width;
/* warning BIG-TIME CHEAT!!! */
for (i = 0; i <= lines_to_copy; i++)
{
memcpy(&this_scrn[i * 320],&scrn[i * bytes_per_scanline],bytes_to_copy);
}
_ss_palette(STDOUT,0,pal,256);
_gs_wdev(STDOUT, &win); /* get our window device number */
_ss_select(STDOUT, win); /* now select it! */
}
init_saver()
{
pid = getpid(); /* get process-id for unique group value */
rrand(-time(0));
/* in case someone left buffer hanging around in our group */
KilBuf(STDOUT,pid,0);
/* Init out 2x(y) buffer */
GetBlk(STDOUT,pid,1,0,0,2,lines_to_copy - 1);
/* init the Draw pointer */
SetDPtr(STDOUT,0,0);
}
rain()
{
int i, x;
/* move some lines down */
for (i = 0; i < bytes_to_copy/24; i++)
{
/* get a random x value */
x = rrand(bytes_to_copy) - 1;
/* Get 8-bits by Max_y */
GetBlk(STDOUT,pid,1,x << 1,0,2,lines_to_copy - 1);
/* Put it down 1 scanline */
PutBlk(STDOUT,pid,1,x << 1,1);
/* Black out top pixels */
RPoint(STDOUT,x << 1,0);
RPoint(STDOUT,(x << 1) + 1,0);
}
/* Give us a little rest ;-) */
tsleep(2);
}
cleanup()
{
KilBuf(STDOUT,pid,0);
}
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);
}
}
}
static int rndval=10000;
/* rrand( val ) : return a pseudo-random value between 1 and val.
** IF val is -1, it is used as a seed.
** IF val is negative the better the seed.
*/
rrand( val )
int val;
{
int bit0, bit1, bit14, num, i;
/* see if -1 */
if ( val < 0 ) {
rndval = -val;
}
for ( i = 0; i < 10; i++ )
{
bit0 = rndval & 1;
bit1 = (rndval & 2) >> 1;
bit14 = bit0 ^ bit1;
rndval >>=1;
rndval |= (bit14 << 14);
}
num = rndval % val;
return( num ? num : val );
}