Login  Register

Re: Simple example with Nifty

Posted by elect on Nov 08, 2013; 3:12pm
URL: https://forum.jogamp.org/Simple-example-with-Nifty-tp4030529p4030538.html

So I modified it in order to use the GLwindows, I could now add the mouse and keylistener, I also modified the nifty initialization, now it is working, but I see the written ugly



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.FPSAnimator;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.batch.BatchRenderDevice;
import de.lessvoid.nifty.nulldevice.NullSoundDevice;
import de.lessvoid.nifty.renderer.jogl.input.JoglInputSystem;
import de.lessvoid.nifty.renderer.jogl.render.batch.JoglBatchRenderBackendCoreProfile;
import de.lessvoid.nifty.spi.render.RenderDevice;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import de.lessvoid.nifty.spi.time.TimeProvider;

/**
 *
 * @author gbarbieri
 */
public class Test2 implements GLEventListener {

    private GLWindow glWindow;
    private int imageWidth;
    private int imageHeight;
    private Nifty nifty;
    private JoglInputSystem inputSystem;
    private RenderDevice renderDevice;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Test2 test2 = new Test2();
    }

    public Test2() {

        initGL();
    }

    private void initGL() {

        imageWidth = 800;
        imageHeight = 600;

        GLProfile profile = GLProfile.get(GLProfile.GL3);

        GLCapabilities capabilities = new GLCapabilities(profile);

        glWindow = GLWindow.create(capabilities);
       
        glWindow.setAutoSwapBufferMode(true);
       
        glWindow.setSize(imageWidth, imageHeight);
       
        glWindow.addGLEventListener(this);
       
        FPSAnimator animator = new FPSAnimator(glWindow, 60, false);
       
        glWindow.setTitle("Test2");
       
        glWindow.setVisible(true);
       
        animator.start();
    }

    @Override
    public void init(GLAutoDrawable drawable) {

        renderDevice = new BatchRenderDevice(new JoglBatchRenderBackendCoreProfile(), 2048, 2048);

        inputSystem = new JoglInputSystem();

        glWindow.addMouseListener(inputSystem);
        glWindow.addKeyListener(inputSystem);

        nifty = new Nifty(renderDevice, new NullSoundDevice(), inputSystem, new TimeProvider() {
            @Override
            public long getMsTime() {

                return System.currentTimeMillis();
            }
        });

        nifty.fromXml("test/helloworld.xml", "start");
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
    }

    @Override
    public void display(GLAutoDrawable drawable) {
       
        nifty.update();
        nifty.render(true);
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

        GL3 gl3 = drawable.getGL().getGL3();

        gl3.glViewport(x, y, width, height);
    }
}


But I guess it is just now a matter of playing with it..

Thanks