Simple drawing application

grettings! So I had followed this forum post to get a basic drawing sketch working : http://forum.openframeworks.cc/t/drawing-application-/12788/0

I am incorporating the intro sketch (below) but am having an issue working with the ofPolyline class/objects. The sketch as it stands connects the points to the last drawn points of the line. I am trying to establish a sketch that has a completely new line (ending the connection between original line drawn) wherever the mouse is pressed.

code below:

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
  
    ofBackground(0,0,0);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
    line.draw();  
      
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
  
    // first, make a point  
    ofPoint pt;  
    pt.set(x,y);  
      
    // then add it to the line.   
    line.addVertex(pt);  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
}  

I’d do the following:

Create a vector of ofPolyline objects.
Each time the mouse is pressed, add a new ofPolyline object to the vector.
Within mouseDragged, add new points to the last object in the vector (the newly created polyline).
Then loop through your vector to draw each polyline separately.

Hey James,

thanks for the reply. I wanted to thumb through the documentation/test on my own but I am at a loss on how to begin. Would you be able to show a quick sketch illustrating this?

Sure have a look at the code below.

testApp.h

  
  
#pragma once  
  
#include "ofMain.h"  
  
class testApp : public ofBaseApp{  
public:  
    void setup();  
    void update();  
    void draw();  
      
    void mouseDragged(int x, int y, int button);  
    void mousePressed(int x, int y, int button);  
      
    vector<ofPolyline> polylines;  
};  
  
  

testApp.cpp

  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup()  
{  
    ofBackground(50);  
}  
  
//--------------------------------------------------------------  
void testApp::update()  
{  
      
}  
  
//--------------------------------------------------------------  
void testApp::draw()  
{  
    ofSetColor(255);  
    for (int i = 0; i < polylines.size(); i++)  
    {  
        polylines[i].draw();  
    }  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button)  
{  
    polylines[polylines.size() - 1].addVertex(ofPoint(x, y));  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button)  
{  
    ofPolyline polyline;  
    polylines.push_back(polyline);  
}  
  
  

I’d recommend looking into vectors:
http://www.cplusplus.com/reference/vector/vector/

It’s similar to an array but with a great deal of extra functionality.

Thanks for the source code, references! Very helpful. Will dig deeper into vectors and objects of the classes when I can.

Your example worked fine with the mouse gestures, but I seem to have run into a snag with my main intent, which was interfacing this with a TUIO addon (ofxTuio).

Here is the line that compiles, but then gives me a highlighted green error,

  
Thread 1: Program received signal: "EXC_BAD_ACCESS"  

  
void testApp::tuioUpdated(ofxTuioCursor &tuioCursor){  
	ofPoint loc = ofPoint(tuioCursor.getX()*ofGetWidth(),tuioCursor.getY()*ofGetHeight());  
	cout << "Point n" << tuioCursor.getSessionId() << " updated at " << loc << endl;  
      
    polylines[polylines.size() - 1].addVertex(loc );  
      
    ofPolyline polyline;  
    polylines.push_back(polyline);  
      
      
}  

i’ve been digging around in the API, but I think I am missing the mark on something, because I felt I am implementing it correctly;

http://www.tuio.org/?cpp