If you think the Android project Schooner-3D listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2012 Dan Mercer/*fromwww.java2s.com*/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.supermercerbros.gameengine.hud;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.LinkedList;
import android.opengl.GLES20;
import android.view.MotionEvent;
/**
* Represents the Heads-Up-Display-style UI of a game.
*/publicclass GameHud {
// Constants
privatestaticfinalint DEFAULT_VBO_SIZE = 8000; // 400 verts
privatestaticfinalint DEFAULT_IBO_SIZE = 2400; // 800 faces
privatefinalint vboSize;
privatefinalint iboSize;
privatefinal LinkedList<HudElement> elements;
// Coordinate converter
private CoordsConverter converter;
privatevolatileboolean isInUse = false;
// Buffer handles
privateint arrayBuffer;
privateint elementBuffer;
/**
* Constructs a new GameHud.
*/public GameHud() {
this.elements = new LinkedList<HudElement>();
this.vboSize = DEFAULT_VBO_SIZE;
this.iboSize = DEFAULT_IBO_SIZE;
}
/**
* Constructs a new GameHud.
*
* @param vboSize
* @param iboSize
*/public GameHud(int vboSize, int iboSize) {
this.elements = new LinkedList<HudElement>();
this.vboSize = vboSize;
this.iboSize = iboSize;
}
/**
* Adds the given {@link HudElement} to this GameHud
*
* @param element
* The <code>HudElement</code> to add.
*/publicvoid addElement(HudElement element) {
if (isInUse) {
thrownew IllegalStateException(
"Cannot addElement to gameHud that is currently in use");
}
final LinkedList<HudElement> localElements = this.elements;
synchronized (localElements) {
localElements.add(element);
}
}
/**
* Called by GameRenderer
*/publicvoid render() {
// GL Buffer stuff
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, arrayBuffer);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
// Disable depth test and face culling
GLES20.glDisable(GLES20.GL_DEPTH_TEST);
GLES20.glDisable(GLES20.GL_CULL_FACE); // TODO delete this line
// Render elements
final LinkedList<HudElement> localElements = this.elements;
synchronized (localElements) {
for (HudElement element : localElements) {
element.render();
}
}
}
/**
* Called by GameRenderer
*/publicvoid load() {
isInUse = true;
// Generate buffers
finalint[] buffers = newint[2];
GLES20.glGenBuffers(2, buffers, 0);
finalint localArrayBuffer = buffers[0];
finalint localElementBuffer = buffers[1];
arrayBuffer = localArrayBuffer;
elementBuffer = localElementBuffer;
// Bind buffers
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, localArrayBuffer);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, localElementBuffer);
// Construct ByteBuffers
final ByteBuffer vbo = ByteBuffer.allocateDirect(vboSize).order(
ByteOrder.nativeOrder());
final ByteBuffer ibo = ByteBuffer.allocateDirect(iboSize).order(
ByteOrder.nativeOrder());
// Fill Buffers and load Programs
final LinkedList<HudElement> localElements = this.elements;
synchronized (localElements) {
for (HudElement element : localElements) {
element.loadProgram();
element.writeIndicesToBuffer(ibo);
element.writeVertsToBuffer(vbo);
}
}
vbo.rewind();
ibo.rewind();
// Initialize buffers
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vboSize, vbo,
GLES20.GL_STATIC_DRAW);
GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, iboSize, ibo,
GLES20.GL_STATIC_DRAW);
}
/**
* Called by GameRenderer
*/publicvoid unload() {
GLES20.glDeleteBuffers(2, newint[]{arrayBuffer, elementBuffer}, 0);
// TODO: check this against documentation
arrayBuffer = -1;
elementBuffer = -1;
isInUse = false;
}
/**
* Called when a MotionEvent occurs. This calls <code>onTouchEvent</code> on
* all elements contained in this GameHud.
*
* @param event
* @return
*/publicboolean onTouchEvent(MotionEvent event) {
for (HudElement element : elements) {
if (element.onTouchEvent(event, converter)) {
return true;
}
}
return false;
}
/**
* @param width
* @param height
*/publicvoid setDimensions(int width, int height) {
this.converter = new CoordsConverter(width, height);
}
}