Search in sources :

Example 1 with BinaryExporter

use of com.jme3.export.binary.BinaryExporter in project jmonkeyengine by jMonkeyEngine.

the class TestOgreConvert method simpleInitApp.

@Override
public void simpleInitApp() {
    Spatial ogreModel = assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    DirectionalLight dl = new DirectionalLight();
    dl.setColor(ColorRGBA.White);
    dl.setDirection(new Vector3f(0, -1, -1).normalizeLocal());
    rootNode.addLight(dl);
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryExporter exp = new BinaryExporter();
        exp.save(ogreModel, baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        BinaryImporter imp = new BinaryImporter();
        imp.setAssetManager(assetManager);
        Node ogreModelReloaded = (Node) imp.load(bais, null, null);
        AnimControl control = ogreModelReloaded.getControl(AnimControl.class);
        AnimChannel chan = control.createChannel();
        chan.setAnim("Walk");
        rootNode.attachChild(ogreModelReloaded);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : BinaryExporter(com.jme3.export.binary.BinaryExporter) BinaryImporter(com.jme3.export.binary.BinaryImporter) Spatial(com.jme3.scene.Spatial) ByteArrayInputStream(java.io.ByteArrayInputStream) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Node(com.jme3.scene.Node) AnimChannel(com.jme3.animation.AnimChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AnimControl(com.jme3.animation.AnimControl)

Example 2 with BinaryExporter

use of com.jme3.export.binary.BinaryExporter in project jmonkeyengine by jMonkeyEngine.

the class SaveGame method saveGame.

/**
     * Saves a savable in a system-dependent way.
     * @param gamePath A unique path for this game, e.g. com/mycompany/mygame
     * @param dataName A unique name for this savegame, e.g. "save_001"
     * @param data The Savable to save
     * @param storageType The specific type of folder to use to save the data
     */
public static void saveGame(String gamePath, String dataName, Savable data, JmeSystem.StorageFolderType storageType) {
    if (storageType == null) {
        Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Base Storage Folder Type is null, using External!");
        storageType = JmeSystem.StorageFolderType.External;
    }
    BinaryExporter ex = BinaryExporter.getInstance();
    OutputStream os = null;
    try {
        File baseFolder = JmeSystem.getStorageFolder(storageType);
        if (baseFolder == null) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
            throw new IllegalStateException("SaveGame dataset cannot be created");
        }
        File daveFolder = new File(baseFolder.getAbsolutePath() + File.separator + gamePath.replace('/', File.separatorChar));
        if (!daveFolder.exists() && !daveFolder.mkdirs()) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
            throw new IllegalStateException("SaveGame dataset cannot be created");
        }
        File saveFile = new File(daveFolder.getAbsolutePath() + File.separator + dataName);
        if (!saveFile.exists()) {
            if (!saveFile.createNewFile()) {
                Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
                throw new IllegalStateException("SaveGame dataset cannot be created");
            }
        }
        os = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(saveFile)));
        ex.save(data, os);
        Logger.getLogger(SaveGame.class.getName()).log(Level.FINE, "Saving data to: {0}", saveFile.getAbsolutePath());
    } catch (IOException ex1) {
        Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
        ex1.printStackTrace();
        throw new IllegalStateException("SaveGame dataset cannot be saved");
    } finally {
        try {
            if (os != null) {
                os.close();
            }
        } catch (IOException ex1) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
            ex1.printStackTrace();
            throw new IllegalStateException("SaveGame dataset cannot be saved");
        }
    }
}
Also used : BinaryExporter(com.jme3.export.binary.BinaryExporter) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BinaryExporter (com.jme3.export.binary.BinaryExporter)2 IOException (java.io.IOException)2 AnimChannel (com.jme3.animation.AnimChannel)1 AnimControl (com.jme3.animation.AnimControl)1 BinaryImporter (com.jme3.export.binary.BinaryImporter)1 DirectionalLight (com.jme3.light.DirectionalLight)1 Vector3f (com.jme3.math.Vector3f)1 Node (com.jme3.scene.Node)1 Spatial (com.jme3.scene.Spatial)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1