Resources.java

/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.explonoid.game;

import com.nokia.example.explonoid.ImageHelper;
import com.nokia.example.explonoid.effects.Spark;
import javax.microedition.lcdui.Image;

public class Resources {

    private static boolean initialized = false;
    public Image background;
    public Image signOn;
    public Image signOff;
    public Image ball;
    public Image life;
    public Image dashboard;
    public Image menuButton;
    public Image newGameButton;
    public Image gameOver;
    public Image plate;
    public Image bricks;
    public Image spark;
    public Image verticalWall;
    public Image horizontalWall;

    /**
     * Audio recources
     */
    public final String SAMPLE_BONUS = "/bonus.mp3";
    public final String SAMPLE_BUTTON = "/button.mp3";
    public final String SAMPLE_DEATH = "/death.mp3";
    public final String SAMPLE_EXPLOSION = "/explosion.mp3";
    public final String SAMPLE_TELEPORT = "/teleport.mp3";
    public final String SAMPLE_TRANSITION = "/transition.mp3";
    public final String SAMPLE_WALL = "/wall.mp3";
    public final String SAMPLE_COLLISION = "/collision.mp3";
    public final String SAMPLE_COLLISION2 = "/collision2.mp3";
    public final String SAMPLE_COLLISION3 = "/collision3.mp3";
    public final String SAMPLE_ELECTRICITY = "/electricity.mp3";
    public float scaling = 1.0f;

    public Resources(float scaling) {
        this.scaling = scaling;
        loadResources();
    }

    /**
     * Free resources
     */
    public void freeResources() {
        initialized = false;
        background = null;
        signOn = null;
        signOff = null;
        ball = null;
        dashboard = null;
        menuButton = null;
        newGameButton = null;
        gameOver = null;
        plate = null;
        bricks = null;
        spark = null;
        verticalWall = null;
        horizontalWall = null;
    }

    /**
     * Load resources
     */
    public void loadResources() {
        if (!initialized) {
            ImageHelper ih = ImageHelper.getInstance();
            background = ih.loadImage("/background.png", scaling);
            signOn = ih.loadImage("/explonoid_on.png", scaling);
            signOff = ih.loadImage("/explonoid_off.png", scaling);
            ball = ih.loadImage("/ball.png", scaling);
            life = ImageHelper.scaleImage(ball, 0.5f);
            dashboard = ih.loadImage("/dashboard.png", scaling);
            menuButton = ih.loadImage("/menu_button.png", scaling);
            newGameButton = ih.loadImage("/new_game_button.png", scaling);
            gameOver = ih.loadImage("/game_over.png", scaling);

            plate = ih.loadImage("/plate.png");
            plate = ImageHelper.scaleImage(plate, scale(plate.getWidth()),
                                  scale(plate.getHeight() / Plate.ROWS) * Plate.ROWS);
            bricks = ih.loadImage("/bricks.png");
            bricks = ImageHelper.scaleImage(bricks, scale(bricks.getWidth() / Brick.COLS) * Brick.COLS,
                                   scale(bricks.getHeight() / Brick.ROWS) * Brick.ROWS);
            spark = ih.loadImage("/spark.png");
            spark = ImageHelper.scaleImage(spark, scale(spark.getWidth() / Spark.COLS) * Spark.COLS,
                                  scale(spark.getHeight()));
            verticalWall = ih.loadImage("/vertical_wall.png");
            verticalWall = ImageHelper.scaleImage(verticalWall, scale(verticalWall.getWidth() / 2) * 2,
                                         scale(verticalWall.getHeight()));
            horizontalWall = ih.loadImage("/horizontal_wall.png");
            horizontalWall = ImageHelper.scaleImage(horizontalWall, scale(horizontalWall.getWidth()),
                                           scale(horizontalWall.getHeight() / 2) * 2);
            initialized = true;
        }
    }

    /**
     * Scale a value and return an integer using symmetrical rounding
     */
    public int scale(double value) {
        double result = scaling * value;
        return (result < 0) ? (int) (result - 0.5) : (int) (result + 0.5);
    }
}