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

View file

@ -10,6 +10,8 @@ public class Vector2I {
} }
/** /**
* Get the X value
*
* @return the x value in the vector * @return the x value in the vector
*/ */
public int getX(){ public int getX(){
@ -17,9 +19,24 @@ public class Vector2I {
} }
/** /**
* Get the Y value
*
* @return the y value in the vector * @return the y value in the vector
*/ */
public int getY(){ public int getY(){
return y; 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 */ /** the sprites of this node */
protected ArrayList<Sprite> sprites; protected ArrayList<Sprite> sprites;
public Node(String ref) { public Node(String name) {
super(ref); super(name);
sprites = new ArrayList<Sprite>(); sprites = new ArrayList<Sprite>();
} }

View file

@ -38,6 +38,18 @@ public class Sprite {
location = new Vector2I(0,0); 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. * Create a new sprite from a specified image.
* *
@ -47,9 +59,9 @@ public class Sprite {
public Sprite(String name, String ref) { public Sprite(String name, String ref) {
try { try {
this.name = name; 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) { } catch (IOException e) {
// a tad abrupt, but our purposes if you can't find a // a tad abrupt, but our purposes if you can't find a
// sprite's image you might as well give up. // 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() { public Vector2I getLocation() {
if(texture == null){ return location;
return 0;
}
return texture.getImageHeight();
} }
/** /**
* set the location of this sprite in pixels * set the location of this sprite in pixels
*
* @param x The x coordinate of the sprite * @param x The x coordinate of the sprite
* @param y The y 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 * set the location of this sprite in pixels
*
* @param v The location of the sprite * @param v The location of the sprite
*/ */
public void setLocation(Vector2I v) { public void setLocation(Vector2I v) {
@ -124,28 +135,28 @@ public class Sprite {
public void render() { public void render() {
// store the current model matrix // store the current model matrix
GL11.glPushMatrix(); GL11.glPushMatrix();
// bind to the appropriate texture for this sprite // bind to the appropriate texture for this sprite
texture.bind(); texture.bind();
// translate to the right location and prepare to draw // translate to the right location and prepare to draw
GL11.glTranslatef(location.getX(), location.getY(), 0); GL11.glTranslatef(location.getX(), location.getY(), 0);
GL11.glColor3f(1,1,1); GL11.glColor3f(1,1,1);
// draw a quad textured to match the sprite // draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS); GL11.glBegin(GL11.GL_QUADS);
{ {
GL11.glTexCoord2f(0, 0); GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0); GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(0, texture.getImageWidth()); GL11.glTexCoord2f(0, texture.getHeight());
GL11.glVertex2f(0, texture.getImageHeight()); GL11.glVertex2f(0, texture.getImageHeight());
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight()); GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
GL11.glVertex2f(texture.getImageWidth(),texture.getImageHeight()); GL11.glVertex2f(texture.getImageWidth(),texture.getImageHeight());
GL11.glTexCoord2f(texture.getWidth(), 0); GL11.glTexCoord2f(texture.getWidth(), 0);
GL11.glVertex2f(texture.getImageWidth(),0); GL11.glVertex2f(texture.getImageWidth(),0);
} }
GL11.glEnd(); GL11.glEnd();
// restore the model view matrix to prevent contamination // restore the model view matrix to prevent contamination
GL11.glPopMatrix(); GL11.glPopMatrix();
} }

View file

@ -1,9 +1,13 @@
package ei.engine.state; package ei.engine.state;
abstract class GameState { public abstract class GameState {
private String name; private String name;
private boolean enabled = false; private boolean enabled = false;
public GameState(String name){
this.name = name;
}
/** /**
* set if this State is enabled * set if this State is enabled
* @param b * @param b
@ -26,7 +30,7 @@ abstract class GameState {
return name; 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){ public GameState getState(String name){
int i = getId(name); int i = getId(name);
if(i >= 0){ if(i >= 0){
gameStates.get(i); return gameStates.get(i);
} }
return null; return null;
} }
@ -99,7 +99,7 @@ public class GameStateManager {
private int getId(String name){ private int getId(String name){
for(int i=0; i<gameStates.size() ;i++){ for(int i=0; i<gameStates.size() ;i++){
if(gameStates.get(i).getName().equals(name)){ if(gameStates.get(i).getName().equals(name)){
return i; return i;
} }
} }
return -1; return -1;

View file

@ -1,5 +1,20 @@
package ei.game; 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);
}
}