Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Dad1916 on October 09, 2006

Title: Looking for White noise / tv static algorithms
Post by: Dad1916 on October 09, 2006
Wondering if anyone has any pseudo code or theories for coding white noise aka. tv static.
- Black background.
- random White/grey pixels?
Title: Re: Looking for White noise / tv static algorithms
Post by: benny! on October 09, 2006
I only know the famous tiny ptc test code by gaffer.

//
// TinyPTC by Gaffer
// www.gaffer.org/tinyptc
//

#include "tinyptc.h"

#define WIDTH 320
#define HEIGHT 200
#define SIZE WIDTH*HEIGHT
   
static int noise;
static int carry;
static int index;
static int seed = 0x12345;
static int pixel[SIZE];

int main()
{
    if (!ptc_open("test",WIDTH,HEIGHT)) return 1;
    while (1)
    {
        for (index=0; index<SIZE; index++)
        {
            noise = seed;
            noise >>= 3;
            noise ^= seed;
            carry = noise & 1;
            noise >>= 1;
            seed >>= 1;
            seed |= (carry << 30);
            noise &= 0xFF;
            pixel[index] = (noise<<16) | (noise<<8) | noise;
        }
        ptc_update(pixel);
    }
}


And btw. If you want this fx as a screensaver you can dl it from my page :

http://www.weltenkonstrukteur.de/?site=works&prd=TVNoise
Title: Re: Looking for White noise / tv static algorithms
Post by: Shockwave on October 09, 2006
I've found that the effect works best with only a few different colours, say 3 greys and black.
Title: Re: Looking for White noise / tv static algorithms
Post by: Dad1916 on October 09, 2006
Thanks for the feedback. I'm try to convert the code above to BlitzMax but am stuck on 1 or two lines:
            noise >>= 3;

            noise ^= seed;


What is >>= and ^= ? Is this c++ ?
Title: Re: Looking for White noise / tv static algorithms
Post by: Jim on October 09, 2006
It's C.

>> is Shr
^ is Xor

So
noise>>=3 is
noise = noise Shr 3
and
noise^=seed is
noise=noise Xor seed

Basically though what's being done here is a random number generator.
Both Blitz and C have one built in so it's not really necessary to write your own.
In Blitz use
noise=Rand(255)
in C use
#include <stdio.h>
noise=rand()&0xff
and in freebasic do
#include "crt.bi"
noise=rand() and &hff

You might want to make sure it's totally random too.  You need to seed the random number generator.
In the sample posted that means change 'seed' to be something else.
In Blitz you do
SeedRnd MilliSecs()
In C you do
#include <time.h>
srand(time(NULL));

Jim