This is a big commit. Added mouse cursors and a load bar. Started whit the game. and many many other things

This commit is contained in:
Ziver Koc 2007-03-29 23:21:00 +00:00
parent 448fca2fdf
commit 4e7722fedb
34 changed files with 597 additions and 95 deletions

View file

@ -86,13 +86,18 @@ public class LWJGLGameWindow {
// Create the display window
Display.create();
// enable textures since we're going to use these for our sprites
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
// disable the OpenGL depth test since we're rendering 2D graphics
GL11.glDisable(GL11.GL_DEPTH_TEST);
// disable lights (we do not nead them)
GL11.glDisable(GL11.GL_LIGHTING);
// Enable Smooth Shading
GL11.glShadeModel(GL11.GL_SMOOTH);
// Black Background
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Depth Buffer Setup
GL11.glClearDepth(1.0f);
@ -105,18 +110,16 @@ public class LWJGLGameWindow {
// Select The Modelview Matrix (controls model orientation)
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
// disable the OpenGL depth test since we're rendering 2D graphics
GL11.glDisable(GL11.GL_DEPTH_TEST);
//Enable Blending
GL11.glEnable(GL11.GL_BLEND);
//Type Of Blending To Perform
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
Display.setVSyncEnabled(true);
// The last steps
timer = new FpsTimer();

View file

@ -8,6 +8,8 @@ package ei.engine.effects;
* Visit Our Sites At www.tiptup.com and nehe.gamedev.net
*/
import java.awt.Rectangle;
import org.lwjgl.opengl.GL11;
import ei.engine.scene.Entity;
@ -155,6 +157,8 @@ public class Particles extends Entity{
texture.bindGL();
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
for(int i=0;i<maxParticles;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
@ -218,6 +222,7 @@ public class Particles extends Entity{
}
}
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
}
@ -249,6 +254,16 @@ public class Particles extends Entity{
particle[i].active = true;
}
}
/**
* Returns the bound of this class
*/
public Rectangle getBound() {
return new Rectangle(
(int)getLocation().getX(),
(int)getLocation().getY(),
(int)size, (int)size);
}
}
class Particle { // Particles Structure

View file

@ -9,7 +9,6 @@ public abstract class Input{
public Input(String name){
this.enabled = true;
InputHandler.getInstance().addInput(this);
}
/**

View file

@ -1,9 +1,11 @@
package ei.engine.input;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import ei.engine.LWJGLGameWindow;
import ei.engine.math.Vector2f;
import ei.engine.math.Vector3f;
import ei.engine.scene.Sprite;
/**
@ -12,6 +14,9 @@ import ei.engine.scene.Sprite;
*
*/
public abstract class MouseInput extends Input{
public static final int LEFT_MOUSE_BUTTON = 0;
public static final int RIGHT_MOUSE_BUTTON = 1;
public static final int MIDDLE_MOUSE_BUTTON = 2;
// The x pos of the mouse
private static int cursorX;
// The y pos of the mouse
@ -22,19 +27,42 @@ public abstract class MouseInput extends Input{
public MouseInput(String name) {
super(name);
// init mouse: this will hide the native cursor (see drawCursor())
Mouse.setGrabbed(true);
Mouse.setGrabbed(false);
// set initial cursor pos to center screen
cursorX = (int) LWJGLGameWindow.getWidth() / 2;
cursorY = (int) LWJGLGameWindow.getHeight() / 2;
}
public MouseInput(String name,String texture){
super(name);
// init mouse: this will hide the native cursor (see drawCursor())
Mouse.setGrabbed(true);
cursor = new Sprite(name+" spatial",texture);
// set initial cursor pos to center screen
cursorX = (int) LWJGLGameWindow.getWidth() / 2;
cursorY = (int) LWJGLGameWindow.getHeight() / 2;
}
/**
* Returns the sprite of th mouse
*
* @return The sprite of the mouse
*/
public Sprite getSprite(){
return cursor;
}
/**
* Renders the mous texture if texture set
*/
public void render() {
if(cursor != null){
cursor.render();
GL11.glPushMatrix();
Vector3f v = LWJGLGameWindow.getCamera().getLocation();
GL11.glTranslatef(v.getX(),v.getY(), v.getZ());
cursor.render();
GL11.glPopMatrix();
}
}
@ -61,36 +89,37 @@ public abstract class MouseInput extends Input{
else if (cursorY > LWJGLGameWindow.getHeight()) {
cursorY = LWJGLGameWindow.getHeight();
}
mouseMove(cursorX,cursorY);
}
mouseUpdate(cursorX,cursorY,mouseDW);
while ( Mouse.next() ) {
if(Mouse.getEventButton() == 0 && Mouse.getEventButtonState() == true) {
mouseDown(cursorX, cursorY);
if(Mouse.getEventButton() >= 0 && Mouse.getEventButtonState() == true) {
mouseDown(Mouse.getEventButton(),cursorX, cursorY);
}
if(Mouse.getEventButton() == 0 && Mouse.getEventButtonState() == false) {
mouseUp(cursorX, cursorY);
if(Mouse.getEventButton() >= 0 && Mouse.getEventButtonState() == false) {
mouseUp(Mouse.getEventButton(),cursorX, cursorY);
}
}
if(cursor != null){
cursor.setLocation(new Vector2f(cursorX, cursorY));
cursor.setLocation(new Vector2f(cursorX,
LWJGLGameWindow.getHeight()-cursorY));
}
}
/**
* Called by update() when mouse moves
*/
public abstract void mouseMove(int x, int y);
public abstract void mouseUpdate(int x, int y, int w);
/**
* Called by update() when mouse button is pressed
*/
public abstract void mouseDown(int x, int y);
public abstract void mouseDown(int event,int x, int y);
/**
* Called by update() when mouse button is released
*/
public abstract void mouseUp(int x, int y);
public abstract void mouseUp(int event,int x, int y);
}

View file

@ -1,5 +1,6 @@
package ei.engine.scene;
import java.awt.Rectangle;
import java.util.HashMap;
import org.lwjgl.opengl.GL11;
@ -113,17 +114,14 @@ public class AnimatedSprite extends Entity {
// store the current model matrix
GL11.glPushMatrix();
//Sets the scale of the sprite
super.setScaleGL();
//Sets the location
super.setTranslationGL();
//the rotation
super.setRotationGL();
//and sets back the rotation so that the rotation pivot is the center of the sprite
super.setTranslationGL(
-textures.get(currentAnimation)[textureId].getImageWidth()/2,
-textures.get(currentAnimation)[textureId].getImageHeight()/2,
0);
super.setRotationGL(
textures.get(currentAnimation)[textureId].getImageWidth()/2,
textures.get(currentAnimation)[textureId].getImageHeight()/2);
//Sets the scale of the sprite
super.setScaleGL();
// bind to the appropriate texture for this sprite
textures.get(currentAnimation)[textureId].bindGL();
@ -160,4 +158,15 @@ public class AnimatedSprite extends Entity {
}
}
}
/**
* Returns the bound of this class
*/
public Rectangle getBound() {
return new Rectangle(
(int)getLocation().getX(),
(int)getLocation().getY(),
textures.get(currentAnimation)[textureId].getImageWidth(),
textures.get(currentAnimation)[textureId].getImageHeight());
}
}

View file

@ -3,6 +3,8 @@
*/
package ei.engine.scene;
import java.awt.Rectangle;
import org.lwjgl.opengl.GL11;
import ei.engine.math.Vector2f;
@ -118,18 +120,6 @@ public abstract class Entity {
GL11.glTranslatef(location.getX(), location.getY(), location.getZ());
}
/**
* Sets the location of the entity in opengl
*
* @param x The value to add x
* @param y The value to add y
* @param z The value to add z
*/
protected void setTranslationGL(float x, float y, float z){
// translate to the right location and prepare to draw
GL11.glTranslatef(x,y, z);
}
protected void setScaleGL(){
// translate to the right location and prepare to draw
GL11.glScalef(size.getX(), size.getY(), size.getZ());
@ -138,21 +128,33 @@ public abstract class Entity {
/**
* Sets the rotation of the entity in opengl
* before rendering.
*
* @param rx The x coordinate of the center of rotation
* @param ry The y coordinate of the center of rotation
*/
protected void setRotationGL(){
protected void setRotationGL(float rx, float ry){
//GL11.glTranslatef(rx,ry, location.getZ());
// rotating the entity
GL11.glRotatef(rotation.getX(), 1.0f, 0.0f, 0.0f); // Rotate On The X Axis
GL11.glRotatef(rotation.getY(), 0.0f, 1.0f, 0.0f); // Rotate On The Y Axis
GL11.glRotatef(rotation.getZ(), 0.0f, 0.0f, 1.0f); // Rotate On The Z Axis
GL11.glTranslatef(-rx,-ry, location.getZ());
}
public boolean intersects(Entity e){
return getBound().intersects(e.getBound());
}
/**
* the render method should be implemented for every entity
* the render method can be implemented for every entity
*/
public abstract void render();
public void render(){}
/**
* the update method can be implemented for every entity
*/
public void update(){}
public abstract Rectangle getBound();
}

View file

@ -1,5 +1,7 @@
package ei.engine.scene;
import java.awt.Rectangle;
import org.lwjgl.opengl.GL11;
import ei.engine.texture.Texture;
@ -74,10 +76,14 @@ public class Sprite extends Entity {
return texture.getImageHeight();
}
protected void bindTexture(){
/**
* Returns the texture
* @return The texture
*/
public Texture getTexture(){
return texture;
}
/**
* Draw the sprite
*/
@ -85,14 +91,12 @@ public class Sprite extends Entity {
// store the current model matrix
GL11.glPushMatrix();
//Sets the scale of the sprite
super.setScaleGL();
//Sets the location
super.setTranslationGL();
//the rotation
super.setRotationGL();
//and sets back the rotation so that the rotation pivot is the center of the sprite
super.setTranslationGL(-texture.getImageWidth()/2,-texture.getImageHeight()/2,0);
super.setRotationGL(texture.getImageWidth()/2,texture.getImageHeight()/2);
//Sets the scale of the sprite
super.setScaleGL();
// bind to the appropriate texture for this sprite
texture.bindGL();
@ -120,4 +124,15 @@ public class Sprite extends Entity {
GL11.glPopMatrix();
}
/**
* Returns the bound of this class
*/
public Rectangle getBound() {
return new Rectangle(
(int)getLocation().getX(),
(int)getLocation().getY(),
texture.getImageWidth(),
texture.getImageHeight());
}
}

View file

@ -1,12 +1,14 @@
package ei.engine.sound;
import java.awt.Rectangle;
import ei.engine.math.Vector3f;
import ei.engine.scene.Entity;
/**
* A sound that can be played through OpenAL
*
* @author Kevin Glass
* @author Ziver Koc
*/
public class Sound extends Entity{
private static int soundId = 1;
@ -91,9 +93,14 @@ public class Sound extends Entity{
return velocity;
}
/**
* unimplamented method
*
/**
* Returns the bound of this class
*/
public void render() {}
public Rectangle getBound() {
return new Rectangle(
(int)getLocation().getX(),
(int)getLocation().getY(),
10, 10);
}
}

View file

@ -14,7 +14,6 @@ import ei.engine.util.MultiPrintStream;
/**
* Responsible for holding and playing the sounds used in the game.
*
* @author Kevin Glass
* @author Ziver Koc
*/
public class SoundManager {

View file

@ -1,11 +1,24 @@
package ei.engine.state;
import ei.engine.input.InputHandler;
public abstract class GameState {
private String name;
private InputHandler input;
private boolean enabled = false;
public GameState(String name){
this.name = name;
input = new InputHandler();
}
/**
* Returns the input handler of this state
*
* @return The input handler
*/
public InputHandler getInput(){
return input;
}
/**
@ -30,6 +43,24 @@ public abstract class GameState {
return name;
}
/**
* Updates the State
*
*/
public void stateUpdate(){
input.update();
update();
}
/**
* Renders the state
*
*/
public void stateRender(){
render();
input.render();
}
public abstract void update();
public abstract void render();

View file

@ -111,7 +111,7 @@ public class GameStateManager {
public void update(){
for(int i=0; i<gameStates.size() ;i++){
if(gameStates.get(i).isEnabled()){
gameStates.get(i).update();
gameStates.get(i).stateUpdate();
}
}
}
@ -122,7 +122,7 @@ public class GameStateManager {
public void render(){
for(int i=0; i<gameStates.size() ;i++){
if(gameStates.get(i).isEnabled()){
gameStates.get(i).render();
gameStates.get(i).stateRender();
}
}
}

View file

@ -0,0 +1,19 @@
package ei.engine.test;
import ei.engine.LWJGLGameWindow;
import ei.engine.state.GameStateManager;
public class GameStateTest extends LWJGLGameWindow{
public static void main(String[] args){
new GameStateTest();
}
public GameStateTest() {
super("GameStateTest");
}
protected void init(){
GameStateManager.getInstance().addState(new GameStateTestState("GameStateTestState"));
GameStateManager.getInstance().setActive("GameStateTestState");
}
}

View file

@ -0,0 +1,49 @@
package ei.engine.test;
import ei.engine.LWJGLGameWindow;
import ei.engine.effects.Particles;
import ei.engine.math.Vector2f;
import ei.engine.scene.Node;
import ei.engine.scene.Sprite;
import ei.engine.sound.Sound;
import ei.engine.state.GameState;
public class GameStateTestState extends GameState{
private Node rootNode;
private Sprite sprite1;
private Sound sound1;
private Particles p;
public GameStateTestState(String name){
super(name);
rootNode = new Node("InGameNode");
sprite1 = new Sprite("tank","data/units/tank.png");
//sprite1.setScale(new Vector2f(0.5f,0.5f));
//sprite1.setLocation(new Vector2f(300,300));
rootNode.add(sprite1);
p = new Particles("particle");
p.setLocation(sprite1.getLocation());
rootNode.add(p);
sound1 = new Sound("sound","data/sounds/center.wav");
sound1.loop();
sound1.setLocation(sprite1.getLocation());
rootNode.add(sound1);
}
@Override
public void render() {
rootNode.render();
}
@Override
public void update() {
LWJGLGameWindow.getCamera().getLocation().add(new Vector2f(-1.5f,-1.5f));
sprite1.getRotation().add(0, 0, 0.5f);
rootNode.update();
}
}

View file

@ -55,7 +55,7 @@ import org.lwjgl.util.WaveData;
* @version $Revision$
* $Id$
*/
public class PositionTest extends BasicTest {
public class SoundPositionTest extends BasicTest {
/** *Small* glut implementation :) */
private GLUT glut;
@ -148,10 +148,6 @@ public class PositionTest extends BasicTest {
// =====================================================
LWJGLUtil.log("Setting up window");
// calc center
int centerX = (Display.getDisplayMode().getWidth() - WINDOW_WIDTH) / 2;
int centerY = (Display.getDisplayMode().getHeight() - WINDOW_HEIGHT) / 2;
// setup window
setDisplayMode();
Display.create();
@ -180,7 +176,7 @@ public class PositionTest extends BasicTest {
AL10.alListener(AL10.AL_POSITION, listenerPosition);
AL10.alListener(AL10.AL_VELOCITY, listenerVelocity);
//AL10.alListener(AL10.AL_ORIENTATION, listenerOrientation);
AL10.alListener(AL10.AL_ORIENTATION, listenerOrientation);
// creating buffers
LWJGLUtil.log("Creating buffers");
@ -481,7 +477,7 @@ public class PositionTest extends BasicTest {
* String array containing arguments
*/
public static void main(String[] args) {
PositionTest positionTest = new PositionTest();
SoundPositionTest positionTest = new SoundPositionTest();
positionTest.execute(args);
System.exit(0);
}

View file

@ -0,0 +1,12 @@
/**
*
*/
package ei.engine.texture;
/**
* @author Ziver
*
*/
public class MultiTexture {
}

View file

@ -25,9 +25,9 @@ public class Texture {
/** The width of the image */
private int width;
/** The width of the texture */
private int texWidth;
private float texWidth;
/** The height of the texture */
private int texHeight;
private float texHeight;
/** The ratio of the width of the image to the texture */
private float widthRatio;
/** The ratio of the height of the image to the texture */
@ -123,7 +123,7 @@ public class Texture {
*
* @param texHeight The height of the texture
*/
public void setTextureHeight(int texHeight) {
public void setTextureHeight(float texHeight) {
this.texHeight = texHeight;
setHeight();
}
@ -133,7 +133,7 @@ public class Texture {
*
* @param texWidth The width of the texture
*/
public void setTextureWidth(int texWidth) {
public void setTextureWidth(float texWidth) {
this.texWidth = texWidth;
setWidth();
}

View file

@ -91,12 +91,12 @@ public class TextureLoader {
* @throws IOException Indicates a failure to access the resource
*/
public Texture getTexture(String resourceName){
MultiPrintStream.out.println("Loading texture: "+resourceName);
Texture tex = (Texture) table.get(resourceName);
if (tex != null) {
return tex;
}
MultiPrintStream.out.println("Loading texture: "+resourceName);
try {
tex = getTexture(resourceName,