diff options
| -rw-r--r-- | json-instagram/config.make | 4 | ||||
| -rw-r--r-- | json-instagram/src/exampleApp.cpp | 47 | ||||
| -rw-r--r-- | json-instagram/src/exampleApp.h | 118 | ||||
| -rwxr-xr-x | maker | 2 | ||||
| -rw-r--r-- | offsetProject/src/main.cpp | 2 | ||||
| -rw-r--r-- | offsetProject/src/ofApp.cpp | 6 | ||||
| -rw-r--r-- | offsetProject/src/ofApp.h | 2 | ||||
| -rw-r--r-- | test_poco_http.c | 2 |
8 files changed, 153 insertions, 30 deletions
diff --git a/json-instagram/config.make b/json-instagram/config.make index 93c489d..d5a6b93 100644 --- a/json-instagram/config.make +++ b/json-instagram/config.make @@ -78,6 +78,8 @@ ################################################################################ # PROJECT_LDFLAGS=-Wl,-rpath=./libs +PROJECT_LDFLAGS= -ljsoncpp + ################################################################################ # PROJECT DEFINES # Create a space-delimited list of DEFINES. The list will be converted into @@ -104,7 +106,7 @@ # # Note: Leave a leading space when adding list items with the += operator ################################################################################ -# PROJECT_CFLAGS = + PROJECT_CFLAGS = `pkg-config --cflags jsoncpp` ################################################################################ # PROJECT OPTIMIZATION CFLAGS diff --git a/json-instagram/src/exampleApp.cpp b/json-instagram/src/exampleApp.cpp index 0559dce..deb4f35 100644 --- a/json-instagram/src/exampleApp.cpp +++ b/json-instagram/src/exampleApp.cpp @@ -14,34 +14,30 @@ could save with the tag id as the name of file, simpler how exactly do we parse or mipmap the images do we worry about memory - I guess not +identify the images + */ +long ofToLong(const string& intString) { + long x = 0; + istringstream cur(intString); + cur >> x; + return x; +} + //------------------------------------------------------------------------------ void exampleApp::setup() { ofSetFrameRate(24); - - std::string url = "https://api.instagram.com/v1/tags/tycleeson/media/recent?client_id=c1d74d701fdf4ddd9f8d30ee9e8f944b"; - - if (!response.open(url)) { - cout << "Failed to parse JSON\n" << endl; - } - //else cout<<response.getRawString()<<endl; - - - int numImages = MIN(5,response["data"].size()); - for(int i=0; i< numImages; i++) { - std::string url = response["data"][i]["images"]["standard_resolution"]["url"].asString(); - std::string id = response["data"][i]["caption"]["id"].asString(); - cout<<"fetching "<<id<<":"<<url<<endl; - - - ofImage img; - img.loadImage(url); - images.push_back( img ); - } - - + loader.set_interval(5.00f); + cout << "starting" << endl; + loader.start(); + cout << "started" << endl; +} + +void exampleApp::update (){ + + ofSetWindowTitle(ofToString(ofGetFrameRate())); } //------------------------------------------------------------------------------ @@ -49,7 +45,8 @@ void exampleApp::draw() { ofBackground(0); - for(int i=0; i<images.size(); i++) { - images[i].draw(i*30, i*30); - } + //for(int i=0; i<images.size(); i++) { + // images[i].draw(i*30, i*30); + //} + loader.draw(); } diff --git a/json-instagram/src/exampleApp.h b/json-instagram/src/exampleApp.h index 1dcb032..021e241 100644 --- a/json-instagram/src/exampleApp.h +++ b/json-instagram/src/exampleApp.h @@ -4,12 +4,126 @@ #include "ofMain.h" #include "ofxJSONElement.h" +long ofToLong(const string& intString); + +class instagramLoader : public ofThread{ + + public: + + float interval; //time between refreshes in seconds + + int count; // threaded fucntions that share data need to use lock (mutex) + // and unlock in order to write to that data + // otherwise it's possible to get crashes. + // + // also no opengl specific stuff will work in a thread... + // threads can't create textures, or draw stuff on the screen + // since opengl is single thread safe + + //-------------------------- + std::string url; + ofxJSONElement response; + std::map<std::string,ofImage> images; + deque<std::string> to_update; + + instagramLoader(){ + count = 0; + url = "https://api.instagram.com/v1/tags/tycleeson/media/recent?client_id=c1d74d701fdf4ddd9f8d30ee9e8f944b"; + } + + void set_interval(float _interval){ + interval=_interval; + } + + void start(){ + startThread(true, false); // blocking, verbose + } + + void stop(){ + stopThread(); + } + + //-------------------------- + void threadedFunction(){ + + cout << "Api: " << url<<endl; + + while( isThreadRunning() != 0 ){ + + cout<<"."<<std::flush; + + if (!response.open(url)) { + cout << "Failed to parse JSON\n" << endl; + } + else { //int numImages = MIN(5,response["data"].size()); + + + for(int i=0; i< response["data"].size(); i++) { + //cout << "response " <<response["data"][i]["caption"]["id"].asString()<< endl; + + if (images.find(response["data"][i]["caption"]["id"].asString())==images.end()){ + std::string url = response["data"][i]["images"]["standard_resolution"]["url"].asString(); + std::string id = response["data"][i]["caption"]["id"].asString(); + cout<<"fetching "<<id<<":"<<url<<endl; + + ofImage img; + img.setUseTexture(false); + img.loadImage(url); + if( lock() ){ + images[id]=img; + to_update.push_back(id); + unlock(); + } + } + } + } + ofSleepMillis(interval * 1000); + } + } + + //-------------------------- + void draw(){ + if( lock() ){ + if (to_update.size()){ + std::string im = to_update.front(); + + const ofPixels& pix = images[im].getPixelsRef(); + images[im].getTextureReference().allocate( + pix.getWidth() + ,pix.getHeight() + ,ofGetGlInternalFormat(pix) + ); + + images[im].setUseTexture(true); + images[im].update(); + + to_update.pop_front(); + + int drawcount=0; + for (map<string,ofImage>::iterator i=images.begin();i!=images.end();++i){ + if(i->second.isUsingTexture()){ + drawcount++; + } + } + cout<<"loaded "<<im<<" "<<ofToLong(im)%(long)(ofGetWidth()-images[im].getWidth()+1)<<","<<ofToLong(im)%(long)(ofGetHeight()-images[im].getHeight()+1)<<endl; + } + for (map<string,ofImage>::iterator i=images.begin();i!=images.end();++i){ + if(i->second.isUsingTexture()){ + i->second.draw(ofToLong(i->first)%(long)(ofGetWidth()-i->second.getWidth()+1),ofToLong(i->first)%(long)(ofGetHeight()-i->second.getHeight()+1)); + } + } + unlock(); + } + } + + +}; class exampleApp : public ofBaseApp { public: void setup(); + void update(); void draw(); + instagramLoader loader; - ofxJSONElement response; - std::vector<ofImage> images; }; @@ -1 +1 @@ -g++ -o test_poco test_poco_http.c -lPocoNet -lPocoFoundation -lPocoNetSSL +g++ -o test_poco test_poco_http.c `pkg-config --cflags jsoncpp` -lPocoNet -lPocoFoundation -lPocoNetSSL diff --git a/offsetProject/src/main.cpp b/offsetProject/src/main.cpp index bce5f94..1fa9125 100644 --- a/offsetProject/src/main.cpp +++ b/offsetProject/src/main.cpp @@ -1,6 +1,6 @@ #include "ofApp.h" int main() { - ofSetupOpenGL(1280, 960, OF_WINDOW); + ofSetupOpenGL(1600, 900, OF_WINDOW); ofRunApp(new ofApp()); } diff --git a/offsetProject/src/ofApp.cpp b/offsetProject/src/ofApp.cpp index 1f73212..02875f2 100644 --- a/offsetProject/src/ofApp.cpp +++ b/offsetProject/src/ofApp.cpp @@ -69,6 +69,8 @@ void ofApp::setup() { } mode=MODE_COLOURTILES; + + fullscreen=false; } //-------------------------------------------------------------- @@ -297,6 +299,10 @@ void ofApp::keyPressed (int key) { mode=(mode+1)%NUM_MODES; break; + case ' ': + fullscreen=!fullscreen; + ofSetFullscreen(fullscreen); + break; } } diff --git a/offsetProject/src/ofApp.h b/offsetProject/src/ofApp.h index 9406a0f..4dca112 100644 --- a/offsetProject/src/ofApp.h +++ b/offsetProject/src/ofApp.h @@ -67,4 +67,6 @@ public: int extend_w,extend_h; int mode; + + bool fullscreen; }; diff --git a/test_poco_http.c b/test_poco_http.c index 8107a22..2347d69 100644 --- a/test_poco_http.c +++ b/test_poco_http.c @@ -8,6 +8,8 @@ #include <iostream> #include <string> +#include <json/json.h> + #include "Poco/Net/HTTPSClientSession.h" #include "Poco/Net/InvalidCertificateHandler.h" #include "Poco/Net/AcceptCertificateHandler.h" |
