Added a small and simple test EI.java. and fixed some buggs
This commit is contained in:
parent
bea0632d5d
commit
dbee794932
8 changed files with 150 additions and 53 deletions
|
|
@ -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(){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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+"]";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<gameStates.size() ;i++){
|
||||
if(gameStates.get(i).getName().equals(name)){
|
||||
return i;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
30
src/ei/game/gamestate/InGameState.java
Normal file
30
src/ei/game/gamestate/InGameState.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue