1
0
mirror of https://github.com/XProger/OpenLara.git synced 2025-08-06 05:07:48 +02:00

#15 android gamepad support

This commit is contained in:
XProger
2017-03-17 03:20:40 +03:00
parent 4493c0ed24
commit 6a25f64a61
3 changed files with 76 additions and 48 deletions

View File

@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.xproger.openlara"
android:versionCode="6"
android:versionName="1.4" >
android:versionCode="1"
android:versionName="0.1" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" />
<supports-screens android:smallScreens="true" android:largeScreens="true" android:normalScreens="true" android:xlargeScreens="true" />
@@ -11,10 +11,8 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<!--
android:hasCode="true"
android:debuggable="true"
-->
android:debuggable="true"
-->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"

View File

@@ -26,14 +26,14 @@ char Stream::cacheDir[255];
char Stream::contentDir[255];
JNI_METHOD(void, nativeInit)(JNIEnv* env, jobject obj, jstring packName, jstring cacheDir, jint levelOffset, jint musicOffset) {
const char* str;
const char* str;
Stream::contentDir[0] = Stream::cacheDir[0] = 0;
Stream::contentDir[0] = Stream::cacheDir[0] = 0;
str = env->GetStringUTFChars(cacheDir, NULL);
strcat(Stream::cacheDir, str);
env->ReleaseStringUTFChars(cacheDir, str);
str = env->GetStringUTFChars(packName, NULL);
str = env->GetStringUTFChars(packName, NULL);
Stream *level = new Stream(str);
Stream *music = new Stream(str);
env->ReleaseStringUTFChars(packName, str);
@@ -85,38 +85,45 @@ float DeadZone(float x) {
return x = fabsf(x) < 0.2f ? 0.0f : x;
}
int getPOV(int x, int y) {
switch (x) {
case -1 : {
if (y == -1) return 8;
if (y == 0) return 7;
if (y == +1) return 6;
}
case 0 : {
if (y == -1) return 1;
if (y == 0) return 0;
if (y == +1) return 5;
}
case +1 : {
if (y == -1) return 2;
if (y == 0) return 3;
if (y == +1) return 4;
}
}
return 0;
}
JNI_METHOD(void, nativeTouch)(JNIEnv* env, jobject obj, jint id, jint state, jfloat x, jfloat y) {
if (id > 1) return;
/* gamepad
// gamepad
if (state < 0) {
state = -state;
Input::Joystick &joy = Input::joy;
switch (state) {
case 3 :
joy.L.x = DeadZone(x);
joy.L.y = DeadZone(y);
break;
case 4 :
joy.R.x = DeadZone(x);
joy.R.y = DeadZone(y);
break;
default:
joy.down[(int)x] = state != 1;
case -3 : Input::setPos(ikJoyL, vec2(DeadZone(x), DeadZone(y))); break;
case -4 : Input::setPos(ikJoyR, vec2(DeadZone(x), DeadZone(y))); break;
case -5 : Input::setPos(ikJoyPOV, vec2(float(getPOV(sign(x), sign(y))), 0.0f)); break;
default : Input::setDown(InputKey(ikJoyA + (int)x), state != -1);
}
return;
}
*/
if (state == 3 && x < Core::width / 2) {
vec2 center(Core::width * 0.25f, Core::height * 0.6f);
vec2 pos(x, y);
vec2 d = pos - center;
float angle = atan2(d.x, -d.y) + PI * 0.125f;
if (angle < 0.0f) angle += PI2;
int pov = int(angle / (PI * 0.25f));
Input::setPos(ikJoyPOV, vec2(float(1 + pov), 0.0f));
Input::setPos(ikJoyL, d.normal());
};
if (state == 2 && x > Core::width / 2) {
@@ -132,7 +139,7 @@ JNI_METHOD(void, nativeTouch)(JNIEnv* env, jobject obj, jint id, jint state, jfl
}
if (state == 1) {
Input::setPos(ikJoyPOV, vec2(0.0f));
Input::setPos(ikJoyL, vec2(0.0f));
Input::setDown(ikJoyA, false);
Input::setDown(ikJoyX, false);
Input::setDown(ikJoyY, false);

View File

@@ -10,6 +10,8 @@ import android.media.AudioManager;
import android.media.AudioTrack;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
//import com.google.vr.ndk.base.AndroidCompat;
//import com.google.vr.ndk.base.GvrLayout;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
@@ -27,6 +29,7 @@ import android.view.WindowManager;
public class MainActivity extends Activity implements OnTouchListener, OnGenericMotionListener, OnKeyListener {
private GLSurfaceView view;
//private GvrLayout gvrLayout;
private Wrapper wrapper;
private SparseIntArray joys = new SparseIntArray();
@@ -34,18 +37,19 @@ public class MainActivity extends Activity implements OnTouchListener, OnGeneric
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
super.onCreate(savedInstanceState);
view = new GLSurfaceView(this);
view.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
view.setEGLContextClientVersion(2);
view.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
view.setPreserveEGLContextOnPause(true);
view.setRenderer(wrapper = new Wrapper());
view.setFocusable(true);
@@ -54,8 +58,15 @@ public class MainActivity extends Activity implements OnTouchListener, OnGeneric
view.setOnTouchListener(this);
view.setOnGenericMotionListener(this);
view.setOnKeyListener(this);
/*
gvrLayout = new GvrLayout(this);
gvrLayout.setPresentationView(view);
if (gvrLayout.setAsyncReprojectionEnabled(true))
AndroidCompat.setSustainedPerformanceMode(this, true);
setContentView(gvrLayout);
*/
setContentView(view);
try {
String packName = getPackageManager().getPackageInfo(getPackageName(), 1).applicationInfo.sourceDir;
AssetFileDescriptor fLevel = this.getResources().openRawResourceFd(R.raw.level2);
@@ -113,7 +124,7 @@ public class MainActivity extends Activity implements OnTouchListener, OnGeneric
private int getJoyIndex(InputDevice dev) {
int src = dev.getSources();
if ((src & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD ||
(src & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) {
(src & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) {
int id = dev.getId();
int index = joys.get(id, -1);
@@ -133,27 +144,39 @@ public class MainActivity extends Activity implements OnTouchListener, OnGeneric
if (index == -1) return false;
wrapper.onTouch(index, -3, event.getAxisValue(MotionEvent.AXIS_X),
event.getAxisValue(MotionEvent.AXIS_Y));
event.getAxisValue(MotionEvent.AXIS_Y));
wrapper.onTouch(index, -4, event.getAxisValue(MotionEvent.AXIS_Z),
event.getAxisValue(MotionEvent.AXIS_RZ));
event.getAxisValue(MotionEvent.AXIS_RZ));
wrapper.onTouch(index, -5, event.getAxisValue(MotionEvent.AXIS_HAT_X),
event.getAxisValue(MotionEvent.AXIS_HAT_Y));
return true;
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
int index = getJoyIndex(event.getDevice());
if (index == -1) return false;
if (index == -1)
return false;
int btn;
switch (keyCode) {
case KeyEvent.KEYCODE_BUTTON_A : btn = 0; break;
case KeyEvent.KEYCODE_BUTTON_B : btn = 1; break;
case KeyEvent.KEYCODE_BUTTON_X : btn = 2; break;
case KeyEvent.KEYCODE_BUTTON_Y : btn = 3; break;
default : btn = -1;
case KeyEvent.KEYCODE_BUTTON_A : btn = 0; break;
case KeyEvent.KEYCODE_BUTTON_B : btn = 1; break;
case KeyEvent.KEYCODE_BUTTON_X : btn = 2; break;
case KeyEvent.KEYCODE_BUTTON_Y : btn = 3; break;
case KeyEvent.KEYCODE_BUTTON_L1 : btn = 4; break;
case KeyEvent.KEYCODE_BUTTON_R1 : btn = 5; break;
case KeyEvent.KEYCODE_BUTTON_SELECT : btn = 6; break;
case KeyEvent.KEYCODE_BUTTON_START : btn = 7; break;
case KeyEvent.KEYCODE_BUTTON_THUMBL : btn = 8; break;
case KeyEvent.KEYCODE_BUTTON_THUMBR : btn = 9; break;
case KeyEvent.KEYCODE_BUTTON_L2 : btn = 10; break;
case KeyEvent.KEYCODE_BUTTON_R2 : btn = 11; break;
default : btn = -1;
}
if (btn != -1) {
@@ -165,6 +188,7 @@ public class MainActivity extends Activity implements OnTouchListener, OnGeneric
static {
System.loadLibrary("game");
// System.loadLibrary("gvr");
// System.load("/storage/emulated/0/libMGD.so");
}
}
@@ -175,7 +199,6 @@ class Sound {
public void start(final Wrapper wrapper) {
int bufferSize = AudioTrack.getMinBufferSize(22050, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT);
System.out.println(String.format("sound buffer size: %d", bufferSize));
buffer = new short [bufferSize / 2];
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,