Strange self replicating pattern when inputing text onscreen

I’m reposting this as a new topic because the problem is now completely different from my original post of “Falling text looping like crazy until crashed”.

My new problem is with the drawing of the letters, but instead there is a strange self-replicating pattern that is happening. It’s difficult to describe except to show it. One thing to note is that for every letter I press, the set before it pushes towards the left.

Also - I don’t know if this has to do with the strange replication, but only the text on the furthermost right triggers any sound when “touched”.

Something like this also happened with an earlier version of the code, but I wasn’t able to solve it and moved in a different direction. Now it’s come back to haunt me. Someone suggested it may be something in this version of oF. Has anyone encountered something like this or know of a possible resolve? It’s driving me kooky.

Thank you in advance!

  
#include "testApp.h"  
#include "stdio.h"  
#include <cstdlib>   
  
string str ="";  
int priorX=10;  
char letters[100];  
char theChars;  
int currLetter = 0;  
//boolean arrayFilled = false;  
int x[100];  
int y[100];  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	  
	  
	str =""; //initialize it as an empty string  
	  
	  
//-------------------------------load sound samples	  
	beatdistorted1.loadSound("sounds/beat distorted_house1.wav");  
	beatdistorted2.loadSound("sounds/beat distorted_house2.wav");  
	beatdistorted3.loadSound("sounds/beat distorted_house3.wav");  
	beatdistorted4.loadSound("sounds/beat distorted_house4.wav");  
	beatdistorted5.loadSound("sounds/beat distorted_house5.wav");  
	beatdistorted6.loadSound("sounds/beat distorted_house6.wav");  
	digital_dub1.loadSound("sounds/digital_dub1.wav");  
	digital_dub2.loadSound("sounds/digital_dub2.wav");  
	special_fx1.loadSound("sounds/special_fx1.wav");  
	  
	  
    ofSetFrameRate(30);  
    ofBackground(255,255,255); // draw on a white background  
	theFont.loadFont("Arial Black.ttf", 20,true,true,true); // Arial  
	ofSetLogLevel(OF_LOG_WARNING);  
	ofSetWindowShape(640,480);  
	ofSetFullscreen(false);  
	  
    vidGrabber.setVerbose(true);  
    vidGrabber.initGrabber(640,480);  
	  
	colorImg.allocate(640,480);  
	grayImage.allocate(640,480);  
	grayBg.allocate(640,480);  
	grayDiff.allocate(640,480);  
	bLearnBakground = true;  
	threshold = 80;  
}  
//--------------------------------------------------------------  
void testApp::update(){  
    bool bNewFrame = false;  
    vidGrabber.grabFrame();  
	bNewFrame = vidGrabber.isFrameNew();  
	  
	if (bNewFrame){  
        colorImg.setFromPixels(vidGrabber.getPixels(), 640,480);  
		colorImg.mirror(false,true);  
       grayImage = colorImg;  
		if (bLearnBakground == true){  
			grayBg = grayImage;  
			bLearnBakground = false;  
		}  
		// take the abs value of the difference between background and incoming and then threshold:  
		grayDiff.absDiff(grayBg, grayImage);  
		grayDiff.threshold(threshold);  
	}  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
	//for drawing beat colored letters  
	char tempStr[255];  
	  
//------------------draw letters	  
	  
	ofSetColor(0xffffff);  
	colorImg.draw(0,0);  
	//display runtime info for debugging  
	ofSetColor(0xff0000);  
	ofDrawBitmapString("fps: "+ofToString(ofGetFrameRate()), 10, 10);  
	ofDrawBitmapString("threshold: "+ofToString(threshold), 10, 20);  
	//advance the location of each character of the string  
	//int i;  
    //for (int i = 0; i<sizeof(letters); i++) {  
		int max;  
		if (arrayFilled) max = 50;  
		else max = currLetter;  
		for (int i=0; i < max; i++) {  
		  
			//New positions  
			x[i] = priorX;  
			  
			x[i]=i*640/max;  
			y[i] = y[i]+5;  
			if (y[i]>480) {  
				y[i]=0;  
			}  
		  
		//Draw the character, get its outline  
			// draw in green  
			ofFill();  
		  
			  
			ofDrawBitmapString(&letters[i], x[i], y[i]);  
			ofRectangle theRect = theFont.getStringBoundingBox(&letters[i],x[i],y[i]);  
			ofNoFill();  
			  
			// if colliding with something, backup you are not colliding.  
			// must be a better approach here that can still avoid jitter.  
			while (grayDiff.countNonZeroInRegion(theRect.x,theRect.y,theRect.width,theRect.height)>0) {  
				ofSetColor(255,0,0); //red  
				theRect.y = theRect.y -1;  
				y[i] = y[i] - 1;  
			  
			// play a sound  
				if (letters[i] == 'a'){ // do somehing  
					beatdistorted1.play();  
				} else if (letters[i] == 'b') {  
					beatdistorted2.play();  
				} else if (letters[i] == 'c') {  
					beatdistorted3.play();  
				} else if (letters[i] == 'd') {  
					beatdistorted4.play();  
				}  
				break;  
			  
			  
				if (y[i]<0) {  
					y[i]=0;  
					break;  
				}   
			}  
			  
	  
			if (theRect.width>10) {  
				priorX = priorX + theRect.width;  
			} else {  
				priorX = priorX + 10;  
				ofSetColor(0, 191, 255); //deep sky blue  
			}  
		}  
	// start drawing characters 10 in from the side  
		priorX = 10;  
  
}  
//--------------------------------------------------------------  
void testApp::keyPressed  (int key){  
  
	currLetter = currLetter+1;  
	letters[currLetter] = key;  
	//currLetter = currLetter+1;  
	if (currLetter >= 100) {  
		currLetter = 0;  
		arrayFilled = true;  
	}  
	  
	//----------------------------- threshold for background contrast  
	switch (key){  
		case ' ':  
			bLearnBakground = true;  
			break;  
		case '+':  
			threshold ++;  
			if (threshold > 255) threshold = 255;  
			break;  
		case '-':  
			threshold --;  
			if (threshold < 0) threshold = 0;  
			break;  
			  
	  
		//--------------map letters to sound samples  
  
			  
  
		case 'a': beatdistorted1.play();   
			break;  
		case 'b': beatdistorted2.play();   
			break;  
		case 'c': beatdistorted3.play();   
			break;  
		case 'd': beatdistorted4.play();   
			break;  
		case 'e': beatdistorted5.play();   
			break;  
		case 'f': beatdistorted6.play();   
			break;  
		case 'g': digital_dub1.play();   
			break;  
		case 'h': digital_dub2.play();   
			break;  
		case 'i': special_fx1.play();   
			break;  
  
	  
	}  
}  
  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){  
}  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
}  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
}  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
}  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){  
}  
//--------------------------------------------------------------  
void testApp::windowResized(int w, int h){  
}