image buffer technique (need some guidance )
in
Programming Questions
•
2 years ago
Hey Guys,
I'm trying to optimize my program's excessive amount redraw. I'm wanting to derive a technique that checks to see if the state of things has changed visually and if they have, redraw, update whatever. If they haven't then I want to put these items into some sort of image buffer. so that there is only ever one image being redrawn of on the canvas (in the main draw loop) when things are idol.
Hope this makes sense. Here is a small section of the code from what I am working on. It would be awesome if someone could edit this code and show me an example of how to implement this. Please note I am not looking to use the loop() and noLoop() functions as this means that the rollover functions cant not update when the entire program is at a halt.
Thanks in advance!
I'm trying to optimize my program's excessive amount redraw. I'm wanting to derive a technique that checks to see if the state of things has changed visually and if they have, redraw, update whatever. If they haven't then I want to put these items into some sort of image buffer. so that there is only ever one image being redrawn of on the canvas (in the main draw loop) when things are idol.
Hope this makes sense. Here is a small section of the code from what I am working on. It would be awesome if someone could edit this code and show me an example of how to implement this. Please note I am not looking to use the loop() and noLoop() functions as this means that the rollover functions cant not update when the entire program is at a halt.
Thanks in advance!
- BezierSection bezSection1;
void setup() {
size(600, 400);
noStroke();
smooth();
bezSection1 = new BezierSection(1, new PVector(60, 300), new PVector(300, 300), new PVector( 200,90), new PVector(440,90));
}
void draw() {
background(41);
bezSection1.display(mouseX);
}
// release any point attached to the mouse
void mouseReleased () {
bezSection1.release();
}
// for dragg'n dem points
void mousePressed () {
bezSection1.press();
}
class BezierSection {
// variables for control and anchor points.
int pts = 4;
int ptSize = 8;
float[]x = new float[4];
float[]y = new float[4];
boolean[]isDragSafe = new boolean[4];
float[]xOld = new float[4];
float[]yOld = new float[4];
float[]xOffset = new float[4];
float[]yOffset = new float[4];
boolean[]over = new boolean[4];
boolean hasChanged =false;
float stuff;
BezierSection(float t, PVector A, PVector B, PVector C, PVector D) {
// fill coordinate arrays
xOld[0] = x[0] = A.x;
xOld[1] = x[1] = B.x;
xOld[2] = x[2] = C.x;
xOld[3] = x[3] = D.x;
yOld[0] = y[0] = A.y;
yOld[1] = y[1] = B.y;
yOld[2] = y[2] = C.y;
yOld[3] = y[3] = D.y;
//initialize dragging flags to false
for (int i =0; i< pts; i++) {
isDragSafe[i] = false;
}
}
void display(float timeSliderPos) {
// test print function
///////////////////////////////////println(getCVPositions());
//draw curves and control handles
strokeWeight(2);
stroke(138, 216, 20);
noFill();
bezier (x[0], y[0], x[1], y[1], x[2], y[2], x[3], y[3]);
// draw bezier handles
strokeWeight(2);
stroke(107);
line(x[1], y[1], x[0], y[0]);
line(x[3], y[3], x[2], y[2]);
// draw anchor/control points
noStroke();
fill(0);
for (int i =0; i< pts; i++) {
if (i ==0 || i==3) {
fill(0);
ellipse(x[i], y[i], ptSize+4, ptSize+4);
}
else {
fill(231, 163, 27);
rectMode(CENTER);
rect(x[i], y[i], ptSize, ptSize);
}
}
over(); //currently just used as a rollover hint for the CVs
// start dragging if flag true
if (isDragSafe[0]&&isDragSafe[1]) {// if it's anchor 1 and it's handle then move them both and add and offset to the handle
// getOffset();
x[0] = mouseX;
y[0] = mouseY ;
x[1] = mouseX - xOffset[1];
y[1] = mouseY - yOffset[1];
xOld[1] = x[1];
yOld[1] = y[1];
}
if (isDragSafe[1]&&!isDragSafe[0]) {
x[1] = mouseX;
xOld[1] = x[1];
y[1] = mouseY;
yOld[1] = y[1];
}
if (isDragSafe[2]&&isDragSafe[3]) {// if it's anchor 2 and it's handle then move them both and add and offset to the handle
x[3] = mouseX;
y[3] = mouseY;
x[2] = mouseX - xOffset[2];
y[2] = mouseY - yOffset[2];
xOld[2] = x[2];
yOld[2] = y[2];
}
if (isDragSafe[2]&&!isDragSafe[3]) {
x[2] = mouseX;
xOld[2] = x[2];
y[2] = mouseY;
yOld[2] = y[2];
}
fill(231, 163, 27);
// If statement to be called and used in conjunction with image buffer (once I learn how)
if (xOld[0] == x[0]&&
xOld[1] == x[1]&&
xOld[2] == x[2]&&
xOld[3] == x[3]&&
yOld[0] == y[0]&&
yOld[1] == y[1]&&
yOld[2] == y[2]&&
yOld[3] == y[3]){
println("hasn't changed");
hasChanged = false;
}
else {
println("has changed!!!");
xOld[0] = x[0];
xOld[1] = x[1];
xOld[2] = x[2];
xOld[3] = x[3];
yOld[0] = y[0];
yOld[1] = y[1];
yOld[2] = y[2];
yOld[3] = y[3];
hasChanged = true;
}
}
void press() {
for (int i =0; i< pts; i++) {
if (mouseX>=x[i]-ptSize-5 && mouseX<=x[i]+ptSize+5 &&
mouseY>=y[i]-ptSize-5 && mouseY<=y[i]+ptSize+5) {
if (i==0) {
isDragSafe[i] = true;
isDragSafe[i+1] = true;
yOffset[1] = (y[0] - yOld[1]); //Work out the offset between the anchor and the handle
xOffset[1] = (x[0] - xOld[1]);
}
if (i==3) {
isDragSafe[i] = true;
isDragSafe[i-1] = true;
yOffset[2] = (y[3] - yOld[2]); //Work out the offset between the anchor and the handle
xOffset[2] = (x[3] - xOld[2]);
}
else {
isDragSafe[i] = true;
}
}
}
}
void over() { // this is all just for rollover feedback
for (int i =0; i< pts; i++) {
if (mouseX>=x[i]-ptSize-5 && mouseX<=x[i]+ptSize+5 &&
mouseY>=y[i]-ptSize-5 && mouseY<=y[i]+ptSize+5) {
if (i==0 && isDragSafe[1] == false && isDragSafe[2] == false && isDragSafe[3] == false ) {
fill(0);
noStroke();
ellipse(x[i], y[i], 15, 15);
}
if (i==1 && isDragSafe[2] == false && isDragSafe[3] == false ) {
fill(231, 163, 27);
noStroke();
rect(x[i], y[i], 12, 12);
}
if (i==2 && isDragSafe[0] == false && isDragSafe[1] == false) {
fill(231, 163, 27);
noStroke();
rect(x[i], y[i], 12, 12);
}
if (i==3 && isDragSafe[0] == false&& isDragSafe[1] == false && isDragSafe[2] == false) {
fill(0);
noStroke();
ellipse(x[i], y[i], 15, 15);
}
}
}
}
void release() {
for (int i =0; i< pts; i++) {
isDragSafe[i] = false;
}
}
void move() {
cursor(ARROW);
for (int i =0; i< pts; i++) {
if (mouseX>=x[i]-15 && mouseX<=x[i]+15 && mouseY>=y[i]-15 && mouseY<=y[i]+15) {
cursor(HAND);
}
}
}
PVector[] getCVPositions() {
PVector[] vals = {
new PVector(x[0], y[0]), new PVector(x[1], y[1]), new PVector(x[2], y[2]), new PVector(x[3], y[3])
};
return vals;
}
boolean getBezLockedState() {
if (isDragSafe[0] || isDragSafe[1]||isDragSafe[2]||isDragSafe[3]) {
return true;
}
return false;
}
}
1