1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 package org.ogre3d.android;
30 
31 import org.ogre3d.android.OgreActivityJNI;
32 
33 import android.app.Activity;
34 import android.hardware.Sensor;
35 import android.hardware.SensorEvent;
36 import android.hardware.SensorEventListener;
37 import android.os.Bundle;
38 import android.os.Handler;
39 import android.view.Surface;
40 import android.view.SurfaceHolder;
41 import android.view.SurfaceHolder.Callback;
42 import android.view.SurfaceView;
43 import android.content.res.AssetManager;
44 
45 public class MainActivity extends Activity implements SensorEventListener {
46 	protected Handler handler = null;
47 	protected SurfaceView surfaceView = null;
48 	protected Surface lastSurface = null;
49 
50 	private Runnable renderer = null;
51 	private boolean paused = false;
52 	private boolean initOGRE = false;
53 	private AssetManager assetMgr = null;
54 
55 	@Override
onCreate(Bundle savedInstanceState)56 	public void onCreate(Bundle savedInstanceState) {
57 		super.onCreate(savedInstanceState);
58 		handler = new Handler();
59 		sysInit();
60 	}
61 
62 	@Override
onPause()63 	protected void onPause() {
64 		super.onPause();
65 		handler.removeCallbacks(renderer);
66 		paused = true;
67 	}
68 
69 	@Override
onResume()70 	protected void onResume() {
71 		super.onResume();
72 		paused = false;
73 		handler.post(renderer);
74 	}
75 
76 	@Override
onDestroy()77 	protected void onDestroy() {
78 		super.onDestroy();
79 
80 		Runnable destroyer = new Runnable() {
81 			public void run() {
82 				OgreActivityJNI.destroy();
83 			}
84 		};
85 		handler.post(destroyer);
86 	}
87 
sysInit()88 	private void sysInit() {
89 		final Runnable initRunnable = new Runnable() {
90 			public void run() {
91 				if (!initOGRE) {
92 					initOGRE = true;
93 
94 					if(assetMgr == null) {
95 						assetMgr = getResources().getAssets();
96 					}
97 
98 					OgreActivityJNI.create(assetMgr);
99 
100 					renderer = new Runnable() {
101 						public void run() {
102 
103 							if (paused)
104 								return;
105 
106 							if (!wndCreate && lastSurface != null) {
107 								wndCreate = true;
108 								OgreActivityJNI.initWindow(lastSurface);
109 								handler.post(this);
110 								return;
111 							}
112 
113 							if (initOGRE && wndCreate)
114 								OgreActivityJNI.renderOneFrame();
115 
116 							handler.post(this);
117 						}
118 					};
119 
120 					handler.post(renderer);
121 				}
122 			}
123 
124 		};
125 
126 		SurfaceView view = new SurfaceView(this);
127 		SurfaceHolder holder = view.getHolder();
128 		// holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
129 		surfaceView = view;
130 
131 		holder.addCallback(new Callback() {
132 			public void surfaceCreated(SurfaceHolder holder) {
133 				if (holder.getSurface() != null
134 						&& holder.getSurface().isValid()) {
135 					lastSurface = holder.getSurface();
136 					handler.post(initRunnable);
137 				}
138 			}
139 
140 			public void surfaceDestroyed(SurfaceHolder holder) {
141 				if (initOGRE && wndCreate) {
142 					wndCreate = false;
143 					lastSurface = null;
144 					handler.post(new Runnable() {
145 						public void run() {
146 							OgreActivityJNI.termWindow();
147 						}
148 					});
149 				}
150 			}
151 
152 			public void surfaceChanged(SurfaceHolder holder, int format,
153 					int width, int height) {
154 
155 			}
156 		});
157 		setContentView(surfaceView);
158 	}
159 
160 	boolean wndCreate = false;
161 
onAccuracyChanged(Sensor sensor, int accuracy)162 	public void onAccuracyChanged(Sensor sensor, int accuracy) {
163 
164 	}
165 
onSensorChanged(SensorEvent event)166 	public void onSensorChanged(SensorEvent event) {
167 		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
168 		}
169 	}
170 
171 	static {
172 		System.loadLibrary("OgreJNI");
173 	}
174 }
175