diff --git a/src/ei/engine/LWJGLGameWindow.java b/src/ei/engine/LWJGLGameWindow.java index a31af86..4ede109 100644 --- a/src/ei/engine/LWJGLGameWindow.java +++ b/src/ei/engine/LWJGLGameWindow.java @@ -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,8 +53,11 @@ 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,14 +65,16 @@ 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); + + init(); + - // Create the display window - Display.create(); } - + /** * 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 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,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 + * this method shid be overriden */ protected void cleanup() { // Close the window @@ -115,6 +152,7 @@ public class LWJGLGameWindow { /** * 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 @@ -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(){ - - } - } diff --git a/src/ei/engine/math/Vector2I.java b/src/ei/engine/math/Vector2I.java index bbe7976..e53c808 100644 --- a/src/ei/engine/math/Vector2I.java +++ b/src/ei/engine/math/Vector2I.java @@ -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+"]"; + } } diff --git a/src/ei/engine/scene/Node.java b/src/ei/engine/scene/Node.java index 3cd7e82..7c04528 100644 --- a/src/ei/engine/scene/Node.java +++ b/src/ei/engine/scene/Node.java @@ -6,8 +6,8 @@ public class Node extends Sprite { /** the sprites of this node */ protected ArrayList sprites; - public Node(String ref) { - super(ref); + public Node(String name) { + super(name); sprites = new ArrayList(); } diff --git a/src/ei/engine/scene/Sprite.java b/src/ei/engine/scene/Sprite.java index 4ff9d15..fa42fd5 100644 --- a/src/ei/engine/scene/Sprite.java +++ b/src/ei/engine/scene/Sprite.java @@ -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) { @@ -124,28 +135,28 @@ public class Sprite { public void render() { // store the current model matrix GL11.glPushMatrix(); - + // bind to the appropriate texture for this sprite texture.bind(); - + // translate to the right location and prepare to draw 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 - GL11.glBegin(GL11.GL_QUADS); + GL11.glBegin(GL11.GL_QUADS); { - GL11.glTexCoord2f(0, 0); - GL11.glVertex2f(0, 0); - GL11.glTexCoord2f(0, texture.getImageWidth()); - GL11.glVertex2f(0, texture.getImageHeight()); - GL11.glTexCoord2f(texture.getWidth(), texture.getHeight()); - GL11.glVertex2f(texture.getImageWidth(),texture.getImageHeight()); - GL11.glTexCoord2f(texture.getWidth(), 0); - GL11.glVertex2f(texture.getImageWidth(),0); + GL11.glTexCoord2f(0, 0); + GL11.glVertex2f(0, 0); + GL11.glTexCoord2f(0, texture.getHeight()); + GL11.glVertex2f(0, texture.getImageHeight()); + GL11.glTexCoord2f(texture.getWidth(), texture.getHeight()); + GL11.glVertex2f(texture.getImageWidth(),texture.getImageHeight()); + GL11.glTexCoord2f(texture.getWidth(), 0); + GL11.glVertex2f(texture.getImageWidth(),0); } GL11.glEnd(); - + // restore the model view matrix to prevent contamination GL11.glPopMatrix(); } diff --git a/src/ei/engine/state/GameState.java b/src/ei/engine/state/GameState.java index 7ae8f1a..4068f5c 100644 --- a/src/ei/engine/state/GameState.java +++ b/src/ei/engine/state/GameState.java @@ -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(); } diff --git a/src/ei/engine/state/GameStateManager.java b/src/ei/engine/state/GameStateManager.java index e868a4c..9cbc1fb 100644 --- a/src/ei/engine/state/GameStateManager.java +++ b/src/ei/engine/state/GameStateManager.java @@ -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; } @@ -99,7 +99,7 @@ public class GameStateManager { private int getId(String name){ for(int i=0; i