DevilTextureLoader.java
001 /*
002  *
003  * Created: Jun  7 2006
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 
023 // 
024 package rtype;
025 
026 // Mostly commented out because LWJGL do not support DEVil anymore :( !
027 
028 import java.nio.ByteBuffer;
029 import java.nio.ByteOrder;
030 import java.nio.IntBuffer;
031 //import org.lwjgl.devil.IL;
032 //import org.lwjgl.devil.ILU;
033 import org.lwjgl.opengl.GL11;
034 
035 import rtype.entity.IEntity;
036 import rtype.entity.Logger;
037 
038 public class DevilTextureLoader //implements ITextureLoader 
039 {
040   /*
041   private static Texture[] textures = new Texture[1024];
042   private static Texture[][] animations = new Texture[1024][1024];
043   
044   public DevilTextureLoader () 
045   {
046     try
047     {
048       IL.create();
049           ILU.create();
050     }
051     catch(Exception e)
052     {
053       e.printStackTrace();
054     }
055   }
056   
057    // (non-Javadoc)
058    // @see rtype.ITextureLoader#init()
059    
060   public void init()
061   {
062     
063     textures[IEntity.PLANET] = loadTexture("Data/Stage1Layer1.png",550,473,512,512);
064     textures[IEntity.SPACE_TRASH_1] = loadTexture("Data/Stage1Layer1.png",256,438,256,256); // Big huge spaceship with 5 reactors
065     textures[IEntity.SPACE_TRASH_2] = loadTexture("Data/Stage1Layer1.png",257,700,256,256); 
066     textures[IEntity.SPACE_TRASH_3] = loadTexture("Data/Stage1Layer1.png",373,956,64,64); // Round trash
067     textures[IEntity.SPACE_TRASH_4] = loadTexture("Data/Stage1Layer1.png",492,102,256,128); // Sattelite trash
068     textures[IEntity.STAR_1] = loadTexture("Data/Stage1Layer1.png",612,1011,8,8);
069     textures[IEntity.STAR_2] = loadTexture("Data/Stage1Layer1.png",649,990,16,32);
070     textures[IEntity.STAR_3] = loadTexture("Data/Stage1Layer1.png",674,994,16,16);
071     textures[IEntity.STAR_4] = loadTexture("Data/Stage1Layer1.png",716,1004,16,16);
072     textures[IEntity.STAR_5] = loadTexture("Data/Stage1Layer1.png",738,990,32,32);
073     textures[IEntity.STAR_6] = loadTexture("Data/Stage1Layer1.png",780,994,16,16);
074     textures[IEntity.SCANLINE] = loadTexture("Data/Scanlines.png",0,0,32,32);
075     textures[IEntity.BULLET] = loadTexture("Data/BulletSet1.png",32,00,32,32);
076     textures[IEntity.FORCEBLAST] = loadTexture("Data/forceblast.png",0,0,128,64);
077     textures[IEntity.ORB_BEAM] = loadTexture("Data/GlowBullets.png",32,0,32,32);
078     textures[IEntity.ENEMY_BULLET] = loadTexture("Data/BulletSet1.png",0,0,32,32);
079     textures[IEntity.ENEMY_PIECE_1] = loadTexture("Data/SpaceTrash.png",00,00,32,32);
080     textures[IEntity.ENEMY_PIECE_2] = loadTexture("Data/SpaceTrash.png",32,00,32,32);
081     textures[IEntity.ENEMY_PIECE_3] = loadTexture("Data/SpaceTrash.png",64,00,32,32);
082     textures[IEntity.ENEMY_PIECE_4] = loadTexture("Data/SpaceTrash.png",96,00,32,32);
083     textures[IEntity.ENEMY_PIECE_5] = loadTexture("Data/SpaceTrash.png",00,32,32,32);
084     textures[IEntity.ENEMY_PIECE_6] = loadTexture("Data/SpaceTrash.png",32,32,32,32);
085     textures[IEntity.ENEMY_PIECE_7] = loadTexture("Data/SpaceTrash.png",64,32,32,32);
086     textures[IEntity.ENEMY_PIECE_8] = loadTexture("Data/SpaceTrash.png",96,32,32,32);
087     textures[IEntity.BULLET_RAPID_FIRE] = loadTexture("Data/BulletSet1.png",00,64,32,32);
088     textures[IEntity.BULLET_MULTI_SHOOT] = loadTexture("Data/BulletSet1.png",0,32,32,32);
089     textures[IEntity.BULLET_BASE] = loadTexture("Data/BulletSet1.png",32,32,32,32);
090     textures[IEntity.SPACE_TRASH_5] = loadTexture("Data/Stage1Layer1.png",148,721,64,64); // Extreme left trash
091     
092     animations[IEntity.PLAYER_SHIP] = loadAnimation("Data/PlayerShip.png",4,4,128,128);
093     animations[IEntity.GREEN_ORB] = loadAnimation("Data/Orb1.png",8,2,64,64);
094     animations[IEntity.BLUE_ORB] = loadAnimation("Data/Orb1.png",8,2,64,64,0,128);
095     animations[IEntity.PINK_ORB] = loadAnimation("Data/Orb1.png",8,2,64,64,0,256);
096     animations[IEntity.RED_ORB] = loadAnimation("Data/Orb1.png",8,2,64,64,0,384);
097     animations[IEntity.FORCE_CHARGE] = loadAnimation("Data/forcecharge.png",8,4,64,64);
098     animations[IEntity.PLAYER_SPEED] = loadAnimation("Data/PlayerSpeed.png",8,4,64,64);
099     animations[IEntity.EXPLOSION1] = loadAnimation("Data/Explosion2.png",8,4,64,64);
100     animations[IEntity.EXPLOSION2] = loadAnimation("Data/ExplosionSmall.png",8,4,128,128);
101     animations[IEntity.LADYBIRD]   = loadAnimation("Data/ladybird.png",4,4,64,64);
102     animations[IEntity.FIRE_BALL]   = loadAnimation("Data/FireBall.png",4,4,128,128);
103     animations[IEntity.FONT]   = loadAnimation("Data/Fonts.png",16,16,32,32);
104     animations[IEntity.BULLET_HIT_YELLOW] = loadAnimation("Data/BulletHit.png",4,2,32,32);
105     animations[IEntity.BULLET_HIT_GREEN]  = loadAnimation("Data/BulletHit2.png",4,2,32,32);
106     animations[IEntity.BULLET_HIT_BLUE]   = loadAnimation("Data/BulletHit3.png",4,2,32,32);
107     animations[IEntity.BIT_UPGRADE]   = loadAnimation("Data/BitUpgrade.png",4,2,32,32);
108     animations[IEntity.BONUS_MULTI_SHOOT_ORB]        = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,00);
109     animations[IEntity.BONUS_LIGHTNING_ORB]          = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,64);
110     animations[IEntity.BONUS_RAPID_SHOOT_ORB]  = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,128);
111     animations[IEntity.BONUS_GRAVITY_ORB]    = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,192);
112     animations[IEntity.BONUS_SPEED]       = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,256);
113     animations[IEntity.BONUS_CRYSTAL_ORB]= loadAnimation("Data/PowerUp1.png",8,1,64,64,00,320);
114     animations[IEntity.BONUS_BOOSTER] = loadAnimation("Data/PowerUp1.png",8,1,64,64,00,384);
115     animations[IEntity.IMPLOSION]   = loadAnimation("Data/Implosion.png",8,4,64,64);
116     animations[IEntity.MISSILE]   = loadAnimation("Data/HomingMissile.png",4,4,64,64);
117     animations[IEntity.SMOKE]   = loadAnimation("Data/SmokePuff.png",4,4,32,32);
118   }
119   
120   // (non-Javadoc)
121   // @see rtype.ITextureLoader#getTexture(int)
122    
123   public  Texture getTexture(int textureID)
124   {
125     return textures[textureID];
126   }
127   
128    // (non-Javadoc)
129    // @see rtype.ITextureLoader#getAnimation(int)
130    
131   public  Texture[] getAnimation(int animationID)
132   {
133     return animations[animationID];
134   }
135   
136   private  Texture loadTexture(String path,int xOffset, int yOffSet, int textWidth, int textHeight) {
137     System.out.println("Loading texture:"+path);
138     int ilType = 0;
139         int glType = 0;
140         
141         IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
142         IL.ilGenImages(image);
143         IL.ilBindImage(image.get(0));
144         IL.ilLoadImage(path);
145 
146         int bytesPerPixel = IL.ilGetInteger(IL.IL_IMAGE_BYTES_PER_PIXEL);
147         ByteBuffer scratch = ByteBuffer.allocateDirect(textWidth * textHeight  * bytesPerPixel);
148         
149         if(bytesPerPixel == 3) 
150         {
151             ilType = IL.IL_RGB;
152             glType = GL11.GL_RGB;
153         }
154         else 
155           if(bytesPerPixel == 4) 
156           {
157           
158               ilType = IL.IL_RGBA;
159               glType = GL11.GL_RGBA;
160           }
161         
162         
163         IL.ilCopyPixels(xOffset,yOffSet,0,textWidth,textHeight, 1,ilType, IL.IL_BYTE, scratch);
164         
165         // Create A IntBuffer For Image Address In Memory
166         IntBuffer buf = ByteBuffer.allocateDirect(12).order(ByteOrder.nativeOrder()).asIntBuffer();
167         GL11.glGenTextures(buf); // Create Texture In OpenGL
168 
169          // Create Nearest Filtered Texture
170         GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
171         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
172         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
173         GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,glType,textWidth,textHeight, 0,glType, GL11.GL_UNSIGNED_BYTE, scratch);
174 
175         Texture newTexture = new Texture();
176         newTexture.textureId = buf.get(0);     // Return Image Addresses In Memory
177         newTexture.textureHeight = textHeight;
178         newTexture.textureWidth = textWidth;
179         
180         return newTexture;
181     }
182 
183   private  Texture[] loadAnimation(String path,int cols, int rows, int textWidth, int textHeight) 
184   {
185     return loadAnimation(path,cols,rows,textWidth,textHeight,0,0);
186   }
187 
188   private  Texture[] loadAnimation(String path,int cols, int rows, int textWidth, int textHeight, int xOffSet, int yOffSet) 
189   {
190     System.out.println("Loading animation:"+path);
191     Texture[] toReturntextures = new Texture[cols*rows];
192     
193     int ilType = 0;
194         int glType = 0;
195         
196         IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
197         IL.ilGenImages(image);
198         IL.ilBindImage(image.get(0));
199         IL.ilLoadImage(path);
200 
201         int bytesPerPixel = IL.ilGetInteger(IL.IL_IMAGE_BYTES_PER_PIXEL);
202         ByteBuffer scratch = ByteBuffer.allocateDirect(textWidth * textHeight  * bytesPerPixel);
203         if(bytesPerPixel == 3) 
204         {
205             ilType = IL.IL_RGB;
206             glType = GL11.GL_RGB;
207         }
208         else 
209           if(bytesPerPixel == 4) 
210           {
211           
212               ilType = IL.IL_RGBA;
213               glType = GL11.GL_RGBA;
214           }
215         
216             
217     for (int i=0;i< rows ; i++)
218       for (int j=0;j< cols ; j++)
219       {
220         IL.ilCopyPixels(xOffSet+j*textWidth,yOffSet+i*textHeight,0,textWidth,textHeight, 1,ilType, IL.IL_BYTE, scratch);
221             
222             // Create A IntBuffer For Image Address In Memory
223             IntBuffer buf = ByteBuffer.allocateDirect(12).order(ByteOrder.nativeOrder()).asIntBuffer();
224             GL11.glGenTextures(buf); // Create Texture In OpenGL
225 
226              // Create Nearest Filtered Texture
227             GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
228             GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
229             GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
230             GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,glType,textWidth,textHeight, 0,glType, GL11.GL_UNSIGNED_BYTE, scratch);
231 
232             Texture newTexture = new Texture();
233             newTexture.textureId = buf.get(0);     // Return Image Addresses In Memory
234             newTexture.textureHeight = textHeight;
235             newTexture.textureWidth = textWidth;
236             
237             toReturntextures[i*cols+j] = newTexture;
238             toReturntextures[i*cols+j].textureHeight = textHeight;
239             toReturntextures[i*cols+j].textureWidth = textWidth;
240       }
241     
242     return toReturntextures;
243   }
244   */
245 }