Added a small and simple test EI.java. and fixed some buggs

This commit is contained in:
Ziver Koc 2007-03-12 19:13:08 +00:00
parent bea0632d5d
commit dbee794932
8 changed files with 150 additions and 53 deletions

View file

@ -7,6 +7,8 @@ import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import ei.engine.state.GameStateManager;
public class LWJGLGameWindow {
private boolean exit = false;
private int width;
@ -51,7 +53,10 @@ public class LWJGLGameWindow {
Display.setFullscreen(fullscreen);
// grab the mouse, dont want that hideous cursor when we're playing!
Mouse.setGrabbed(true);
Mouse.setGrabbed(false);
// Create the display window
Display.create();
// enable textures since we're going to use these for our sprites
GL11.glEnable(GL11.GL_TEXTURE_2D);
@ -60,12 +65,14 @@ public class LWJGLGameWindow {
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, -1, 1);
// Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
Display.setVSyncEnabled(true);
// Create the display window
Display.create();
init();
}
/**
@ -84,6 +91,7 @@ public class LWJGLGameWindow {
// The window is in the foreground, so we should play the game
else if (Display.isActive()) {
GameStateManager.getInstance().update();
update();
mainRender();
Display.sync(fps);
@ -95,6 +103,7 @@ public class LWJGLGameWindow {
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
GameStateManager.getInstance().update();
update();
// Only bother rendering if the window is visible or dirty
@ -105,24 +114,6 @@ public class LWJGLGameWindow {
}
}
/**
* Do any game-specific cleanup
*/
protected void cleanup() {
// Close the window
Display.destroy();
}
/**
* Do all calculations, handle input, etc.
*/
protected void update() {
// Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
exit = true;
}
}
/**
* Render the current frame
*/
@ -132,13 +123,42 @@ public class LWJGLGameWindow {
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
//let subsystem paint
GameStateManager.getInstance().render();
render();
//update window contents
Display.update();
}
protected void render(){
/**
* this method shid be overriden
*/
protected void render(){}
/**
* this method shid be overriden
*/
protected void init(){}
/**
* Do any game-specific cleanup
* this method shid be overriden
*/
protected void cleanup() {
// Close the window
Display.destroy();
}
/**
* Do all calculations, handle input, etc.
* this method shid be overriden
*/
protected void update() {
// Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
exit = true;
}
}
}

View file

@ -10,6 +10,8 @@ public class Vector2I {
}
/**
* Get the X value
*
* @return the x value in the vector
*/
public int getX(){
@ -17,9 +19,24 @@ public class Vector2I {
}
/**
* Get the Y value
*
* @return the y value in the vector
*/
public int getY(){
return y;
}
/**
* Add to the vectro
* @param i The amount to add
*/
public void add(int i){
x += i;
y += i;
}
public String toString(){
return "Vector2I["+x+","+y+"]";
}
}

View file

@ -6,8 +6,8 @@ public class Node extends Sprite {
/** the sprites of this node */
protected ArrayList<Sprite> sprites;
public Node(String ref) {
super(ref);
public Node(String name) {
super(name);
sprites = new ArrayList<Sprite>();
}

View file

@ -38,6 +38,18 @@ public class Sprite {
location = new Vector2I(0,0);
}
/**
* Create a new sprite from a specified texture.
*
* @param name The name of the sprite
* @param texture The texture to use
*/
public Sprite(String name, Texture texture) {
this.name = name;
this.texture = texture;
this.location = new Vector2I(texture.getImageWidth(),texture.getImageHeight());
}
/**
* Create a new sprite from a specified image.
*
@ -47,9 +59,9 @@ public class Sprite {
public Sprite(String name, String ref) {
try {
this.name = name;
texture = TextureLoader.getTextureLoaderInstance().getTexture(ref);
this.texture = TextureLoader.getTextureLoaderInstance().getTexture(ref);
location = new Vector2I(texture.getImageWidth(),texture.getImageHeight());
this.location = new Vector2I(texture.getImageWidth(),texture.getImageHeight());
} catch (IOException e) {
// a tad abrupt, but our purposes if you can't find a
// sprite's image you might as well give up.
@ -90,19 +102,17 @@ public class Sprite {
}
/**
* Get the height of this sprite in pixels
* Get the Location of the sprite
*
* @return The height of this sprite in pixels
* @return The Location of the sprite
*/
public int getLocation() {
if(texture == null){
return 0;
}
return texture.getImageHeight();
public Vector2I getLocation() {
return location;
}
/**
* set the location of this sprite in pixels
*
* @param x The x coordinate of the sprite
* @param y The y coordinate of the sprite
*/
@ -112,6 +122,7 @@ public class Sprite {
/**
* set the location of this sprite in pixels
*
* @param v The location of the sprite
*/
public void setLocation(Vector2I v) {
@ -137,7 +148,7 @@ public class Sprite {
{
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(0, texture.getImageWidth());
GL11.glTexCoord2f(0, texture.getHeight());
GL11.glVertex2f(0, texture.getImageHeight());
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
GL11.glVertex2f(texture.getImageWidth(),texture.getImageHeight());

View file

@ -1,9 +1,13 @@
package ei.engine.state;
abstract class GameState {
public abstract class GameState {
private String name;
private boolean enabled = false;
public GameState(String name){
this.name = name;
}
/**
* set if this State is enabled
* @param b
@ -26,7 +30,7 @@ abstract class GameState {
return name;
}
abstract void update();
public abstract void update();
abstract void render();
public abstract void render();
}

View file

@ -58,7 +58,7 @@ public class GameStateManager {
public GameState getState(String name){
int i = getId(name);
if(i >= 0){
gameStates.get(i);
return gameStates.get(i);
}
return null;
}

View file

@ -1,5 +1,20 @@
package ei.game;
public class EI {
import ei.engine.LWJGLGameWindow;
import ei.engine.state.GameStateManager;
import ei.game.gamestate.InGameState;
public class EI extends LWJGLGameWindow{
public static void main(String[] args){
new EI();
}
public EI() {
super("EI");
}
protected void init(){
GameStateManager.getInstance().addState(new InGameState("InGameState"));
GameStateManager.getInstance().setActive("InGameState");
}
}

View file

@ -0,0 +1,30 @@
package ei.game.gamestate;
import ei.engine.scene.Node;
import ei.engine.scene.Sprite;
import ei.engine.state.GameState;
public class InGameState extends GameState{
private Node rootNode;
private Sprite s1;
public InGameState(String name){
super(name);
rootNode = new Node("InGameNode");
s1 = new Sprite("tank","data/units/tank.png");
rootNode.add(s1);
s1.setLocation(1, 1);
}
@Override
public void render() {
rootNode.render();
}
@Override
public void update() {
s1.getLocation().add(1);
}
}