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_plasma.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-11-02
|
7KB
|
340 lines
/* saver_plasma.c */
/* A screen-saver program for use with SSaver, */
/* by Mike Haaland, James Jones */
/* This program works on a 256-color screen */
#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;
extern int errno;
void rotate_colors();
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, 3, 0, 0, 40, 26, 0, 0, 0);
if (_gs_currscr(STDOUT,&orgwin) == -1)
exit(0);
CurOff(STDOUT);
BColor(STDOUT, 0);
_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);
init_plasma();
while(!sigcode)
{
if (gotosleep)
sleep(0);
else
/* Do our thing */
rotate_colors();
}
}
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);
}
}
}
#include <stdio.h>
#include <time.h>
/* If your library has no rand() function */
#define randomize() srand(time(0))
#define random(x) ( rand() % (x + 1) )
unsigned char *scrn_mem;
#define FINDSCRN scrn_mem = (unsigned char *)_gs_scadd(STDOUT)
#define abs(x) mabs(x)
void plot256();
void setMode();
void setVGApalette();
void set_color();
void initPalette();
void subdivide();
void rotate_colors();
void setVGAreg();
short readPixel();
short Xlow = 0, Ylow = 0, Xres = 319, Yres = 199, scale_factor = 1;
short interval = 24, sm_indx = 25;
unsigned char Pal_Array[256][3];
mabs(value)
int value;
{
return( (value < 0) ? -value : value);
}
init_plasma()
{
char *ptr, *strchr();
FINDSCRN;
initPalette();
randomize();
plot256(Xlow,Ylow,random(255) + 1);
plot256(Xres,Ylow,random(255) + 1);
plot256(Xres,Yres,random(255) + 1);
plot256(Xlow,Yres,random(255) + 1);
subdivide(Xlow,Ylow,Xres,Yres);
}
/* plot256() - Function to plot point to VGA 256 Color Screen */
void plot256(x, y, color)
int x, y, color;
{
unsigned char *scrn_byte;
scrn_byte = scrn_mem + (y * 320) + x;
*scrn_byte = (unsigned char)color & 0xff;
}
/* setVGAreg() = Function to set individual VGA color register */
void setVGAreg( reg_no, red, green, blue)
int reg_no, red, green, blue;
{
short r, g, b;
r = (red * 255) / 63;
g = (green * 255) / 63;
b = (blue * 255) / 63;
Palette(STDOUT,red,green,blue);
}
/* setVGApalette() = Function to set all 256 color registers */
void setVGApalette(buffer)
unsigned char *buffer;
{
unsigned char buf[768];
register short count;
for (count = 0; count < 768; count++ )
buf[count] = (*buffer++ * 255) / 63;
_ss_palette(STDOUT,0,buf,256);
}
/* subdivide() = Divides up a display section and fills with color. */
void subdivide(x1, y1, x2, y2)
int x1, y1, x2, y2;
{
short sm_indx;
short x, y, color;
if (sigcode)
return;
while (gotosleep)
sleep(0);
if ( ((x2 - x1) < 2) && ((y2 - y1) < 2) )
return;
x = (x1 + x2) >> 1;
y = (y1 + y2) >> 1;
set_color(x1,y1,x,y1,x2,y1);
set_color(x2,y1,x2,y,x2,y2);
set_color(x1,y2,x,y2,x2,y2);
set_color(x1,y1,x1,y,x1,y2);
color = (readPixel(x1,y1) + readPixel(x2,y1) + readPixel(x2,y2)
+ readPixel(x1,y2) + 2) >> 2; /* was >> 1 */
plot256 (x, y, color);
subdivide(x1,y1,x,y);
subdivide(x,y1,x2,y);
subdivide(x,y,x2,y2);
subdivide(x1,y,x,y2);
}
/* set_color() = Picks the new color for the pixel */
void set_color( xa, ya, x, y, xb, yb)
int xa, ya, x, y, xb, yb;
{
long color;
color = abs(xa - xb) + abs(ya - yb);
if (color == 0)
return;
color = ( (rand() % (color * 2)) - color);
color += (readPixel(xa,ya) + readPixel(xb,yb) + 1) >> 1;
if (color < 1)
color = 1;
if (color > 255)
color = 255;
if (readPixel(x,y) == 0)
plot256(x,y,color);
}
/* initPalette() = Sets the colors of the VGA Palette */
void initPalette()
{
short max_color = 63;
short sm_indx;
Pal_Array[0][0] = 0;
Pal_Array[0][1] = 0;
Pal_Array[0][2] = 0;
for (sm_indx=1; sm_indx<86; sm_indx++)
{
Pal_Array[sm_indx][0] = 0;
Pal_Array[sm_indx][1] = (sm_indx * max_color) / 85;
Pal_Array[sm_indx][2] = ((86 - sm_indx) * max_color) / 85;
Pal_Array[sm_indx+85][0] = (sm_indx * max_color) / 85;
Pal_Array[sm_indx+85][1] = ((86 - sm_indx) * max_color) / 85;
Pal_Array[sm_indx+85][2] = 0;
Pal_Array[sm_indx+170][0] = ((86 - sm_indx) * max_color) / 85;
Pal_Array[sm_indx+170][1] = 0;
Pal_Array[sm_indx+170][2] = (sm_indx * max_color) / 85;
}
setVGApalette(Pal_Array[0]);
}
/* readPixel = Read a Pixel from the Screen */
short readPixel( x, y)
int x, y;
{
unsigned char *scrn_byte;
scrn_byte = scrn_mem + (y * 320) + x;
return ((short) *scrn_byte);
}
/* rotateColors() = Rotates the VGA Colors */
void rotate_colors()
{
short i;
short old_red, old_grn, old_blu;
short new_red, new_grn, new_blu;
old_red = Pal_Array[255][0];
old_grn = Pal_Array[255][1];
old_blu = Pal_Array[255][2];
new_red = Pal_Array[1][0];
new_grn = Pal_Array[1][1];
new_blu = Pal_Array[1][2];
for (i=1; i<255; i++)
{
Pal_Array[i][0] = Pal_Array[i+1][0];
Pal_Array[i][1] = Pal_Array[i+1][1];
Pal_Array[i][2] = Pal_Array[i+1][2];
}
Pal_Array[255][0] = new_red;
Pal_Array[255][1] = new_grn;
Pal_Array[255][2] = new_blu;
setVGApalette(Pal_Array[0]);
tsleep(2);
}
#if 0
/* rotateColors() = Rotates the VGA Colors and changed the hue */
void do_rotate_colors()
{
short i, j, k;
short old_red, old_grn, old_blu;
short new_red, new_grn, new_blu;
short last_step=32;
char ch;
while (!sigcode)
{
old_red = Pal_Array[255][0];
old_grn = Pal_Array[255][1];
old_blu = Pal_Array[255][2];
new_red = rand() % 63;
new_grn = rand() % 63;
new_blu = rand() % 63;
for (j=1; j<last_step; j++)
{
for (i=1; i<255; i++)
{
Pal_Array[i][0] = Pal_Array[i+1][0];
Pal_Array[i][1] = Pal_Array[i+1][1];
Pal_Array[i][2] = Pal_Array[i+1][2];
}
Pal_Array[255][0] = old_red + ((new_red-old_red)*j)/last_step;
Pal_Array[255][1] = old_grn + ((new_grn-old_grn)*j)/last_step;
Pal_Array[255][2] = old_blu + ((new_blu-old_blu)*j)/last_step;
setVGApalette(Pal_Array[0]);
}
}
}
#endif
static long seed = 1L;
int rand()
{
seed = (1103515245L * seed + 12345) & 0xFFFFFFFF;
return((short) (seed & 077777));
}
int srand (new_seed)
long new_seed;
{
seed = new_seed & 0xFFFFFFFF;
}