PageRenderTime 42ms CodeModel.GetById 20ms app.highlight 7ms RepoModel.GetById 14ms app.codeStats 0ms

/TheElements/src/sand/falling/opengl/SandView.java

http://thelements.googlecode.com/
Java | 89 lines | 70 code | 13 blank | 6 comment | 11 complexity | 10692b093680e7ef535431ca97a63c39 MD5 | raw file
 1package sand.falling.opengl;
 2
 3import javax.microedition.khronos.egl.EGLConfig;
 4import javax.microedition.khronos.opengles.GL10;
 5
 6import android.content.Context;
 7import android.opengl.GLSurfaceView;
 8import android.util.AttributeSet;
 9import android.util.Log;
10import android.view.MotionEvent;
11
12public class SandView extends GLSurfaceView
13{
14	private static int fd; //Set the "finger down" variable
15	
16    public SandView(Context context, AttributeSet attrs)
17    {
18        super(context, attrs);
19    	mRenderer = new DemoRenderer(); //Set up the Renderer for the View
20        setRenderer(mRenderer); //Set it as the main
21        Log.v("DemoActivity", "SandView Constructor");
22        
23    }
24
25    //When finger is held down, flood of events killing framerate, need to put in it's own thread at some point
26    //and then use the sleep tactic
27    public boolean onTouchEvent(final MotionEvent event) //Finger down
28    {
29    	// Gets the mouse position
30    	if (event.getAction() == MotionEvent.ACTION_DOWN)
31    	{
32			if (fd != 1) //if it has changed
33			{
34				MainActivity.setFingerState(1); //sets the finger state in jni
35    		}
36    		else
37    		{
38    			fd = 1;
39    		}
40    	}
41    	else if (event.getAction() == MotionEvent.ACTION_UP)
42    	{
43    		MainActivity.setFingerState(2);
44    	}
45    	
46    	if(MainActivity.size == 0)
47    	{
48    		//Both x and y are halved because it needs to be zoomed in
49    		MainActivity.setMouseLocation((int)event.getX()/2, (int)event.getY()/2); //sets the mouse position in jdk
50    	}
51    	else
52    	{
53    		//Not zoomed in
54    		MainActivity.setMouseLocation((int)event.getX(), (int)event.getY()); //sets the mouse position in jdk		
55    	}
56    	
57    	return true;
58    }
59
60    DemoRenderer mRenderer; //Declare the renderer
61}
62
63class DemoRenderer implements GLSurfaceView.Renderer
64{
65    public void onSurfaceCreated(GL10 gl, EGLConfig config)
66    {
67        nativeInit();
68    }
69
70    public void onSurfaceChanged(GL10 gl, int w, int h)
71    {
72        //gl.glViewport(0, 0, w, h);
73        nativeResize(w, h);
74        if (MainActivity.loaddemov == true) //loads the demo from the sdcard on first run.
75        {
76        	MainActivity.loadDemo();
77        	MainActivity.loaddemov = false;
78        }
79    }
80
81    public void onDrawFrame(GL10 gl)
82    {
83		nativeRender(); //Actual rendering - everything happens here
84    }
85
86    private static native void nativeInit(); //Jni init
87    private static native void nativeResize(int w, int h); //Jni resize
88    private static native void nativeRender(); //Jni rendering function - everything happens here
89}