project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/SDLActivity.java
author Xeli
Tue, 09 Aug 2011 20:56:34 +0200
branchhedgeroid
changeset 5514 294c92eea729
parent 5508 dcf1b3645af6
child 5538 dcfa3bf24a2a
permissions -rw-r--r--
fixed minor download bug
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5389
b3cc5b4099f1 Changed package name from org.hedgewars to org.hedgewars.mobile
Xeli
parents: 5387
diff changeset
     1
package org.hedgewars.mobile;
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
     2
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
     3
import javax.microedition.khronos.egl.EGL10;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
     4
import javax.microedition.khronos.egl.EGLConfig;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
     5
import javax.microedition.khronos.egl.EGLContext;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
     6
import javax.microedition.khronos.egl.EGLDisplay;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
     7
import javax.microedition.khronos.egl.EGLSurface;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
     8
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
     9
import org.hedgewars.mobile.EngineProtocol.EngineProtocolNetwork;
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    10
import org.hedgewars.mobile.EngineProtocol.GameConfig;
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    11
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    12
import android.app.Activity;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    13
import android.content.Context;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    14
import android.graphics.Canvas;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    15
import android.graphics.PixelFormat;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    16
import android.hardware.Sensor;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    17
import android.hardware.SensorEvent;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    18
import android.hardware.SensorEventListener;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    19
import android.hardware.SensorManager;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    20
import android.media.AudioFormat;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    21
import android.media.AudioManager;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    22
import android.media.AudioTrack;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    23
import android.os.Bundle;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    24
import android.os.Handler;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    25
import android.os.Message;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    26
import android.util.Log;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    27
import android.view.KeyEvent;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    28
import android.view.MotionEvent;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    29
import android.view.SurfaceHolder;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    30
import android.view.SurfaceView;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    31
import android.view.View;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    32
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    33
/**
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    34
 * SDL Activity
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    35
 */
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    36
public class SDLActivity extends Activity {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    37
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    38
	// Main components
5406
a3f98a8a0b80 fixed Data path
Xeli
parents: 5389
diff changeset
    39
	public static SDLActivity mSingleton;
5363
7f7800581012 Using the correct resolution rahter than a hardcoded one
Xeli
parents: 5347
diff changeset
    40
	public static SDLSurface mSurface;
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    41
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    42
	// Audio
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    43
	private static Thread mAudioThread;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    44
	private static AudioTrack mAudioTrack;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    45
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    46
	// Load the .so
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    47
	static {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    48
		System.loadLibrary("SDL");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    49
		System.loadLibrary("SDL_image");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    50
		System.loadLibrary("mikmod");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    51
		System.loadLibrary("SDL_net");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    52
		System.loadLibrary("SDL_mixer");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    53
		System.loadLibrary("SDL_ttf");
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    54
		System.loadLibrary("lua5.1");
5371
bb809cd50781 Preloaded libhwengine.so and added an argument to nativeInit()
Xeli
parents: 5363
diff changeset
    55
		System.loadLibrary("hwengine");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    56
		System.loadLibrary("main");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    57
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    58
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    59
	// Setup
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    60
	protected void onCreate(Bundle savedInstanceState) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    61
		// Log.v("SDL", "onCreate()");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    62
		super.onCreate(savedInstanceState);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    63
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    64
		// So we can call stuff from static callbacks
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    65
		mSingleton = this;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    66
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    67
		// Set up the surface
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    68
		GameConfig config = getIntent().getParcelableExtra("config");
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    69
		mSurface = new SDLSurface(getApplication(), config);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    70
		setContentView(mSurface);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    71
		SurfaceHolder holder = mSurface.getHolder();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    72
		holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    73
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    74
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    75
	// Events
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    76
	protected void onPause() {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    77
		// Log.v("SDL", "onPause()");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    78
		super.onPause();
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    79
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    80
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    81
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    82
	protected void onResume() {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    83
		// Log.v("SDL", "onResume()");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    84
		super.onResume();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    85
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    86
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    87
	// Messages from the SDLMain thread
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    88
	static int COMMAND_CHANGE_TITLE = 1;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    89
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    90
	// Handler for the messages
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    91
	Handler commandHandler = new Handler() {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    92
		public void handleMessage(Message msg) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    93
			if (msg.arg1 == COMMAND_CHANGE_TITLE) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
    94
				setTitle((String) msg.obj);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    95
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    96
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    97
	};
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    98
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
    99
	// Send a message from the SDLMain thread
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   100
	void sendCommand(int command, Object data) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   101
		Message msg = commandHandler.obtainMessage();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   102
		msg.arg1 = command;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   103
		msg.obj = data;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   104
		commandHandler.sendMessage(msg);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   105
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   106
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   107
	// C functions we call
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   108
	public static native void nativeInit(String[] argv);
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   109
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   110
	public static native void nativeQuit();
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   111
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   112
	public static native void onNativeResize(int x, int y, int format);
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   113
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   114
	public static native void onNativeKeyDown(int keycode);
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   115
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   116
	public static native void onNativeKeyUp(int keycode);
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   117
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   118
	public static native void onNativeTouch(int action, float x, float y,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   119
			float p);
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   120
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   121
	public static native void onNativeAccel(float x, float y, float z);
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   122
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   123
	public static native void nativeRunAudioThread();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   124
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   125
	// Java functions called from C
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   126
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   127
	public static boolean createGLContext(int majorVersion, int minorVersion) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   128
		return mSurface.initEGL(majorVersion, minorVersion);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   129
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   130
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   131
	public static void flipBuffers() {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   132
		mSurface.flipEGL();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   133
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   134
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   135
	public static void setActivityTitle(String title) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   136
		// Called from SDLMain() thread and can't directly affect the view
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   137
		mSingleton.sendCommand(COMMAND_CHANGE_TITLE, title);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   138
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   139
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   140
	// Audio
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   141
	private static Object buf;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   142
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   143
	public static Object audioInit(int sampleRate, boolean is16Bit,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   144
			boolean isStereo, int desiredFrames) {
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   145
		int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   146
				: AudioFormat.CHANNEL_CONFIGURATION_MONO;
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   147
		int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   148
				: AudioFormat.ENCODING_PCM_8BIT;
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   149
		int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   150
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   151
		Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono")
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   152
				+ " " + (is16Bit ? "16-bit" : "8-bit") + " "
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   153
				+ ((float) sampleRate / 1000f) + "kHz, " + desiredFrames
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   154
				+ " frames buffer");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   155
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   156
		// Let the user pick a larger buffer if they really want -- but ye
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   157
		// gods they probably shouldn't, the minimums are horrifyingly high
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   158
		// latency already
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   159
		desiredFrames = Math.max(
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   160
				desiredFrames,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   161
				(AudioTrack.getMinBufferSize(sampleRate, channelConfig,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   162
						audioFormat) + frameSize - 1)
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   163
						/ frameSize);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   164
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   165
		mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   166
				channelConfig, audioFormat, desiredFrames * frameSize,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   167
				AudioTrack.MODE_STREAM);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   168
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   169
		audioStartThread();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   170
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   171
		Log.v("SDL",
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   172
				"SDL audio: got "
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   173
						+ ((mAudioTrack.getChannelCount() >= 2) ? "stereo"
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   174
								: "mono")
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   175
						+ " "
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   176
						+ ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit"
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   177
								: "8-bit") + " "
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   178
						+ ((float) mAudioTrack.getSampleRate() / 1000f)
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   179
						+ "kHz, " + desiredFrames + " frames buffer");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   180
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   181
		if (is16Bit) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   182
			buf = new short[desiredFrames * (isStereo ? 2 : 1)];
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   183
		} else {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   184
			buf = new byte[desiredFrames * (isStereo ? 2 : 1)];
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   185
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   186
		return buf;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   187
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   188
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   189
	public static void audioStartThread() {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   190
		mAudioThread = new Thread(new Runnable() {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   191
			public void run() {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   192
				mAudioTrack.play();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   193
				nativeRunAudioThread();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   194
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   195
		});
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   196
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   197
		// I'd take REALTIME if I could get it!
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   198
		mAudioThread.setPriority(Thread.MAX_PRIORITY);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   199
		mAudioThread.start();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   200
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   201
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   202
	public static void audioWriteShortBuffer(short[] buffer) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   203
		for (int i = 0; i < buffer.length;) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   204
			int result = mAudioTrack.write(buffer, i, buffer.length - i);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   205
			if (result > 0) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   206
				i += result;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   207
			} else if (result == 0) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   208
				try {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   209
					Thread.sleep(1);
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   210
				} catch (InterruptedException e) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   211
					// Nom nom
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   212
				}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   213
			} else {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   214
				Log.w("SDL", "SDL audio: error return from write(short)");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   215
				return;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   216
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   217
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   218
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   219
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   220
	public static void audioWriteByteBuffer(byte[] buffer) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   221
		for (int i = 0; i < buffer.length;) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   222
			int result = mAudioTrack.write(buffer, i, buffer.length - i);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   223
			if (result > 0) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   224
				i += result;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   225
			} else if (result == 0) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   226
				try {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   227
					Thread.sleep(1);
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   228
				} catch (InterruptedException e) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   229
					// Nom nom
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   230
				}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   231
			} else {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   232
				Log.w("SDL", "SDL audio: error return from write(short)");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   233
				return;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   234
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   235
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   236
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   237
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   238
	public static void audioQuit() {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   239
		if (mAudioThread != null) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   240
			try {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   241
				mAudioThread.join();
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   242
			} catch (Exception e) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   243
				Log.v("SDL", "Problem stopping audio thread: " + e);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   244
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   245
			mAudioThread = null;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   246
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   247
			// Log.v("SDL", "Finished waiting for audio thread");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   248
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   249
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   250
		if (mAudioTrack != null) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   251
			mAudioTrack.stop();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   252
			mAudioTrack = null;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   253
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   254
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   255
}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   256
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   257
/**
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   258
 * Simple nativeInit() runnable
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   259
 */
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   260
class SDLMain implements Runnable {
5363
7f7800581012 Using the correct resolution rahter than a hardcoded one
Xeli
parents: 5347
diff changeset
   261
	private int surfaceWidth, surfaceHeight;
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   262
	private GameConfig config;
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   263
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   264
	public SDLMain(int width, int height, GameConfig _config) {
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   265
		config = _config;
5363
7f7800581012 Using the correct resolution rahter than a hardcoded one
Xeli
parents: 5347
diff changeset
   266
		surfaceWidth = width;
7f7800581012 Using the correct resolution rahter than a hardcoded one
Xeli
parents: 5347
diff changeset
   267
		surfaceHeight = height;
7f7800581012 Using the correct resolution rahter than a hardcoded one
Xeli
parents: 5347
diff changeset
   268
	}
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   269
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   270
	public void run() {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   271
		// Runs SDL_main()
5406
a3f98a8a0b80 fixed Data path
Xeli
parents: 5389
diff changeset
   272
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   273
		EngineProtocolNetwork ipc = new EngineProtocolNetwork(config);
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   274
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   275
		String path = Utils.getDownloadPath(SDLActivity.mSingleton);
5508
dcf1b3645af6 Fixed IPC provide gameconfig
Xeli
parents: 5469
diff changeset
   276
		//path = path.substring(0, path.length()-1);//remove the trailing '/'
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   277
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   278
		SDLActivity.nativeInit(new String[] { String.valueOf(ipc.port),
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   279
				String.valueOf(surfaceWidth), String.valueOf(surfaceHeight),
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   280
				"0", "null", "xeli", "1", "1", "1", "0", "", path });
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   281
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   282
		// Log.v("SDL", "SDL thread terminated");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   283
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   284
}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   285
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   286
/**
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   287
 * SDLSurface. This is what we draw on, so we need to know when it's created in
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   288
 * order to do anything useful.
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   289
 * 
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   290
 * Because of this, that's where we set up the SDL thread
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   291
 */
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   292
class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   293
		View.OnKeyListener, View.OnTouchListener, SensorEventListener {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   294
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   295
	// This is what SDL runs in. It invokes SDL_main(), eventually
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   296
	private Thread mSDLThread;
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   297
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   298
	// EGL private objects
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   299
	private EGLContext mEGLContext;
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   300
	private EGLSurface mEGLSurface;
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   301
	private EGLDisplay mEGLDisplay;
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   302
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   303
	// Sensors
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   304
	private static SensorManager mSensorManager;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   305
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   306
	private GameConfig config;
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   307
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   308
	// Startup
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   309
	public SDLSurface(Context context, GameConfig _config) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   310
		super(context);
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   311
		getHolder().addCallback(this);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   312
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   313
		setFocusable(true);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   314
		setFocusableInTouchMode(true);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   315
		requestFocus();
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   316
		setOnKeyListener(this);
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   317
		setOnTouchListener(this);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   318
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   319
		mSensorManager = (SensorManager) context.getSystemService("sensor");
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   320
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   321
		config = _config;
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   322
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   323
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   324
	// Called when we have a valid drawing surface
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   325
	public void surfaceCreated(SurfaceHolder holder) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   326
		// Log.v("SDL", "surfaceCreated()");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   327
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   328
		enableSensor(Sensor.TYPE_ACCELEROMETER, true);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   329
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   330
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   331
	// Called when we lose the surface
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   332
	public void surfaceDestroyed(SurfaceHolder holder) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   333
		// Log.v("SDL", "surfaceDestroyed()");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   334
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   335
		// Send a quit message to the application
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   336
		SDLActivity.nativeQuit();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   337
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   338
		// Now wait for the SDL thread to quit
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   339
		if (mSDLThread != null) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   340
			try {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   341
				mSDLThread.join();
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   342
			} catch (Exception e) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   343
				Log.v("SDL", "Problem stopping thread: " + e);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   344
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   345
			mSDLThread = null;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   346
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   347
			// Log.v("SDL", "Finished waiting for SDL thread");
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   348
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   349
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   350
		enableSensor(Sensor.TYPE_ACCELEROMETER, false);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   351
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   352
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   353
	// Called when the surface is resized
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   354
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   355
			int height) {
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   356
		Log.d("SDL", "surfaceChanged()" + width + " + " + height);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   357
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   358
		int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   359
		switch (format) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   360
		case PixelFormat.A_8:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   361
			Log.v("SDL", "pixel format A_8");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   362
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   363
		case PixelFormat.LA_88:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   364
			Log.v("SDL", "pixel format LA_88");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   365
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   366
		case PixelFormat.L_8:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   367
			Log.v("SDL", "pixel format L_8");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   368
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   369
		case PixelFormat.RGBA_4444:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   370
			Log.v("SDL", "pixel format RGBA_4444");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   371
			sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   372
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   373
		case PixelFormat.RGBA_5551:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   374
			Log.v("SDL", "pixel format RGBA_5551");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   375
			sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   376
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   377
		case PixelFormat.RGBA_8888:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   378
			Log.v("SDL", "pixel format RGBA_8888");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   379
			sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   380
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   381
		case PixelFormat.RGBX_8888:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   382
			Log.v("SDL", "pixel format RGBX_8888");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   383
			sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   384
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   385
		case PixelFormat.RGB_332:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   386
			Log.v("SDL", "pixel format RGB_332");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   387
			sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   388
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   389
		case PixelFormat.RGB_565:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   390
			Log.v("SDL", "pixel format RGB_565");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   391
			sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   392
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   393
		case PixelFormat.RGB_888:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   394
			Log.v("SDL", "pixel format RGB_888");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   395
			// Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   396
			sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   397
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   398
		default:
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   399
			Log.v("SDL", "pixel format unknown " + format);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   400
			break;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   401
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   402
		SDLActivity.onNativeResize(width, height, sdlFormat);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   403
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   404
		// Now start up the C app thread
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   405
		if (mSDLThread == null) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   406
			mSDLThread = new Thread(new SDLMain(width, height, config),
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   407
					"SDLThread");
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   408
			mSDLThread.start();
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   409
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   410
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   411
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   412
	// unused
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   413
	public void onDraw(Canvas canvas) {
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   414
	}
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   415
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   416
	// EGL functions
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   417
	public boolean initEGL(int majorVersion, int minorVersion) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   418
		Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "."
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   419
				+ minorVersion);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   420
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   421
		try {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   422
			EGL10 egl = (EGL10) EGLContext.getEGL();
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   423
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   424
			EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   425
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   426
			int[] version = new int[2];
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   427
			egl.eglInitialize(dpy, version);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   428
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   429
			int EGL_OPENGL_ES_BIT = 1;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   430
			int EGL_OPENGL_ES2_BIT = 4;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   431
			int renderableType = 0;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   432
			if (majorVersion == 2) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   433
				renderableType = EGL_OPENGL_ES2_BIT;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   434
			} else if (majorVersion == 1) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   435
				renderableType = EGL_OPENGL_ES_BIT;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   436
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   437
			int[] configSpec = {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   438
					// EGL10.EGL_DEPTH_SIZE, 16,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   439
					EGL10.EGL_RENDERABLE_TYPE, renderableType, EGL10.EGL_NONE };
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   440
			EGLConfig[] configs = new EGLConfig[1];
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   441
			int[] num_config = new int[1];
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   442
			if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config)
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   443
					|| num_config[0] == 0) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   444
				Log.e("SDL", "No EGL config available");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   445
				return false;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   446
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   447
			EGLConfig config = configs[0];
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   448
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   449
			EGLContext ctx = egl.eglCreateContext(dpy, config,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   450
					EGL10.EGL_NO_CONTEXT, null);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   451
			if (ctx == EGL10.EGL_NO_CONTEXT) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   452
				Log.e("SDL", "Couldn't create context");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   453
				return false;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   454
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   455
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   456
			EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   457
					null);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   458
			if (surface == EGL10.EGL_NO_SURFACE) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   459
				Log.e("SDL", "Couldn't create surface");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   460
				return false;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   461
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   462
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   463
			if (!egl.eglMakeCurrent(dpy, surface, surface, ctx)) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   464
				Log.e("SDL", "Couldn't make context current");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   465
				return false;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   466
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   467
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   468
			mEGLContext = ctx;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   469
			mEGLDisplay = dpy;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   470
			mEGLSurface = surface;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   471
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   472
		} catch (Exception e) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   473
			Log.v("SDL", e + "");
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   474
			for (StackTraceElement s : e.getStackTrace()) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   475
				Log.v("SDL", s.toString());
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   476
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   477
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   478
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   479
		return true;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   480
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   481
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   482
	// EGL buffer flip
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   483
	public void flipEGL() {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   484
		try {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   485
			EGL10 egl = (EGL10) EGLContext.getEGL();
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   486
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   487
			egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   488
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   489
			// drawing here
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   490
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   491
			egl.eglWaitGL();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   492
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   493
			egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   494
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   495
		} catch (Exception e) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   496
			Log.v("SDL", "flipEGL(): " + e);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   497
			for (StackTraceElement s : e.getStackTrace()) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   498
				Log.v("SDL", s.toString());
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   499
			}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   500
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   501
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   502
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   503
	// Key events
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   504
	public boolean onKey(View v, int keyCode, KeyEvent event) {
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   505
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   506
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   507
			// Log.v("SDL", "key down: " + keyCode);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   508
			SDLActivity.onNativeKeyDown(keyCode);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   509
			return true;
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   510
		} else if (event.getAction() == KeyEvent.ACTION_UP) {
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   511
			// Log.v("SDL", "key up: " + keyCode);
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   512
			SDLActivity.onNativeKeyUp(keyCode);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   513
			return true;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   514
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   515
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   516
		return false;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   517
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   518
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   519
	// Touch events
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   520
	public boolean onTouch(View v, MotionEvent event) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   521
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   522
		int action = event.getAction();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   523
		float x = event.getX();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   524
		float y = event.getY();
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   525
		float p = event.getPressure();
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   526
		// TODO: Anything else we need to pass?
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   527
		SDLActivity.onNativeTouch(action, x, y, p);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   528
		return true;
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   529
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   530
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   531
	// Sensor events
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   532
	public void enableSensor(int sensortype, boolean enabled) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   533
		// TODO: This uses getDefaultSensor - what if we have >1 accels?
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   534
		if (enabled) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   535
			mSensorManager.registerListener(this,
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   536
					mSensorManager.getDefaultSensor(sensortype),
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   537
					SensorManager.SENSOR_DELAY_GAME, null);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   538
		} else {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   539
			mSensorManager.unregisterListener(this,
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   540
					mSensorManager.getDefaultSensor(sensortype));
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   541
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   542
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   543
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   544
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   545
		// TODO
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   546
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   547
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   548
	public void onSensorChanged(SensorEvent event) {
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   549
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
5469
2ad2a4261b56 send the gameconfig
Xeli
parents: 5416
diff changeset
   550
			SDLActivity.onNativeAccel(event.values[0], event.values[1],
5331
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   551
					event.values[2]);
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   552
		}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   553
	}
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   554
3f93e036ca94 Changed the package path from org.libsdl.app to org.hedgewars
Xeli
parents:
diff changeset
   555
}