package org.ourunix.android.jpct.modeload; import java.lang.reflect.Field; import javax.microedition.khronos.egl.EGL10; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.egl.EGLDisplay; import javax.microedition.khronos.opengles.GL10; import android.annotation.SuppressLint; import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.view.MotionEvent; import com.threed.jpct.Camera; import com.threed.jpct.FrameBuffer; import com.threed.jpct.Light; import com.threed.jpct.Logger; import com.threed.jpct.Object3D; import com.threed.jpct.Primitives; import com.threed.jpct.RGBColor; import com.threed.jpct.SimpleVector; import com.threed.jpct.Texture; import com.threed.jpct.TextureManager; import com.threed.jpct.World; import com.threed.jpct.util.BitmapHelper; import com.threed.jpct.util.MemoryHelper; /** * A simple demo. This shows more how to use jPCT-AE than it shows how to write * a proper application for Android. It includes basic activity management to * handle pause and resume... * * @author EgonOlsen * */ public class MainActivity extends Activity { // Used to handle pause and resume... private static MainActivity master = null; private GLSurfaceView mGLView; private MyRender render = null; private FrameBuffer fb = null; private World world = null; private RGBColor back = new RGBColor(50, 50, 100); private float touchTurn = 0; private float touchTurnUp = 0; private float xpos = -1; private float ypos = -1; private Object3D cube = null; private int fps = 0; private Light sun = null; @SuppressLint("NewApi") protected void onCreate(Bundle savedInstanceState) { Logger.log("onCreate"); if (master != null) { copy(master); } super.onCreate(savedInstanceState); mGLView = new GLSurfaceView(getApplication()); mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() { public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { // Ensure that we get a 16bit framebuffer. Otherwise, we'll fall // back to Pixelflinger on some device (read: Samsung I7500) int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE }; EGLConfig[] configs = new EGLConfig[1]; int[] result = new int[1]; egl.eglChooseConfig(display, attributes, configs, 1, result); return configs[0]; } }); render = new MyRender(this); mGLView.setRenderer(render); setContentView(mGLView); } @Override protected void onPause() { super.onPause(); mGLView.onPause(); } @Override protected void onResume() { super.onResume(); mGLView.onResume(); } @Override protected void onStop() { super.onStop(); } private void copy(Object src) { try { Logger.log("Copying data from master Activity!"); Field[] fs = src.getClass().getDeclaredFields(); for (Field f : fs) { f.setAccessible(true); f.set(this, f.get(src)); } } catch (Exception e) { throw new RuntimeException(e); } } public boolean onTouchEvent(MotionEvent me) { if (me.getAction() == MotionEvent.ACTION_DOWN) { xpos = me.getX(); ypos = me.getY(); return true; } if (me.getAction() == MotionEvent.ACTION_UP) { xpos = -1; ypos = -1; touchTurn = 0; touchTurnUp = 0; render.setTouchTurn(0); render.setTouchTurnUp(0); return true; } if (me.getAction() == MotionEvent.ACTION_MOVE) { float xd = me.getX() - xpos; float yd = me.getY() - ypos; xpos = me.getX(); ypos = me.getY(); touchTurn = xd / -100f; touchTurnUp = yd / -100f; render.setTouchTurn(touchTurn); render.setTouchTurnUp(touchTurnUp); //System.out.println("Move"); return true; } try { Thread.sleep(15); } catch (Exception e) { // No need for this... } return super.onTouchEvent(me); } }