📄 m3gcanvas.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.m3g.Background;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Group;
import javax.microedition.m3g.Light;
import javax.microedition.m3g.Loader;
import javax.microedition.m3g.Mesh;
import javax.microedition.m3g.Object3D;
import javax.microedition.m3g.PolygonMode;
import javax.microedition.m3g.Transform;
import javax.microedition.m3g.World;
public class M3GCanvas
extends GameCanvas
implements Runnable {
// Thread-control
boolean running = false;
boolean done = true;
// If the game should end
public static boolean gameOver = false;
// Rendering hints
public static final int STRONG_RENDERING_HINTS = Graphics3D.ANTIALIAS | Graphics3D.TRUE_COLOR | Graphics3D.DITHER;
public static final int WEAK_RENDERING_HINTS = 0;
public static int RENDERING_HINTS = STRONG_RENDERING_HINTS;
// Key array
boolean[] key = new boolean[5];
// Key constants
public static final int FIRE = 0;
public static final int UP = FIRE + 1;
public static final int DOWN = UP + 1;
public static final int LEFT = DOWN + 1;
public static final int RIGHT = LEFT + 1;
// Global identity matrix
Transform identity = new Transform();
// Global Graphics3D object
Graphics3D g3d = null;
// The background
Background back = null;
// The global camera object
Camera cam = null;
// The particle system
ParticleSystem ps = null;
FountainEffect fx = null;
// The playing field
Mesh paddle;
Group playingField;
Ball ball;
// Transforms
Transform trLeftWall, trRightWall, trTopWall, trBottomWall;
Transform trPaddle;
Transform trCam = new Transform();
// Paddle's coords
float[] paddleCoords = {0.0f, 0.0f, -5.0f};
// Wall constants
public static final int TOP_WALL = 0;
public static final int LEFT_WALL = 1;
public static final int RIGHT_WALL = 2;
public static final int BOTTOM_WALL = 3;
public static final int PADDLE_WALL = 4;
public static final int PLAYING_FIELD = 5;
// Vectors for our walls
// Explanation: Each wall holds two vectors that
// define the plane (See linear algebra) and
// the wall's normal vector.
float[][][] wallVec = new float[PLAYING_FIELD][3][3];
/** Constructs the canvas
*/
public M3GCanvas(int fps)
{
// We don't want to capture keys normally
super(true);
// We want a fullscreen canvas
setFullScreenMode(true);
// Create our playing field
createField();
// Load our camera
loadCamera();
// Load our background
loadBackground();
// Set up graphics 3d
setUp();
}
/** Prepares the Graphics3D engine for immediate mode rendering by adding a light */
private void setUp()
{
// Get the instance
g3d = Graphics3D.getInstance();
// Add a light to our scene, so we can see something
g3d.addLight(createAmbientLight(), identity);
}
/** Creates a simple ambient light */
private Light createAmbientLight()
{
Light l = new Light();
l.setMode(Light.AMBIENT);
l.setIntensity(1.0f);
return l;
}
/** When fullscreen mode is set, some devices will call
* this method to notify us of the new width/height.
* However, we don't really care about the width/height
* in this tutorial so we just let it be
*/
public void sizeChanged(int newWidth, int newHeight)
{
}
/** Loads our camera */
private void loadCamera()
{
// Create a new camera
cam = new Camera();
// Set the perspective of our camera (choose a pretty wide FoV for a nifty tube effect)
cam.setPerspective(130.0f, (float)getWidth() / (float)getHeight(), 0.1f, 50.0f);
}
/** Loads the background */
private void loadBackground()
{
// Create a new background, set bg color to black
back = new Background();
back.setColor(0);
}
/** Creates our playing field. It will instantiate the ball and the three
* walls (fourth wall is the "screen"
*/
private void createField()
{
try
{
loadBall();
createPaddle();
createWalls();
}
catch(IOException e)
{
System.out.println("Loading error: " + e);
}
}
/**
*
*/
private void createWalls() {
// Create all planes with our nifty MeshFactory class (we need several for collision)
Mesh wall1 = MeshFactory.createPlane("/res/wall.png", PolygonMode.CULL_BACK);
Mesh wall2 = MeshFactory.createPlane("/res/wall.png", PolygonMode.CULL_BACK);
Mesh wall3 = MeshFactory.createPlane("/res/wall.png", PolygonMode.CULL_BACK);
Mesh wall4 = MeshFactory.createPlane("/res/wall.png", PolygonMode.CULL_BACK);
// We want nice perspective correction here
MeshOperator.setPerspectiveCorrection(wall1, true);
MeshOperator.setPerspectiveCorrection(wall2, true);
MeshOperator.setPerspectiveCorrection(wall3, true);
MeshOperator.setPerspectiveCorrection(wall4, true);
// Set the left wall at its true position
trLeftWall = new Transform();
trLeftWall.postTranslate(-4.0f, 0.0f, -5.0f);
trLeftWall.postRotate(90, 0.0f, 1.0f, 0.0f);
trLeftWall.postScale(5.0f, 5.0f, 5.0f);
wall1.setTransform(trLeftWall);
// Make its vectors
float[] v = VectorOps.vector(0.0f, 1.0f, 0.0f);
float[] u = VectorOps.vector(0.0f, 0.0f, 1.0f);
float[] normVec = VectorOps.calcNormal(v, u);
wallVec[LEFT_WALL][0] = v;
wallVec[LEFT_WALL][1] = u;
wallVec[LEFT_WALL][2] = normVec;
// Set the right wall at its true position
trRightWall = new Transform();
trRightWall.postTranslate(4.0f, 0.0f, -5.0f);
trRightWall.postRotate(-90, 0.0f, 1.0f, 0.0f);
trRightWall.postRotate(180, 0.0f, 0.0f, 1.0f);
trRightWall.postScale(5.0f, 5.0f, 5.0f);
wall2.setTransform(trRightWall);
// Same vectors as the left wall
wallVec[RIGHT_WALL][0] = v;
wallVec[RIGHT_WALL][1] = u;
wallVec[RIGHT_WALL][2] = normVec;
// Set the top wall at its true position
trTopWall = new Transform();
trTopWall.postTranslate(0.0f, 4.0f, -5.0f);
trTopWall.postRotate(90, 1.0f, 0.0f, 0.0f);
trTopWall.postRotate(-90, 0.0f, 0.0f, 1.0f);
trTopWall.postScale(5.0f, 5.0f, 5.0f);
wall3.setTransform(trTopWall);
// Make its vectors
v = VectorOps.vector(1.0f, 0.0f, 0.0f);
u = VectorOps.vector(0.0f, 0.0f, 1.0f);
normVec = VectorOps.calcNormal(v, u);
wallVec[TOP_WALL][0] = v;
wallVec[TOP_WALL][1] = u;
wallVec[TOP_WALL][2] = normVec;
// Set the bottom wall at its true position
trBottomWall = new Transform();
trBottomWall.postTranslate(0.0f, -4.0f, -5.0f);
trBottomWall.postRotate(-90, 1.0f, 0.0f, 0.0f);
trBottomWall.postRotate(90, 0.0f, 0.0f, 1.0f);
trBottomWall.postScale(5.0f, 5.0f, 5.0f);
wall4.setTransform(trBottomWall);
// Same vectors as top wall
wallVec[BOTTOM_WALL][0] = v;
wallVec[BOTTOM_WALL][1] = u;
wallVec[BOTTOM_WALL][2] = normVec;
// So we can recognize them later
wall1.setUserID(LEFT_WALL);
wall2.setUserID(RIGHT_WALL);
wall3.setUserID(TOP_WALL);
wall4.setUserID(BOTTOM_WALL);
// Make sure we can collide with them
wall1.setPickingEnable(true);
wall2.setPickingEnable(true);
wall3.setPickingEnable(true);
wall4.setPickingEnable(true);
// Add walls to field group
playingField.addChild(wall1);
playingField.addChild(wall2);
playingField.addChild(wall3);
playingField.addChild(wall4);
}
/**
*
*/
private void createPaddle()
{
// Create a plane using our nifty MeshFactory class
paddle = MeshFactory.createPlane("/res/paddle.png", PolygonMode.CULL_BACK);
// Set the paddle at its initial position
trPaddle = new Transform();
trPaddle.postTranslate(paddleCoords[0], paddleCoords[1], paddleCoords[2]);
paddle.setTransform(trPaddle);
// Make sure it's collidable
paddle.setPickingEnable(true);
paddle.setUserID(PADDLE_WALL);
// Add to the playing field
playingField = new Group();
playingField.setUserID(PLAYING_FIELD);
playingField.addChild(paddle);
// Create its vector
float[] v = {0.0f, 1.0f, 0.0f};
float[] u = {1.0f, 0.0f, 0.0f};
float[] normVec = VectorOps.calcNormal(v, u);
wallVec[PADDLE_WALL][0] = v;
wallVec[PADDLE_WALL][1] = u;
wallVec[PADDLE_WALL][2] = normVec;
}
/**
* Loads our ball
*/
private void loadBall() throws IOException
{
// Simply allocate an instance of the Ball class
ball = new Ball();
}
/** Draws to screen
*/
private void draw(Graphics g)
{
// Envelop all in a try/catch block just in case
try
{
// Get the Graphics3D context
g3d = Graphics3D.getInstance();
// First bind the graphics object. We use our pre-defined rendering hints.
g3d.bindTarget(g, true, RENDERING_HINTS);
// Clear background
g3d.clear(back);
// Bind camera at fixed position in origo
g3d.setCamera(cam, trCam);
// Render the playing field and ball
g3d.render(playingField, identity);
ball.render(g3d, playingField, wallVec, paddleCoords);
// Check controls for paddle movement
if(key[UP])
{
paddleCoords[1] += 0.2f;
if(paddleCoords[1] > 3.0f)
paddleCoords[1] = 3.0f;
}
if(key[DOWN])
{
paddleCoords[1] -= 0.2f;
if(paddleCoords[1] < -3.0f)
paddleCoords[1] = -3.0f;
}
if(key[LEFT])
{
paddleCoords[0] -= 0.2f;
if(paddleCoords[0] < -3.0f)
paddleCoords[0] = -3.0f;
}
if(key[RIGHT])
{
paddleCoords[0] += 0.2f;
if(paddleCoords[0] > 3.0f)
paddleCoords[0] = 3.0f;
}
// Set paddle's coords
trPaddle.setIdentity();
trPaddle.postTranslate(paddleCoords[0], paddleCoords[1], paddleCoords[2]);
paddle.setTransform(trPaddle);
// Quit if user presses fire
if(key[FIRE])
ball.start();
}
catch(Exception e)
{
reportException(e);
}
finally
{
// Always remember to release!
g3d.releaseTarget();
}
// Do some old-fashioned 2D drawing
if(!ball.isMoving())
{
g.setColor(0);
g.drawString("Press fire to start!", 2, 2, Graphics.TOP | Graphics.LEFT);
}
else
{
int red = Math.min(255, ball.getBounces() * 12);
g.setColor(red, 0, 0);
g.drawString("Score: " + ball.getBounces(), 2, 2, Graphics.TOP | Graphics.LEFT);
}
}
/** Starts the canvas by firing up a thread
*/
public void start() {
Thread myThread = new Thread(this);
// Make sure we know we are running
running = true;
done = false;
// Start
myThread.start();
}
/** Run, runs the whole thread. Also keeps track of FPS
*/
public void run() {
while(running) {
try {
// Call the process method (computes keys)
process();
// Draw everything
draw(getGraphics());
flushGraphics();
// Sleep to prevent starvation
try{ Thread.sleep(30); } catch(Exception e) {}
}
catch(Exception e) {
reportException(e);
}
}
// Notify completion
done = true;
}
/**
* @param e
*/
private void reportException(Exception e) {
System.out.println(e.getMessage());
System.out.println(e);
e.printStackTrace();
}
/** Pauses the game
*/
public void pause() {}
/** Stops the game
*/
public void stop() { running = false; }
/** Processes keys
*/
protected void process()
{
int keys = getKeyStates();
if((keys & GameCanvas.FIRE_PRESSED) != 0)
key[FIRE] = true;
else
key[FIRE] = false;
if((keys & GameCanvas.UP_PRESSED) != 0)
key[UP] = true;
else
key[UP] = false;
if((keys & GameCanvas.DOWN_PRESSED) != 0)
key[DOWN] = true;
else
key[DOWN] = false;
if((keys & GameCanvas.LEFT_PRESSED) != 0)
key[LEFT] = true;
else
key[LEFT] = false;
if((keys & GameCanvas.RIGHT_PRESSED) != 0)
key[RIGHT] = true;
else
key[RIGHT] = false;
}
/** Checks if thread is running
*/
public boolean isRunning() { return running; }
/** checks if thread has finished its execution completely
*/
public boolean isDone() { return done; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -