Initial import.

This commit is contained in:
Ziver Koc 2007-03-07 21:42:12 +00:00
parent 5f299890cb
commit 330607c72b
23 changed files with 338 additions and 0 deletions

View file

@ -0,0 +1,144 @@
package ei.engine;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class LWJGLGameWindow {
private boolean exit = false;
private int width;
private int height;
private int fps;
private boolean fullscreen;
private String title;
public LWJGLGameWindow(String title){
this(800, 600, 60, false, title);
}
public LWJGLGameWindow(int width, int height, int fps, boolean fullscreen, String title){
this.width = width;
this.height = height;
this.fps = fps;
this.fullscreen = fullscreen;
this.title = title;
try {
initDisplay();
run();
} catch (Exception e) {
e.printStackTrace(System.err);
Sys.alert(title, "An error occured and the game will exit.");
} finally {
cleanup();
}
System.exit(0);
}
/**
* Initialise the game
* @throws Exception if init fails
*/
private void initDisplay() throws Exception {
DisplayMode dm = new DisplayMode(width, height);
Display.setDisplayMode(dm);
Display.setTitle(title);
Display.setFullscreen(fullscreen);
// grab the mouse, dont want that hideous cursor when we're playing!
Mouse.setGrabbed(true);
// enable textures since we're going to use these for our sprites
GL11.glEnable(GL11.GL_TEXTURE_2D);
// disable the OpenGL depth test since we're rendering 2D graphics
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
// 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();
}
/**
* Runs the game (the "main loop")
*/
private void run() {
while (!exit) {
// Always call Window.update(), all the time - it does some behind the
// scenes work, and also displays the rendered output
Display.update();
// Check for close requests
if (Display.isCloseRequested()) {
exit = true;
}
// The window is in the foreground, so we should play the game
else if (Display.isActive()) {
update();
mainRender();
Display.sync(fps);
}
// The window is not in the foreground, so we can allow other stuff to run and
// infrequently update
else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
update();
// Only bother rendering if the window is visible or dirty
if (Display.isVisible() || Display.isDirty()) {
mainRender();
}
}
}
}
/**
* 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
*/
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

@ -0,0 +1,32 @@
package ei.engine.state;
abstract class GameState {
private String name;
private boolean enabled = false;
/**
* set if this State is enabled
* @param b
*/
public void setEnabled(boolean b){
enabled = b;
}
/**
* @return the status of the GameState
*/
public boolean isEnabled(){
return enabled;
}
/**
* @return the name of the GameState
*/
public String getName(){
return name;
}
abstract void update();
abstract void render();
}

View file

@ -0,0 +1,131 @@
package ei.engine.state;
import java.util.ArrayList;
public class GameStateManager {
private static GameStateManager instance;
private ArrayList<GameState> gameStates;
private GameStateManager(){
gameStates = new ArrayList<GameState>();
}
/**
* Add A GameState to the GameStateManager
* @param g The GameState to add
* @return true if successful else false
*/
public boolean addState(GameState g){
if(!gameStates.contains(g)){
gameStates.add(g);
return true;
}
return false;
}
/**
* Remove a GameState from the GameStateManager
* @param g The state to remove
* @return true if successful else false
*/
public boolean removeState(GameState g){
if(gameStates.contains(g)){
gameStates.remove(g);
return true;
}
return false;
}
/**
* Removes a GameState by its name
* @param name the name of the GameState to remove
* @return true if successful else false
*/
public boolean removeStateByName(String name){
int i = getId(name);
if(i >= 0){
gameStates.remove(i);
return true;
}
return false;
}
/**
* Returns the GameState in GameStateManager whit the given name
* @param name the name of the GameState
* @return the GameState by the name else null
*/
public GameState getState(String name){
int i = getId(name);
if(i >= 0){
gameStates.get(i);
}
return null;
}
/**
* Activates a GameState by the name
* @param name the name of the GameState
* @return true if successful else false
*/
public boolean setActive(String name){
GameState g = getState(name);
if(g != null){
g.setEnabled(true);
return true;
}
return false;
}
/**
* Deactivates a GameState by the name
* @param name the name of the GameState
* @return true if successful else false
*/
public boolean setDeActive(String name){
GameState g = getState(name);
if(g != null){
g.setEnabled(false);
return true;
}
return false;
}
private int getId(String name){
for(int i=0; i<gameStates.size() ;i++){
if(gameStates.get(i).getName().equals(name)){
return i;
}
}
return -1;
}
/**
* updates all the active GameStates
*/
public void update(){
for(int i=0; i<gameStates.size() ;i++){
if(gameStates.get(i).isEnabled()){
gameStates.get(i).update();
}
}
}
/**
* renders all the active GameStates
*/
public void render(){
for(int i=0; i<gameStates.size() ;i++){
if(gameStates.get(i).isEnabled()){
gameStates.get(i).render();
}
}
}
public static GameStateManager getInstance(){
if(instance == null){
instance = new GameStateManager();
}
return instance;
}
}

5
src/ei/game/EI.java Normal file
View file

@ -0,0 +1,5 @@
package ei.game;
public class EI {
}