Implementing windowed to fullscreen mechanics

Im trying to implement mechanics into my game where i can use the console to change reso with a command and go windowed to fullscreen what would be the correct approach to do that while making sure everything stays responsive and working as it should ?

i already have the console and everything set up

someone told me in reddit “In my experience you can change window size at runtime but you can’t change whether you have a decorated window - either you start in fullScreen() or you get the window handle, no way around it afaik.”

do you think theres really no way to do that?

This hack used to work in Processing 3 with FX2D renderer. Not sure about 4.

	private PSurfaceFX surface;
	/**
	 * JavaFX Canvas - an image that can be drawn on using a set of graphics
	 * commands. A node of the {@link #scene}.
	 */
	private Canvas canvas;
	/**
	 * JavaFX Scene. Embedded in the {@link #stage} - the container for all other
	 * content (JavaFX nodes).
	 */
	protected Scene scene;
	/**
	 * JavaFX Stage - the top level JavaFX container (titlebar, etc.).
	 */
	private Stage stage;


	protected PSurface initSurface() {
		surface = (PSurfaceFX) super.initSurface();
		canvas = (Canvas) surface.getNative();
		canvas.widthProperty().unbind(); // used for scaling
		canvas.heightProperty().unbind(); // used for scaling
		scene = canvas.getScene();
		stage = (Stage) scene.getWindow();
		return surface;
	}


	public void keyPressed() {
		stage.setFullScreen(!stage.isFullScreen());
	}
1 Like

Reminder: If you’re posting to multiple sites, please link between the crossposts so we can see what help you’ve already received. See: Asking Questions — Are you posting anywhere else?

:arrow_right: r/processing: Going windowed > fullscreen & changing resolution

Hi, I have another question.

Does the code you provided support switching between different monitor sizes? I’m developing a game that needs to work seamlessly across all screen types. On my 2K monitor, everything works perfectly, but when I had others test it on smaller resolutions, they noticed some issues. For example, if the game has a grid as a background with a wall-like border around the edges, they see less of the grid, and the border is misaligned. Additionally, when they try to switch between fullscreen and windowed mode, which works fine for me, the game crashes with a “NullPointerException” error.

Do you have any idea if this is possible to do via processing ? Because i choose it to develop my game and i don’t want to re-make it another platform just because of this little thing.
So any type of help would be very much appreciated!

hi @winterchan,

Hard to say, without seeing what happens … usually the fullscreen always matches the resolution the client has, but maybe you’ve some things hardcoded. If you want it to work with different resolutions you should carefully review you drawings not hardcoded in any way, but always relative to the design. Means don’t use s.th like hardcoded values (width - 20px) but use percentage values (width*.95, 95% of width). For the NullPointerException you should verify your game if there is one by mistake under specific circumstances…

Cheers
— mnse

Thanks, we tried to do that, for example

That’s how it looks on my pc 2560x1440

That’s how it looks on a laptop with 1920x1080

as you can see i have 14 grid cube above the middle empty square but he has 10
we use the exact same code:

int cols, rows;
int gridSize;  // Size of one grid brick
int gridCubes = 4;  // Number of grid bricks each maze block occupies
int[][] maze;
int buttonX, buttonY, buttonW, buttonH;
int centerX, centerY, centerSize;

void settings() {
  fullScreen();  // This enables fullscreen mode by default
}

void setup() {
  calculateGridSize();
  initializeGrid();
  setupButton();
}

void draw() {
  background(255);
  drawMaze();
  drawButton();
}

void mousePressed() {
  if (mouseX > buttonX && mouseX < buttonX + buttonW && mouseY > buttonY && mouseY < buttonY + buttonH) {
    generateMaze();
  }
}

void calculateGridSize() {
  // Calculate the grid size based on the screen dimensions
  cols = width / (gridCubes * 10);  // Adjust 10 to change the number of blocks horizontally
  rows = height / (gridCubes * 10); // Adjust 10 to change the number of blocks vertically
  
  gridSize = min(width / (cols * gridCubes), height / (rows * gridCubes));
  
  cols = width / (gridSize * gridCubes);
  rows = height / (gridSize * gridCubes);

  // Define the center square that cannot have maze blocks
  centerX = cols / 2;
  centerY = rows / 2;
  centerSize = min(cols, rows) / 4;  // Adjust this value to change the size of the central square
}

void initializeGrid() {
  maze = new int[cols][rows];
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      maze[i][j] = 0;
    }
  }
  generateMaze();
}

void setupButton() {
  buttonW = int(width * 0.15);
  buttonH = int(height * 0.07);
  buttonX = (width - buttonW) / 2;
  buttonY = height - buttonH - 20;
}

void drawMaze() {
  stroke(0);
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      if (maze[i][j] == 1) {
        fill(150);
      } else {
        fill(255);
      }
      rect(i * gridSize * gridCubes, j * gridSize * gridCubes, gridSize * gridCubes, gridSize * gridCubes);
    }
  }
}

void drawButton() {
  fill(200);
  stroke(0);
  rect(buttonX, buttonY, buttonW, buttonH);
  fill(0);
  textAlign(CENTER, CENTER);
  textSize(16);
  text("Generate", buttonX + buttonW / 2, buttonY + buttonH / 2);
}

void generateMaze() {
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      // Exclude blocks within the central square
      if (i >= centerX - centerSize / 2 && i < centerX + centerSize / 2 && j >= centerY - centerSize / 2 && j < centerY + centerSize / 2) {
        maze[i][j] = 0;
      } else if (i == 0 || j == 0 || i == cols - 1 || j == rows - 1 || (int)random(2) == 1) {
        maze[i][j] = 1;
      } else {
        maze[i][j] = 0;
      }
    }
  }
}

Do you have any idea why this is happening?
Thanks in advanced! :100:

This may be related:

:)

2 Likes

Thanks! @glv
We managed to use that info to make it work :100:

2 Likes