Skip to content

Instantly share code, notes, and snippets.

@tado
Last active May 20, 2017 23:47
Show Gist options
  • Save tado/0da9e35850a3d347379475d4976bcd0e to your computer and use it in GitHub Desktop.
Save tado/0da9e35850a3d347379475d4976bcd0e to your computer and use it in GitHub Desktop.
Quick, Draw! example for openFrameworks
#include "ofApp.h"
#include <fstream>
void ofApp::setup(){
ofBackground(255);
ofSetColor(0);
ofNoFill();
ofSetBackgroundAuto(false);
//select file
string inputFile = "face.ndjson";
ifstream input(ofToDataPath(inputFile), ifstream::in);
//read JSON data line by line
ofxJSONElement jsonElement;
Json::Reader reader;
string currentLine;
while(getline(input, currentLine)) {
reader.parse(currentLine, jsonElement);
drawDataJson.push_back(jsonElement);
}
cout << "Done : read " << drawDataJson.size() << " drawings!"<< endl;
objNum = int(ofRandom(drawDataJson.size()));
strokeNum = 0;
vertexNum = 0;
}
void ofApp::update(){
}
void ofApp::draw(){
ofSetLineWidth(2.0);
ofPushMatrix();
ofTranslate(ofGetWidth()/2-128, ofGetHeight()/2-128);
ofBeginShape();
for (int i = 0; i < vertexNum; i++) {
int x = drawDataJson[objNum]["drawing"][strokeNum][0][i].asInt();
int y = drawDataJson[objNum]["drawing"][strokeNum][1][i].asInt();
ofVertex(x, y);
}
ofEndShape();
ofPopMatrix();
vertexNum++;
if (vertexNum > drawDataJson[objNum]["drawing"][strokeNum][0].size()) {
vertexNum = 0;
strokeNum++;
if (strokeNum > drawDataJson[objNum]["drawing"].size()-1) {
vertexNum = 0;
strokeNum = 0;
objNum = int(ofRandom(drawDataJson.size()));
sleep(1);
ofBackground(255);
}
}
}
#pragma once
#include "ofMain.h"
#include "ofxJSON.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
vector<ofxJSONElement> drawDataJson;
int vertexNum;
int objNum;
int strokeNum;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment