Added a main menu and fixed som things. added explotions to and fixed the economy
|
|
@ -4,8 +4,8 @@
|
|||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="lib" path="lib/lwjgl.jar"/>
|
||||
<classpathentry kind="lib" path="lib/jinput.jar"/>
|
||||
<classpathentry kind="lib" path="lib/lwjgl_util.jar"/>
|
||||
<classpathentry kind="lib" path="lib/jogg-0.0.7.jar"/>
|
||||
<classpathentry kind="lib" path="lib/jorbis-0.0.15.jar"/>
|
||||
<classpathentry kind="lib" path="lib/lwjgl_util.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
|
|||
BIN
lib/jinput.jar
BIN
lib/lwjgl.jar
BIN
lwjgl.dll
BIN
raw/media/hud/buildmenu.xcf
Normal file
BIN
raw/media/hud/money.xcf
Normal file
BIN
raw/media/logo.xcf
Normal file
BIN
raw/media/ui/new1.zip
Normal file
BIN
raw/media/ui/new2.zip
Normal file
BIN
raw/media/ui/quit1.zip
Normal file
BIN
raw/media/ui/quit2.zip
Normal file
BIN
raw/media/ui/resume1.zip
Normal file
BIN
raw/media/ui/resume2.zip
Normal file
BIN
raw/media/ui/resume_disabeld.zip
Normal file
BIN
src/data/logo.png
Normal file
|
After Width: | Height: | Size: 51 KiB |
BIN
src/data/ui/Thumbs.db
Normal file
BIN
src/data/ui/new.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
BIN
src/data/ui/new_ontop.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
BIN
src/data/ui/quit.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
src/data/ui/quit_ontop.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
src/data/ui/resume.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
src/data/ui/resume_disabled.png
Normal file
|
After Width: | Height: | Size: 2 KiB |
BIN
src/data/ui/resume_ontop.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 945 B |
|
Before Width: | Height: | Size: 4 KiB |
|
|
@ -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
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,11 +3,11 @@ package ei.game;
|
|||
import ei.engine.LWJGLGameWindow;
|
||||
import ei.engine.state.GameStateManager;
|
||||
import ei.game.gamestate.InGameState;
|
||||
import ei.game.gamestate.LoadingState;
|
||||
import ei.game.gamestate.MenuState;
|
||||
import ei.game.gamestate.SplashState;
|
||||
|
||||
public class EI extends LWJGLGameWindow{
|
||||
public static final boolean debug = true;
|
||||
public static final boolean debug = false;
|
||||
|
||||
public static void main(String[] args){
|
||||
new EI();
|
||||
|
|
@ -23,10 +23,11 @@ public class EI extends LWJGLGameWindow{
|
|||
GameStateManager.getInstance().setActive("InGameState");
|
||||
}
|
||||
else{
|
||||
GameStateManager.getInstance().addState(new InGameState("InGameState"));
|
||||
GameStateManager.getInstance().addState(new LoadingState("LoadingState","InGameState"));
|
||||
GameStateManager.getInstance().addState(new SplashState("SplashState","LoadingState"));
|
||||
GameStateManager.getInstance().addState(new SplashState("SplashState","MenuState"));
|
||||
GameStateManager.getInstance().addState(new MenuState("MenuState"));
|
||||
GameStateManager.getInstance().setActive("SplashState");
|
||||
}
|
||||
}
|
||||
|
||||
protected void update() {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import ei.engine.scene.Node;
|
|||
import ei.engine.sound.Sound;
|
||||
import ei.engine.state.GameState;
|
||||
import ei.game.hud.InGameHud;
|
||||
import ei.game.input.InGameKeyboardInput;
|
||||
import ei.game.input.InGameMouseInput;
|
||||
import ei.game.player.HumanPlayer;
|
||||
import ei.game.player.PlayerHandler;
|
||||
|
|
@ -22,12 +23,21 @@ public class InGameState extends GameState{
|
|||
|
||||
public InGameState(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
PlayerHandler.getInstance().clear();
|
||||
WeaponHandler.getInstance().clear();
|
||||
|
||||
rootNode = new Node("InGameNode");
|
||||
|
||||
map = new Map(20,20);
|
||||
map.init("data/map/default");
|
||||
InGameMouseInput mouse = new InGameMouseInput(map);
|
||||
super.getInput().addInput(mouse);
|
||||
InGameKeyboardInput keyboard = new InGameKeyboardInput();
|
||||
super.getInput().addInput(keyboard);
|
||||
|
||||
HumanPlayer player = new HumanPlayer();
|
||||
Tank t1 = new Tank(player);
|
||||
|
|
@ -40,7 +50,6 @@ public class InGameState extends GameState{
|
|||
player.addUnit(new Bomber(7, 0, player));
|
||||
player.addUnit(new APU(4, 0, player));
|
||||
player.addUnit(new APU(5, 0, player));
|
||||
//player.addUnit(new CommandCenter(10, 10, player));
|
||||
|
||||
PlayerHandler.getInstance().addPlayer(player);
|
||||
|
||||
|
|
@ -53,7 +62,7 @@ public class InGameState extends GameState{
|
|||
rootNode.add(hud.getNode());
|
||||
|
||||
music = new Sound("music", "data/sounds/ei.ogg");
|
||||
music.loop();
|
||||
music.loop();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -81,5 +90,4 @@ public class InGameState extends GameState{
|
|||
public static Map getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
package ei.game.gamestate;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
|
|
@ -9,6 +12,7 @@ import ei.engine.sound.SoundLoader;
|
|||
import ei.engine.state.GameState;
|
||||
import ei.engine.state.GameStateManager;
|
||||
import ei.engine.texture.TextureLoader;
|
||||
import ei.engine.util.FileFinderHasher;
|
||||
|
||||
/**
|
||||
* This class handels the loading of the
|
||||
|
|
@ -17,6 +21,13 @@ import ei.engine.texture.TextureLoader;
|
|||
* @author Ziver
|
||||
*/
|
||||
public class LoadingState extends GameState{
|
||||
//The extensions of the files
|
||||
private static final String[] TEXTURES = {
|
||||
"jpg","png","bmp"
|
||||
};
|
||||
private static final String[] SOUNDS = {
|
||||
"wav","ogg"
|
||||
};
|
||||
// The things to load
|
||||
private Queue<String> loadTextures;
|
||||
private Queue<String> loadSounds;
|
||||
|
|
@ -32,6 +43,10 @@ public class LoadingState extends GameState{
|
|||
public LoadingState(String name,String nextState) {
|
||||
super(name);
|
||||
this.nextState = nextState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
loadTextures = new LinkedList<String>();
|
||||
loadSounds = new LinkedList<String>();
|
||||
|
||||
|
|
@ -40,6 +55,33 @@ public class LoadingState extends GameState{
|
|||
progress.setBarTexture(new Sprite("ProgressBar","data/loadbar_front.png"));
|
||||
progress.setProgressTexture(new Sprite("Progress","data/loadbar.png"));
|
||||
progress.setBackgroundTexture(new Sprite("progressBackground","data/loadbar_back.png"));
|
||||
|
||||
try {
|
||||
File dir = new File(getClass().getClassLoader().getResource("data/").toURI());
|
||||
ArrayList<File> files = FileFinderHasher.Search(dir);
|
||||
for(int i=0; i<files.size() ;i++){
|
||||
String ext = files.get(i).getName();
|
||||
ext = ext.substring(ext.lastIndexOf ('.')+1,ext.length());
|
||||
|
||||
if(contains(TEXTURES,ext)){
|
||||
addTexture(files.get(i).getPath().substring(files.get(i).getPath().lastIndexOf("data"), files.get(i).getPath().length()));
|
||||
}
|
||||
else if(contains(SOUNDS,ext)){
|
||||
addSound(files.get(i).getPath().substring(files.get(i).getPath().lastIndexOf("data"), files.get(i).getPath().length()));
|
||||
}
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean contains(String[] array, String search){
|
||||
for(int i=0; i<array.length ;i++){
|
||||
if(array[i].equals(search)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -73,7 +115,7 @@ public class LoadingState extends GameState{
|
|||
progress.setValue(progress.getValue()+1);
|
||||
}
|
||||
else{
|
||||
progress.setValue(progress.getValue()+1);
|
||||
//progress.setValue(progress.getValue()+1);
|
||||
if(progress.getProcent() >= 100){
|
||||
//deactivate this state and activate the next one
|
||||
GameStateManager.getInstance().removeState(this);
|
||||
|
|
|
|||
104
src/ei/game/gamestate/MenuState.java
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
package ei.game.gamestate;
|
||||
|
||||
import ei.engine.LWJGLGameWindow;
|
||||
import ei.engine.math.Vector2f;
|
||||
import ei.engine.scene.Node;
|
||||
import ei.engine.scene.Sprite;
|
||||
import ei.engine.state.GameState;
|
||||
import ei.engine.state.GameStateManager;
|
||||
import ei.engine.ui.Button;
|
||||
import ei.engine.ui.UiComponent;
|
||||
import ei.engine.ui.UiHandler;
|
||||
import ei.engine.ui.UiListener;
|
||||
import ei.game.input.MenuKeyboardInput;
|
||||
import ei.game.input.MenuMouseInput;
|
||||
|
||||
public class MenuState extends GameState implements UiListener{
|
||||
private Node menuNode;
|
||||
|
||||
private Button resume;
|
||||
private Button newGame;
|
||||
private Button quit;
|
||||
|
||||
public MenuState(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
menuNode = new Node("MenuNode");
|
||||
|
||||
int x = LWJGLGameWindow.getWidth()/2;
|
||||
|
||||
Sprite logo = new Sprite("Logo","data/logo.png");
|
||||
logo.setLocation(new Vector2f(x,100));
|
||||
menuNode.add(logo);
|
||||
|
||||
UiHandler ui = new UiHandler("MainMenu");
|
||||
|
||||
Sprite s1 = new Sprite("ResumeButton","data/ui/resume.png");
|
||||
resume = new Button("Resume", s1.getSize());
|
||||
resume.setButtonSprite(s1);
|
||||
resume.setOnTopButtonSprite(new Sprite("ResumeButton_Selected","data/ui/resume_ontop.png"));
|
||||
resume.setDisabledButtonSprite(new Sprite("ResumeButton_Disabled","data/ui/resume_disabled.png"));
|
||||
resume.getNode().setLocation(new Vector2f(x,250));
|
||||
resume.addListener(this);
|
||||
resume.setEnabled(false);
|
||||
ui.addUi(resume);
|
||||
|
||||
Sprite s2 = new Sprite("NewButton","data/ui/new.png");
|
||||
newGame = new Button("New", s2.getSize());
|
||||
newGame.setButtonSprite(s2);
|
||||
newGame.setOnTopButtonSprite(new Sprite("NewButton_Selected","data/ui/new_ontop.png"));
|
||||
newGame.getNode().setLocation(new Vector2f(x,300));
|
||||
newGame.addListener(this);
|
||||
ui.addUi(newGame);
|
||||
|
||||
Sprite s3 = new Sprite("QuitButton","data/ui/quit.png");
|
||||
quit = new Button("Quit", s3.getSize());
|
||||
quit.setButtonSprite(s3);
|
||||
quit.setOnTopButtonSprite(new Sprite("QuitButton_Selected","data/ui/quit_ontop.png"));
|
||||
quit.getNode().setLocation(new Vector2f(x,350));
|
||||
quit.addListener(this);
|
||||
ui.addUi(quit);
|
||||
|
||||
menuNode.add(ui.getNode());
|
||||
|
||||
MenuMouseInput mouse = new MenuMouseInput(ui);
|
||||
super.getInput().addInput(mouse);
|
||||
super.getInput().addInput(new MenuKeyboardInput());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
menuNode.render();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
menuNode.update();
|
||||
if(GameStateManager.getInstance().getState("InGameState") != null){
|
||||
resume.setEnabled(true);
|
||||
}
|
||||
else{
|
||||
resume.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void ActionEvent(UiComponent source){
|
||||
if(resume == source){
|
||||
GameStateManager.getInstance().setDeActive(this.getName());
|
||||
GameStateManager.getInstance().setActive("InGameState");
|
||||
}
|
||||
else if(newGame == source){
|
||||
GameStateManager.getInstance().removeStateByName("InGameState");
|
||||
GameStateManager.getInstance().addState(new InGameState("InGameState"));
|
||||
GameStateManager.getInstance().addState(new LoadingState("LoadingState","InGameState"));
|
||||
GameStateManager.getInstance().setDeActive(this.getName());
|
||||
GameStateManager.getInstance().setActive("LoadingState");
|
||||
}
|
||||
else if(quit == source){
|
||||
LWJGLGameWindow.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,6 +16,11 @@ public class SplashState extends GameState{
|
|||
public SplashState(String name,String next) {
|
||||
super(name);
|
||||
nextState = next;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
splash = new Fade("Splash");
|
||||
|
||||
Sprite s = new Sprite("Splash","data/splash.png");
|
||||
|
|
@ -26,7 +31,7 @@ public class SplashState extends GameState{
|
|||
|
||||
time = 200;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
splash.render();
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import ei.game.scene.units.Tank;
|
|||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public class InGameBuildHud {
|
||||
public class InGameBuildHud implements UiListener{
|
||||
private UiHandler ui;
|
||||
private Player player;
|
||||
private BitmapText queueSize;
|
||||
|
|
@ -33,14 +33,13 @@ public class InGameBuildHud {
|
|||
ui = new UiHandler("BuildMenu");
|
||||
|
||||
Vector2f size = new Vector2f(40,40);
|
||||
HudListener listener = new HudListener(this);
|
||||
|
||||
removeLastQueue = new Button("TankButton", size);
|
||||
queueSize = new BitmapText("QueueSize");
|
||||
queueSize.setText("0");
|
||||
removeLastQueue.setButtonSprite(queueSize);
|
||||
removeLastQueue.getNode().setLocation(new Vector2f(x,y));
|
||||
removeLastQueue.addListener(listener);
|
||||
removeLastQueue.addListener(this);
|
||||
ui.addUi(removeLastQueue);
|
||||
|
||||
apuBuildButton = new Button("TankButton", size);
|
||||
|
|
@ -51,7 +50,7 @@ public class InGameBuildHud {
|
|||
s2.setSize(size);
|
||||
apuBuildButton.setOnTopButtonSprite(s2);
|
||||
apuBuildButton.getNode().setLocation(new Vector2f(x+(size.getX()*2),y));
|
||||
apuBuildButton.addListener(listener);
|
||||
apuBuildButton.addListener(this);
|
||||
ui.addUi(apuBuildButton);
|
||||
|
||||
tankBuildButton = new Button("TankButton", size);
|
||||
|
|
@ -62,7 +61,7 @@ public class InGameBuildHud {
|
|||
s4.setSize(size);
|
||||
tankBuildButton.setOnTopButtonSprite(s4);
|
||||
tankBuildButton.getNode().setLocation(new Vector2f(x+(size.getX()*4),y));
|
||||
tankBuildButton.addListener(listener);
|
||||
tankBuildButton.addListener(this);
|
||||
ui.addUi(tankBuildButton);
|
||||
|
||||
bomberBuildButton = new Button("TankButton", size);
|
||||
|
|
@ -73,7 +72,7 @@ public class InGameBuildHud {
|
|||
s6.setSize(size);
|
||||
bomberBuildButton.setOnTopButtonSprite(s6);
|
||||
bomberBuildButton.getNode().setLocation(new Vector2f(x+(size.getX()*6),y));
|
||||
bomberBuildButton.addListener(listener);
|
||||
bomberBuildButton.addListener(this);
|
||||
ui.addUi(bomberBuildButton);
|
||||
}
|
||||
|
||||
|
|
@ -93,31 +92,23 @@ public class InGameBuildHud {
|
|||
return player;
|
||||
}
|
||||
|
||||
class HudListener implements UiListener{
|
||||
private InGameBuildHud hud;
|
||||
|
||||
public HudListener(InGameBuildHud h){
|
||||
hud = h;
|
||||
public void ActionEvent(UiComponent source){
|
||||
// builds a apu
|
||||
if(source == apuBuildButton){
|
||||
player.getCC().buildUnit(new APU(0,0,player));
|
||||
}
|
||||
// builds a tank
|
||||
else if(source == tankBuildButton){
|
||||
player.getCC().buildUnit(new Tank(0,0,player));
|
||||
}
|
||||
// builds a bomber
|
||||
else if(source == bomberBuildButton){
|
||||
player.getCC().buildUnit(new Bomber(0,0,player));
|
||||
}
|
||||
|
||||
public void ActionEvent(UiComponent source){
|
||||
// builds a apu
|
||||
if(source == hud.apuBuildButton){
|
||||
hud.getPlayer().getCC().buildUnit(new APU(0,0,player));
|
||||
}
|
||||
// builds a tank
|
||||
else if(source == hud.tankBuildButton){
|
||||
hud.getPlayer().getCC().buildUnit(new Tank(0,0,player));
|
||||
}
|
||||
// builds a bomber
|
||||
else if(source == hud.bomberBuildButton){
|
||||
hud.getPlayer().getCC().buildUnit(new Bomber(0,0,player));
|
||||
}
|
||||
|
||||
// removes the last unit in the queue
|
||||
else if(source == hud.removeLastQueue){
|
||||
hud.getPlayer().getCC().removeLast();
|
||||
}
|
||||
// removes the last unit in the queue
|
||||
else if(source == removeLastQueue){
|
||||
player.getCC().removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,7 +60,6 @@ public class InGameHud {
|
|||
money.setText(""+player.getKredits());
|
||||
money.setLocation(new Vector2f(LWJGLGameWindow.getWidth()-money.getBound().width,5));
|
||||
|
||||
player.addKredits(100);
|
||||
buildBar.setValue(player.getCC().getBuildProgress());
|
||||
|
||||
buildHud.update();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,31 @@
|
|||
package ei.game.input;
|
||||
|
||||
public class InGameKeyboardInput {
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import ei.engine.LWJGLGameWindow;
|
||||
import ei.engine.input.KeyboardInput;
|
||||
import ei.engine.state.GameStateManager;
|
||||
|
||||
public class InGameKeyboardInput extends KeyboardInput{
|
||||
|
||||
public InGameKeyboardInput() {
|
||||
super("InGameKeyboardInput");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyDown(int keycode) {
|
||||
if (Keyboard.KEY_ESCAPE == keycode) {
|
||||
if(GameStateManager.getInstance().getState("MenuState") != null){
|
||||
GameStateManager.getInstance().setDeActive("InGameState");
|
||||
GameStateManager.getInstance().setActive("MenuState");
|
||||
}
|
||||
else{
|
||||
LWJGLGameWindow.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyUp(int keycode) {}
|
||||
|
||||
}
|
||||
|
|
|
|||
25
src/ei/game/input/MenuKeyboardInput.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package ei.game.input;
|
||||
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import ei.engine.LWJGLGameWindow;
|
||||
import ei.engine.input.KeyboardInput;
|
||||
import ei.engine.state.GameStateManager;
|
||||
|
||||
public class MenuKeyboardInput extends KeyboardInput{
|
||||
|
||||
public MenuKeyboardInput() {
|
||||
super("InGameKeyboardInput");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyDown(int keycode) {
|
||||
if (Keyboard.KEY_ESCAPE == keycode) {
|
||||
LWJGLGameWindow.exit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyUp(int keycode) {}
|
||||
|
||||
}
|
||||
35
src/ei/game/input/MenuMouseInput.java
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package ei.game.input;
|
||||
|
||||
import ei.engine.input.MouseInput;
|
||||
import ei.engine.math.Vector2f;
|
||||
import ei.engine.scene.Sprite;
|
||||
import ei.engine.ui.UiHandler;
|
||||
|
||||
public class MenuMouseInput extends MouseInput{
|
||||
private UiHandler ui;
|
||||
|
||||
public MenuMouseInput(UiHandler ui) {
|
||||
super("MenuMouseInput","data/cursor/cursor.png");
|
||||
this.ui = ui;
|
||||
|
||||
//inits the mouse texture
|
||||
Sprite s = getSprite();
|
||||
s.setSize(new Vector2f(38,50));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseUpdate(int x, int y, int w) {
|
||||
ui.mousePos(x, y);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDown(int event,int x, int y) {
|
||||
ui.mouseDown(x, y, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseUp(int event,int x, int y) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -39,6 +39,14 @@ public abstract class Player {
|
|||
unitsNode.remove(u.getNode());
|
||||
}
|
||||
|
||||
public GameEntity getUnit(int i){
|
||||
return units.get(i);
|
||||
}
|
||||
|
||||
public int unitCount(){
|
||||
return units.size();
|
||||
}
|
||||
|
||||
public Node getNode(){
|
||||
return unitsNode;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,10 @@ public class PlayerHandler {
|
|||
// The player list
|
||||
private ArrayList<Player> players;
|
||||
private Node playerNode;
|
||||
// economy stuff
|
||||
private static final int fundsDelay = 60*30;
|
||||
private static final int funds = 1000;
|
||||
private int fundsTimmer;
|
||||
|
||||
/**
|
||||
* Creates a PlayerHandler
|
||||
|
|
@ -18,6 +22,7 @@ public class PlayerHandler {
|
|||
public PlayerHandler(){
|
||||
players = new ArrayList<Player>();
|
||||
playerNode = new Node("PlayerNode");
|
||||
fundsTimmer = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -59,6 +64,22 @@ public class PlayerHandler {
|
|||
}
|
||||
|
||||
public void update(){
|
||||
// sends the funds to the players
|
||||
fundsTimmer++;
|
||||
if(fundsTimmer >= fundsDelay){
|
||||
for(int i=0; i<players.size() ;i++){
|
||||
int maintenance = 0;
|
||||
for(int j=0; j<players.get(i).unitCount() ;j++){
|
||||
maintenance += players.get(i).getUnit(j).getMaintenanceCost();
|
||||
}
|
||||
int kredit = funds - maintenance;
|
||||
if(kredit > 0){
|
||||
players.get(i).addKredits(kredit);
|
||||
}
|
||||
}
|
||||
fundsTimmer = 0;
|
||||
}
|
||||
// updates the players
|
||||
for(int i=0; i<players.size() ;i++){
|
||||
players.get(i).update();
|
||||
}
|
||||
|
|
@ -75,4 +96,10 @@ public class PlayerHandler {
|
|||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void clear(){
|
||||
players.clear();
|
||||
playerNode.clear();
|
||||
fundsTimmer = 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -96,5 +96,7 @@ public abstract class GameEntity{
|
|||
public abstract void attack(Vector2i target, boolean play);
|
||||
|
||||
public abstract Entity getSprite();
|
||||
|
||||
public abstract int getMaintenanceCost();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -127,9 +127,9 @@ public abstract class Building extends GameEntity{
|
|||
/**
|
||||
* Updating the unit
|
||||
*/
|
||||
public void update() {
|
||||
|
||||
public void update() {
|
||||
if(getLife()<=0) {
|
||||
destroyed();
|
||||
removeBuilding();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,10 +5,13 @@ import ei.engine.math.Vector2i;
|
|||
import ei.engine.scene.Sprite;
|
||||
import ei.game.player.Player;
|
||||
import ei.game.scene.SelectBox;
|
||||
import ei.game.scene.weapons.Explotion;
|
||||
import ei.game.scene.weapons.WeaponHandler;
|
||||
|
||||
public class CommandCenter extends Building{
|
||||
private SelectBox selectionBox;
|
||||
private Sprite sprite;
|
||||
|
||||
public CommandCenter(Player p) {
|
||||
this(0, 0, p);
|
||||
}
|
||||
|
|
@ -29,7 +32,7 @@ public class CommandCenter extends Building{
|
|||
* This unit type is now destroyed.
|
||||
*/
|
||||
public void destroyed(){
|
||||
|
||||
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
|
||||
}
|
||||
/**
|
||||
* Doesnt matter anyway. Since buildings cant attack.
|
||||
|
|
@ -52,4 +55,9 @@ public class CommandCenter extends Building{
|
|||
public Sprite getSprite() {
|
||||
return this.sprite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaintenanceCost() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,4 +31,11 @@ public class Stone extends MapEntity{
|
|||
public Entity getSprite() {
|
||||
return stone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaintenanceCost() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,10 @@ import ei.engine.scene.Sprite;
|
|||
import ei.engine.sound.Sound;
|
||||
import ei.game.player.Player;
|
||||
import ei.game.scene.SelectBox;
|
||||
import ei.game.scene.weapons.Explotion;
|
||||
import ei.game.scene.weapons.MachineGun;
|
||||
import ei.game.scene.weapons.Weapon;
|
||||
import ei.game.scene.weapons.WeaponHandler;
|
||||
|
||||
public class APU extends Unit{
|
||||
private SelectBox selectionBox;
|
||||
|
|
@ -59,7 +61,7 @@ public class APU extends Unit{
|
|||
* This unit type is now destroyed.
|
||||
*/
|
||||
public void destroyed(){
|
||||
|
||||
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
|
||||
}
|
||||
public Sound getGunSound() {
|
||||
return gunSound;
|
||||
|
|
@ -95,4 +97,9 @@ public class APU extends Unit{
|
|||
public Sprite getSprite() {
|
||||
return this.sprite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaintenanceCost() {
|
||||
return 50;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ import ei.engine.texture.AnimatedTexture;
|
|||
import ei.game.player.Player;
|
||||
import ei.game.scene.SelectBox;
|
||||
import ei.game.scene.weapons.BomberWeapon;
|
||||
import ei.game.scene.weapons.Explotion;
|
||||
import ei.game.scene.weapons.Weapon;
|
||||
import ei.game.scene.weapons.WeaponHandler;
|
||||
|
||||
public class Bomber extends Unit{
|
||||
private SelectBox selectionBox;
|
||||
|
|
@ -68,7 +70,7 @@ public class Bomber extends Unit{
|
|||
* This unit type is now destroyed.
|
||||
*/
|
||||
public void destroyed(){
|
||||
|
||||
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
|
||||
}
|
||||
public Sound getGunSound() {
|
||||
return gunSound;
|
||||
|
|
@ -104,4 +106,9 @@ public class Bomber extends Unit{
|
|||
public Sprite getSprite() {
|
||||
return this.sprite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaintenanceCost() {
|
||||
return 50;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,9 @@ import ei.engine.sound.Sound;
|
|||
import ei.game.player.Player;
|
||||
import ei.game.scene.SelectBox;
|
||||
import ei.game.scene.weapons.CannonBall;
|
||||
import ei.game.scene.weapons.Explotion;
|
||||
import ei.game.scene.weapons.Weapon;
|
||||
import ei.game.scene.weapons.WeaponHandler;
|
||||
|
||||
public class Tank extends Unit{
|
||||
private SelectBox selectionBox;
|
||||
|
|
@ -71,7 +73,7 @@ public class Tank extends Unit{
|
|||
* This unit type is now destroyed.
|
||||
*/
|
||||
public void destroyed(){
|
||||
|
||||
WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
|
||||
}
|
||||
/**
|
||||
* returns the velocity of the unit type.
|
||||
|
|
@ -95,4 +97,9 @@ public class Tank extends Unit{
|
|||
public Sprite getSprite() {
|
||||
return this.sprite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaintenanceCost() {
|
||||
return 50;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ public abstract class Unit extends GameEntity{
|
|||
private GameEntity attack;
|
||||
private int buildTime;
|
||||
private int price;
|
||||
private int maintenance;
|
||||
// The wepon reload timer
|
||||
private int weponTimer;
|
||||
// The path to travel
|
||||
|
|
@ -178,6 +177,7 @@ public abstract class Unit extends GameEntity{
|
|||
public void update() {
|
||||
weponTimer++;
|
||||
if(getLife()<=0) {
|
||||
destroyed();
|
||||
removeUnit();
|
||||
}
|
||||
else if(moveTo != null) {
|
||||
|
|
|
|||
43
src/ei/game/scene/weapons/Explotion.java
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package ei.game.scene.weapons;
|
||||
|
||||
import ei.engine.effects.Particles;
|
||||
import ei.engine.math.Vector2f;
|
||||
|
||||
public class Explotion extends Weapon{
|
||||
private Particles part;
|
||||
|
||||
public Explotion(Vector2f startPos) {
|
||||
super(startPos);
|
||||
setVelocity(4);
|
||||
setRange(Integer.MIN_VALUE);
|
||||
setDamage(Integer.MIN_VALUE);
|
||||
setReload(Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
public Particles getWeapon() {
|
||||
part = new Particles("bomber");
|
||||
part = new Particles("weapon");
|
||||
part.MaxSpeedX=700;
|
||||
part.MaxSpeedY=700;
|
||||
part.MaxSpeedZ=0;
|
||||
part.slowdown = 1;
|
||||
part.rainbow = false;
|
||||
part.regenerate = false;
|
||||
part.size=10;
|
||||
float colors[][][]= // Rainbow Of Colors
|
||||
{
|
||||
{{1.0f,1.0f,0.0f},{0.74f,0.74f,0.74f}}
|
||||
|
||||
};
|
||||
part.colors = colors;
|
||||
return part;
|
||||
}
|
||||
|
||||
public void update() {
|
||||
if(part.isDead()) {
|
||||
WeaponHandler.getInstance().removeWeapon(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -11,9 +11,9 @@ import ei.engine.scene.Node;
|
|||
*
|
||||
*/
|
||||
public class WeaponHandler {
|
||||
private static WeaponHandler instance;
|
||||
private Node weaponNode;
|
||||
private ArrayList<Weapon> weapons;
|
||||
private static WeaponHandler instance;
|
||||
|
||||
/**
|
||||
* Created a weaponhandler.
|
||||
|
|
@ -21,9 +21,9 @@ public class WeaponHandler {
|
|||
*/
|
||||
public WeaponHandler() {
|
||||
weapons = new ArrayList<Weapon>();
|
||||
weaponNode = new Node("weapon");
|
||||
|
||||
weaponNode = new Node("weapon");
|
||||
}
|
||||
|
||||
public boolean addWeapon(Weapon w){
|
||||
if(!weapons.contains(w)){
|
||||
weapons.add(w);
|
||||
|
|
@ -35,6 +35,7 @@ public class WeaponHandler {
|
|||
|
||||
/**
|
||||
* Removes a player from the handler
|
||||
*
|
||||
* @param p The player to remove
|
||||
* @return true if succesful else false
|
||||
*/
|
||||
|
|
@ -46,6 +47,7 @@ public class WeaponHandler {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates all the weapons.
|
||||
*
|
||||
|
|
@ -55,8 +57,10 @@ public class WeaponHandler {
|
|||
weapons.get(i).update();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* return the current weaponNode.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Entity getNode() {
|
||||
|
|
@ -75,7 +79,10 @@ public class WeaponHandler {
|
|||
return instance;
|
||||
}
|
||||
|
||||
|
||||
public void clear(){
|
||||
weapons.clear();
|
||||
weaponNode.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||