Paul-TextRain

by ppm @ 10:45 pm 11 January 2011

Text Rain clone à la openFrameworks:

The significant lines of code for Text Rain are:

void app::createLetter() {
    letter *l;
    // Index in textStr from which to take this letter
    int lPos;

    // Find first open position in text
    for(int i=0; ix = ofRandom(0.0, (float) screenWidth);

            lPos = l->x / ((float) screenWidth) * ((float) textStrLen);
            if(lPos < 0)
                lPos = 0;
            if(lPos >= textStrLen)
                lPos = textStrLen - 1;

            l->c = textStr[lPos];
            l->y = 0;
            l->vel = 0.5;
            text[i] = l;
            break;
        }
    }
}

void app::destLetter(int i) {
    free(text[i]);
    text[i] = (letter *) 0;
}

void app::setup(){
	camWidth  = 320;
	camHeight = 240;
	pixelMax = 3*camWidth*camHeight;

	vidGrabber.setVerbose(true);
	vidGrabber.initGrabber(camWidth,camHeight);

	textStrLen = strlen(textStr);

	// Create text
    textMax = 1000;
	text = new letter* [textMax];
    for(int i=0; iy > yMax) {
                destLetter(i);
            } else {
                // Move this letter down at its own speed
                l->y += l->vel;

                // Move this letter up as long as it is on an obstacle
                for(;;) {
                    pixel = 3 * ( ((int)l->y)*camWidth + ((int)l->x) );
                    if(pixel >= 0 && pixel < pixelMax &&
                       img[pixel] + img[pixel+1] + img[pixel+2] < obShade) {
                        l->y -= 1.0;
                    } else {
                        break;
                    }
                }
            }
        }
    }

    // Create a new letter every 60 frames
    if( !(ofGetFrameNum()%60) ) {
        createLetter();
    }
}

void app::draw(){
    letter *l;
    char str[2] = "a";

	ofSetColor(0xffffff);
	vidGrabber.draw(0,0);

	ofSetColor(255,255,255);

	// Draw all letters
    for(int i=0; ic;
            ofDrawBitmapString(str, (int) l->x, (int) l->y);
        }
    }
}

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2022 Interactive Art & Computational Design / Spring 2011 | powered by WordPress with Barecity