⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertiesgamesettings.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jme.system;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;
import java.util.logging.Level;


/**
 * <code>PropertiesGameSettings</code> handles loading and saving a properties
 * file that
 * defines the display settings. A property file is identified during creation
 * of the object. The properties file should have the following format:
 * <PRE><CODE>
 * FREQ=60
 * RENDERER=LWJGL
 * WIDTH=1280
 * HEIGHT=1024
 * DEPTH=32
 * FULLSCREEN=false
 * </CODE></PRE>
 *
 * @author Mark Powell
 * @version $Revision: 4091 $
 */
public class PropertiesGameSettings extends AbstractGameSettings {
    private static final Logger logger = Logger.getLogger(
            PropertiesGameSettings.class.getName());

    //property object
    private Properties prop;
    //the file that contains our properties.
    private String filename;
    private static boolean dfltsInitted = false;

    /**
     * Legacy wrapper constructor
     *
     * @see #PropertiesGameSettings(String, String)
     */
    public PropertiesGameSettings(String userFile) {
        this(userFile, "game-defaults.properties");
    }

    /**
     * Constructor creates the <code>PropertiesGameSettings</code> object for use.
     *
     * @param personalFilename the properties file to use, read from filesystem.
     *                      Must not be null.
     * @param dfltsFilename the properties file to use, read from CLASSPATH.
     *                      Null to not seek any runtime defaults file.
     * @throws JmeException if the personalFilename is null.
     */
    public PropertiesGameSettings(
            String personalFilename, String dfltsFilename) {
        if (null == personalFilename) {
            throw new JmeException("Must give a valid filename");
        }
        if (!dfltsInitted) {
            dfltsInitted = true;
            // default* setting values are static, therefore, regardless of
            // how many GameSettings we instantiate, the defaults are
            // assigned only once.
            assignDefaults(dfltsFilename);
        }

        this.filename = personalFilename;
        isNew = !(new File(filename).isFile());
        prop = new Properties();

        logger.info("PropertiesGameSettings created");
    }

    /**
     * <code>load</code> attempts to load the properties file defined during
     * instantiation and put all properties in the table. If there is a problem
     * loading or reading the file, false is returned. If all goes well, true is
     * returned.
     * 
     * @return the success of the load, true indicated success and false
     *         indicates failure.
     */
    public boolean load() {
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(filename);
        } catch (FileNotFoundException e) {
            logger.warning("Could not load properties. Creating a new one.");
            return false;
        }

        try {
            if (fin != null) {
                prop.load(fin);
                fin.close();
            }
        } catch (IOException e) {
            logger.warning("Could not load properties. Creating a new one.");
            return false;
        }

        //confirm that the properties file has all the data we need.
        if (null == prop.getProperty("WIDTH")
            || null == prop.getProperty("HEIGHT")
            || null == prop.getProperty("DEPTH")
            || null == prop.getProperty("FULLSCREEN")) {
            logger.warning("Properties file not complete.");
            return false;
        }

        logger.info("Read properties");
        return true;
    }

    /**
     * Persists current property mappings to designated file, overwriting
     * if file already present.
     *
     * @throws IOException for I/O failures
     */
    public void save() throws IOException {
        FileOutputStream fout = new FileOutputStream(filename);
        prop.store(fout, "Game Settings written by " + getClass().getName()
                + " at " + new java.util.Date());

        fout.close();
        logger.info("Saved properties");
    }

    /**
     * <code>save(int, int, int, int, boolean, String)</code>
     * overwrites the properties file with the given parameters.
     *
     * @param width the width of the resolution.
     * @param height the height of the resolution.
     * @param depth the bits per pixel.
     * @param freq the frequency of the monitor.
     * @param fullscreen use fullscreen or not.
     * @deprecated
     * @return true if save was successful, false otherwise.
     */
    public boolean save(int width, int height, int depth, int freq,
            boolean fullscreen, String renderer) { 

        prop.clear();
        setWidth(width);
        setHeight(height);
        setDepth(depth);
        setFrequency(freq);
        setFullscreen(fullscreen);
        setRenderer(renderer);

        try {
            save();
        } catch (IOException e) {
            logger.warning("Could not save properties: " + e);
            return false;
        }
        return true;
    }

    /**
     * <code>getWidth</code> returns the width as read from the properties
     * file. If the properties file does not contain width or was not read
     * properly, the default width is returned.
     *
     * @return the width determined by the properties file, or the default.
     */
    public int getWidth() {
        String w = prop.getProperty("WIDTH");
        if (null == w) {
            return defaultWidth;
        } 
            
        return Integer.parseInt(w);        
    }

    /**
     * <code>getHeight</code> returns the height as read from the properties
     * file. If the properties file does not contain height or was not read
     * properly, the default height is returned.
     *
     * @return the height determined by the properties file, or the default.
     */
    public int getHeight() {
        String h = prop.getProperty("HEIGHT");
        if (null == h) {
            return defaultHeight;
        }
            
        return Integer.parseInt(h);        
    }

    /**
     * <code>getDepth</code> returns the depth as read from the properties
     * file. If the properties file does not contain depth or was not read
     * properly, the default depth is returned.
     *
     * @return the depth determined by the properties file, or the default.
     */
    public int getDepth() {
        String d = prop.getProperty("DEPTH");
        if (null == d) {
            return defaultDepth;
        } 
            
        return Integer.parseInt(d);        
    }

    /**
     * <code>getFrequency</code> returns the frequency of the monitor as read from
     * the properties file. If the properties file does not contain frequency
     * or was not read properly the default frequency is returned.
     *
     * @see GameSettings#getFrequency()
     * @return the frequency determined by the properties file, or the default.
     */
    public int getFrequency() {
        String f = prop.getProperty("FREQ");
        if(null == f) {
            return defaultFrequency;
        } 
            
        return Integer.parseInt(f);        
    }

    /**
     * <code>isFullscreen</code> returns the fullscreen flag as read from the
     * properties file. If the properties file does not contain the fullscreen
     * flag or was not read properly, the default value is returned.
     *
     * @see PreferencesGameSettings#isFullscreen()
     * @return the fullscreen flag determined by the properties file, or the
     *      default.
     */
    public boolean isFullscreen() {
        String f = prop.getProperty("FULLSCREEN");
        if(null == f) {
            return defaultFullscreen;
        }
            
        return Boolean.valueOf(prop.getProperty("FULLSCREEN"));        
    }

    /**
     *
     * <code>getRenderer</code> returns the requested rendering API, or the
     * default.
     * @return the rendering API or the default.
     */
    public String getRenderer() {
        String renderer = prop.getProperty("RENDERER");
        if(null == renderer) {
            return defaultRenderer;
        } 
            
        return renderer;        
    }

    /**
     * <code>get</code> takes an arbitrary string as a key and returns any
     * value associated with it, null if none.
     * @param key the key to use for data retrieval.
     * @return the string associated with the key, null if none.
     */
    public String get(String key) {
        return prop.getProperty(key);
    }

    /* REMAINING METHODS COMPLETE IMPLEMENTATION OF GameSettings INTERFACE */
    /**
     * @see GameSettings#clear()
     */
    public void clear() {
        prop.clear();
    }

    /**
     * @see GameSettings#get(String, String)
     */
    public String get(String name, String defaultValue) {
        String value = get(name);
        return (value == null) ? defaultValue : value;
    }

    /**
     * If the properties file does not contain the setting or was not read
     * properly, the default value is returned.
     *
     * @see GameSettings#getAlphaBits()
     * @throws InternalError in all cases
     */
    public int getAlphaBits() {
        String s = prop.getProperty("ALPHA_BITS");
        return (s == null) ? defaultAlphaBits : Integer.parseInt(s);
    }

    /**
     * If the properties file does not contain the setting or was not read
     * properly, the default value is returned.
     *
     * @see GameSettings#getDepthBits()
     * @throws InternalError in all cases
     */
    public int getDepthBits() {
        String s = prop.getProperty("DEPTH_BITS");
        return (s == null) ? defaultDepthBits : Integer.parseInt(s);
    }

    /**
     * If the properties file does not contain the setting or was not read
     * properly, the default value is returned.
     *
     * @see GameSettings#getFramerate()
     * @throws InternalError in all cases
     */
    public int getFramerate() {
        String s = prop.getProperty("FRAMERATE");
        return (s == null) ? defaultFramerate : Integer.parseInt(s);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -