Hi! Having some funny issues here with calling ofBackground() in keyPressed(int key){
After having this issue in high Sierra on an older version, I am now using the nightly build: of_v20181009_osx_release with Mojave and the issue is still persisting.
if it is relevant, I am on a 2017 MacBook Pro
basically I just have this simple conditional inside keyPressed,
if (key == 'r' || key == 'R'){
ofBackground(255);
}
setBackgroundAuto is set to false, and the frame rate is set as 60fps.
When the app is running and r is pressed, the newly placed background begins flickering on and off again very fast.
The problem does not occur if I just use a global incrementing integer (like a timer) to trigger a conditional which does the same thing (make a new background) in draw.
I thought a bool assignment from keyPressed controlling a conditional in draw might solve the issue, but the flickering comes back if I do that.
Flickering issue:
in my ofApp.cpp:
#include “ofApp.h”
//--------------------------------------------------------------
void ofApp::setup(){
ofSetWindowShape(600,600);
ofBackground(127);
ofSetBackgroundAuto(false);
ofSetFrameRate(60);
ofSetColor(255, 0, 0, 10);
}
//--------------------------------------------------------------
void ofApp::update(){
circleRes = ofMap(mouseX, 0, ofGetWidth(), 3, 10, false);
circleRad = mouseY/2;
}
//--------------------------------------------------------------
void ofApp::draw(){
//calling ofBackground for only one frame every 100 in here also creates the same issue:
//timer++;
//if ( timer > 100){
//ofBackground(255);
//timer = 0;
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
//keys ‘1’, ‘2’, ‘3’ change colour of shapes being drawn just fine
if (key == 49){
ofSetColor(0, 0, 0, 10);
}
if (key == 50){
ofSetColor(255, 0, 0, 10);
}
if (key == 51){
ofSetColor(0, 255, 0, 10);
}
if (key == 'r'||key == 'R'){
ofBackground(255);
}
if (key == 's' || key == 'S'){
ofSaveScreen(ofToString(ofGetFrameNum())+".png");
}
}
//--------------------------------------------------------------
void ofApp::keyReleased( int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved( int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged( int x, int y, int button){
ofNoFill();
//ofSetColor(255, 0, 0, 20);
ofSetCircleResolution(circleRes);
ofDrawCircle(ofGetWidth()/2, ofGetHeight()/2, circleRad);
}
and my ofApp.h looks like this:
#pragma once
#include “ofMain.h”
class ofApp : 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 mouseEntered( int x, int y);
void mouseExited( int x, int y);
void windowResized( int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
int timer = 0;
int circleRes, circleRad;
bool reset = false ;
};