JNLPTextureLoader.java
001 /*
002  *
003  * Created: Jan  7 2008
004  *
005  * Copyright (C) 1999-2000 Fabien Sanglard
006  
007  * This program is free software; you can redistribute it and/or
008  * modify it under the terms of the GNU General Public License
009  * as published by the Free Software Foundation; either version 2
010  * of the License, or (at your option) any later version.
011  
012  * This program is distributed in the hope that it will be useful,
013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015  * GNU General Public License for more details.
016  
017  * You should have received a copy of the GNU General Public License
018  * along with this program; if not, write to the Free Software
019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020  */
021 
022 package rtype;
023 
024 import java.awt.image.BufferedImage;
025 import java.awt.image.DataBufferByte;
026 import java.awt.image.Raster;
027 import java.io.File;
028 import java.io.IOException;
029 import java.nio.ByteBuffer;
030 import java.nio.ByteOrder;
031 import java.nio.IntBuffer;
032 import java.util.HashMap;
033 import java.util.HashSet;
034 import java.util.Iterator;
035 import java.util.Set;
036 
037 import javax.imageio.ImageIO;
038 import javax.imageio.ImageReader;
039 import javax.imageio.stream.FileImageInputStream;
040 
041 //import org.lwjgl.devil.IL;
042 import org.lwjgl.opengl.GL11;
043 
044 import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
045 
046 import rtype.entity.IEntity;
047 import rtype.entity.Logger;
048 
049 
050 public class JNLPTextureLoader implements ITextureLoader 
051 {
052   private static Texture[] textures = new Texture[1024];
053   private static Texture[][] animations = new Texture[1024][1024];
054   
055   
056   public JNLPTextureLoader()
057   {    
058     }
059 
060   /* (non-Javadoc)
061    * @see rtype.ITextureLoader#init()
062    */
063   public void init()
064   {
065     textures[IEntity.PLANET= loadTexture("Data/Stage1Layer1.png",490,473,512,512);
066     
067     textures[IEntity.SPACE_TRASH_1= loadTexture("Data/Stage1Layer1.png",256,438,256,256)// Big huge spaceship with 5 reactors
068     textures[IEntity.SPACE_TRASH_2= loadTexture("Data/Stage1Layer1.png",257,700,256,256)
069     textures[IEntity.SPACE_TRASH_3= loadTexture("Data/Stage1Layer1.png",373,956,64,64)// Round trash
070     textures[IEntity.SPACE_TRASH_4= loadTexture("Data/Stage1Layer1.png",492,102,256,128)// Sattelite trash
071     textures[IEntity.STAR_1= loadTexture("Data/Stage1Layer1.png",612,1011,8,8);
072     textures[IEntity.STAR_2= loadTexture("Data/Stage1Layer1.png",649,990,16,32);
073     textures[IEntity.STAR_3= loadTexture("Data/Stage1Layer1.png",674,994,16,16);
074     textures[IEntity.STAR_4= loadTexture("Data/Stage1Layer1.png",716,1004,16,16);
075     textures[IEntity.STAR_5= loadTexture("Data/Stage1Layer1.png",738,990,32,32);
076     textures[IEntity.STAR_6= loadTexture("Data/Stage1Layer1.png",780,994,16,16);
077     
078     textures[IEntity.BULLET= loadTexture("Data/BulletSet1.png",32,00,32,32);
079     
080     textures[IEntity.ORB_BEAM= loadTexture("Data/GlowBullets.png",32,0,32,32);
081     textures[IEntity.ENEMY_BULLET= loadTexture("Data/BulletSet1.png",0,0,32,32);
082     textures[IEntity.ENEMY_PIECE_1= loadTexture("Data/SpaceTrash.png",00,00,32,32);
083     textures[IEntity.ENEMY_PIECE_2= loadTexture("Data/SpaceTrash.png",32,00,32,32);
084     textures[IEntity.ENEMY_PIECE_3= loadTexture("Data/SpaceTrash.png",64,00,32,32);
085     textures[IEntity.ENEMY_PIECE_4= loadTexture("Data/SpaceTrash.png",96,00,32,32);
086     textures[IEntity.ENEMY_PIECE_5= loadTexture("Data/SpaceTrash.png",00,32,32,32);
087     textures[IEntity.ENEMY_PIECE_6= loadTexture("Data/SpaceTrash.png",32,32,32,32);
088     textures[IEntity.ENEMY_PIECE_7= loadTexture("Data/SpaceTrash.png",64,32,32,32);
089     textures[IEntity.ENEMY_PIECE_8= loadTexture("Data/SpaceTrash.png",96,32,32,32);
090     textures[IEntity.BULLET_RAPID_FIRE= loadTexture("Data/BulletSet1.png",00,64,32,32);
091     textures[IEntity.BULLET_MULTI_SHOOT= loadTexture("Data/BulletSet1.png",0,32,32,32);
092     textures[IEntity.BULLET_BASE= loadTexture("Data/BulletSet1.png",32,32,32,32);
093     textures[IEntity.SPACE_TRASH_5= loadTexture("Data/Stage1Layer1.png",148,721,64,64)// Extreme left trash
094     
095     textures[IEntity.FORCEBLAST= loadTexture("Data/forceblast.png",0,0,128,64);
096     
097     
098     
099     //textures[IEntity.SCANLINE] = loadTexture("Data/Scanlines.png",0,0,32,32);
100     
101     
102     animations[IEntity.PLAYER_SHIP= loadAnimation("Data/PlayerShip.png",4,4,128,128);
103     animations[IEntity.GREEN_ORB= loadAnimation("Data/Orb1.png",8,2,64,64);
104     animations[IEntity.BLUE_ORB= loadAnimation("Data/Orb1.png",8,2,64,64,0,128);
105     animations[IEntity.PINK_ORB= loadAnimation("Data/Orb1.png",8,2,64,64,0,256);
106     animations[IEntity.RED_ORB= loadAnimation("Data/Orb1.png",8,2,64,64,0,384);
107     animations[IEntity.FORCE_CHARGE= loadAnimation("Data/forcecharge.png",8,4,64,64);
108     animations[IEntity.PLAYER_SPEED= loadAnimation("Data/PlayerSpeed.png",8,4,64,64);
109     animations[IEntity.EXPLOSION1= loadAnimation("Data/Explosion2.png",8,4,64,64);
110     animations[IEntity.EXPLOSION2= loadAnimation("Data/ExplosionSmall.png",8,4,128,128);
111     animations[IEntity.LADYBIRD]   = loadAnimation("Data/ladybird.png",4,4,64,64);
112     animations[IEntity.FIRE_BALL]   = loadAnimation("Data/FireBall.png",4,4,128,128);
113     animations[IEntity.FONT]   = loadAnimation("Data/Fonts.png",16,16,32,32);
114     animations[IEntity.BULLET_HIT_YELLOW= loadAnimation("Data/BulletHit.png",4,2,32,32);
115     animations[IEntity.BULLET_HIT_GREEN]  = loadAnimation("Data/BulletHit2.png",4,2,32,32);
116     animations[IEntity.BULLET_HIT_BLUE]   = loadAnimation("Data/BulletHit3.png",4,2,32,32);
117     animations[IEntity.BIT_UPGRADE]   = loadAnimation("Data/BitUpgrade.png",4,2,32,32);
118     animations[IEntity.BONUS_MULTI_SHOOT_ORB]        = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,00);
119     animations[IEntity.BONUS_LIGHTNING_ORB]          = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,64);
120     animations[IEntity.BONUS_RAPID_SHOOT_ORB]  = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,128);
121     animations[IEntity.BONUS_GRAVITY_ORB]    = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,192);
122     animations[IEntity.BONUS_SPEED]       = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,256);
123     animations[IEntity.BONUS_CRYSTAL_ORB]= loadAnimation("Data/PowerUp1.png",8,1,64,64,00,320);
124     animations[IEntity.BONUS_BOOSTER= loadAnimation("Data/PowerUp1.png",8,1,64,64,00,384);
125     animations[IEntity.IMPLOSION]   = loadAnimation("Data/Implosion.png",8,4,64,64);
126     animations[IEntity.MISSILE]   = loadAnimation("Data/HomingMissile.png",4,4,64,64);
127     animations[IEntity.SMOKE]   = loadAnimation("Data/SmokePuff.png",4,4,32,32);
128   }
129   
130   /* (non-Javadoc)
131    * @see rtype.ITextureLoader#getTexture(int)
132    */
133   public  Texture getTexture(int textureID)
134   {
135     return textures[textureID];
136   }
137   
138   /* (non-Javadoc)
139    * @see rtype.ITextureLoader#getAnimation(int)
140    */
141   public  Texture[] getAnimation(int animationID)
142   {
143     return animations[animationID];
144   }
145   
146   Hashtable imageCache = new Hashtable();
147   
148   // Offset are in term off pixel, not byte, the image loader figure out alone what is the bytesPerPixel
149   private  Texture loadTexture(String path,int xOffSet, int yOffSet, int textWidth, int textHeight) {
150     
151     
152     BufferedImage buffImage = null;
153     try
154     {
155       if (imageCache.get(path!= null)
156         buffImage = (BufferedImage)imageCache.get(path);
157       else
158       {
159         System.out.println("Loading image:"+path);
160         buffImage = ImageIO.read(new FileImageInputStreamnew File(path)));
161         imageCache.put(path, buffImage);
162       }
163     }
164     catch (IOException e)
165     {
166       e.printStackTrace();
167     }
168           
169     int bytesPerPixel = buffImage.getColorModel().getPixelSize() 8;
170     
171     ByteBuffer scratch = ByteBuffer.allocateDirect(textWidth*textHeight*bytesPerPixel).order(ByteOrder.nativeOrder());  
172     DataBufferByte data = ((DataBufferBytebuffImage.getRaster().getDataBuffer());
173     
174     for (int i = ; i < textHeight ; i++)
175       scratch.put(data.getData(),(xOffSet+(yOffSet+i)*buffImage.getWidth())*bytesPerPixel, textWidth * bytesPerPixel);
176     
177         scratch.rewind();
178 
179         
180         // Create A IntBuffer For Image Address In Memory
181         IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
182         GL11.glGenTextures(buf)// Create Texture In OpenGL
183 
184          // Create Nearest Filtered Texture
185         GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
186         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
187         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
188         GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,GL11.GL_RGBA,textWidth,textHeight, 0,GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, scratch);
189 
190         Texture newTexture = new Texture();
191         newTexture.textureId = buf.get(0);     // Return Image Addresses In Memory
192         newTexture.textureHeight = textHeight;
193         newTexture.textureWidth = textWidth;
194         
195         return newTexture;
196     }
197 
198   
199 
200   
201   private  Texture[] loadAnimation(String path,int cols, int rows, int textWidth, int textHeight
202   {
203     return loadAnimation(path,cols,rows,textWidth,textHeight,0,0);
204   }
205 
206   private  Texture[] loadAnimation(String path,int cols, int rows, int textWidth, int textHeight, int xOffSet, int yOffSet
207   {
208     Texture[] toReturntextures = new Texture[cols*rows];
209     
210     
211     BufferedImage buffImage = null;
212     try
213     {
214       if (imageCache.get(path!= null)
215         buffImage = (BufferedImage)imageCache.get(path);
216       else
217       {
218         System.out.println("Loading image:"+path);
219         buffImage = ImageIO.read(new FileImageInputStreamnew File(path)));
220         imageCache.put(path, buffImage);
221       }
222     }
223     catch (IOException e)
224     {
225       e.printStackTrace();
226     }
227         int bytesPerPixel = buffImage.getColorModel().getPixelSize() 8;
228         
229         
230         ByteBuffer scratch = ByteBuffer.allocateDirect(textWidth*textHeight*bytesPerPixel).order(ByteOrder.nativeOrder());
231          
232         byte[] data = ((DataBufferBytebuffImage.getRaster().getDataBuffer()). getData()
233                
234             
235     for (int i=0;i< rows ; i++)
236       for (int j=0;j< cols ; j++)
237       {
238         toReturntextures[i*cols+j= loadTexture(path,j*textWidth+xOffSet,i*textHeight+yOffSet,textWidth,textHeight);
239         /*
240         for (int z = 0 ; z < textHeight ; z ++)
241           scratch.put(data,(xOffSet+(yOffSet+z)*buffImage.getWidth())*bytesPerPixel  + (j * textWidth + i* buffImage.getWidth()) * bytesPerPixel, textWidth*bytesPerPixel);
242         
243         scratch.rewind();
244             
245             // Create A IntBuffer For Image Address In Memory
246             IntBuffer buf = ByteBuffer.allocateDirect(12).order(ByteOrder.nativeOrder()).asIntBuffer();
247             GL11.glGenTextures(buf); // Create Texture In OpenGL
248 
249              // Create Nearest Filtered Texture
250             GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
251             GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
252             GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
253             GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,GL11.GL_RGBA,textWidth,textHeight, 0,GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, scratch);
254 
255             Texture newTexture = new Texture();
256             newTexture.textureId = buf.get(0);     // Return Image Addresses In Memory
257             newTexture.textureHeight = textHeight;
258             newTexture.textureWidth = textWidth;
259             
260             toReturntextures[i*cols+j] = newTexture;
261             toReturntextures[i*cols+j].textureHeight = textHeight;
262             toReturntextures[i*cols+j].textureWidth = textWidth;
263             */
264       }
265     
266     return toReturntextures;
267   }
268 
269 }