Răsfoiți Sursa

CrownAccelerometer implemented

mikymod 13 ani în urmă
părinte
comite
ef9010ef94

+ 45 - 26
samples/android/triangle.cpp

@@ -11,47 +11,66 @@ extern "C"
 	JNIEXPORT void JNICALL Java_crown_android_CrownLib_frame(JNIEnv* env, jobject obj);
 };
 
-void draw_triangle()
+class MainScene : public AccelerometerListener
 {
-	static GLfloat vertices[] = {	-1.0f, -1.0f, -2.0f,
-									1.0f, -1.0f, -2.0f,
-									0.0f, 1.0f, -2.0f};
+	
+public:
 
-	GetDevice()->GetRenderer()->SetMatrix(MT_MODEL, Mat4::IDENTITY);
+	MainScene()
+	{
+		get_input_manager()->register_accelerometer_listener(this);
+	}
 
-	Mat4 projection;
-	projection.build_projection_perspective_rh(90.0f, 800.0f/480.0f, 0.1f, 100.0f);
-	GetDevice()->GetRenderer()->SetMatrix(MT_PROJECTION, projection);
+	void accelerometer_changed(const AccelerometerEvent& event)
+	{
+		Log::I("Accelerometer changed");
+	}
 
-	GetDevice()->GetRenderer()->SetClearColor(Color4::RED);
+	void draw_triangle()
+	{
+		static GLfloat vertices[] = {	-1.0f, -1.0f, -2.0f,
+										1.0f, -1.0f, -2.0f,
+										0.0f, 1.0f, -2.0f};
 
-	glEnableClientState(GL_VERTEX_ARRAY);
-	glVertexPointer(3, GL_FLOAT, 0, vertices);
+		GetDevice()->GetRenderer()->SetMatrix(MT_MODEL, Mat4::IDENTITY);
 
-	glDrawArrays(GL_TRIANGLES, 0, 9);
+		Mat4 projection;
+		projection.build_projection_perspective_rh(90.0f, 800.0f/480.0f, 0.1f, 100.0f);
+		GetDevice()->GetRenderer()->SetMatrix(MT_PROJECTION, projection);
 
-	glDisableClientState(GL_VERTEX_ARRAY);
-}
+		GetDevice()->GetRenderer()->SetClearColor(Color4::RED);
 
-void frame()
-{
-	Device* mDevice = GetDevice();
+		glEnableClientState(GL_VERTEX_ARRAY);
+		glVertexPointer(3, GL_FLOAT, 0, vertices);
 
-	os::event_loop();
+		glDrawArrays(GL_TRIANGLES, 0, 9);
 
-	GetInputManager()->EventLoop();
+		glDisableClientState(GL_VERTEX_ARRAY);
+	}
+
+	void frame()
+	{
+		Device* mDevice = GetDevice();
+
+		os::event_loop();
+
+		get_input_manager()->event_loop();
+
+		GetDevice()->GetRenderer()->_BeginFrame();
+		draw_triangle();
+		GetDevice()->GetRenderer()->_EndFrame();
+
+		os::swap_buffers();
+	}
+};
+
+MainScene* scene = new MainScene();
 
-	GetDevice()->GetRenderer()->_BeginFrame();
-	draw_triangle();
-	GetDevice()->GetRenderer()->_EndFrame();
 
-	os::swap_buffers();
-}
 
 JNIEXPORT void JNICALL Java_crown_android_CrownLib_frame(JNIEnv* env, jobject obj)
 {
-	Log::I("Render frame.");
-	frame();
+	scene->frame();
 }
 
 } // namespace crown

+ 2 - 2
src/Config.h

@@ -62,8 +62,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 	b) you can choose OpenGL|ES if and only if you are
 	   on a Linux or Android machine.
 */
-#define CROWN_USE_OPENGL		//!< Whether to build with OpenGL
-//#define CROWN_USE_OPENGLES		//!< Whether to build with OpenGL|ES
+//#define CROWN_USE_OPENGL		//!< Whether to build with OpenGL
+#define CROWN_USE_OPENGLES		//!< Whether to build with OpenGL|ES
 
 //#define CROWN_USE_WINDOWING	//!< Whether to build with windowing
 

+ 7 - 1
src/FPSSystem.cpp

@@ -20,7 +20,8 @@ FPSSystem::FPSSystem(MovableCamera* camera) :
 	m_down_pressed(false),
 	m_left_pressed(false)
 {
-	get_input_manager()->register_keyboard_listener(this);
+//	get_input_manager()->register_keyboard_listener(this);
+	get_input_manager()->register_accelerometer_listener(this);
 	//get_input_manager()->register_mouse_listener(this);
 	get_input_manager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
 	
@@ -90,6 +91,11 @@ void FPSSystem::key_released(const KeyboardEvent& event)
 	}
 }
 
+//-----------------------------------------------------------------------
+void FPSSystem::accelerometer_changed(const AccelerometerEvent& event)
+{
+	set_view_by_cursor();
+}
 //-----------------------------------------------------------------------
 void FPSSystem::set_camera(MovableCamera* camera)
 {

+ 2 - 1
src/FPSSystem.h

@@ -10,7 +10,7 @@
 namespace crown
 {
 /// TODO: set_view_by_cursor must be implemented through scripting
-class FPSSystem : public MouseListener, public KeyboardListener
+class FPSSystem : public MouseListener, public KeyboardListener, public AccelerometerListener
 {
 public:
 
@@ -25,6 +25,7 @@ public:
 
 	virtual void 	key_pressed(const KeyboardEvent& event);
 	virtual void 	key_released(const KeyboardEvent& event);
+	virtual void 	accelerometer_changed(const AccelerometerEvent& event);
 
 private:
 

+ 7 - 0
src/input/EventDispatcher.cpp

@@ -25,6 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "EventDispatcher.h"
 #include "Allocator.h"
+#include "Log.h"
 
 namespace crown
 {
@@ -67,6 +68,12 @@ void EventDispatcher::add_touch_listener(TouchListener* listener)
 	m_touch_listener_list.push_back(listener);
 }
 
+void EventDispatcher::add_accelerometer_listener(AccelerometerListener* listener)
+{
+	assert(listener != NULL);
+	m_acc_listener_list.push_back(listener);
+}
+
 //-----------------------------------------------------------------------------
 void EventDispatcher::button_pressed(const MouseEvent& event)
 {

+ 1 - 1
src/input/InputManager.cpp

@@ -125,8 +125,8 @@ void InputManager::event_loop()
 				sensor_event.x = event.data_a;
 				sensor_event.y = event.data_b;
 				sensor_event.z = event.data_c;
-
 				m_event_dispatcher.accelerometer_changed(sensor_event);
+				break;
 			}
 			default:
 			{

+ 1 - 0
src/input/InputManager.h

@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "EventDispatcher.h"
 #include "Point2.h"
 #include "Vec2.h"
+#include "Log.h"
 
 namespace crown
 {

+ 2 - 2
src/os/android/AndroidInput.cpp

@@ -9,7 +9,7 @@ namespace os
 
 extern "C" 
 {
-	//!< OS push_event() jni bind
+	/// OS push_event() jni bind
     JNIEXPORT void JNICALL Java_crown_android_CrownLib_pushEvent(JNIEnv * env, jobject obj, jint type, jint a, jint b, jint c, jint d);
 };
 
@@ -22,7 +22,7 @@ void init_input()
 //-----------------------------------------------------------------------------
 void event_loop()
 {
-	// not necessary
+	// not necessary, implemented in Android
 }
 
 //-----------------------------------------------------------------------------

+ 16 - 4
src/os/android/AndroidOS.cpp

@@ -36,6 +36,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "OS.h"
 #include "AndroidOS.h"
+#include "Log.h"
 
 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "crown", __VA_ARGS__))
 #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "crown", __VA_ARGS__))
@@ -51,10 +52,13 @@ extern "C"
 {
 	// This is sadly necessary in order to get the asset manager from java...
     JNIEXPORT void JNICALL Java_crown_android_CrownLib_initAssetManager(JNIEnv* env, jobject obj, jobject assetManager);
+	JNIEXPORT void JNICALL Java_crown_android_CrownLib_setRenderWindowMetrics(JNIEnv* env, jobject obj, jint width, jint height);
 };
 
 static timespec			base_time;
 static AAssetManager*	asset_manager = NULL;
+static uint32_t			window_width;
+static uint32_t			window_height;
 
 //-----------------------------------------------------------------------------
 void printf(const char* string, ...)
@@ -256,10 +260,10 @@ bool destroy_render_window()
 //-----------------------------------------------------------------------------
 void get_render_window_metrics(uint32_t& width, uint32_t& height)
 {
-	// TODO: must be implemented in android CrownView through JNI
-	// tmp implementation, for testing on htc desire
-	width = 480;
-	height = 800;
+	width = window_width;
+	height = window_height;
+
+	Log::I("width=%d", width);
 }
 
 //-----------------------------------------------------------------------------
@@ -274,6 +278,14 @@ JNIEXPORT void JNICALL Java_crown_android_CrownLib_initAssetManager(JNIEnv* env,
 	asset_manager = AAssetManager_fromJava(env, assetManager);
 }
 
+//-----------------------------------------------------------------------------
+JNIEXPORT void JNICALL Java_crown_android_CrownLib_setRenderWindowMetrics(JNIEnv* env, jobject obj, jint width, jint height)
+{
+	window_width = width;
+	window_height = height;
+	Log::I("setRenderWindowMetrics called. %d", (uint32_t)width);
+}
+
 } // namespace os
 } // namespace crown