package com.fallen.client;
import com.jme.app.AbstractGame;
import com.jme.system.GameSettings;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.Timer;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.state.LightState;
import com.jme.scene.state.ZBufferState;
import com.jme.light.DirectionalLight;
import com.jme.input.InputSystem;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.fallen.client.util.IVars;
/**
* Created by IntelliJ IDEA.
* User: GodSent
* Date: Apr 15, 2009
* Time: 7:47:09 PM
* To change this template use File | Settings | File Templates.
*/
public abstract class Game extends AbstractGame {
protected GameSettings getNewSettings() {
return null;
}
public void start() {
super.display = DisplaySystem.getDisplaySystem("LWJGL");
this.initSystem();
super.assertDisplayCreated();
this.initGame();
try {
while(true) {
InputSystem.update();
update(0);
render(0);
display.getRenderer().displayBackBuffer();
Thread.sleep(100L);//adjust this for your own use
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
protected void quit() {
System.exit(0);
}
protected void update(float v) {
if (KeyBindingManager.getKeyBindingManager().isValidCommand( "exit", false)) {
quit();
return;
}
IVars.TIMER.update();
IVars.MAIN_NODE.updateGeometricState(IVars.TIMER.getTimePerFrame(), true);
}
protected void render(float v) {
Renderer renderer = super.display.getRenderer();
renderer.clearBuffers();
renderer.draw(IVars.MAIN_NODE);
}
protected void initSystem() {
super.display.createWindow(765, 503, 24, -1, false);
super.display.setTitle("Fallen Empire 3D");
Camera cam = super.display.getRenderer().createCamera(display.getWidth(), display.getHeight());
cam.setFrustumPerspective( 45.0f, (float) display.getWidth()
/ (float) display.getHeight(), 1, 1000 );
cam.setParallelProjection( false );
cam.update();
Vector3f loc = new Vector3f(500.0f, 150.0f, 500.0f);
Vector3f left = new Vector3f(-1.0f, 0.0f, 0.0f);
Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
Vector3f dir = new Vector3f(0.0f, 0f, -1.0f);
cam.setFrame(loc, left, up, dir);
cam.update();
IVars.CAMERA = cam;
cam = null;//aid the jvm
super.display.getRenderer().setCamera(IVars.CAMERA);
KeyBindingManager.getKeyBindingManager().set("exit", KeyInput.KEY_ESCAPE);
IVars.TIMER = Timer.getTimer();
}
protected void initGame() {
IVars.MAIN_NODE = new Node("MAIN_NODE");
IVars.WIRE_STATE = display.getRenderer().createWireframeState();
IVars.WIRE_STATE.setEnabled(false);
IVars.MAIN_NODE.setRenderState(IVars.WIRE_STATE);
ZBufferState buf = display.getRenderer().createZBufferState();
buf.setEnabled(true);
buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
IVars.MAIN_NODE.setRenderState(buf);
DirectionalLight light = new DirectionalLight();
light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
light.setDirection(new Vector3f(1,-1,0));
light.setEnabled(true);
LightState lightState = display.getRenderer().createLightState();
lightState.setEnabled(true);
lightState.attach(light);
IVars.MAIN_NODE.setRenderState(lightState);
startGameLogic();
IVars.MAIN_NODE.updateGeometricState(0.0f, true);
IVars.MAIN_NODE.updateRenderState();
IVars.TIMER.reset();
}
protected void reinit() {
initGame();
}
protected void cleanup() {
super.display.getRenderer().cleanup();
super.display.getRenderer().getQueue().clearBuckets();
TextureManager.doTextureCleanup();
KeyInput.destroyIfInitalized();
MouseInput.destroyIfInitalized();
}
protected abstract void startGameLogic();
}
package com.fallen.client.util;
import com.jme.renderer.Camera;
import com.jme.input.InputHandler;
import com.jme.scene.Node;
import com.jme.scene.state.LightState;
import com.jme.scene.state.WireframeState;
import com.jme.util.Timer;
/**
* Created by IntelliJ IDEA.
* User: GodSent
* Date: Apr 16, 2009
* Time: 3:47:13 PM
* To change this template use File | Settings | File Templates.
*/
public final class IVars {
public static Camera CAMERA;
public static InputHandler POV;
public static Node MAIN_NODE;
public static WireframeState WIRE_STATE;
public static Timer TIMER;
}
ex usage:
package com.fallen.client;
import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.image.Texture;
import com.jmex.terrain.TerrainBlock;
import com.jmex.terrain.util.MidPointHeightMap;
import com.fallen.client.util.IVars;
/**
* Created by IntelliJ IDEA.
* User: GodSent
* Date: Apr 16, 2009
* Time: 5:27:09 AM
* To change this template use File | Settings | File Templates.
*/
public class GameWrapper extends Game {
protected void startGameLogic() {
MidPointHeightMap heightMap = new MidPointHeightMap(64, 1f);
Vector3f terrainScale = new Vector3f(20, 0.5f, 20);
TerrainBlock tb = new TerrainBlock("Terrain", heightMap.getSize(), terrainScale,
heightMap.getHeightMap(), new Vector3f(0, 0, 0));
tb.setModelBound(new BoundingBox());
tb.updateModelBound();
TextureState ts = display.getRenderer().createTextureState();
ts.setEnabled(true);
Texture t1 = TextureManager.loadTexture("./data/textures/terrain/road.jpg"
, Texture.MinificationFilter.NearestNeighborLinearMipMap
, Texture.MagnificationFilter.Bilinear);
ts.setTexture(t1, 0);
tb.setRenderState(ts);
IVars.MAIN_NODE.attachChild(tb);
}
}
Suggestions?