Login  Register

Feasible Fullscreen Model?

Posted by klatu on Jan 16, 2012; 3:59pm
URL: https://forum.jogamp.org/Feasible-Fullscreen-Model-tp3663579.html

Hi!

After hours and hours of trying to wrap my head around JOGL, NEWT, etc I have finally managed to implement a simple JOGL application that:
-displays something using OpenGL
-has common GUI support (AWT)
-and can toggle fullscreen mode (without bugging out!)

Considering that I have rewritten the same app three times before, never fully utilizing all of the above, I just wanted to get some confirmation that the basic model I have now is feasible at all before I spent any more time on it.

Please leave suggestions on how to avoid problems down the road. If there is a better solution, please let me know.

CODE:
import java.awt.*;
import java.awt.Frame;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;

import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;
import com.jogamp.newt.awt.NewtCanvasAWT;

import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.newt.event.KeyListener;
import com.jogamp.newt.event.KeyEvent;


public class NewtSuite
implements GLEventListener, KeyListener, ActionListener
{
	private	long		fps_time_start,
				fps_time_current,
				fps_time_passed;
	private	int		fps_frames, fps_frames_com;
	private			FPSAnimator	gl_FPSAnimator;
	private			GL2			gGL;
	private			GLU			gGLU;

	private			GLWindow		gl_GLWindow;

	NewtCanvasAWT	gl_canvas; 
	
	
	private boolean isFullscreen;
	
	
	private Frame	frame_normal, frame_full;
	
	
	public NewtSuite()
	{
		init_GLWindow();
		reparent_normal();
		gl_FPSAnimator	= new FPSAnimator(gl_GLWindow, 120);
		gl_FPSAnimator.start();
	}
	
	
	private void init_GLWindow()
	{
		gl_GLWindow	= GLWindow.create(getGLCapabilities());
		gl_GLWindow.setSize(800, 600);
		gl_GLWindow.addGLEventListener(this);
		gl_GLWindow.addKeyListener(this);
		gl_GLWindow.setVisible(true);

		gl_canvas	= new NewtCanvasAWT(gl_GLWindow);
		
		isFullscreen	= false;
	}
	
	private void reparent_normal()
	{
		frame_normal = new Frame("AWT Frame Normal");
		
		frame_normal.addWindowListener
			(new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent e) {
					gl_FPSAnimator.stop();
					System.exit(0);
				}
			}
		);
		
		Button btn;
		
		frame_normal.setLayout(new BorderLayout());
		
		btn	= new Button("North");
		btn.setActionCommand("North");
		btn.addActionListener(this);
		frame_normal.add(btn, BorderLayout.NORTH);
		
		btn	= new Button("South");
		btn.setActionCommand("South");
		btn.addActionListener(this);
		frame_normal.add(btn, BorderLayout.SOUTH);

		btn	= new Button("East");
		btn.setActionCommand("East");
		btn.addActionListener(this);
		frame_normal.add(btn, BorderLayout.EAST);

		btn	= new Button("West");
		btn.setActionCommand("West");
		btn.addActionListener(this);		
		frame_normal.add(btn, BorderLayout.WEST);
		frame_normal.setSize(500, 500);
		frame_normal.setLocation(0, 0);
		

		frame_normal.setUndecorated(false);
		frame_normal.setExtendedState(Frame.NORMAL);
		frame_normal.setVisible(true);
		
		frame_normal.add(gl_canvas);
	}

	private void reparent_full()
	{
		frame_full = new Frame("AWT Frame Normal");
		
		frame_full.addWindowListener
			(new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent e) {
					gl_FPSAnimator.stop();
					System.exit(0);
				}
			}
		);
		
		Button btn;
		
		frame_full.setLayout(new BorderLayout());
		
		btn	= new Button("North");
		btn.setActionCommand("North");
		btn.addActionListener(this);
		frame_full.add(btn, BorderLayout.NORTH);
		
		btn	= new Button("South");
		btn.setActionCommand("South");
		btn.addActionListener(this);
		frame_full.add(btn, BorderLayout.SOUTH);

		btn	= new Button("East");
		btn.setActionCommand("East");
		btn.addActionListener(this);
		frame_full.add(btn, BorderLayout.EAST);

		btn	= new Button("West");
		btn.setActionCommand("West");
		btn.addActionListener(this);		
		frame_full.add(btn, BorderLayout.WEST);
		frame_full.setSize(500, 500);
		frame_full.setLocation(0, 0);
		

		frame_full.setUndecorated(true);
		frame_full.setExtendedState(Frame.MAXIMIZED_BOTH);
		frame_full.setVisible(true);
		
		frame_full.add(gl_canvas);
	}
	
	private void toggle_fullscreen()
	{
		if (frame_full != null && frame_full.isVisible()){
			
			reparent_normal();
			isFullscreen	= false;
			frame_full.setVisible(false);
		}else{
		
			reparent_full();
			isFullscreen	= true;
			frame_normal.setVisible(false);
		}
		gl_GLWindow.requestFocus();
	}

	@Override
	public void keyPressed(KeyEvent e)
	{
		System.out.println("pressed");
		toggle_fullscreen();
	}
	
	@Override
	public void keyReleased(KeyEvent e)
	{

	}
	
	@Override
	public void keyTyped(KeyEvent e)
	{

	}

	public void actionPerformed(ActionEvent e)
	{
		System.out.println(e.getActionCommand()+"");
	
	}
	
	@Override
	public void init(GLAutoDrawable arg0) {
		this.gGL		= arg0.getGL().getGL2();
		this.gGLU	= new GLU();
		this.gGL.glEnable(GL2.GL_DEPTH_TEST);
		this.gGL.glEnable(GL2.GL_TEXTURE_2D);

		this.gGL.glEnableClientState(GL2.GL_VERTEX_ARRAY);
		this.gGL.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
		
		
		fps_time_start	= System.currentTimeMillis();
		fps_frames		= 0;
	}

	@Override
	public void display(GLAutoDrawable arg0) {
		this.gGL.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
		this.gGL.glMatrixMode(GL2.GL_PROJECTION);
		this.gGL.glLoadIdentity();
		this.gGL.glOrtho(0,500,0,500,-1,1);
		this.gGL.glBegin(GL2.GL_QUADS);
			this.gGL.glTexCoord2d	(0.0,1.0);
			this.gGL.glVertex2d	(100, 100); // Bottom Left
			this.gGL.glTexCoord2d	(1.0,1.0);
			this.gGL.glVertex2d	(200, 100); // Bottom Right
			this.gGL.glTexCoord2d	(1.0,0.0);
			this.gGL.glVertex2d	(200, 200); // Top Right
			this.gGL.glTexCoord2d	(0.0,0.0);
			this.gGL.glVertex2d	(100, 200+ fps_frames); // Top Left
		this.gGL.glEnd();
		
		
		fps_frames++;

		fps_time_current	= System.currentTimeMillis();
		
		fps_time_passed		= fps_time_current - fps_time_start;
		if (fps_time_passed > 1000)
		{
			System.out.println(""+fps_frames);
			fps_frames_com	= fps_frames;
			fps_frames		= 0;
			fps_time_start	= fps_time_current;
			
		}
	}
	
	@Override
	public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, int height) {

	}

	public GLCapabilities getGLCapabilities() {
		GLProfile glp = GLProfile.getDefault();
		return new GLCapabilities(glp);
	}
	
	@Override
	public void dispose(GLAutoDrawable arg0) {
	
	}

	public static void main(String[] args) {
		new NewtSuite();
	}

}