Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Problem iterating through vector of Polylines

$
0
0

@George_Still wrote:

hi all! i’m having some trouble iterating through a vector of Polylines.

here’s my header code:

#pragma once

#include "ofMain.h"
#include "ofxBox2d.h"
#include "ofxTwitter.h"

class ofApp : public ofBaseApp{

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

    void onStatus(const ofxTwitter::Status& status);
    void onError(const ofxTwitter::Error& error);
    void onException(const std::exception& exception);
    void onMessage(const ofJson& json);
    
    ofTrueTypeFont myFont;
    
    ofxBox2d box2d;
    vector <shared_ptr<ofxBox2dPolygon>>  polyShapes;
    vector <vector<ofPolyline>> polylines;
    vector<std::string> tweetLetters;
    
    ofxTwitter::SearchClient client;
    
    uint64_t count = 0;
    uint64_t countMissed = 0;
    
    std::string tweetText;
};

and here’s my implementation:

#include "ofApp.h"

//          Small tweets = bigger letters, slower fall
//          Big tweets = smaller letters, faster fall

//--------------------------------------------------------------
void ofApp::setup()
{
    myFont.load("Baskerville.ttc", 48, false, false, true);
    
    box2d.init();
    box2d.setGravity(0, 1);
    box2d.createGround();
    box2d.setFPS(60.0);
    box2d.registerGrabbing();
    
    twitterSetup();
    
    ofBackground(255);
    ofSetColor(0,255,255,150);
    ofFill();
}

//--------------------------------------------------------------
void ofApp::update()
{
    box2d.update();
    polylines.clear();
    tweetLetters.clear();
}

//--------------------------------------------------------------
void ofApp::draw()
{
    handleTweets();
    
    vector <ofPath> lettersInTweet = myFont.getStringAsPoints(tweetText);
    
    for(auto it = lettersInTweet.begin(); it != lettersInTweet.end(); ++it)
    {
        polylines.push_back(it->getOutline());
    }

    for(auto it = polylines.begin(); it != polylines.end(); ++it)
    {
        auto line = *it->begin();
        auto poly = std::make_shared<ofxBox2dPolygon>();
        
        poly->addVertices(line.getVertices());
        poly->setPhysics(1.0, 0.1, 0.3);
        poly->triangulatePoly();
        poly->create(box2d.getWorld());
        
        polyShapes.push_back(poly);
    }
    
    for(int i = 0; i < polyShapes.size(); i++)
    {
        polyShapes[i]->draw();
    }
}

void ofApp::twitterSetup()
{
    client.registerSearchEvents(this);
    client.setCredentialsFromFile("credentials.json");
    client.setPollingInterval(6000);
    ofxTwitter::SearchQuery query(":)");
    query.setLanguage("en");
    client.search(query);
}

void ofApp::onStatus(const ofxTwitter::Status& status)
{
    count++;
    tweetText = status.text();
}


void ofApp::onError(const ofxTwitter::Error& error)
{
    ofLogError("ofApp::onError") << "Error: " << error.code() << " " << error.message();
}


void ofApp::onException(const std::exception& notice)
{
    ofLogError("ofApp::onException") << "Exception: " << notice.what();
}


void ofApp::onMessage(const ofJson& json)
{
    // This is the raw message json and is ignored here.
}

void ofApp::handleTweets()
{
    auto total = count + countMissed;
    
    std::stringstream ss;
    ss << "  Received: " << count << std::endl;
    ss << "    Missed: " << countMissed << std::endl;
    ss << "     Total: " << total << std::endl;
    
    std::string received = "...";
    
    if (total > 0)
    {
        received = ofToString(static_cast<double>(count) / total * 100, 2);
    }
    
    ss << "% Received: " << received;
    
    ofDrawBitmapStringHighlight(ss.str(), 14, 14);
}

i get this error from the following line in my draw() loop:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)

        auto line = *it->begin();

i changed the code to this:

        auto line = it->begin();

but then got a different error when I ran it:

Thread 1: EXC_BAD_ACCESS (code=1, address=0xf9)

i’d really appreciate any ideas on how to fix this as i’m stumped. thanks for reading!

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles