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.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,7 +53,10 @@ 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,12 +65,14 @@ 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);
|
||||||
|
|
||||||
// Create the display window
|
init();
|
||||||
Display.create();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -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,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
|
* Render the current frame
|
||||||
*/
|
*/
|
||||||
|
|
@ -132,13 +123,42 @@ public class LWJGLGameWindow {
|
||||||
GL11.glMatrixMode(GL11.GL_MODELVIEW);
|
GL11.glMatrixMode(GL11.GL_MODELVIEW);
|
||||||
GL11.glLoadIdentity();
|
GL11.glLoadIdentity();
|
||||||
|
|
||||||
|
//let subsystem paint
|
||||||
|
GameStateManager.getInstance().render();
|
||||||
render();
|
render();
|
||||||
|
|
||||||
|
//update window contents
|
||||||
Display.update();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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+"]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
@ -137,7 +148,7 @@ public class Sprite {
|
||||||
{
|
{
|
||||||
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());
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
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