Skip to content

Commit

Permalink
Added two Examples for resizable windows ... to complete the showcase…
Browse files Browse the repository at this point in the history
… of this PR.
  • Loading branch information
diwi committed Jan 5, 2018
1 parent 4c4be8d commit 1f59536
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/examples/Resizeable/Resizeable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package examples.Resizeable;

import peasy.PeasyCam;
import processing.core.PApplet;

public class Resizeable extends PApplet {

//
// Resizeable Window example.
//

PeasyCam cam;

public void settings() {
size(800, 600, P3D);
smooth(8);
}

public void setup() {
surface.setResizable(true);
cam = new PeasyCam(this, 400);
}

public void handleResize(){

cam.setViewport(0, 0, width, height);
cam.feed();
}

public void draw() {

handleResize();

perspective(60 * DEG_TO_RAD, width/(float)height, 1, 20000);
rotateX(-.5f);
rotateY(-.5f);
lights();
scale(10);
strokeWeight(1 / 10f);
background(0);
fill(96, 255, 0);
box(30);
pushMatrix();
translate(0, 0, 20);
fill(0, 96, 255);
box(5);
popMatrix();

cam.beginHUD();
fill(0, 128);
rect(0, 0, 70, 30);
fill(255);
text("" + nfc(frameRate, 2), 10, 18);
cam.endHUD();
}

public static void main(String args[]) {
PApplet.main(new String[] { Resizeable.class.getName() });
}

}
131 changes: 131 additions & 0 deletions test/examples/Resizeable_Offscreen/Resizeable_Offscreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package examples.Resizeable_Offscreen;

import peasy.PeasyCam;
import processing.core.PApplet;
import processing.core.PShape;
import processing.opengl.PGraphics3D;

public class Resizeable_Offscreen extends PApplet {

//
// Resizeable Window example, using an offscreen rendertarget.
//

PeasyCam cam;

// offscreen render target
PGraphics3D pg;

int window_w = 0;
int window_h = 0;

public void settings() {
size(800, 600, P2D);
smooth(8);
}

public void setup() {
surface.setResizable(true);

int w = 2 * width / 3 - 10;
int h = height - 20;
pg = (PGraphics3D)createGraphics(w, h, P3D);

cam = new PeasyCam(this, pg, 400);
}

public void handleResize() {

int w = 2 * width / 3 - 10;
int h = height - 20;

// check if window got resized
if (window_w != width || window_h != height) {

// Unfortunately processing has no easy, official way to resize a PGraphics.
// The following is a workaround to resize a PGraphics while still keeping
// the reference.
int smooth = pg.smooth;
pg.dispose();
pg.removeCache(pg);
pg.setPrimary(false);
pg.setParent(this);
pg.setSize(w, h);
pg.initialized = false;
pg.smooth = smooth;

// Alternatively we could create a new PGraphics instead and pass it to
// the camera,... but PeasyCam.g is final atm.
// TODO: discuss final PeasyCam.g

// pg.dispose();
// pg = (PGraphics3D) createGraphics(w, h, P3D);
// cam.setCanvas(pg);

}

// update new window dimension
window_w = width;
window_h = height;

// update camera viewport
cam.setViewport(10, 10, w, h);
// apply camera feed
cam.feed();
}

public void draw() {

handleResize();

// render offscreen
pg.beginDraw();
{
pg.perspective(60 * DEG_TO_RAD, pg.width / (float)pg.height, 1, 20000);
pg.rotateX(-.5f);
pg.rotateY(-.5f);
pg.lights();
pg.scale(10);
pg.strokeWeight(1 / 10f);
pg.background(16);
pg.fill(255, 128, 0);
pg.box(30);
pg.pushMatrix();
pg.translate(0, 0, 20);
pg.fill(0, 96, 255);
pg.box(5);
pg.popMatrix();

cam.beginHUD();
{
pg.fill(0, 128);
pg.rect(0, 0, 150, 30);
pg.fill(255);
pg.text("Distance: " + nf((float)cam.getDistance(), 1, 2), 10, 18);
}
cam.endHUD();
}
pg.endDraw();


// render onscreen (the primary graphics is 2D)
background(48);

// offscreen result
int[] vp = cam.getViewport();
image(pg, vp[0], vp[1], vp[2], vp[3]);

// text
fill(255);
int tx = vp[0] + vp[2] + 15 ;
int ty = 30;
text("FrameRate: " + nfc(frameRate, 2), tx, ty);
}



public static void main(String args[]) {
PApplet.main(new String[] { Resizeable_Offscreen.class.getName() });
}

}

0 comments on commit 1f59536

Please sign in to comment.