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 package rtype;
023
024 import java.awt.image.BufferedImage;
025 import java.awt.image.DataBufferByte;
026 import java.io.IOException;
027 import java.nio.ByteBuffer;
028 import java.nio.ByteOrder;
029 import java.nio.IntBuffer;
030
031 import javax.imageio.ImageIO;
032
033 import org.lwjgl.opengl.GL11;
034
035 import rtype.entity.IEntity;
036
037 import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
038
039 public class WorkAroundTextureLoader implements ITextureLoader
040 {
041 private static Texture[] textures = new Texture[1024];
042 private static Texture[][] animations = new Texture[1024][1024];
043
044
045 public WorkAroundTextureLoader()
046 {
047 }
048
049 /* (non-Javadoc)
050 * @see rtype.ITextureLoader#init()
051 */
052 public void init()
053 {
054 textures[IEntity.PLANET] = loadTexture("/Data/Stage1Layer1.png",510,510,512,512);
055
056 textures[IEntity.SPACE_TRASH_1] = loadTexture("/Data/Stage1Layer1.png",256,438,256,256); // Big huge spaceship with 5 reactors
057 textures[IEntity.SPACE_TRASH_2] = loadTexture("/Data/Stage1Layer1.png",257,700,256,256);
058 textures[IEntity.SPACE_TRASH_3] = loadTexture("/Data/Stage1Layer1.png",373,956,64,64); // Round trash
059 textures[IEntity.SPACE_TRASH_4] = loadTexture("/Data/Stage1Layer1.png",492,102,256,128); // Sattelite trash
060 textures[IEntity.STAR_1] = loadTexture("/Data/Stage1Layer1.png",612,1011,8,8);
061 textures[IEntity.STAR_2] = loadTexture("/Data/Stage1Layer1.png",649,990,16,32);
062 textures[IEntity.STAR_3] = loadTexture("/Data/Stage1Layer1.png",674,994,16,16);
063 textures[IEntity.STAR_4] = loadTexture("/Data/Stage1Layer1.png",716,1004,16,16);
064 textures[IEntity.STAR_5] = loadTexture("/Data/Stage1Layer1.png",738,990,32,32);
065 textures[IEntity.STAR_6] = loadTexture("/Data/Stage1Layer1.png",780,994,16,16);
066
067 textures[IEntity.BULLET] = loadTexture("/Data/BulletSet1.png",32,00,32,32);
068
069 textures[IEntity.ORB_BEAM] = loadTexture("/Data/GlowBullets.png",32,0,32,32);
070 textures[IEntity.ENEMY_BULLET] = loadTexture("/Data/BulletSet1.png",0,0,32,32);
071 textures[IEntity.ENEMY_PIECE_1] = loadTexture("/Data/SpaceTrash.png",00,00,32,32);
072 textures[IEntity.ENEMY_PIECE_2] = loadTexture("/Data/SpaceTrash.png",32,00,32,32);
073 textures[IEntity.ENEMY_PIECE_3] = loadTexture("/Data/SpaceTrash.png",64,00,32,32);
074 textures[IEntity.ENEMY_PIECE_4] = loadTexture("/Data/SpaceTrash.png",96,00,32,32);
075 textures[IEntity.ENEMY_PIECE_5] = loadTexture("/Data/SpaceTrash.png",00,32,32,32);
076 textures[IEntity.ENEMY_PIECE_6] = loadTexture("/Data/SpaceTrash.png",32,32,32,32);
077 textures[IEntity.ENEMY_PIECE_7] = loadTexture("/Data/SpaceTrash.png",64,32,32,32);
078 textures[IEntity.ENEMY_PIECE_8] = loadTexture("/Data/SpaceTrash.png",96,32,32,32);
079 textures[IEntity.BULLET_RAPID_FIRE] = loadTexture("/Data/BulletSet1.png",00,64,32,32);
080 textures[IEntity.BULLET_MULTI_SHOOT] = loadTexture("/Data/BulletSet1.png",0,32,32,32);
081 textures[IEntity.BULLET_BASE] = loadTexture("/Data/BulletSet1.png",32,32,32,32);
082 textures[IEntity.SPACE_TRASH_5] = loadTexture("/Data/Stage1Layer1.png",148,721,64,64); // Extreme left trash
083
084 textures[IEntity.FORCEBLAST] = loadTexture("/Data/forceblast.png",0,0,128,64);
085
086 //textures[IEntity.SCANLINE] = loadTexture("Data/Scanlines.png",0,0,32,32);
087
088 animations[IEntity.PLAYER_SHIP] = loadAnimation("/Data/PlayerShip.png",4,4,128,128);
089 animations[IEntity.GREEN_ORB] = loadAnimation("/Data/Orb1.png",8,2,64,64);
090 animations[IEntity.BLUE_ORB] = loadAnimation("/Data/Orb1.png",8,2,64,64,0,128);
091 animations[IEntity.PINK_ORB] = loadAnimation("/Data/Orb1.png",8,2,64,64,0,256);
092 animations[IEntity.RED_ORB] = loadAnimation("/Data/Orb1.png",8,2,64,64,0,384);
093 animations[IEntity.FORCE_CHARGE] = loadAnimation("/Data/forceCharge.png",8,4,64,64);
094 animations[IEntity.PLAYER_SPEED] = loadAnimation("/Data/PlayerSpeed.png",8,4,64,64);
095 animations[IEntity.EXPLOSION1] = loadAnimation("/Data/Explosion2.png",8,4,64,64);
096 animations[IEntity.EXPLOSION2] = loadAnimation("/Data/ExplosionSmall.png",8,4,128,128);
097 animations[IEntity.LADYBIRD] = loadAnimation("/Data/Ladybird.png",4,4,64,64);
098 animations[IEntity.FIRE_BALL] = loadAnimation("/Data/Fireball.png",4,4,128,128);
099 animations[IEntity.FONT] = loadAnimation("/Data/Fonts.png",16,16,32,32);
100 animations[IEntity.BULLET_HIT_YELLOW] = loadAnimation("/Data/BulletHit.png",4,2,32,32);
101 animations[IEntity.BULLET_HIT_GREEN] = loadAnimation("/Data/BulletHit2.png",4,2,32,32);
102 animations[IEntity.BULLET_HIT_BLUE] = loadAnimation("/Data/BulletHit3.png",4,2,32,32);
103 animations[IEntity.BIT_UPGRADE] = loadAnimation("/Data/BitUpgrade.png",4,2,32,32);
104 animations[IEntity.BONUS_MULTI_SHOOT_ORB] = loadAnimation("/Data/PowerUp1.png",8,1,64,64,00,00);
105 animations[IEntity.BONUS_LIGHTNING_ORB] = loadAnimation("/Data/PowerUp1.png",8,1,64,64,00,64);
106 animations[IEntity.BONUS_RAPID_SHOOT_ORB] = loadAnimation("/Data/PowerUp1.png",8,1,64,64,00,128);
107 animations[IEntity.BONUS_GRAVITY_ORB] = loadAnimation("/Data/PowerUp1.png",8,1,64,64,00,192);
108 animations[IEntity.BONUS_SPEED] = loadAnimation("/Data/PowerUp1.png",8,1,64,64,00,256);
109 animations[IEntity.BONUS_CRYSTAL_ORB]= loadAnimation("/Data/PowerUp1.png",8,1,64,64,00,320);
110 animations[IEntity.BONUS_BOOSTER] = loadAnimation("/Data/PowerUp1.png",8,1,64,64,00,384);
111 animations[IEntity.IMPLOSION] = loadAnimation("/Data/Implosion.png",8,4,64,64);
112 animations[IEntity.MISSILE] = loadAnimation("/Data/HomingMissile.png",4,4,64,64);
113 animations[IEntity.SMOKE] = loadAnimation("/Data/SmokePuff.png",4,4,32,32);
114 }
115
116 /* (non-Javadoc)
117 * @see rtype.ITextureLoader#getTexture(int)
118 */
119 public Texture getTexture(int textureID)
120 {
121 return textures[textureID];
122 }
123
124 /* (non-Javadoc)
125 * @see rtype.ITextureLoader#getAnimation(int)
126 */
127 public Texture[] getAnimation(int animationID)
128 {
129 return animations[animationID];
130 }
131
132 Hashtable imageCache = new Hashtable();
133
134 // Offset are in term off pixel, not byte, the image loader figure out alone what is the bytesPerPixel
135 private Texture loadTexture(String path,int xOffSet, int yOffSet, int textWidth, int textHeight) {
136
137
138 BufferedImage buffImage = null;
139 try
140 {
141 if (imageCache.get(path) != null)
142 buffImage = (BufferedImage)imageCache.get(path);
143 else
144 {
145 System.out.println("Loading image:"+path);
146 buffImage = ImageIO.read(this.getClass().getResource(path));
147 imageCache.put(path, buffImage);
148 }
149 }
150 catch (IOException e)
151 {
152 e.printStackTrace();
153 }
154
155 int bytesPerPixel = buffImage.getColorModel().getPixelSize() / 8;
156
157 ByteBuffer scratch = ByteBuffer.allocateDirect(textWidth*textHeight*bytesPerPixel).order(ByteOrder.nativeOrder());
158 DataBufferByte data = ((DataBufferByte) buffImage.getRaster().getDataBuffer());
159
160 for (int i = 0 ; i < textHeight ; i++)
161 scratch.put(data.getData(),(xOffSet+(yOffSet+i)*buffImage.getWidth())*bytesPerPixel, textWidth * bytesPerPixel);
162
163 scratch.rewind();
164
165
166 // Create A IntBuffer For Image Address In Memory
167 IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
168 GL11.glGenTextures(buf); // Create Texture In OpenGL
169
170 // Create Nearest Filtered Texture
171 GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
172 GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
173 GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
174 GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,GL11.GL_RGBA,textWidth,textHeight, 0,GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, scratch);
175
176 Texture newTexture = new Texture();
177 newTexture.textureId = buf.get(0); // Return Image Addresses In Memory
178 newTexture.textureHeight = textHeight;
179 newTexture.textureWidth = textWidth;
180
181 return newTexture;
182 }
183
184
185
186
187 private Texture[] loadAnimation(String path,int cols, int rows, int textWidth, int textHeight)
188 {
189 return loadAnimation(path,cols,rows,textWidth,textHeight,0,0);
190 }
191
192 private Texture[] loadAnimation(String path,int cols, int rows, int textWidth, int textHeight, int xOffSet, int yOffSet)
193 {
194 Texture[] toReturntextures = new Texture[cols*rows];
195
196
197
198 for (int i=0;i< rows ; i++)
199 for (int j=0;j< cols ; j++)
200 {
201 toReturntextures[i*cols+j] = loadTexture(path,j*textWidth+xOffSet,i*textHeight+yOffSet,textWidth,textHeight);
202 }
203
204 return toReturntextures;
205 }
206
207 }
|