How to ensure variables initialized from .h file are being used properly


#1

I am working on a text rain project. I have seen an example but for processing and I am currently trying to translate it for OF usage.

Problem: I am ensure as why certain variables are being marked as undefined when they were added in the ofApp.h file. Could it be that I have added a class inside the ofApp.cpp?

This is my ofApp.cpp

#include "ofApp.h"
#include <string>

//--------------------------------------------------------------
void ofApp::setup() {
    
    vidGrabber.initGrabber(1280, 720);
    
//    video = new Capture (this, width,height);
//    video.start();
    
    string poemString = "A poem about bodies";
    
    nLetters = poemString.length();
    poemLetters = new TextRainLetter[nLetters];
    for (int i=0; i<nLetters; i++) {
        char c = poemString.at(i);
        float x = ofGetWidth() * ((float)(i+1)/(nLetters+1));
        float y = initialLetterYPosition;
        poemLetters[i] = new TextRainLetter(c,x,y);
    }
}

class TextRainLetter {
    
    char  c;
    float x;
    float y;
public:
    TextRainLetter (char cc, float xx, float yy) {
        c = cc;
        x = xx;
        y = yy;
    }
    
    //-----------------------------------
    void update() {
   
        int flippedX = (int)(ofGetWidth() - 1 - x);
        int index = ofGetWidth() *(int)y + flippedX;
        index = ofClamp(index, 0, ofGetWidth() * ofGetHeight() - 1);
      
        int thresholdTolerance = 5;
        int thresholdLo = brightnessThreshold - thresholdTolerance;
        int thresholdHi = brightnessThreshold + thresholdTolerance;
        
        float pixelBrightness = vidGrabber.getPixels().getColor(index).getBrightness();
        
        if (pixelBrightness > thresholdHi) {
            y += letterGravity; //move downward
            
        } else {
            while ((y > initialLetterYPosition) && (pixelBrightness < thresholdLo)){
                y -= letterGravity;
                index = ofGetWidth() *(int)y + flippedX;
                index = ofClamp(index, 0, ofGetWidth() * ofGetHeight() - 1);
                pixelBrightness = vidGrabber.getBrightness(index);
            }
        }
        
        if ((y >= ofGetHeight()-1) || (y < initialLetterYPosition)){
            y = initialLetterYPosition;
        }
    }
    
    //-----------------------------------
    void reset(){
        y = initialLetterYPosition;
    }
    
    //-----------------------------------
    void draw() {
//        // Draw the letter. Use a simple black "drop shadow"
//        // to achieve improved contrast for the typography.
//        fill(0,0,0);
//        text (""+c, x+1,y+1);
//        text (""+c, x-1,y+1);
//        text (""+c, x+1,y-1);
//        text (""+c, x-1,y-1);
//        fill(255,255,255);
//        text (""+c, x,y);
    }
};


//--------------------------------------------------------------
void ofApp::update(){
    vidGrabber.update();
}

//--------------------------------------------------------------
void ofApp::draw(){
    if (video.available()) {
        video.read();
        video.loadPixels();
        
        // this translate & scale flips the video left/right.
        pushMatrix();
        translate (width,0);
        scale (-1,1);
        image (video, 0, 0, width, height);
        popMatrix();
        
        for (int i=0; i<nLetters; i++) {
            poemLetters[i].update();
            poemLetters[i].draw();
        }
    }
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    if (key == OF_KEY_UP) {
        brightnessThreshold = min(255, brightnessThreshold + 1);
        cout << "Brightness Threshold:" << brightnessThreshold << endl;
    } else if (key == OF_KEY_DOWN) {
        brightnessThreshold = max(0, brightnessThreshold - 1);
        cout << "Brightness Threshold:" << brightnessThreshold << endl;
    } else if (key == 49){
        for (int i = 0; i < nLetters; i++) {
            poemLetters[i].reset();
        }
    }
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){

}

//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}

This is my ofApp.h file

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
    
    ofTrueTypeFont myFont;
    ofVideoGrabber vidGrabber;
    int ySpeed,yLoc,xLoc;

    
    float letterGravity = 1.0;
    int brightnessThreshold = 100;
    float initialLetterYPosition = 10;
    TextRainLetter poemLetters[];
    int nLetters;
		
};

Many Thanks!


#2

Hello. Variable defined in the App.h file are always accessible in the ofApp.cpp file, in setup, update, draw and all the other methods. In your case, which variable is causing you troubles?


#3

Although I do initialized them in my ofApp.h file the following im not sure why its being marked as undefined: TextRainLetter, vidGrabber, letterGravity, initialLetterYPosition, and brightnessThreshold.


#4

This variable was initialized in ofApp.h. The class “TextRainLetter” does not know nothing about it. I would suggest you to read these 2 chapters of the ofBook, https://openframeworks.cc/ofBook/chapters/cplusplus_basics.html and https://openframeworks.cc/ofBook/chapters/OOPs!.html, they give you a clear overview of classes in OF.