Mail Archives: djgpp/1996/10/06/13:28:57
From: | "Mark Fleig" <markfleig AT worldnet DOT att DOT net>
|
Newsgroups: | comp.os.msdos.djgpp,rec.games.programmer
|
Subject: | Allegro plasma routine
|
Date: | 6 Oct 1996 04:52:23 GMT
|
Organization: | AT&T WorldNet Services
|
Lines: | 190
|
Message-ID: | <01bbb341$f99d43a0$adc192cf@apache.d51.lilly.com>
|
NNTP-Posting-Host: | 173.bridgeton-002.mo.dial-access.att.net
|
Mime-Version: | 1.0
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Here it is. Hope it works. (did for me anyway.)
-fleig
--
=============================================================
mark fleig
markfleig AT worldnet DOT att DOT net
mfleig AT juno DOT com
--------------------------------------- cut here
--------------------------------------------
/*
* plasma2.c: Allegro routine for fractal plasma clouds. (Oct.96)
*
* This is a port to Allegro of the Fractint plasma routine posted by
* Cory Bloyd. It uses the old divide and adjust routines, and accepts
* keyboard commands to change graininess and random seed instead
* of command line parameters. Enjoy. -mark fleig
*
* How to make the routine better:
* 1) Scrap putpixel() and getpixel() and use direct memory access.
* 2) Use the newer plasma routines. They're probably more efficient.
* 3) Knock the resolution up to 800x600. Looks great.
*/
/* Here is the original header block from Cory:
* plasma
* Random, cloud-like formations. Requires 4 or more colors.
* A recursive algorithm repeatedly subdivides the screen and
* colors pixels according to an average of surrounding pixels
* and a random color, less random as the grid size decreases.
* Three parameters: 'graininess' (.5 to 50, default = 2), old/new
* algorithm, seed value used.
*/
#include <stdlib.h>
#include <stdio.h>
#include "allegro.h"
static int iparmx; /* iparmx = parm.x * 16 */
static int shiftvalue; /* shift based on #colors */
static int pcolors;
/*-------------------------------------------
* Random adjustment of the current pixel box.
*-------------------------------------------*/
static int adjust(int xa,int ya,int x,int y,int xb,int yb)
{
long pseudorandom;
pseudorandom = ((long)iparmx)*((random() % 32766)-16383);
pseudorandom = pseudorandom*(abs(xa-xb)+abs(ya-yb));
pseudorandom = pseudorandom >> shiftvalue;
pseudorandom = ((getpixel(screen,xa,ya)+getpixel(screen,xb,yb)+1)>>1)+
pseudorandom;
if (pseudorandom < 1) pseudorandom = 1;
if (pseudorandom >= pcolors) pseudorandom = pcolors-1;
putpixel(screen, x, y, (int)pseudorandom);
return((int)pseudorandom);
}
/*-------------------------------------------
* Semi-recursive division of the current box.
* Keep subdividing until the size of the box
* is one pixel wide. Then drop out to the next
* iteration.
*-------------------------------------------*/
static void subDivide(int x1,int y1,int x2,int y2)
{
int x,y;
int v,i;
if(x2-x1<2 && y2-y1<2)
return;
if(keypressed())
return;
x = (x1+x2)>>1;
y = (y1+y2)>>1;
if((v=getpixel(screen,x,y1)) == 0)
v=adjust(x1,y1,x ,y1,x2,y1);
i=v;
if((v=getpixel(screen,x2,y)) == 0)
v=adjust(x2,y1,x2,y ,x2,y2);
i+=v;
if((v=getpixel(screen,x,y2)) == 0)
v=adjust(x1,y2,x ,y2,x2,y2);
i+=v;
if((v=getpixel(screen,x1,y)) == 0)
v=adjust(x1,y1,x1,y ,x1,y2);
i+=v;
if(getpixel(screen,x,y) == 0)
putpixel(screen,x,y,(i+2)>>2);
subDivide(x1,y1,x ,y);
subDivide(x ,y1,x2,y);
subDivide(x ,y ,x2,y2);
subDivide(x1,y ,x ,y2);
}
/*-------------------------------------------
* Main routine: Fill the pallete and place
* random color dots in each corner of the screen.
* This is the first box to begin subdivision. The
* subDivide() routine will begin with these four
* color points and average the center pixel, splitting
* the big box into four smaller boxes for further
* division. As the boxes get smaller, the amount of
* randomness is reduced to smooth color transitions.
*-------------------------------------------*/
void main()
{
PALLETE dacbox;
RGB Red = {
63, 0, 0 };
RGB Green = {
0,63, 0 };
RGB Blue = {
0, 0,63 };
int c,i;
int x, y;
int rseed;
iparmx = 16;
shiftvalue = 18;
rseed = 2;
allegro_init();
install_keyboard();
set_gfx_mode(GFX_VGA, 320, 200, 0, 0);
dacbox[0].r = 0 ;
dacbox[0].g= 0 ;
dacbox[0].b = 0 ;
for(i=1;i<=85;i++)
{
dacbox[i].r = (i*Green.r + (86-i)*Blue.r)/85;
dacbox[i].g = (i*Green.g + (86-i)*Blue.g)/85;
dacbox[i].b = (i*Green.b + (86-i)*Blue.b)/85;
dacbox[i+85].r = (i*Red.r + (86-i)*Green.r)/85;
dacbox[i+85].g = (i*Red.g + (86-i)*Green.g)/85;
dacbox[i+85].b = (i*Red.b + (86-i)*Green.b)/85;
dacbox[i+170].r = (i*Blue.r + (86-i)*Red.r)/85;
dacbox[i+170].g = (i*Blue.g + (86-i)*Red.g)/85;
dacbox[i+170].b = (i*Blue.b + (86-i)*Red.b)/85;
}
set_pallete(dacbox);
/*---------------------------------------------------------------------*/
clear_keybuf();
clear(screen);
while (!key[KEY_ESC]) {
if (key[KEY_LEFT]) {rseed--; clear(screen);}
if (key[KEY_RIGHT]) {rseed++; clear(screen);}
if (key[KEY_UP]) {iparmx += 1; clear(screen);}
if (key[KEY_DOWN]) {iparmx -= 1; clear(screen); }
if (!key[KEY_ESC]) clear_keybuf();
if (iparmx < 0) iparmx = 0;
srand(rseed);
pcolors = 255;
putpixel(screen,0,0,1+(((rand()/pcolors)
*(pcolors-1))>>(shiftvalue-11)));
putpixel(screen,SCREEN_W-1,0,1+(((rand()/pcolors)
*(pcolors-1))>>(shiftvalue-11)));
putpixel(screen,SCREEN_W-1,SCREEN_H-1,1+(((rand()/pcolors)
*(pcolors-1))>>(shiftvalue-11)));
putpixel(screen,0,SCREEN_H-1,1+(((rand()/pcolors)
*(pcolors-1))>>(shiftvalue-11)));
subDivide(0,0,SCREEN_W-1,SCREEN_H-1);
}
/*------------------------------------------------------------------*/
clear_keybuf();
exit(0);
}
--------------------------------------- cut here
--------------------------------------------
- Raw text -