Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / PhysicsEffects / project / Android / PfxApp_1_Simple / src / pfx / renderingsupport / PhysicsEffectsRenderer.java
1 /*\r
2  Applied Research Associates Inc. (c)2011\r
3 \r
4  Redistribution and use in source and binary forms,\r
5    with or without modification, are permitted provided that the\r
6    following conditions are met:\r
7     * Redistributions of source code must retain the above copyright\r
8       notice, this list of conditions and the following disclaimer.\r
9     * Redistributions in binary form must reproduce the above copyright\r
10       notice, this list of conditions and the following disclaimer in the\r
11       documentation and/or other materials provided with the distribution.\r
12     * Neither the name of the Applied Research Associates Inc nor the names\r
13       of its contributors may be used to endorse or promote products derived\r
14       from this software without specific prior written permission.\r
15 \r
16    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
17    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
18    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
19    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
20    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
21    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
22    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
23    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
24    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
25    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
26    POSSIBILITY OF SUCH DAMAGE.\r
27 */\r
28 \r
29 package pfx.renderingsupport;\r
30 \r
31 import javax.microedition.khronos.egl.EGLConfig;\r
32 import javax.microedition.khronos.opengles.GL10;\r
33 \r
34 import android.opengl.GLSurfaceView;\r
35 import android.util.Log;\r
36 \r
37 /**\r
38  * Implementation of the renderer. It calls the native physicseffects code\r
39  * to run on the init and drawframe allowing the sample to perform its simulation.\r
40  * @author ahamilton\r
41  *\r
42  */\r
43 public class PhysicsEffectsRenderer implements GLSurfaceView.Renderer {\r
44     boolean startup = true;\r
45     boolean notEnded = true;\r
46     static long startuptime;\r
47     static long endtime;\r
48     int frameCounter;\r
49 \r
50         /**\r
51          * Initilize the physics simulation after the GL surface is created.\r
52          *\r
53          */\r
54         public void onSurfaceCreated(GL10 gl, EGLConfig config)\r
55         {\r
56         Log.v("Init physics", "Initing physics");\r
57         nativePhysicsEffectsInit();\r
58         Log.v("Init physics", "Inited physics");\r
59     }\r
60 \r
61         /**\r
62          * Set the viewport for a changed GL surface\r
63          *\r
64          */\r
65         public void onSurfaceChanged(GL10 gl, int w, int h)\r
66         {\r
67         gl.glViewport(0, 0, w, h);\r
68     }\r
69 \r
70         /**\r
71          * Perform one simulation step and render the scene.\r
72          *\r
73          */\r
74         public void onDrawFrame(GL10 gl)\r
75         {\r
76         if(startup){\r
77                 startup = false;\r
78                 frameCounter = 0;\r
79                 startuptime = System.currentTimeMillis();\r
80                 endtime = startuptime + 10000;                    // sets run time to be 30 seconds \r
81         }\r
82 \r
83         nativePhysicsEffectsSimulate();\r
84 \r
85         frameCounter++;\r
86                 long lCurrentTime = System.currentTimeMillis();\r
87         if(lCurrentTime > endtime && notEnded) {\r
88                         float fFrameRate = (1000.0f * (float)frameCounter) / ((float)lCurrentTime - startuptime);\r
89                         Log.v("PHYSICS TIMING STUDY", "Number of Frames: " + Integer.toString(frameCounter));\r
90                         Log.v("PHYSICS TIMING STUDY", "Time ellapsed (milliseconds): " + Long.toString(lCurrentTime - startuptime));\r
91                 Log.v("PHYSICS TIMING STUDY", "Bullet Frame Rate: " + Float.toString(fFrameRate) + "fps");\r
92                 notEnded = false;\r
93         }\r
94     }\r
95 \r
96     private static native void nativePhysicsEffectsInit();\r
97     private static native void nativePhysicsEffectsSimulate();\r
98     private static native void nativePhysicsEffectsUpdate();\r
99     private static native void nativePhysicsEffectsPhysStep();\r
100     private static native void nativePhysicsEffectsRender();\r
101 }\r