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_slider.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-17
|
7KB
|
368 lines
/* saver_slider.c - puzzle screensaver for ssaver */
#define MYRAND
#define srand(x) rrand(-time(0));
#include <stdio.h>
#include <modes.h>
#include <types.h>
#include <wind.h>
#include "saver.h"
int orgwin;
int win;
int twin;
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;
#define GRID_X 20
#define GRID_Y 13
int hole_x, hole_y;
int pid;
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
slider();
}
cleanup();
}
copy_screen(devnum)
int devnum;
{
SCINFO sc;
char devname[32];
int devpath;
int i;
int lines_to_copy, bytes_to_copy;
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()
{
int i, j;
pid = getpid(); /* get process-id for unique group value */
ScaleSw(STDOUT,1);
srand(0);
/* init the Draw pointer */
SetDPtr(STDOUT,0,0);
FColor(STDOUT,0);
/* Lets break up the screen */
for (i = 1; i < 21; i++)
{
SetDPtr(STDOUT,(i << 5) - 1,0);
RLine(STDOUT,0,207);
}
for (i = 1; i < 14; i++)
{
SetDPtr(STDOUT,0,(i << 4) - 1);
RLine(STDOUT,639,0);
}
/* reset the draw pointer */
SetDPtr(STDOUT,0,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,32,16);
/* pick our hole */
hole_x = rrand(GRID_X) - 1;
hole_y = rrand(GRID_Y) - 1;
#if 0
/* clear the hole */
SetDPtr(STDOUT,hole_x << 5,hole_y << 4);
RBar(STDOUT,31,15);
#endif
}
slider()
{
static int last = 0;
int i, j;
int way;
int dx, dy;
int w, h, x, y;
do {
way = rrand(4) - 1;
/* which way to move the hole */
switch(way) {
case 0: /* up */
dx = 0;
dy = -1;
break;
case 1: /* down */
dx = 0;
dy = 1;
break;
case 2: /* left */
dx = -1;
dy = 0;
break;
case 3: /* right */
dx = 1;
dy = 0;
break;
}
} while (way == last ||
hole_x + dx >= GRID_X || hole_x + dx < 0 ||
hole_y + dy >= GRID_Y || hole_y + dy < 0);
last = (way == 1 ? 0 : way == 0 ? 1 : way == 2 ? 3 : 2);
switch (way) {
case 0:
h = rrand(hole_y);
x = hole_x << 5;
y = hole_y << 4;
hole_y -= h;
for (j = 0; j < 16; j += 2)
{
for (i = 0; i < h; i++)
{
GetBlk(STDOUT,pid,1,x,y - ((i + 1) << 4) + j,32,16);
PutBlk(STDOUT,pid,1,x,(y - ((i + 1) << 4) + j) + 2);
}
SetDPtr(STDOUT,hole_x << 5,(hole_y << 4));
RBar(STDOUT,31,1+j);
tsleep(2);
if (sigcode)
break;
}
break;
case 1:
h = rrand(GRID_Y - hole_y - 1);
x = hole_x << 5;
y = hole_y << 4;
hole_y += h;
for (j = 0; j < 16; j += 2)
{
for (i = 0; i < h; i++)
{
GetBlk(STDOUT,pid,1,x,y + ((i + 1) << 4) - j,32,16);
PutBlk(STDOUT,pid,1,x,(y + ((i + 1) << 4) - j) - 2);
}
SetDPtr(STDOUT,hole_x << 5,(hole_y << 4) + (14 - j));
RBar(STDOUT,31,1+j);
tsleep(2);
if (sigcode)
break;
}
break;
case 2:
w = rrand(hole_x);
x = hole_x << 5;
y = hole_y << 4;
hole_x -= w;
for (j = 0; j < 32; j += 4)
{
for (i = 0; i < w; i++)
{
GetBlk(STDOUT,pid,1, x - ((i + 1) << 5) + j,y,32,16);
PutBlk(STDOUT,pid,1,(x - ((i + 1) << 5) + j) + 4,y);
}
SetDPtr(STDOUT,hole_x << 5,(hole_y << 4));
RBar(STDOUT,3+j,15);
tsleep(2);
if (sigcode)
break;
}
break;
case 3:
w = rrand(GRID_X - hole_x - 1);
x = hole_x << 5;
y = hole_y << 4;
hole_x += w;
for (j = 0; j < 32; j += 4)
{
for (i = 0; i < w; i++)
{
GetBlk(STDOUT,pid,1, x + ((i + 1) << 5) - j,y,32,16);
PutBlk(STDOUT,pid,1,(x + ((i + 1) << 5) - j) - 4,y);
}
SetDPtr(STDOUT,(hole_x << 5) + (28 - j),hole_y << 4);
RBar(STDOUT,3+j,15);
tsleep(2);
if (sigcode)
break;
}
break;
}
/* Give us a little rest ;-) */
if (!sigcode)
tsleep(0x80000100);
}
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);
}
}
}
#ifdef MYRAND
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 );
}
#endif