Offscreen buffer with PGraphics not being re-drawn?
in
Programming Questions
•
11 months ago
This is confusing me. This sketch draws the screen as it should if not drawing offscreen, with drawTerrain() drawing the shapes per frame properly. However as soon as i draw to buf and then try to load the resulting image, the image doesn't seem to update, continuing to render only the first frame.
Can someone tell me what's wrong?
Can someone tell me what's wrong?
- PGraphics buf;
PImage bg, bgmask;
float playerX = 0;
float playerY = 0;
float playerSpeed = 0;
float playerAcceleration = 0;
int terrainPointSpacing = 100;
int levelSize = 4096 * 8;
int maxTerrainPoints = levelSize/terrainPointSpacing;
float[] lowerTerrainPointX = new float[maxTerrainPoints];
float[] lowerTerrainPointY = new float[maxTerrainPoints];
float[] upperTerrainPointX = new float[maxTerrainPoints];
float[] upperTerrainPointY = new float[maxTerrainPoints];
//PImage img = loadImage("TilePanel01.png");
void setup() {
size(800, 480);
buf = createGraphics(width, height);
playerX = width/2;
playerY = height/2;
createTerrain();
}
void draw() {
playerX += playerSpeed;
if (keyPressed) {
if (keyCode == RIGHT) {
playerSpeed += 0.01;
}
if (keyCode == LEFT) {
playerSpeed -= 0.01;
}
if (keyCode == DOWN) {
playerY += 5;
}
if (keyCode == UP) {
playerY -= 5;
}
}
background(0);
//bgmask = buf.get(0, 0, buf.width, buf.height);
pushMatrix();
translate((width/2) - playerX, 0);
drawTerrain();
image(buf, 0, 0);
ellipse(playerX, playerY, 5, 5);
popMatrix();
}
void drawTerrain() {
int leftNode = int((playerX - (width/2)) / terrainPointSpacing);
buf.beginDraw();
buf.background(0);
buf.beginShape();
// texture(img);
buf.vertex((playerX - (width/2)), height);
for (int foo = leftNode; foo < (leftNode + (width / terrainPointSpacing) + 2); foo++) {
buf.vertex(lowerTerrainPointX[foo], height - lowerTerrainPointY[foo]);
}
buf.vertex((playerX + (width/2)), height);
buf.endShape(CLOSE);
buf.beginShape();
buf.vertex((playerX - (width/2)), 0);
for (int foo = leftNode; foo < (leftNode + (width / terrainPointSpacing) + 2); foo++) {
buf.vertex(upperTerrainPointX[foo], upperTerrainPointY[foo]);
}
buf.vertex((playerX + (width/2)), 0);
buf.endShape(CLOSE);
buf.endDraw();
}
void createTerrain() {
for (int foo = 0; foo < maxTerrainPoints; foo++) {
lowerTerrainPointX[foo] = foo * terrainPointSpacing;
lowerTerrainPointY[foo] = random(20, 100);
upperTerrainPointX[foo] = foo * terrainPointSpacing;
upperTerrainPointY[foo] = random(20, 100);
}
}
1