Skip to content

Instantly share code, notes, and snippets.

@jkosoy
Created February 17, 2013 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkosoy/4972178 to your computer and use it in GitHub Desktop.
Save jkosoy/4972178 to your computer and use it in GitHub Desktop.
Example code to reproduce the error in ofxFastFboReader (https://github.com/satoruhiga/ofxFastFboReader) I discovered. Create an app, resize the window and press "s" to save a different file in your data directory. Sometimes this will work and other times it won't.
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
ofSetVerticalSync(true);
ofEnableAlphaBlending();
ofSetFrameRate(30);
counter = 0;
fboReader.setAsync(false);
rect = ofRectangle(0,0,15,15);
speed = ofVec2f(5,5);
pos = ofVec2f(10,10);
}
//--------------------------------------------------------------
void testApp::update(){
if(bSavingImage) {
saveImage();
bSavingImage = false;
}
pos += speed;
if(rect.y+rect.height >= ofGetWindowHeight() && speed.y > 0) {
speed.y *= -1;
}
else if(rect.y < 0 && speed.y < 0) {
speed.y *= -1;
}
if(rect.x+rect.width >= ofGetWindowWidth() && speed.x > 0) {
speed.x *= -1;
}
else if(rect.x < 0 && speed.x < 0) {
speed.x *= -1;
}
rect.position += speed;
}
void testApp::prepareToSaveImage() {
bSavingImage = true;
}
void testApp::saveImage() {
ofFbo fbo;
fbo.allocate(ofGetWindowWidth(), ofGetWindowHeight(),GL_RGB);
fbo.begin(); {
draw();
} fbo.end();
ofPixels px;
fboReader.readToPixels(fbo, px);
ofImage img;
img.setFromPixels(px);
img.saveImage(ofToString(counter) + ".png");
counter++;
cout << "saved." << endl;
}
//--------------------------------------------------------------
void testApp::draw(){
ofBackground(0);
ofSetColor(0,255,0);
ofRect(rect);
if(!bSavingImage) {
ofSetColor(ofColor::white);
ofDrawBitmapString("Press s to save.",20,20);
ofDrawBitmapString(ofToString(ofGetWindowWidth()) + " x " + ofToString(ofGetWindowHeight()), 20,40);
}
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
if(key == 's') {
prepareToSaveImage();
}
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
#pragma once
#include "ofMain.h"
#include "ofxFastFboReader.h"
class testApp : 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 windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
bool bSavingImage;
void saveImage();
void prepareToSaveImage();
ofxFastFboReader fboReader;
unsigned int counter;
ofVec2f pos;
ofVec2f speed;
ofRectangle rect;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment