package net.mkonrad.synes.util;

import java.io.IOException;
import java.util.Hashtable;

import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.swing.JComponent;

import net.mkonrad.synes.gui.SceneCore;


import com.sun.j3d.utils.image.TextureLoader;

public class TextureTable extends Hashtable<String, Texture2D>
{
//  private JComponent _imgObserver;
//  
//  public TextureTable(JComponent imgObserver)
//  {
//    _imgObserver = imgObserver;
//  }
  
  private boolean loadTexture(String name)
  {
    if (this.containsKey(name)) //is already loaded
      return false;
    
    //TextureLoader loader = new TextureLoader(name, _imgObserver);
    TextureLoader loader = new TextureLoader(name, null);
    ImageComponent2D img = loader.getImage();

    DbgTools.print("Loading texture " + name + " with size: " + img.getWidth() + "x" + img.getHeight());
    
    Texture2D tex = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
        img.getWidth(), img.getHeight());
    tex.setImage(0, img);
    tex.setEnable(true);
    tex.setMagFilter(Texture.NICEST);
    
    this.put(name, tex);
    
    return true;
  }
  
  public Texture2D getTexture(String name)
  {
    name = SceneCore.getInstrTextureDir() + "/" + name;
    
    loadTexture(name);
    
    return this.get(name);
  }
}
