sriharshachilakapati

Resource Manager (Solved)

Aug 14th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.gej.core;
  2.  
  3. import java.awt.Image;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6.  
  7. import com.gej.sound.WavPlayer;
  8. import com.gej.sound.WavSound;
  9.  
  10. /**
  11.  * A resource manager to load the resources in separate thread. Can load images
  12.  * and sounds and say the load percentage. Get the resources from the manager
  13.  * and assign them in the update() method of the Game class like this.
  14.  *
  15.  * <pre>
  16.  *
  17.  * Image myImg = null;
  18.  * WavSound mySound = null;
  19.  *
  20.  * public void initResources(){
  21.  *     ResourceManager.defineImage("resources/myImg.png");
  22.  *     ResourceManager.defineSound("resources/mySound.wav");
  23.  *     ResourceManager.loadResources();
  24.  * }
  25.  *
  26.  * public void update(long elapsedTime){
  27.  *     switch (getState()){
  28.  *         case GAME_LOADING:
  29.  *              // Loading completed
  30.  *              if (!ResourceManager.isLoading()){
  31.  *                  // Get the loaded image
  32.  *                  myImg = ResourceManager.getImage("resources/myImg.png");
  33.  *                  // Get the loaded sounded
  34.  *                  mySound = ResourceManager.getSound("resources/mySound.wav");
  35.  *                  // Change game state
  36.  *                  setState(GameState.GAME_PLAYING);
  37.  *              }
  38.  *              break;
  39.  *         case GAME_PLAYING:
  40.  *              .....
  41.  *     }
  42.  * }
  43.  * </pre>
  44.  *
  45.  * @author Sri Harsha Chilakapati
  46.  */
  47. public class ResourceManager extends Thread {
  48.  
  49.     // Prevent instantiation by others
  50.     private ResourceManager() {
  51.     }
  52.  
  53.     // Private variables. To calculate the percentage
  54.     private static int numResources = 0;
  55.     private static int numLoaded = 0;
  56.  
  57.     // Maps to store images and sounds
  58.     private static final HashMap<String, Image> imgMap = new HashMap<String, Image>();
  59.     private static final HashMap<String, WavSound> sndMap = new HashMap<String, WavSound>();
  60.  
  61.     // Lists of images and sounds to load
  62.     private static final ArrayList<String> images = new ArrayList<String>();
  63.     private static final ArrayList<String> sounds = new ArrayList<String>();
  64.  
  65.     /**
  66.      * Loads the resources in a separate thread
  67.      */
  68.     public final void run(){
  69.         System.out.println("Total resources :   " + numResources);
  70.         Game.setState(GameState.GAME_LOADING);
  71.         for (int i = 0; i < images.size(); i++) {
  72.             String imageName = images.get(i);
  73.             Image image = Game.loadImage(imageName);
  74.             System.out.println("Loading " + imageName);
  75.             imgMap.put(imageName, image);
  76.             numLoaded++;
  77.         }
  78.         images.clear();
  79.         for (int i = 0; i < sounds.size(); i++) {
  80.             String soundName = sounds.get(i);
  81.             WavSound sound = WavPlayer.loadSound(soundName);
  82.             System.out.println("Loading " + soundName);
  83.             sndMap.put(soundName, sound);
  84.             numLoaded++;
  85.         }
  86.         sounds.clear();
  87.         System.out.println("Loaded resources :   " + numLoaded);
  88.     }
  89.  
  90.     /**
  91.      * Defines an image to load
  92.      *
  93.      * @param imgName The name of the image
  94.      */
  95.     public static final void defineImage(String imgName){
  96.         images.add(imgName);
  97.         numResources++;
  98.     }
  99.  
  100.     /**
  101.      * Defines a sound to load
  102.      *
  103.      * @param sndName The name of the sound
  104.      */
  105.     public static final void defineSound(String sndName){
  106.         sounds.add(sndName);
  107.         numResources++;
  108.     }
  109.  
  110.     /**
  111.      * Starts loading resources in a new thread
  112.      */
  113.     public static final void loadResources(){
  114.         numLoaded = 0;
  115.         new ResourceManager().start();
  116.     }
  117.  
  118.     /**
  119.      * Gets a loaded image if exists in the map
  120.      *
  121.      * @param imgName The name of the image
  122.      * @return The loaded image
  123.      */
  124.     public static final Image getImage(String imgName){
  125.         reset();
  126.         return imgMap.get(imgName);
  127.     }
  128.  
  129.     /**
  130.      * Gets a loaded sound if exists in the map
  131.      *
  132.      * @param sndName The name of the sound
  133.      * @return The loaded sound
  134.      */
  135.     public static final WavSound getSound(String sndName){
  136.         reset();
  137.         return sndMap.get(sndName);
  138.     }
  139.  
  140.     /**
  141.      * Calculates the load percentage of this resource manager
  142.      *
  143.      * @return The percentage of loading resources
  144.      */
  145.     public static final int getLoadPercentage(){
  146.         return (int) ((float) ((float) numLoaded / (float) numResources) * 100);
  147.     }
  148.  
  149.     /**
  150.      * Checks if the manager is loading any resources
  151.      *
  152.      * @return True if loading, else false
  153.      */
  154.     public static final boolean isLoading(){
  155.         return !(numResources == numLoaded);
  156.     }
  157.  
  158.     /**
  159.      * Resets this resource manager to use multiple times
  160.      */
  161.     public static final void reset(){
  162.         numResources = 0;
  163.         numLoaded = 0;
  164.         images.clear();
  165.         sounds.clear();
  166.     }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment