Rendering another screen on top of main game screen in fullscreen mode
- by wolf
my game runs in fullscreen mode and uses active rendering. The graphics are drawn on the fullscreen window in each game loop:
public void render() {
Window w = screen.getFullScreenWindow();
Graphics2D g = screen.getGraphics();
renderer.render(g, level, w.getWidth(), w.getHeight());
g.dispose();
screen.update();
}
This is the screen.update() method:
public void update(){
Window w = device.getFullScreenWindow();
if(w != null){
BufferStrategy s = w.getBufferStrategy();
if(!s.contentsLost()){
s.show();
}
}
}
I want to display another screen on my main game screen (menu, inventory etc). Lets say I have a JPanel inventory, which has a grid of inventory cells (manually drawn) and some Swing components like JPopupMenu. So i tried adding that to my window and repainting it in the game loop, which worked okay most of the time... but sometimes the panel wouldn't get displayed. Blindly moving things around in the inventory worked, but it just didn't display. When i alt-tabbed out and back again, it displayed properly.
I also tried drawing the rest of the inventory on my full screen window and using a JPanel to display only the buttons and popupmenus. The inventory displayed properly, but the Swing components keep flickering. I'm guessing this is because I don't know how to combine active and passive rendering.
public void render() {
Graphics2D g = screen.getGraphics();
invManager.render(g);
g.dispose();
screen.update();
invPanel.repaint();
}
Should i use something else instead of a JPanel? I don't really need active rendering for these screens, but I don't understand why they sometimes just don't display. Or maybe I should just make my own custom components instead of using Swing? I also read somewhere that using multiple panels/frames in a game is bad practice so should I draw everything on one window/frame/panel?
If I CAN use JPanels for this, should I add and remove them every time the inventory is toggled? Or just change their visibility?