I, a few days ago, discovered some new things I wish I had mentioned sooner…
As I was writing this, I was only editing the older code with newer functionality, but,
…apparently it’s possible to ditch that all and do this with literally two lines of code!:
// The two golden lines!:
window.setFullscreen(fullscreen);
while (fullscreen? !window.isFullscreen() : window.isFullscreen());
// Note: I had this code before the two lines as well:
//pfullscreen = fullscreen;
// I was also wrapping the two lines in an `if`:
//if (pfullscreen != fullscreen)
// I removed this check since it was adding a little bit of delay and
// also took away benefit number `3`, as I will now list in the post.
Again, make sure to place all of that in post()
!
Write registerMethod("post", this);
in setup()
!
There are benefits to using this method which I also just tested:
- MUCH faster!
- Welcome to true fullscreen. the older method allowed you to use the
Meta + Up Arrow
and Meta + Down Arrow
keys to adjust the window a little bit here and there. PS the ‘Meta’ key is the same as ‘Windows key’.
- You can spam press
F11
, and your sketch won’t freeze!
- The window now retains its previous position and dimensions when exiting fullscreen.
Edit made 5 hours later, the same day or whatever! :
It’s been another hour!
Here’s some test code!:
…for the copy-paste speedrunners in us
import com.jogamp.newt.opengl.GLWindow;
// [https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/opengl/GLWindow.html];
GLWindow window;
boolean fullscreen;
// You NEED this!:
void settings() {
size(640, 480, P3D);
}
void setup() {
registerMethod("post", this);
surface.setResizable(true);
window = (GLWindow)surface.getNative();
}
void draw() {
background(abs(sin(millis() * 0.001f)) * 255); // Delete this!
}
void keyPressed() {
if (keyCode == 107)
fullscreen = !fullscreen;
}
void post() {
window.setFullscreen(fullscreen);
while (fullscreen? !window.isFullscreen() : window.isFullscreen());
}
Here’s how I want to explain it:
I deeply, deeply, very deeply encourage you to read this!:
import com.jogamp.newt.opengl.GLWindow;
// [https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/opengl/GLWindow.html];
GLWindow window;
boolean fullscreen, pfullscreen;
// ^^^ `pfullscreen` is used to track when an update occurs.
// Please take a look at the very end of this code snippet!
void setup() {
size(640, 480, P3D);
registerMethod("post", this);
sphereDetail(8);
surface.setResizable(true);
surface.setTitle("`F11` - A Story:");
window = (GLWindow)surface.getNative();
}
void draw() {
// "Why not give some more code away?" - Brahvim ";D!:
hint(DISABLE_DEPTH_TEST);
fill(0, 40);
rect(0, 0, width, height);
hint(ENABLE_DEPTH_TEST);
// "Why not give those without knowledge of 3D graphics what they would like?":
// I want my graphics to scale with the screen's size.
// My eyes are `60` (`PI / 3`: `2 * PI` AKA `TAU` is a full `360`) degrees wide,
// ...and I'm giving you the aspect ratio of this screen.
// If something has a depth less than `0.01`, don't draw it.
// If it goes above ten thousand, don't draw it then either!
perspective(PI / 3, (float)width / (float)height, 0.01f, 10000);
// Here, I tell your eyes how my world is:
camera(
// This is where the eyes stand:
width / 2, height / 2, -400,
// They look at this beautiful point my universe:
width / 2, height / 2, 0,
// This is the direction of our sky, my guy!:
0, 1, 0);
stroke(255);
strokeWeight(8);
line(window.getX(), 0, window.getX(), 400);
// Oh! And I use actual conventions now, by the way.
// `p_` for parameters... `f` after all `float`s... livin'-my-life!
line(map(window.getX(), 0, displayWidth, 0, width * 1.75f), 0,
map(window.getX(), 0, displayWidth, 0, width * 1.75f),
map(window.getY(), 0, displayHeight, 0, height * 2.5f));
if (mousePressed)
stroke(255, 0, 0);
strokeWeight(1);
noFill();
lights();
// Thanks to OpenGL Mathematics, our graphics now fit
// every screen!
// However, this separates our world and the screen's
// coordinate systems. `mouseX` and `mouseY` don't
// put objects in the right places!
// After having spent months on trying to solve
// this problem, I have found that...
// The truly working fix is this:
// [http://andrewmarsh.com/blog/2011/12/04/gluunproject-p3d-and-opengl-sketches/]
// Modify it a bit:
//perspective(FOV, ASPECT, I_CALL_THIS_THE_MOUSE_Z_VALUE, FAR_PLANE_DEPTH);
//camera(); // You put what you want here!
//classFromSirAndrewMarsh.captureViewMatrix((PGraphics3D)g);
// `0.9f`: at the near clipping plane.
// `0.9999f`: at the far clipping plane.
// If you the second comment on that post, the `Y` needs to
// be inverted:
//classFromSirAndrewMarsh.gluUnProject(mouseX, height - mouseY,
////0.9f + map(mouseY, height, 0, 0, 0.1f),
//0, PVector_that_stores_the_result);
// Now, you call the function with the arguments you want!:
//perspective(FOV, ASPECT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH);
// Here's one of the tricks I discovered super early into trying
// to solve the problem.
// Note: it only tries to approximate where the mouse is.
// It COMPELTELY *breaks* in fullscreen:
translate(
map(mouseX, 0, width, width, 0),
map(mouseY, 0, height, 0, height));
// Alright! Time for our beautiful graphics!~
rotateX(millis() * 0.001f);
rotateZ(millis() * 0.001f);
sphere(50);
// ...that was just an example, what did you expect? :joy:
}
void keyPressed() {
// Minecraft uses `F11`:
if (keyCode == 107)
fullscreen = !fullscreen;
// Most games will use `Alt + Enter`.
// Here's how to implement it!:
// `PApplet` has a list (`ArrayList`..?)
// called `pressedKeys` inside, but it is `private`,
// ..and I don't want to play around with reflection, so...
// Declare this in the global scope (AKA before `setup()`):
//ArrayList<Integer> keysHeld = new ArrayList<Integer>(3);
// In `keyPressed()`:
//keysHeld.add(keyCode);
//In `keyReleased()`:
//try {
// keysHeld.remove(keysHeld.indexOf(keyCode));
//}
//catch(IndexOutOfBoundsException ioobe) {
//}
// Make this cool new function!:
//boolean keyIsPressed(int p_keyCode) {
//return keysHeld.contains(p_keyCode);
//}
// ...sorry if you don't like my conventions (such as `p_`).
// ..and here's the code that remains inside this function!:
//if (keyIsPressed(ALT) && keyCode == ENTER)
//fullscreen = !fullscreen;
// Remember: the right-side `Alt` key, `AltGr`,
// AKA "Alt-Graph", is different. You might want
// to import in `java.awt.event.KeyEvent`, and use:
// `KeyEvent.VK_ALT_GRAPH`, ...or both
// `KeyEvent.VK_ALT` and.... 'alt-graph'.
}
// The gold we all were looking for. Right here:
// :)
void post() {
window.setFullscreen(fullscreen);
while (fullscreen? !window.isFullscreen() : window.isFullscreen());
//pfullscreen = fullscreen;
//if (pfullscreen != fullscreen)
//windowResizedFunction();
// PS The JogAmp documentation probably states ways to get a callback,
// but we should stick to doing this. No idea what things,
// the Processing developers might do.
}
The Alt
+ Enter
speedrun version:
Copy-pasting at it’s finest, eh?
import com.jogamp.newt.opengl.GLWindow;
// [https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/opengl/GLWindow.html];
GLWindow window;
boolean fullscreen;
ArrayList<Integer> keysHeld = new ArrayList<Integer>(3);
void settings() {
size(640, 480, P3D);
}
void setup() {
registerMethod("post", this);
surface.setTitle("`Alt` + `Enter` for the win!");
surface.setResizable(true);
window = (GLWindow)surface.getNative();
}
void draw() {
background(abs(sin(millis() * 0.001f)) * 255);
}
void keyPressed() {
keysHeld.add(keyCode);
if (keyCode == 107)
fullscreen = !fullscreen;
if (keyIsPressed(ALT) && keyCode == ENTER)
fullscreen = !fullscreen;
}
void keyReleased() {
try {
keysHeld.remove(keysHeld.indexOf(keyCode));
}
catch(IndexOutOfBoundsException ioobe) {
}
}
boolean keyIsPressed(int p_keyCode) {
return keysHeld.contains(p_keyCode);
}
void post() {
window.setFullscreen(fullscreen);
while (fullscreen? !window.isFullscreen() : window.isFullscreen());
}