Package org.pollux3d.app

Source Code of org.pollux3d.app.PolluxApplication

/*
* Copyright (c) 2011 pollux3d
* All rights reserved.
*/
package org.pollux3d.app;

import java.util.concurrent.atomic.AtomicInteger;

import org.pollux3d.core.Pollux;
import org.pollux3d.gesture.GestureSystem;
import org.pollux3d.tuio.TuioDeviceNetwork;
import org.pollux3d.tuio.TuioInput;
import org.pollux3d.tuio.win7.TuioDeviceWin7;

import com.jme3.app.Application;
import com.jme3.app.StatsView;
import com.jme3.app.state.AppStateManager;
import com.jme3.audio.AudioRenderer;
import com.jme3.audio.Listener;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.input.FlyByCamera;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial.CullHint;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeContext.Type;
import com.jme3.system.JmeSystem;
import com.jme3.util.BufferUtils;

/**
* <code>SimpleApplication</code> extends the {@link com.jme3.app.Application}
* class to provide default functionality like a first-person camera,
* and an accessible root node that is updated and rendered regularly.
* Additionally, <code>SimpleApplication</code> will display a statistics view
* using the {@link com.jme3.app.StatsView} class. It will display
* the current frames-per-second value on-screen in addition to the statistics.
* Several keys have special functionality in <code>SimpleApplication</code>:<br/>
*
* <table>
* <tr><td>Esc</td><td>- Close the application</td></tr>
* <tr><td>C</td><td>- Display the camera position and rotation in the console.</td></tr>
* <tr><td>M</td><td>- Display memory usage in the console.</td></tr>
* </table>
*/
public abstract class PolluxApplication extends Application {

    protected Node rootNode = new Node("Root Node");
    protected Node guiNode = new Node("Gui Node");
    protected float secondCounter = 0.0f;
    protected BitmapText fpsText;
    protected BitmapFont guiFont;
    protected StatsView statsView;
    protected FlyByCamera flyCam;
    protected boolean showSettings = true;
    private AppActionListener actionListener = new AppActionListener();
  protected String windowName = "TuioGame";
  protected TuioInput tuioInput;
  protected GestureSystem gestureSystem;
  private AtomicInteger ticks = new AtomicInteger(0);
  protected Pollux pollux = null;
  protected AudioRenderer ar;

  private class AppActionListener implements ActionListener {

        public void onAction(String name, boolean value, float tpf) {
            if (!value) {
                return;
            }

            if (name.equals("SIMPLEAPP_Exit")) {
              // added by floh 01/11/2011
              // clean destroy audio environemt
              ar.cleanup();
                stop();
            } else if (name.equals("SIMPLEAPP_CameraPos")) {
                if (cam != null) {
                    Vector3f loc = cam.getLocation();
                    Quaternion rot = cam.getRotation();
                    System.out.println("Camera Position: ("
                            + loc.x + ", " + loc.y + ", " + loc.z + ")");
                    System.out.println("Camera Rotation: " + rot);
                    System.out.println("Camera Direction: " + cam.getDirection());
                }
            } else if (name.equals("SIMPLEAPP_Memory")) {
                BufferUtils.printCurrentDirectMemory(null);
            }
        }
    }

    public PolluxApplication() {
        super();
    }

    @Override
    public void start() {
     
        setSettings(new AppSettings(true));
        PolluxSettingsDialog.show(settings);
       
        super.start();
    }

    /**
     * Retrieves flyCam
     * @return flyCam Camera object
     *
     */
    public FlyByCamera getFlyByCamera() {
        return flyCam;
    }

    /**
     * Retrieves guiNode
     * @return guiNode Node object
     *
     */
    public Node getGuiNode() {
        return guiNode;
    }

    /**
     * Retrieves rootNode
     * @return rootNode Node object
     *
     */
    public Node getRootNode() {
        return rootNode;
    }

    public boolean isShowSettings() {
        return showSettings;
    }
    //added by floh -- 31/10/2011
    public AudioRenderer getAudioRenderer() {
    return ar;
  }

    /**
     * Toggles settings window to display at start-up
     * @param showSettings Sets true/false
     *
     */
    public void setShowSettings(boolean showSettings) {
        this.showSettings = showSettings;
    }

    /**
     * Attaches FPS statistics to guiNode and displays it on the screen.
     *
     */
    public void loadFPSText() {
        guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
        fpsText = new BitmapText(guiFont, false);
        fpsText.setLocalTranslation(0, fpsText.getLineHeight(), 0);
        fpsText.setText("Frames per second");
        guiNode.attachChild(fpsText);
    }

    /**
     * Attaches Statistics View to guiNode and displays it on the screen
     * above FPS statistics line.
     *
     */
    public void loadStatsView() {
        statsView = new StatsView("Statistics View", assetManager, renderer.getStatistics());
//         move it up so it appears above fps text
        statsView.setLocalTranslation(0, fpsText.getLineHeight(), 0);
        guiNode.attachChild(statsView);
    }
   
    public void initTuio() {
    this.tuioInput = TuioInput.get();
    this.tuioInput.setScreen(this.getCamera().getWidth(), this.getCamera().getHeight());
    this.context.setTitle(windowName);
    this.tuioInput.setWindowName(windowName);
    if (settings.getString("inputSystem").equals(PolluxSettingsDialog.InputSystems.Win7)) {
      this.tuioInput.setTuioDevice(new TuioDeviceWin7());
    } else if (settings.getString("inputSystem").equals(PolluxSettingsDialog.InputSystems.Network)) {
      this.tuioInput.setTuioDevice(new TuioDeviceNetwork());
    } else if (settings.getString("inputSystem").equals(PolluxSettingsDialog.InputSystems.Mouse)) {
      this.tuioInput.setInputManager(inputManager);
      this.tuioInput.setUseMouse(true);
    }
   
    // GestureSystem
    this.gestureSystem = GestureSystem.get();
    this.tuioInput.addListener(gestureSystem);
    }
   

    @Override
    public void initialize() {
     
        settings = context.getSettings();
     
      // setup the MultiResClasspathLocator
        if (settings.getString("quality").equals("high")) {
          MultiResClasspathLocator.setResolution(MultiResClasspathLocator.HIGH);
        } else if (settings.getString("quality").equals("low")) {
          MultiResClasspathLocator.setResolution(MultiResClasspathLocator.LOW);
        }
     
      MultiResClasspathLocator.addSupportedExtention("png");
      MultiResClasspathLocator.addSupportedExtention("jpg");
      MultiResClasspathLocator.addSupportedExtention("dds");
     
      // load asset manager -----------------------
        assetManager = JmeSystem.newAssetManager(
                Thread.currentThread().getContextClassLoader()
                .getResource("org/pollux3d/AssetManager.cfg"));

        // init Display ------------------------------
        timer = context.getTimer();
      
        renderer = context.getRenderer();
       
        // init Camera --------------------------------
       
        cam = new Camera(settings.getWidth(), settings.getHeight());

        cam.setFrustumPerspective(45f, (float)cam.getWidth() / cam.getHeight(), 1f, 1000f);
        cam.setLocation(new Vector3f(0f, 0f, 10f));
        cam.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);
       

        renderManager = new RenderManager(renderer);
        //Remy - 09/14/2010 setted the timer in the renderManager
        renderManager.setTimer(timer);
        viewPort = renderManager.createMainView("Default", cam);
        viewPort.setClearEnabled(true);

        // Create a new cam for the gui
        Camera guiCam = new Camera(settings.getWidth(), settings.getHeight());
        guiViewPort = renderManager.createPostView("Gui Default", guiCam);
        guiViewPort.setClearEnabled(false);
       
        // init Input ----------------------------------
       
        mouseInput = context.getMouseInput();
        if (mouseInput != null)
            mouseInput.initialize();
       
        keyInput = context.getKeyInput();
        if (keyInput != null)
            keyInput.initialize();

        joyInput = null;
        inputManager = new InputManager(mouseInput, keyInput, joyInput);
       
        // init Input TUIO ----------------------------------
        //initTuio();

       
        // init Audio ----------------------------------
        // edited by floh -- 31/10/2011
        ar = JmeSystem.newAudioRenderer(settings);
        ar.initialize();

        listener = new Listener();
        ar.setListener(listener);
       
        // init StateManager ----------------------------
        stateManager = new AppStateManager(this);
       
        // simple Init ----------------------------------
        guiNode.setQueueBucket(Bucket.Gui);
        guiNode.setCullHint(CullHint.Never);
        //loadFPSText();
        //loadStatsView();
        viewPort.attachScene(rootNode);
        guiViewPort.attachScene(guiNode);

        if (inputManager != null) {
            //flyCam = new FlyByCamera(cam);
            //flyCam.setMoveSpeed(1f);
            //flyCam.registerWithInput(inputManager);

            if (context.getType() == Type.Display) {
                inputManager.addMapping("SIMPLEAPP_Exit", new KeyTrigger(KeyInput.KEY_ESCAPE));
            }
          
            inputManager.addMapping("SIMPLEAPP_CameraPos", new KeyTrigger(KeyInput.KEY_C));
            inputManager.addMapping("SIMPLEAPP_Memory", new KeyTrigger(KeyInput.KEY_M));
            inputManager.addListener(actionListener, "SIMPLEAPP_Exit",
                    "SIMPLEAPP_CameraPos", "SIMPLEAPP_Memory");
        }
   
        //create the Core
        Pollux.create(this);
        pollux = Pollux.get();
        pollux.initAudio(); // added by floh -- 31/10/2011
        pollux.showLoadingScreen();
        /*
        Pollux.get().init();
        */
        /*
        FilterPostProcessor bloomFilter = new FilterPostProcessor(assetManager);
    BloomFilter bf = new BloomFilter(BloomFilter.GlowMode.Objects);
    bf.setBloomIntensity(1f);
    bf.setExposurePower(5f);
    bloomFilter.addFilter(bf);
    viewPort.addProcessor(bloomFilter);
    */
    }

    @Override
    public void update() {
        super.update(); // makes sure to execute AppTasks
        if (speed == 0 || paused) {
            return;
        }
       
        float tpf = timer.getTimePerFrame() * speed;
        /*
        secondCounter += timer.getTimePerFrame();
        int fps = (int) timer.getFrameRate();
        if (secondCounter >= 1.0f) {
            fpsText.setText("Frames per second: " + fps);
            secondCounter = 0.0f;
        }
        */
       
        if (pollux.isInitialized()) {

          // update states
          stateManager.update(tpf);
         
          // update TUIO
          this.tuioInput.update();
          this.gestureSystem.update(tpf);
       
        } else {
          if (ticks.getAndIncrement() > 3)
            pollux.init();
        }

        // simple update and root node
        simpleUpdate(tpf);
        rootNode.updateLogicalState(tpf);
        guiNode.updateLogicalState(tpf);
        rootNode.updateGeometricState();
        guiNode.updateGeometricState();

        // render states
        stateManager.render(renderManager);
        renderManager.render(tpf);
        simpleRender(renderManager);
        stateManager.postRender();
       
    }


    public void simpleUpdate(float tpf) {
    }

    public void simpleRender(RenderManager rm) {
    }
   
  protected void destroyInput(){
    super.destroyInput();
    if (this.tuioInput != null)
      this.tuioInput.destroy();
  }

}
TOP

Related Classes of org.pollux3d.app.PolluxApplication

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.