Added a main menu and fixed som things. added explotions to and fixed the economy
This commit is contained in:
parent
5bb373573d
commit
88771fad29
56 changed files with 1859 additions and 1999 deletions
|
|
@ -1,16 +1,22 @@
|
|||
package ei.engine.state;
|
||||
|
||||
import ei.engine.LWJGLGameWindow;
|
||||
import ei.engine.input.InputHandler;
|
||||
import ei.engine.renderer.Camera;
|
||||
import ei.engine.util.MultiPrintStream;
|
||||
|
||||
public abstract class GameState {
|
||||
private String name;
|
||||
private InputHandler input;
|
||||
private boolean enabled = false;
|
||||
private boolean init;
|
||||
private Camera cam;
|
||||
|
||||
public GameState(String name){
|
||||
this.name = name;
|
||||
input = new InputHandler();
|
||||
init = false;
|
||||
cam = new Camera();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -28,6 +34,15 @@ public abstract class GameState {
|
|||
*/
|
||||
public void setEnabled(boolean b){
|
||||
enabled = b;
|
||||
if(enabled){
|
||||
if(!init){
|
||||
init();
|
||||
init = true;
|
||||
}
|
||||
if(LWJGLGameWindow.getCamera() != cam){
|
||||
LWJGLGameWindow.setCamera(cam);
|
||||
}
|
||||
}
|
||||
MultiPrintStream.out.println("Enabling("+b+") State: "+getName());
|
||||
}
|
||||
|
||||
|
|
@ -63,6 +78,8 @@ public abstract class GameState {
|
|||
input.render();
|
||||
}
|
||||
|
||||
public abstract void init();
|
||||
|
||||
public abstract void update();
|
||||
|
||||
public abstract void render();
|
||||
|
|
|
|||
|
|
@ -19,6 +19,10 @@ public class GameStateTestState extends GameState{
|
|||
|
||||
public GameStateTestState(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
rootNode = new Node("InGameNode");
|
||||
|
||||
sprite1 = new Sprite("tank","data/units/tank.png");
|
||||
|
|
|
|||
|
|
@ -1,141 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2004 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package ei.engine.test.ex;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.openal.AL;
|
||||
import org.lwjgl.openal.AL10;
|
||||
import org.lwjgl.opengl.DisplayMode;
|
||||
|
||||
/**
|
||||
*
|
||||
* This is a basic test, which contains the most used stuff
|
||||
*
|
||||
* @author Brian Matzon <brian@matzon.dk>
|
||||
* @version $Revision$
|
||||
* $Id$
|
||||
*/
|
||||
public abstract class BasicSoundTest {
|
||||
|
||||
/**
|
||||
* Creates an instance of PlayTest
|
||||
*/
|
||||
public BasicSoundTest() {
|
||||
try {
|
||||
String[] imps = AL.getImplementations();
|
||||
if(imps.length > 0) {
|
||||
System.out.println("Available devices: ");
|
||||
for(int i=0; i<imps.length; i++) {
|
||||
System.out.println(" " + i + ": " + imps[i]);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Unable to query available devices: " + e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
AL.create();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Unable to create OpenAL.\nPlease make sure that OpenAL is available on this system. Exception: " + e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdowns OpenAL
|
||||
*/
|
||||
protected void alExit() {
|
||||
if(AL.isCreated()) {
|
||||
AL.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a float buffer to hold specified float array
|
||||
* - strictly a utility method
|
||||
*
|
||||
* @param array to hold
|
||||
* @return created FloatBuffer
|
||||
*/
|
||||
protected FloatBuffer createFloatBuffer(float[] data) {
|
||||
FloatBuffer temp = BufferUtils.createFloatBuffer(data.length).put(data);
|
||||
temp.flip();
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pauses the invoking thread for specified milliseconds
|
||||
*
|
||||
* @param time Milliseconds to sleep
|
||||
*/
|
||||
protected void pause(long time) {
|
||||
try {
|
||||
Thread.sleep(time);
|
||||
} catch (InterruptedException inte) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exits the test NOW, printing errorcode to stdout
|
||||
*
|
||||
* @param error Error code causing exit
|
||||
*/
|
||||
protected void exit(int error) {
|
||||
System.out.println("OpenAL Error: " + AL10.alGetString(error));
|
||||
alExit();
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the display mode for fullscreen mode
|
||||
*/
|
||||
protected boolean setDisplayMode() {
|
||||
try {
|
||||
// get modes
|
||||
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
|
||||
|
||||
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
|
||||
"width=" + 640,
|
||||
"height=" + 480,
|
||||
"freq=" + 60,
|
||||
"bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel()
|
||||
});
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,367 +0,0 @@
|
|||
package ei.engine.test.ex;
|
||||
|
||||
/*
|
||||
* This Code Was Created By Jeff Molofee 2000
|
||||
* Modified by Shawn T. to handle (%3.2f, num) parameters.
|
||||
* A HUGE Thanks To Fredric Echols For Cleaning Up
|
||||
* And Optimizing The Base Code, Making It More Flexible!
|
||||
* If You've Found This Code Useful, Please Let Me Know.
|
||||
* Visit My Site At nehe.gamedev.net
|
||||
*/
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.AffineTransformOp;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.IntBuffer;
|
||||
import java.text.NumberFormat;
|
||||
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.DisplayMode;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.glu.GLU;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
/**
|
||||
* @author Mark Bernard
|
||||
* date: 26-May-2004
|
||||
*
|
||||
* Port of NeHe's Lesson 13 to LWJGL
|
||||
* Title: Bitmap fonts
|
||||
* Uses version 0.9alpha of LWJGL http://www.lwjgl.org/
|
||||
*
|
||||
* Be sure that the LWJGL libraries are in your classpath
|
||||
*
|
||||
* Ported directly from the C++ version
|
||||
*
|
||||
* The main point of this tutorial is to get fonts on the screen. The original OpenGL did
|
||||
* not port directly as it used Windows specific extensions and I could not get some OpenGL
|
||||
* commands to work. In the end, what you see on the screen is the same, but it is written
|
||||
* somewhat differently. I have noted the differences in the code with comments.
|
||||
*
|
||||
* 2004-10-08: Updated to version 0.92alpha of LWJGL.
|
||||
* 2004-12-19: Updated to version 0.94alpha of LWJGL
|
||||
*/
|
||||
public class FontTest {
|
||||
private boolean done = false;
|
||||
private boolean fullscreen = false;
|
||||
private final String windowTitle = "NeHe's OpenGL Lesson 13 for LWJGL (Bitmap Fonts)";
|
||||
private boolean f1 = false;
|
||||
private DisplayMode displayMode;
|
||||
|
||||
private int texture;
|
||||
|
||||
//build colours for font with alpha transparency
|
||||
private static final Color OPAQUE_WHITE = new Color(0xFFFFFFFF, true);
|
||||
private static final Color TRANSPARENT_BLACK = new Color(0x00000000, true);
|
||||
private NumberFormat numberFormat = NumberFormat.getInstance();
|
||||
|
||||
private int base; // Base Display List For The Font Set
|
||||
private float cnt1; // 1st Counter Used To Move Text & For Coloring
|
||||
private float cnt2; // 2nd Counter Used To Move Text & For Coloring
|
||||
|
||||
public static void main(String args[]) {
|
||||
boolean fullscreen = false;
|
||||
if(args.length>0) {
|
||||
if(args[0].equalsIgnoreCase("fullscreen")) {
|
||||
fullscreen = true;
|
||||
}
|
||||
}
|
||||
|
||||
FontTest l13 = new FontTest();
|
||||
l13.run(fullscreen);
|
||||
}
|
||||
public void run(boolean fullscreen) {
|
||||
this.fullscreen = fullscreen;
|
||||
try {
|
||||
init();
|
||||
while (!done) {
|
||||
mainloop();
|
||||
render();
|
||||
Display.update();
|
||||
}
|
||||
cleanup();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
private void mainloop() {
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Exit if Escape is pressed
|
||||
done = true;
|
||||
}
|
||||
if(Display.isCloseRequested()) { // Exit if window is closed
|
||||
done = true;
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_F1) && !f1) { // Is F1 Being Pressed?
|
||||
f1 = true; // Tell Program F1 Is Being Held
|
||||
switchMode(); // Toggle Fullscreen / Windowed Mode
|
||||
}
|
||||
if(!Keyboard.isKeyDown(Keyboard.KEY_F1)) { // Is F1 Being Pressed?
|
||||
f1 = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void switchMode() {
|
||||
fullscreen = !fullscreen;
|
||||
try {
|
||||
Display.setFullscreen(fullscreen);
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean render() {
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
|
||||
GL11.glLoadIdentity(); // Reset The Current Modelview Matrix
|
||||
|
||||
// Position The Text On The Screen
|
||||
GL11.glTranslatef(-0.9f + 0.05f * ((float)Math.cos(cnt1)), 0.32f * ((float)Math.sin(cnt2)), -2.0f); // Move One Unit Into The Screen
|
||||
|
||||
// Pulsing Colors Based On Text Position
|
||||
float red = 1.0f * ((float)Math.cos(cnt1));
|
||||
float green = 1.0f * ((float)Math.sin(cnt2));
|
||||
float blue = 1.0f - 0.5f * ((float)Math.cos(cnt1 + cnt2));
|
||||
GL11.glColor3f(red, green, blue);
|
||||
|
||||
//format the floating point number to 2 decimal places
|
||||
numberFormat.setMaximumFractionDigits(2);
|
||||
numberFormat.setMinimumFractionDigits(2);
|
||||
|
||||
glPrint("Active OpenGL Text With NeHe - " + numberFormat.format(cnt1)); // Print GL Text To The Screen
|
||||
cnt1 += 0.051f; // Increase The First Counter
|
||||
cnt2 += 0.005f; // Increase The Second Counter
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Some liberties had to be taken with this method. I could not get the glCallLists() to work, so
|
||||
* it is done manually instead.
|
||||
*/
|
||||
private void glPrint(String msg) { // Custom GL "Print" Routine
|
||||
if(msg != null) {
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
|
||||
for(int i=0;i<msg.length();i++) {
|
||||
GL11.glCallList(base + msg.charAt(i));
|
||||
GL11.glTranslatef(0.05f, 0.0f, 0.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void createWindow() throws Exception {
|
||||
Display.setFullscreen(fullscreen);
|
||||
DisplayMode d[] = Display.getAvailableDisplayModes();
|
||||
for (int i = 0; i < d.length; i++) {
|
||||
if (d[i].getWidth() == 640
|
||||
&& d[i].getHeight() == 480
|
||||
&& d[i].getBitsPerPixel() == 32) {
|
||||
displayMode = d[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
Display.setDisplayMode(displayMode);
|
||||
Display.setTitle(windowTitle);
|
||||
Display.create();
|
||||
}
|
||||
private void init() throws Exception {
|
||||
createWindow();
|
||||
|
||||
initGL();
|
||||
buildFont();
|
||||
}
|
||||
private void initGL() {
|
||||
GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
|
||||
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
// Black Background
|
||||
GL11.glClearDepth(1.0); // Depth Buffer Setup
|
||||
GL11.glDepthFunc(GL11.GL_LEQUAL);
|
||||
GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
|
||||
//GL11.glEnable(GL11.GL_CULL_FACE);
|
||||
// The Type Of Depth Testing To Do
|
||||
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION);
|
||||
// Select The Projection Matrix
|
||||
GL11.glLoadIdentity(); // Reset The Projection Matrix
|
||||
|
||||
// Calculate The Aspect Ratio Of The Window
|
||||
GLU.gluPerspective(45.0f,
|
||||
(float) displayMode.getWidth() / (float) displayMode.getHeight(),
|
||||
0.1f,100.0f);
|
||||
GL11.glMatrixMode(GL11.GL_MODELVIEW);
|
||||
// Select The Modelview Matrix
|
||||
|
||||
// Really Nice Perspective Calculations
|
||||
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
|
||||
}
|
||||
private void cleanup() {
|
||||
Display.destroy();
|
||||
}
|
||||
|
||||
/* The original tutorial number 13 used a windows specific extension to generate a bitmap
|
||||
* for the font. I had to replace that with a custom bitmap generation that you see below.
|
||||
*
|
||||
*/
|
||||
private void buildFont() { // Build Our Bitmap Font
|
||||
Font font; // Font object
|
||||
|
||||
/* Note that I have set the font to Courier New. This font is not guraunteed to be on all
|
||||
* systems. However it is very common so it is likely to be there. You can replace this name
|
||||
* with any named font on your system or use the Java default names that are guraunteed to be there.
|
||||
* Also note that this will work well with monospace fonts, but does not look as good with
|
||||
* proportional fonts.
|
||||
*/
|
||||
String fontName = "Courier New"; // Name of the font to use
|
||||
BufferedImage fontImage; // image for creating the bitmap
|
||||
int bitmapSize = 512; // set the size for the bitmap texture
|
||||
boolean sizeFound = false;
|
||||
boolean directionSet = false;
|
||||
int delta = 0;
|
||||
int fontSize = 24;
|
||||
|
||||
/* To find out how much space a Font takes, you need to use a the FontMetrics class.
|
||||
* To get the FontMetrics, you need to get it from a Graphics context. A Graphics context is
|
||||
* only available from a displayable surface, ie any class that subclasses Component or any Image.
|
||||
* First the font is set on a Graphics object. Then get the FontMetrics and find out the width
|
||||
* and height of the widest character (W). Then take the largest of the 2 values and find the
|
||||
* maximum size font that will fit in the size allocated.
|
||||
*/
|
||||
while(!sizeFound) {
|
||||
font = new Font(fontName, Font.PLAIN, fontSize); // Font Name
|
||||
// use BufferedImage.TYPE_4BYTE_ABGR to allow alpha blending
|
||||
fontImage = new BufferedImage(bitmapSize, bitmapSize, BufferedImage.TYPE_4BYTE_ABGR);
|
||||
Graphics2D g = (Graphics2D)fontImage.getGraphics();
|
||||
g.setFont(font);
|
||||
FontMetrics fm = g.getFontMetrics();
|
||||
int width = fm.stringWidth("W");
|
||||
int height = fm.getHeight();
|
||||
int lineWidth = (width > height) ? width * 16 : height * 16;
|
||||
if(!directionSet) {
|
||||
if(lineWidth > bitmapSize) {
|
||||
delta = -2;
|
||||
}
|
||||
else {
|
||||
delta = 2;
|
||||
}
|
||||
directionSet = true;
|
||||
}
|
||||
if(delta > 0) {
|
||||
if(lineWidth < bitmapSize) {
|
||||
fontSize += delta;
|
||||
}
|
||||
else {
|
||||
sizeFound = true;
|
||||
fontSize -= delta;
|
||||
}
|
||||
}
|
||||
else if(delta < 0) {
|
||||
if(lineWidth > bitmapSize) {
|
||||
fontSize += delta;
|
||||
}
|
||||
else {
|
||||
sizeFound = true;
|
||||
fontSize -= delta;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Now that a font size has been determined, create the final image, set the font and draw the
|
||||
* standard/extended ASCII character set for that font.
|
||||
*/
|
||||
font = new Font(fontName, Font.BOLD, fontSize); // Font Name
|
||||
// use BufferedImage.TYPE_4BYTE_ABGR to allow alpha blending
|
||||
fontImage = new BufferedImage(bitmapSize, bitmapSize, BufferedImage.TYPE_4BYTE_ABGR);
|
||||
Graphics2D g = (Graphics2D)fontImage.getGraphics();
|
||||
g.setFont(font);
|
||||
g.setColor(OPAQUE_WHITE);
|
||||
g.setBackground(TRANSPARENT_BLACK);
|
||||
FontMetrics fm = g.getFontMetrics();
|
||||
for(int i=0;i<256;i++) {
|
||||
int x = i % 16;
|
||||
int y = i / 16;
|
||||
char ch[] = {(char)i};
|
||||
String temp = new String(ch);
|
||||
g.drawString(temp, (x * 32) + 1, (y * 32) + fm.getAscent());
|
||||
}
|
||||
|
||||
/* The following code is taken directly for the LWJGL example code.
|
||||
* It takes a Java Image and converts it into an OpenGL texture.
|
||||
* This is a very powerful feature as you can use this to generate textures on the fly out
|
||||
* of anything.
|
||||
*/
|
||||
// Flip Image
|
||||
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
|
||||
tx.translate(0, -fontImage.getHeight(null));
|
||||
AffineTransformOp op =
|
||||
new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
|
||||
fontImage = op.filter(fontImage, null);
|
||||
|
||||
// Put Image In Memory
|
||||
ByteBuffer scratch =
|
||||
ByteBuffer.allocateDirect(
|
||||
4 * fontImage.getWidth() * fontImage.getHeight());
|
||||
|
||||
byte data[] =
|
||||
(byte[])fontImage.getRaster().getDataElements(
|
||||
0,
|
||||
0,
|
||||
fontImage.getWidth(),
|
||||
fontImage.getHeight(),
|
||||
null);
|
||||
scratch.clear();
|
||||
scratch.put(data);
|
||||
scratch.rewind();
|
||||
|
||||
// Create A IntBuffer For Image Address In Memory
|
||||
IntBuffer buf =
|
||||
ByteBuffer
|
||||
.allocateDirect(4)
|
||||
.order(ByteOrder.nativeOrder())
|
||||
.asIntBuffer();
|
||||
GL11.glGenTextures(buf); // Create Texture In OpenGL
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
|
||||
// Typical Texture Generation Using Data From The Image
|
||||
|
||||
// Linear Filtering
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
|
||||
// Linear Filtering
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
|
||||
// Generate The Texture
|
||||
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, fontImage.getWidth(), fontImage.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, scratch);
|
||||
|
||||
|
||||
texture = buf.get(0); // Return Image Address In Memory
|
||||
|
||||
base = GL11.glGenLists(256); // Storage For 256 Characters
|
||||
|
||||
/* Generate the display lists. One for each character in the standard/extended ASCII chart.
|
||||
*/
|
||||
float textureDelta = 1.0f / 16.0f;
|
||||
for(int i=0;i<256;i++) {
|
||||
float u = ((float)(i % 16)) / 16.0f;
|
||||
float v = 1.f - (((float)(i / 16)) / 16.0f);
|
||||
GL11.glNewList(base + i, GL11.GL_COMPILE);
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
GL11.glTexCoord2f(u, v);
|
||||
GL11.glVertex3f(-0.0450f, 0.0450f, 0.0f);
|
||||
GL11.glTexCoord2f((u + textureDelta), v);
|
||||
GL11.glVertex3f(0.0450f, 0.0450f, 0.0f);
|
||||
GL11.glTexCoord2f((u + textureDelta), v - textureDelta);
|
||||
GL11.glVertex3f(0.0450f, -0.0450f, 0.0f);
|
||||
GL11.glTexCoord2f(u, v - textureDelta);
|
||||
GL11.glVertex3f(-0.0450f, -0.0450f, 0.0f);
|
||||
GL11.glEnd();
|
||||
GL11.glEndList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,333 +0,0 @@
|
|||
package ei.engine.test.ex;
|
||||
/*
|
||||
* This Code Was Created By Jeff Molofee and GB Schmick 2000
|
||||
* A HUGE Thanks To Fredric Echols For Cleaning Up
|
||||
* And Optimizing The Base Code, Making It More Flexible!
|
||||
* If You've Found This Code Useful, Please Let Me Know.
|
||||
* Visit Our Sites At www.tiptup.com and nehe.gamedev.net
|
||||
*/
|
||||
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.DisplayMode;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.glu.GLU;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import ei.engine.texture.Texture;
|
||||
import ei.engine.texture.TextureLoader;
|
||||
import ei.engine.util.MultiPrintStream;
|
||||
|
||||
/**
|
||||
* @author Mark Bernard
|
||||
* date: 23-Jun-2004
|
||||
*
|
||||
* Port of NeHe's Lesson 19 to LWJGL
|
||||
* Title: Particle Engine Using Triangle Strips
|
||||
* Uses version 0.9alpha of LWJGL http://www.lwjgl.org/
|
||||
*
|
||||
* Be sure that the LWJGL libraries are in your classpath
|
||||
*
|
||||
* Ported directly from the C++ version
|
||||
*
|
||||
* 2004-10-08: Updated to version 0.92alpha of LWJGL.
|
||||
* 2004-12-19: Updated to version 0.94alpha of LWJGL and to use
|
||||
* DevIL for image loading.
|
||||
*/
|
||||
public class ParticleTest {
|
||||
private boolean done = false;
|
||||
private boolean fullscreen = false;
|
||||
private final String windowTitle = "NeHe's OpenGL Lesson 19 for LWJGL (Particle Engine Using Triangle Strips)";
|
||||
private boolean f1 = false;
|
||||
private DisplayMode displayMode;
|
||||
|
||||
private final int MAX_PARTICLES = 1000;
|
||||
private Particle particle[];
|
||||
boolean rainbow = true; // Rainbow Mode?
|
||||
boolean sp; // Spacebar Pressed?
|
||||
boolean rp; // Enter Key Pressed?
|
||||
|
||||
float slowdown = 2.0f; // Slow Down Particles
|
||||
float xspeed; // Base X Speed (To Allow Keyboard Direction Of Tail)
|
||||
float yspeed; // Base Y Speed (To Allow Keyboard Direction Of Tail)
|
||||
float zoom = -20.0f; // Used To Zoom Out
|
||||
|
||||
int col; // Current Color Selection
|
||||
int delay; // Rainbow Effect Delay
|
||||
int texture; // Storage For Our Particle Texture
|
||||
|
||||
private static float colors[][]= // Rainbow Of Colors
|
||||
{
|
||||
{1.0f,0.5f,0.5f},{1.0f,0.75f,0.5f},{1.0f,1.0f,0.5f},{0.75f,1.0f,0.5f},
|
||||
{0.5f,1.0f,0.5f},{0.5f,1.0f,0.75f},{0.5f,1.0f,1.0f},{0.5f,0.75f,1.0f},
|
||||
{0.5f,0.5f,1.0f},{0.75f,0.5f,1.0f},{1.0f,0.5f,1.0f},{1.0f,0.5f,0.75f}
|
||||
};
|
||||
|
||||
public static void main(String args[]) {
|
||||
MultiPrintStream.makeInstance(new MultiPrintStream("log.txt"));
|
||||
boolean fullscreen = false;
|
||||
if(args.length>0) {
|
||||
if(args[0].equalsIgnoreCase("fullscreen")) {
|
||||
fullscreen = true;
|
||||
}
|
||||
}
|
||||
|
||||
ParticleTest l19 = new ParticleTest();
|
||||
l19.run(fullscreen);
|
||||
}
|
||||
public void run(boolean fullscreen) {
|
||||
this.fullscreen = fullscreen;
|
||||
try {
|
||||
init();
|
||||
while (!done) {
|
||||
mainloop();
|
||||
render();
|
||||
Display.update();
|
||||
}
|
||||
cleanup();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
private void mainloop() {
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Exit if Escape is pressed
|
||||
done = true;
|
||||
}
|
||||
if(Display.isCloseRequested()) { // Exit if window is closed
|
||||
done = true;
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_F1) && !f1) { // Is F1 Being Pressed?
|
||||
f1 = true; // Tell Program F1 Is Being Held
|
||||
switchMode(); // Toggle Fullscreen / Windowed Mode
|
||||
}
|
||||
if(!Keyboard.isKeyDown(Keyboard.KEY_F1)) { // Is F1 Being Released?
|
||||
f1 = false;
|
||||
}
|
||||
if((Keyboard.isKeyDown(Keyboard.KEY_SPACE) && !sp) || (rainbow && (delay > 25))) {
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
|
||||
rainbow = false; // If Spacebar Is Pressed Disable Rainbow Mode
|
||||
}
|
||||
sp = true; // Set Flag Telling Us Space Is Pressed
|
||||
delay = 0; // Reset The Rainbow Color Cycling Delay
|
||||
col++; // Change The Particle Color
|
||||
if(col > 11) {
|
||||
col = 0; // If Color Is Too High Reset It
|
||||
}
|
||||
}
|
||||
if(!Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
|
||||
sp = false;
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_ADD) && (slowdown > 1.0f)) {
|
||||
slowdown -= 0.01f; // Speed Up Particles
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT) && (slowdown < 4.0f)) {
|
||||
slowdown += 0.01f; // Slow Down Particles
|
||||
}
|
||||
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_PRIOR)) {
|
||||
zoom += 0.1f; // Zoom In
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_NEXT)) {
|
||||
zoom -= 0.1f; // Zoom Out
|
||||
}
|
||||
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_RETURN) && !rp) { // Return Key Pressed
|
||||
rp = true; // Set Flag Telling Us It's Pressed
|
||||
rainbow = !rainbow; // Toggle Rainbow Mode On / Off
|
||||
}
|
||||
if(!Keyboard.isKeyDown(Keyboard.KEY_RETURN)) {
|
||||
rp = false; // If Return Is Released Clear Flag
|
||||
}
|
||||
|
||||
delay++; // Increase Rainbow Mode Color Cycling Delay Counter
|
||||
}
|
||||
|
||||
private void switchMode() {
|
||||
fullscreen = !fullscreen;
|
||||
try {
|
||||
Display.setFullscreen(fullscreen);
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void createWindow() throws Exception {
|
||||
Display.setFullscreen(fullscreen);
|
||||
DisplayMode d[] = Display.getAvailableDisplayModes();
|
||||
for (int i = 0; i < d.length; i++) {
|
||||
if (d[i].getWidth() == 640
|
||||
&& d[i].getHeight() == 480
|
||||
&& d[i].getBitsPerPixel() == 32) {
|
||||
displayMode = d[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
Display.setDisplayMode(displayMode);
|
||||
Display.setTitle(windowTitle);
|
||||
Display.create();
|
||||
}
|
||||
private void init() throws Exception {
|
||||
particle = new Particle[MAX_PARTICLES];
|
||||
|
||||
for(int i=0;i<MAX_PARTICLES;i++) {
|
||||
particle[i] = new Particle();
|
||||
}
|
||||
|
||||
createWindow();
|
||||
|
||||
loadTextures();
|
||||
initGL();
|
||||
|
||||
}
|
||||
private void loadTextures() { // Load Bitmaps And Convert To Textures
|
||||
texture = loadTexture("data/particle.bmp");
|
||||
}
|
||||
private void initGL() { // All Setup For OpenGL Goes Here
|
||||
GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
|
||||
GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
|
||||
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
|
||||
GL11.glClearDepth(1.0f); // Depth Buffer Setup
|
||||
// Really Nice Perspective Calculations
|
||||
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
|
||||
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
|
||||
GL11.glLoadIdentity(); // Reset The Projection Matrix
|
||||
|
||||
// Calculate The Aspect Ratio Of The Window
|
||||
GLU.gluPerspective(45.0f,
|
||||
(float) displayMode.getWidth() / (float) displayMode.getHeight(),
|
||||
0.1f,100.0f);
|
||||
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
|
||||
|
||||
GL11.glDisable(GL11.GL_DEPTH_TEST);
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE); // Type Of Blending To Perform
|
||||
GL11.glEnable(GL11.GL_BLEND); // Enable Blending
|
||||
|
||||
for (int i=0;i<MAX_PARTICLES;i++) // Initials All The Textures
|
||||
{
|
||||
particle[i].active = true; // Make All The Particles Active
|
||||
particle[i].life = 1.0f; // Give All The Particles Full Life
|
||||
particle[i].fade = ((float)(Math.random() * 100.0)) / 1000.0f + 0.003f; // Random Fade Speed
|
||||
particle[i].r = colors[i * (12 / MAX_PARTICLES)][0]; // Select Red Rainbow Color
|
||||
particle[i].g = colors[i * (12 / MAX_PARTICLES)][1]; // Select Red Rainbow Color
|
||||
particle[i].b = colors[i * (12 / MAX_PARTICLES)][2]; // Select Red Rainbow Color
|
||||
particle[i].xi = ((float)((Math.random() * 50.0)) - 26.0f) * 10.0f; // Random Speed On X Axis
|
||||
particle[i].yi = ((float)((Math.random() * 50.0)) - 25.0f) * 10.0f; // Random Speed On Y Axis
|
||||
particle[i].zi = ((float)((Math.random() * 50.0)) - 25.0f) * 10.0f; // Random Speed On Z Axis
|
||||
particle[i].xg = 0.0f; // Set Horizontal Pull To Zero
|
||||
particle[i].yg = -0.8f; // Set Vertical Pull Downward
|
||||
particle[i].zg = 0.0f; // Set Pull On Z Axis To Zero
|
||||
}
|
||||
}
|
||||
|
||||
private void render() {
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
|
||||
GL11.glLoadIdentity(); // Reset The ModelView Matrix
|
||||
|
||||
for(int i=0;i<MAX_PARTICLES;i++) { // Loop Through All The Particles
|
||||
if (particle[i].active) { // If The Particle Is Active
|
||||
float x = particle[i].x; // Grab Our Particle X Position
|
||||
float y = particle[i].y; // Grab Our Particle Y Position
|
||||
float z = particle[i].z + zoom; // Particle Z Pos + Zoom
|
||||
|
||||
// Draw The Particle Using Our RGB Values, Fade The Particle Based On It's Life
|
||||
GL11.glColor4f(particle[i].r, particle[i].g, particle[i].b, particle[i].life);
|
||||
|
||||
GL11.glBegin(GL11.GL_TRIANGLE_STRIP); // Build Quad From A Triangle Strip
|
||||
GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(x + 0.5f, y + 0.5f, z); // Top Right
|
||||
GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(x - 0.5f, y + 0.5f, z); // Top Left
|
||||
GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(x + 0.5f, y - 0.5f, z); // Bottom Right
|
||||
GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(x - 0.5f, y - 0.5f, z); // Bottom Left
|
||||
GL11.glEnd(); // Done Building Triangle Strip
|
||||
|
||||
particle[i].x += particle[i].xi / (slowdown * 1000);// Move On The X Axis By X Speed
|
||||
particle[i].y += particle[i].yi / (slowdown * 1000);// Move On The Y Axis By Y Speed
|
||||
particle[i].z += particle[i].zi / (slowdown * 1000);// Move On The Z Axis By Z Speed
|
||||
|
||||
particle[i].xi += particle[i].xg; // Take Pull On X Axis Into Account
|
||||
particle[i].yi += particle[i].yg; // Take Pull On Y Axis Into Account
|
||||
particle[i].zi += particle[i].zg; // Take Pull On Z Axis Into Account
|
||||
particle[i].life -= particle[i].fade; // Reduce Particles Life By 'Fade'
|
||||
|
||||
if (particle[i].life < 0.0f) { // If Particle Is Burned Out
|
||||
particle[i].life = 1.0f; // Give It New Life
|
||||
particle[i].fade = ((float)(Math.random() * 100.0)) / 1000.0f + 0.003f; // Random Fade Value
|
||||
particle[i].x = 0.0f; // Center On X Axis
|
||||
particle[i].y = 0.0f; // Center On Y Axis
|
||||
particle[i].z = 0.0f; // Center On Z Axis
|
||||
particle[i].xi = xspeed + ((float)(Math.random() * 60.0)) - 32.0f; // X Axis Speed And Direction
|
||||
particle[i].yi = yspeed + ((float)(Math.random() * 60.0)) - 30.0f; // Y Axis Speed And Direction
|
||||
particle[i].zi = ((float)(Math.random() * 60.0)) - 30.0f; // Z Axis Speed And Direction
|
||||
particle[i].r = colors[col][0]; // Select Red From Color Table
|
||||
particle[i].g = colors[col][1]; // Select Green From Color Table
|
||||
particle[i].b = colors[col][2]; // Select Blue From Color Table
|
||||
}
|
||||
|
||||
// If Number Pad 8 And Y Gravity Is Less Than 1.5 Increase Pull Upwards
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_NUMPAD8) && (particle[i].yg<1.5f)) {
|
||||
particle[i].yg += 0.01f;
|
||||
}
|
||||
|
||||
// If Number Pad 2 And Y Gravity Is Greater Than -1.5 Increase Pull Downwards
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_NUMPAD2) && (particle[i].yg>-1.5f)) {
|
||||
particle[i].yg -= 0.01f;
|
||||
}
|
||||
|
||||
// If Number Pad 6 And X Gravity Is Less Than 1.5 Increase Pull Right
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_NUMPAD6) && (particle[i].xg<1.5f)) {
|
||||
particle[i].xg += 0.01f;
|
||||
}
|
||||
|
||||
// If Number Pad 4 And X Gravity Is Greater Than -1.5 Increase Pull Left
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_NUMPAD4) && (particle[i].xg>-1.5f)) {
|
||||
particle[i].xg -= 0.01f;
|
||||
}
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_TAB)) { // Tab Key Causes A Burst
|
||||
particle[i].x = 0.0f; // Center On X Axis
|
||||
particle[i].y = 0.0f; // Center On Y Axis
|
||||
particle[i].z = 0.0f; // Center On Z Axis
|
||||
particle[i].xi = ((float)(Math.random() * 50.0) - 26.0f) * 10.0f; // Random Speed On X Axis
|
||||
particle[i].yi = ((float)(Math.random() * 50.0) - 25.0f) * 10.0f; // Random Speed On Y Axis
|
||||
particle[i].zi = ((float)(Math.random() * 50.0) - 25.0f) * 10.0f; // Random Speed On Z Axis
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanup() {
|
||||
Display.destroy();
|
||||
}
|
||||
|
||||
private int loadTexture(String path) {
|
||||
Texture tex;
|
||||
tex = TextureLoader.getTextureLoaderInstance().getTexture(path);
|
||||
return tex.getGLTarget(); // Return Image Address In Memory
|
||||
}
|
||||
}
|
||||
|
||||
class Particle { // Particles Structure
|
||||
public boolean active; // Active (Yes/No)
|
||||
public float life; // Particle Life
|
||||
public float fade; // Fade Speed
|
||||
|
||||
public float r; // Red Value
|
||||
public float g; // Green Value
|
||||
public float b; // Blue Value
|
||||
|
||||
public float x; // X Position
|
||||
public float y; // Y Position
|
||||
public float z; // Z Position
|
||||
|
||||
public float xi; // X Direction
|
||||
public float yi; // Y Direction
|
||||
public float zi; // Z Direction
|
||||
|
||||
public float xg; // X Gravity
|
||||
public float yg; // Y Gravity
|
||||
public float zg; // Z Gravity
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1,549 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2004 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package ei.engine.test.ex;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.LWJGLUtil;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.openal.AL;
|
||||
import org.lwjgl.openal.AL10;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.glu.GLU;
|
||||
import org.lwjgl.util.WaveData;
|
||||
|
||||
|
||||
/**
|
||||
* <br>
|
||||
* This test demonstrates OpenAL positioning Based on the example by Chad Armstrong
|
||||
* (http://www.edenwaith.com/products/pige/tutorials/openal.php)
|
||||
*
|
||||
* @author Brian Matzon <brian@matzon.dk>
|
||||
* @version $Revision$
|
||||
* $Id$
|
||||
*/
|
||||
public class SoundPositionTest extends BasicSoundTest {
|
||||
|
||||
/** *Small* glut implementation :) */
|
||||
private GLUT glut;
|
||||
|
||||
/** Width of window */
|
||||
public static final int WINDOW_WIDTH = 640;
|
||||
|
||||
/** Height of window */
|
||||
public static final int WINDOW_HEIGHT = 480;
|
||||
|
||||
/** LEFT enumeration */
|
||||
public static final int LEFT = 0;
|
||||
|
||||
/** CENTER enumeration */
|
||||
public static final int CENTER = 1;
|
||||
|
||||
/** RIGHT enumeration */
|
||||
public static final int RIGHT = 2;
|
||||
|
||||
/** Whether the demo is done */
|
||||
private boolean finished = false;
|
||||
|
||||
/** Whether in pause mode */
|
||||
private boolean pauseMode = false;
|
||||
|
||||
// OpenAL stuff
|
||||
// ===================================================
|
||||
|
||||
/** OpenAL buffers */
|
||||
private IntBuffer soundBuffers = BufferUtils.createIntBuffer(3);
|
||||
|
||||
/** OpenAL sources */
|
||||
private IntBuffer soundSources = BufferUtils.createIntBuffer(3);
|
||||
|
||||
/** Position of listener */
|
||||
private FloatBuffer listenerPosition = createFloatBuffer(new float[] { 0.0f, 0.0f, 0.0f });
|
||||
|
||||
/** Velocity of listener */
|
||||
private FloatBuffer listenerVelocity = createFloatBuffer(new float[] { 0.0f, 0.0f, 0.0f });
|
||||
|
||||
/** Orientation of listener */
|
||||
private FloatBuffer listenerOrientation =
|
||||
createFloatBuffer(new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f });
|
||||
|
||||
/** Position of left sound */
|
||||
private FloatBuffer leftPosition = createFloatBuffer(new float[] { -2.0f, 0.0f, 0.0f });
|
||||
|
||||
/** Velocity of left sound */
|
||||
private FloatBuffer leftVelocity = createFloatBuffer(new float[] { 0.0f, 0.0f, 0.0f });
|
||||
|
||||
/** Position of center sound */
|
||||
private FloatBuffer centerPosition = createFloatBuffer(new float[] { 0.0f, 0.0f, -4.0f });
|
||||
|
||||
/** Velocity of center sound */
|
||||
private FloatBuffer centerVelocity = createFloatBuffer(new float[] { 0.0f, 0.0f, 0.0f });
|
||||
|
||||
/** Position of right sound */
|
||||
private FloatBuffer rightPosition = createFloatBuffer(new float[] { 2.0f, 0.0f, 0.0f });
|
||||
|
||||
/** Velocity of right sound */
|
||||
private FloatBuffer rightVelocity = createFloatBuffer(new float[] { 0.0f, 0.0f, 0.0f });
|
||||
// ---------------------------------------------------
|
||||
|
||||
/**
|
||||
* Runs the actual test, using supplied arguments
|
||||
*/
|
||||
protected void execute(String[] args) {
|
||||
// Setup needed stuff
|
||||
try {
|
||||
setup();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error setting up demonstration: ");
|
||||
e.printStackTrace();
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
// run the actual demonstration
|
||||
run();
|
||||
|
||||
// shutdown
|
||||
shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs setup of demonstration
|
||||
*/
|
||||
private void setup() throws Exception {
|
||||
|
||||
// Setup Window
|
||||
// =====================================================
|
||||
LWJGLUtil.log("Setting up window");
|
||||
|
||||
// setup window
|
||||
setDisplayMode();
|
||||
Display.create();
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Setup OpenGL
|
||||
// =====================================================
|
||||
LWJGLUtil.log("Setting up OpenGL");
|
||||
|
||||
GL11.glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION);
|
||||
GL11.glLoadIdentity();
|
||||
GLU.gluPerspective(50.0f, (float) WINDOW_WIDTH / WINDOW_HEIGHT, 0.0f, 50.0f);
|
||||
GL11.glMatrixMode(GL11.GL_MODELVIEW);
|
||||
GL11.glLoadIdentity();
|
||||
GL11.glTranslatef(0.0f, 0.0f, -6.6f);
|
||||
GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glut = this.new GLUT();
|
||||
|
||||
Display.setVSyncEnabled(true);
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Setup OpenAL
|
||||
// =====================================================
|
||||
LWJGLUtil.log("Setting up OpenAL");
|
||||
|
||||
AL10.alListener(AL10.AL_POSITION, listenerPosition);
|
||||
AL10.alListener(AL10.AL_VELOCITY, listenerVelocity);
|
||||
AL10.alListener(AL10.AL_ORIENTATION, listenerOrientation);
|
||||
|
||||
// creating buffers
|
||||
LWJGLUtil.log("Creating buffers");
|
||||
AL10.alGenBuffers(soundBuffers);
|
||||
soundBuffers.rewind();
|
||||
|
||||
// creating sources
|
||||
AL10.alGenSources(soundSources);
|
||||
soundSources.rewind();
|
||||
|
||||
// load sound files (left, center, right).wav
|
||||
LWJGLUtil.log("Loading soundfiles...");
|
||||
|
||||
LWJGLUtil.log("Loading left.wav");
|
||||
WaveData left = WaveData.create("data/sounds/left.wav");
|
||||
AL10.alBufferData(soundBuffers.get(LEFT), left.format, left.data, left.samplerate);
|
||||
AL10.alSourcef(soundSources.get(LEFT), AL10.AL_PITCH, 1.0f);
|
||||
AL10.alSourcef(soundSources.get(LEFT), AL10.AL_GAIN, 1.0f);
|
||||
AL10.alSource(soundSources.get(LEFT), AL10.AL_POSITION, leftPosition);
|
||||
AL10.alSource(soundSources.get(LEFT), AL10.AL_VELOCITY, leftVelocity);
|
||||
AL10.alSourcei(soundSources.get(LEFT), AL10.AL_BUFFER, soundBuffers.get(LEFT));
|
||||
AL10.alSourcei(soundSources.get(LEFT), AL10.AL_LOOPING, AL10.AL_TRUE);
|
||||
|
||||
LWJGLUtil.log("Loading center.wav");
|
||||
WaveData center = WaveData.create("data/sounds/center.wav");
|
||||
AL10.alBufferData(soundBuffers.get(CENTER), center.format, center.data, center.samplerate);
|
||||
AL10.alSourcef(soundSources.get(CENTER), AL10.AL_PITCH, 1.0f);
|
||||
AL10.alSourcef(soundSources.get(CENTER), AL10.AL_GAIN, 1.0f);
|
||||
AL10.alSource(soundSources.get(CENTER), AL10.AL_POSITION, centerPosition);
|
||||
AL10.alSource(soundSources.get(CENTER), AL10.AL_VELOCITY, centerVelocity);
|
||||
AL10.alSourcei(soundSources.get(CENTER), AL10.AL_BUFFER, soundBuffers.get(CENTER));
|
||||
AL10.alSourcei(soundSources.get(CENTER), AL10.AL_LOOPING, AL10.AL_TRUE);
|
||||
|
||||
LWJGLUtil.log("Loading right.wav");
|
||||
WaveData right = WaveData.create("data/sounds/right.wav");
|
||||
AL10.alBufferData(soundBuffers.get(RIGHT), right.format, right.data, right.samplerate);
|
||||
AL10.alSourcef(soundSources.get(RIGHT), AL10.AL_PITCH, 1.0f);
|
||||
AL10.alSourcef(soundSources.get(RIGHT), AL10.AL_GAIN, 1.0f);
|
||||
AL10.alSource(soundSources.get(RIGHT), AL10.AL_POSITION, rightPosition);
|
||||
AL10.alSource(soundSources.get(RIGHT), AL10.AL_VELOCITY, rightVelocity);
|
||||
AL10.alSourcei(soundSources.get(RIGHT), AL10.AL_BUFFER, soundBuffers.get(RIGHT));
|
||||
AL10.alSourcei(soundSources.get(RIGHT), AL10.AL_LOOPING, AL10.AL_TRUE);
|
||||
|
||||
LWJGLUtil.log("Soundfiles loaded successfully");
|
||||
// -----------------------------------------------------
|
||||
|
||||
Mouse.setGrabbed(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the actual demonstration
|
||||
*/
|
||||
private void run() {
|
||||
boolean firstRun = true;
|
||||
|
||||
System.out.println("Press 1/4 (left), 2/5 (center) or 3/6 (right) to toggle sound");
|
||||
System.out.println("Press LEFT/RIGHT to move along x axis");
|
||||
System.out.println("Press SHIFT and either UP/DOWN to move along y axis");
|
||||
System.out.println("Press UP/DOWN to move along z axis");
|
||||
System.out.println("Move along the x and y axis with the mouse");
|
||||
System.out.println("Press LEFT or RIGHT mouse button to move along z axis");
|
||||
System.out.println("Press ESC to exit demo");
|
||||
|
||||
LWJGLUtil.log(
|
||||
"Listener position: "
|
||||
+ listenerPosition.get(0)
|
||||
+ ", "
|
||||
+ listenerPosition.get(1)
|
||||
+ ", "
|
||||
+ listenerPosition.get(2));
|
||||
LWJGLUtil.log("Left position: " + leftPosition.get(0) + ", " + leftPosition.get(1) + ", " + leftPosition.get(2));
|
||||
LWJGLUtil.log("Center position: " + centerPosition.get(0) + ", " + centerPosition.get(1) + ", " + centerPosition.get(2));
|
||||
LWJGLUtil.log("Right position: " + rightPosition.get(0) + ", " + rightPosition.get(1) + ", " + rightPosition.get(2));
|
||||
|
||||
while (!finished) {
|
||||
// handle any input
|
||||
handleInput();
|
||||
|
||||
// allow window to process internal messages
|
||||
Display.update();
|
||||
|
||||
// render and paint if !minimized and not dirty
|
||||
if(Display.isVisible()) {
|
||||
render();
|
||||
} else {
|
||||
// sleeeeeep
|
||||
pause(100);
|
||||
}
|
||||
|
||||
// act on pause mode
|
||||
paused(!(Display.isVisible() || Display.isActive()));
|
||||
|
||||
// start sound after first paint, since we don't want
|
||||
// the delay before something is painted on the screen
|
||||
if (firstRun && !pauseMode) {
|
||||
firstRun = false;
|
||||
|
||||
// start sounds with delays
|
||||
startSounds();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts playing the sounds at different times
|
||||
*/
|
||||
private void startSounds() {
|
||||
AL10.alSourcePlay(soundSources.get(LEFT));
|
||||
pause(300);
|
||||
AL10.alSourcePlay(soundSources.get(CENTER));
|
||||
pause(500);
|
||||
AL10.alSourcePlay(soundSources.get(RIGHT));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles any changes in pause mode
|
||||
*
|
||||
* @param paused Which pause mode to enter
|
||||
*/
|
||||
private void paused(boolean paused) {
|
||||
// if requesting pause, and not paused - pause and stop sound
|
||||
if(paused && !pauseMode) {
|
||||
pauseMode = true;
|
||||
AL10.alSourcePause(soundSources);
|
||||
System.out.println("pauseMode = true");
|
||||
}
|
||||
|
||||
// else go out of pause mode and start sounds
|
||||
else if(!paused && pauseMode) {
|
||||
pauseMode = false;
|
||||
startSounds();
|
||||
System.out.println("pauseMode = false");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles any input
|
||||
*/
|
||||
private void handleInput() {
|
||||
// User wants to exit?
|
||||
finished = Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE);
|
||||
if (finished) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean shift = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
|
||||
|
||||
// Test for play
|
||||
// ============================================
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_1)) {
|
||||
AL10.alSourcePlay(soundSources.get(LEFT));
|
||||
LWJGLUtil.log("Playing left.wav");
|
||||
}
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_2)) {
|
||||
AL10.alSourcePlay(soundSources.get(CENTER));
|
||||
LWJGLUtil.log("Playing center.wav");
|
||||
}
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_3)) {
|
||||
AL10.alSourcePlay(soundSources.get(RIGHT));
|
||||
LWJGLUtil.log("Playing right.wav");
|
||||
}
|
||||
// --------------------------------------------
|
||||
|
||||
// Test for stop
|
||||
// ============================================
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_4)) {
|
||||
AL10.alSourceStop(soundSources.get(LEFT));
|
||||
LWJGLUtil.log("Stopped left.wav");
|
||||
}
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_5)) {
|
||||
AL10.alSourceStop(soundSources.get(CENTER));
|
||||
LWJGLUtil.log("Stopped center.wav");
|
||||
}
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_6)) {
|
||||
AL10.alSourceStop(soundSources.get(RIGHT));
|
||||
LWJGLUtil.log("Stopped right.wav");
|
||||
}
|
||||
// --------------------------------------------
|
||||
|
||||
// Test for movement with keyboard
|
||||
// ============================================
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
|
||||
listenerPosition.put(0, listenerPosition.get(0) - 0.1f);
|
||||
AL10.alListener(AL10.AL_POSITION, listenerPosition);
|
||||
}
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
|
||||
listenerPosition.put(0, listenerPosition.get(0) + 0.1f);
|
||||
AL10.alListener(AL10.AL_POSITION, listenerPosition);
|
||||
}
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
|
||||
if (shift) {
|
||||
listenerPosition.put(1, listenerPosition.get(1) + 0.1f);
|
||||
} else {
|
||||
listenerPosition.put(2, listenerPosition.get(2) - 0.1f);
|
||||
}
|
||||
AL10.alListener(AL10.AL_POSITION, listenerPosition);
|
||||
}
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
|
||||
if (shift) {
|
||||
listenerPosition.put(1, listenerPosition.get(1) - 0.1f);
|
||||
} else {
|
||||
listenerPosition.put(2, listenerPosition.get(2) + 0.1f);
|
||||
}
|
||||
AL10.alListener(AL10.AL_POSITION, listenerPosition);
|
||||
}
|
||||
// --------------------------------------------
|
||||
|
||||
// Test for movement with Mouse
|
||||
// ============================================
|
||||
listenerPosition.put(0, listenerPosition.get(0) + (0.01f * Mouse.getDX()));
|
||||
listenerPosition.put(1, listenerPosition.get(1) + (0.01f * Mouse.getDY()));
|
||||
if (Mouse.isButtonDown(0)) {
|
||||
listenerPosition.put(2, listenerPosition.get(2) - 0.1f);
|
||||
}
|
||||
if (Mouse.isButtonDown(1)) {
|
||||
listenerPosition.put(2, listenerPosition.get(2) + 0.1f);
|
||||
}
|
||||
|
||||
AL10.alListener(AL10.AL_POSITION, listenerPosition);
|
||||
|
||||
// empty mouse buffer
|
||||
while(Mouse.next());
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the scene
|
||||
*/
|
||||
private void render() {
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
|
||||
GL11.glPushMatrix();
|
||||
{
|
||||
GL11.glRotatef(20.0f, 1.0f, 1.0f, 0.0f);
|
||||
|
||||
// left
|
||||
GL11.glPushMatrix();
|
||||
{
|
||||
GL11.glTranslatef(leftPosition.get(0), leftPosition.get(1), leftPosition.get(2));
|
||||
GL11.glColor3f(1.0f, 0.0f, 0.0f);
|
||||
glut.glutWireCube(0.5f);
|
||||
}
|
||||
GL11.glPopMatrix();
|
||||
|
||||
// center
|
||||
GL11.glPushMatrix();
|
||||
{
|
||||
GL11.glTranslatef(centerPosition.get(0), centerPosition.get(1), centerPosition.get(2));
|
||||
GL11.glColor3f(0.0f, 0.0f, 1.0f);
|
||||
glut.glutWireCube(0.5f);
|
||||
}
|
||||
GL11.glPopMatrix();
|
||||
|
||||
// right
|
||||
GL11.glPushMatrix();
|
||||
{
|
||||
GL11.glTranslatef(rightPosition.get(0), rightPosition.get(1), rightPosition.get(2));
|
||||
GL11.glColor3f(0.0f, 1.0f, 0.0f);
|
||||
glut.glutWireCube(0.5f);
|
||||
}
|
||||
GL11.glPopMatrix();
|
||||
|
||||
// listener
|
||||
GL11.glPushMatrix();
|
||||
{
|
||||
GL11.glTranslatef(listenerPosition.get(0), listenerPosition.get(1), listenerPosition.get(2));
|
||||
GL11.glColor3f(1.0f, 1.0f, 1.0f);
|
||||
glut.glutSolidCube(0.5f);
|
||||
}
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown of demonstration
|
||||
*/
|
||||
private void shutdown() {
|
||||
LWJGLUtil.log("Shutting down OpenAL");
|
||||
AL10.alSourceStop(soundSources);
|
||||
AL10.alDeleteSources(soundSources);
|
||||
AL10.alDeleteBuffers(soundBuffers);
|
||||
AL.destroy();
|
||||
|
||||
LWJGLUtil.log("Shutting down Window");
|
||||
Display.destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* main entry point
|
||||
*
|
||||
* @param args
|
||||
* String array containing arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SoundPositionTest positionTest = new SoundPositionTest();
|
||||
positionTest.execute(args);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Minute implementation of GLUT: <br>COPYRIGHT:
|
||||
*
|
||||
* The OpenGL Utility Toolkit distribution for Win32 (Windows NT & Windows
|
||||
* 95) contains source code modified from the original source code for GLUT
|
||||
* version 3.3 which was developed by Mark J. Kilgard. The original source
|
||||
* code for GLUT is Copyright 1997 by Mark J. Kilgard. GLUT for Win32 is
|
||||
* Copyright 1997 by Nate Robins and is not in the public domain, but it is
|
||||
* freely distributable without licensing fees. It is provided without
|
||||
* guarantee or warrantee expressed or implied. It was ported with the
|
||||
* permission of Mark J. Kilgard by Nate Robins.
|
||||
*
|
||||
* THIS SOURCE CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
|
||||
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OR MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
class GLUT {
|
||||
|
||||
float n[][] = new float[][] { { -1.0f, 0.0f, 0.0f }, {
|
||||
0.0f, 1.0f, 0.0f }, {
|
||||
1.0f, 0.0f, 0.0f }, {
|
||||
0.0f, -1.0f, 0.0f }, {
|
||||
0.0f, 0.0f, 1.0f }, {
|
||||
0.0f, 0.0f, -1.0f }
|
||||
};
|
||||
|
||||
int faces[][] = new int[][] { { 0, 1, 2, 3 }, {
|
||||
3, 2, 6, 7 }, {
|
||||
7, 6, 5, 4 }, {
|
||||
4, 5, 1, 0 }, {
|
||||
5, 6, 2, 1 }, {
|
||||
7, 4, 0, 3 }
|
||||
};
|
||||
float v[][] = new float[8][3];
|
||||
|
||||
public void glutWireCube(float size) {
|
||||
drawBox(size, GL11.GL_LINE_LOOP);
|
||||
}
|
||||
|
||||
public void glutSolidCube(float size) {
|
||||
drawBox(size, GL11.GL_QUADS);
|
||||
}
|
||||
|
||||
private void drawBox(float size, int type) {
|
||||
|
||||
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2;
|
||||
v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2;
|
||||
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2;
|
||||
v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2;
|
||||
v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2;
|
||||
v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2;
|
||||
|
||||
for (int i = 5; i >= 0; i--) {
|
||||
GL11.glBegin(type);
|
||||
GL11.glNormal3f(n[i][0], n[i][1], n[i][2]);
|
||||
GL11.glVertex3f(v[faces[i][0]][0], v[faces[i][0]][1], v[faces[i][0]][2]);
|
||||
GL11.glVertex3f(v[faces[i][1]][0], v[faces[i][1]][1], v[faces[i][1]][2]);
|
||||
GL11.glVertex3f(v[faces[i][2]][0], v[faces[i][2]][1], v[faces[i][2]][2]);
|
||||
GL11.glVertex3f(v[faces[i][3]][0], v[faces[i][3]][1], v[faces[i][3]][2]);
|
||||
GL11.glEnd();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -9,13 +9,20 @@ public class Button extends UiComponent{
|
|||
private Entity normalButton;
|
||||
private Entity ontopButton;
|
||||
private Entity presedButton;
|
||||
private Entity disabledButton;
|
||||
private Node buttonNode;
|
||||
private Vector2f size;
|
||||
private boolean enabled;
|
||||
|
||||
public Button(String name, Vector2f s){
|
||||
super(name);
|
||||
buttonNode = new Node(name+"Node");
|
||||
size = s;
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean b){
|
||||
enabled = b;
|
||||
}
|
||||
|
||||
public void setButtonSprite(Entity s){
|
||||
|
|
@ -30,8 +37,18 @@ public class Button extends UiComponent{
|
|||
presedButton = s;
|
||||
}
|
||||
|
||||
public void setDisabledButtonSprite(Entity s){
|
||||
disabledButton = s;
|
||||
}
|
||||
|
||||
public void mousePos(int x, int y){
|
||||
if(size != null){
|
||||
if(!enabled){
|
||||
if(disabledButton != null && !buttonNode.contains(disabledButton)){
|
||||
buttonNode.clear();
|
||||
buttonNode.add(disabledButton);
|
||||
}
|
||||
}
|
||||
else if(size != null){
|
||||
Vector3f pos = buttonNode.getLocation();
|
||||
if( x >= (pos.getX()-(size.getX()/2)) && x <= (pos.getX()+(size.getX()/2))
|
||||
&& y >= (pos.getY()-(size.getY()/2)) && y <= (pos.getY()+(size.getY()/2))){
|
||||
|
|
@ -50,7 +67,7 @@ public class Button extends UiComponent{
|
|||
}
|
||||
|
||||
public void mouseDown(int x, int y, int event){
|
||||
if(size != null){
|
||||
if(size != null && enabled){
|
||||
Vector3f pos = buttonNode.getLocation();
|
||||
if( x >= (pos.getX()-(size.getX()/2)) && x <= (pos.getX()+(size.getX()/2))
|
||||
&& y >= (pos.getY()-(size.getY()/2)) && y <= (pos.getY()+(size.getY()/2))){
|
||||
|
|
|
|||
82
src/ei/engine/util/FileFinderHasher.java
Normal file
82
src/ei/engine/util/FileFinderHasher.java
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package ei.engine.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class FileFinderHasher {
|
||||
public static void main(String[] args){
|
||||
ArrayList<File> files = Search(new File("C:\\Documents and Settings\\Ziver\\Mina dokument\\Roligt"));
|
||||
for(int i=0; i<files.size(); i++){
|
||||
try {
|
||||
Hash(files.get(i),"MD5");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (FileNotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ArrayList<File> Search(File dir){
|
||||
return Search(dir, new ArrayList<File>());
|
||||
}
|
||||
|
||||
private static ArrayList<File> Search(File dir, ArrayList<File> fileList){
|
||||
String[] temp = dir.list();
|
||||
File file;
|
||||
|
||||
for(int i=0;i<temp.length;i++){
|
||||
file = new File(dir.getPath()+File.separator+temp[i]);
|
||||
//System.out.println(temp[i]);
|
||||
if(file.isDirectory()){
|
||||
//System.out.println(file.getPath()+File.separator+temp[i]+File.separator);
|
||||
Search(new File(dir.getPath()+File.separator+temp[i]+File.separator),fileList);
|
||||
}
|
||||
else if(file.isFile()){
|
||||
fileList.add(file);
|
||||
}
|
||||
}
|
||||
|
||||
return fileList;
|
||||
}
|
||||
|
||||
public static void Hash(File file, String hashType) throws NoSuchAlgorithmException, FileNotFoundException {
|
||||
MessageDigest digest = MessageDigest.getInstance(hashType);//"MD5"
|
||||
InputStream is = new FileInputStream(file);
|
||||
String output = "";
|
||||
byte[] buffer = new byte[8192];
|
||||
int read = 0;
|
||||
try {
|
||||
System.out.println("FILE: " + file.toString());
|
||||
while( (read = is.read(buffer)) > 0) {
|
||||
System.out.print("#");
|
||||
digest.update(buffer, 0, read);
|
||||
}
|
||||
byte[] md5sum = digest.digest();
|
||||
BigInteger bigInt = new BigInteger(1, md5sum);
|
||||
output = bigInt.toString(16);
|
||||
System.out.println(" DONE");
|
||||
System.out.println("MD5: " + output);
|
||||
}
|
||||
catch(IOException e) {
|
||||
throw new RuntimeException("Unable to process file for MD5", e);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch(IOException e) {
|
||||
throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue