project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/SDLActivity.java
author nemo
Tue, 21 Aug 2012 19:40:02 -0400
changeset 7567 da2aa8355d91
parent 7185 92a045156255
child 7221 8d04e85ca204
permissions -rw-r--r--
allow lua to specify template by number
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     1
package org.hedgewars.hedgeroid;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     2
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     3
import javax.microedition.khronos.egl.EGL10;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     4
import javax.microedition.khronos.egl.EGLConfig;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     5
import javax.microedition.khronos.egl.EGLContext;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     6
import javax.microedition.khronos.egl.EGLDisplay;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     7
import javax.microedition.khronos.egl.EGLSurface;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     8
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     9
import org.hedgewars.hedgeroid.EngineProtocol.EngineProtocolNetwork;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    10
import org.hedgewars.hedgeroid.EngineProtocol.GameConfig;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    11
import org.hedgewars.hedgeroid.EngineProtocol.PascalExports;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    12
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    13
import android.app.Activity;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    14
import android.content.Context;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    15
import android.graphics.Canvas;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    16
import android.graphics.PixelFormat;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    17
import android.hardware.Sensor;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    18
import android.hardware.SensorEvent;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    19
import android.hardware.SensorEventListener;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    20
import android.hardware.SensorManager;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    21
import android.media.AudioFormat;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    22
import android.media.AudioManager;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    23
import android.media.AudioTrack;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    24
import android.os.Bundle;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    25
import android.os.Handler;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    26
import android.os.Message;
7167
0b3b306f129a Android: added a callback to java to determine dpi/dip how much we should scale the ui
Xeli
parents: 7083
diff changeset
    27
import android.util.DisplayMetrics;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    28
import android.util.Log;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    29
import android.view.KeyEvent;
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
    30
import android.view.MotionEvent;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    31
import android.view.SurfaceHolder;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    32
import android.view.SurfaceView;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    33
import android.view.View;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    34
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
    35
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    36
/**
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
    37
    SDL Activity
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    38
 */
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    39
public class SDLActivity extends Activity {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    40
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    41
	// Main components
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    42
	public static SDLActivity mSingleton;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    43
	private static SDLSurface mSurface;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    44
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    45
	// This is what SDL runs in. It invokes SDL_main(), eventually
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    46
	private static Thread mSDLThread;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    47
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    48
	// Audio
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    49
	private static Thread mAudioThread;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    50
	private static AudioTrack mAudioTrack;
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
    51
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    52
	// EGL private objects
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    53
	private static EGLContext  mEGLContext;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    54
	private static EGLSurface  mEGLSurface;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    55
	private static EGLDisplay  mEGLDisplay;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    56
	private static EGLConfig   mEGLConfig;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    57
	private static int mGLMajor, mGLMinor;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    58
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    59
	// Load the .so
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    60
	static {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    61
		System.loadLibrary("SDL");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    62
		//System.loadLibrary("SDL_image");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    63
		//System.loadLibrary("SDL_mixer");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    64
		//System.loadLibrary("SDL_ttf");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    65
		System.loadLibrary("main");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    66
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    67
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    68
	// Setup
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    69
	protected void onCreate(Bundle savedInstanceState) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    70
		//Log.v("SDL", "onCreate()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    71
		super.onCreate(savedInstanceState);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    72
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    73
		// So we can call stuff from static callbacks
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    74
		mSingleton = this;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    75
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    76
		// Set up the surface
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    77
		GameConfig config = getIntent().getParcelableExtra("config");
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
    78
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    79
		mSurface = new SDLSurface(getApplication(), config);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    80
		setContentView(mSurface);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    81
		SurfaceHolder holder = mSurface.getHolder();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    82
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    83
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    84
	// Events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    85
	protected void onPause() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    86
		Log.v("SDL", "onPause()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    87
		super.onPause();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    88
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    89
		if(mEGLDisplay != null && mEGLContext != null){
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    90
			EGL10 egl = (EGL10)EGLContext.getEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    91
			egl.eglDestroyContext(mEGLDisplay, mEGLContext);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    92
			mEGLDisplay = null;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    93
			mEGLContext = null;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    94
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    95
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    96
		SDLActivity.nativePause();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    97
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    98
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    99
	protected void onResume() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   100
		Log.v("SDL", "onResume()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   101
		super.onResume();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   102
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   103
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   104
	protected void onDestroy() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   105
		super.onDestroy();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   106
		Log.v("SDL", "onDestroy()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   107
		// Send a quit message to the application
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   108
		SDLActivity.nativeQuit();
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   109
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   110
		// Now wait for the SDL thread to quit
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   111
		if (mSDLThread != null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   112
			try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   113
				mSDLThread.join();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   114
			} catch(Exception e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   115
				Log.v("SDL", "Problem stopping thread: " + e);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   116
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   117
			mSDLThread = null;
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   118
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   119
			//Log.v("SDL", "Finished waiting for SDL thread");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   120
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   121
	}
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   122
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   123
	// Messages from the SDLMain thread
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   124
	static int COMMAND_CHANGE_TITLE = 1;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   125
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   126
	// Handler for the messages
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   127
	Handler commandHandler = new Handler() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   128
		public void handleMessage(Message msg) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   129
			if (msg.arg1 == COMMAND_CHANGE_TITLE) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   130
				setTitle((String)msg.obj);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   131
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   132
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   133
	};
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   134
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   135
	// Send a message from the SDLMain thread
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   136
	void sendCommand(int command, Object data) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   137
		Message msg = commandHandler.obtainMessage();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   138
		msg.arg1 = command;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   139
		msg.obj = data;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   140
		commandHandler.sendMessage(msg);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   141
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   142
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   143
	// C functions we call
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   144
	public static native void nativeInit(String...args);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   145
	public static native void nativeQuit();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   146
	public static native void nativePause();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   147
	public static native void nativeResume();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   148
	public static native void onNativeResize(int x, int y, int format);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   149
	public static native void onNativeKeyDown(int keycode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   150
	public static native void onNativeKeyUp(int keycode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   151
	public static native void onNativeTouch(int touchDevId, int pointerFingerId,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   152
			int action, float x, 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   153
			float y, float p);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   154
	public static native void onNativeAccel(float x, float y, float z);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   155
	public static native void nativeRunAudioThread();
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   156
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   157
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   158
	// Java functions called from C
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   159
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   160
	public static boolean createGLContext(int majorVersion, int minorVersion) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   161
		return initEGL(majorVersion, minorVersion);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   162
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   163
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   164
	public static void flipBuffers() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   165
		flipEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   166
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   167
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   168
	public static void setActivityTitle(String title) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   169
		// Called from SDLMain() thread and can't directly affect the view
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   170
		mSingleton.sendCommand(COMMAND_CHANGE_TITLE, title);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   171
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   172
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   173
	public static Context getContext() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   174
		return mSingleton;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   175
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   176
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   177
	public static void startApp(int width, int height, GameConfig config) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   178
		// Start up the C app thread
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   179
		if (mSDLThread == null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   180
			mSDLThread = new Thread(new SDLMain(width, height, config), "SDLThread");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   181
			mSDLThread.start();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   182
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   183
		else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   184
			SDLActivity.nativeResume();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   185
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   186
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   187
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   188
	// EGL functions
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   189
	public static boolean initEGL(int majorVersion, int minorVersion) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   190
		if (SDLActivity.mEGLDisplay == null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   191
			//Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   192
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   193
			try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   194
				EGL10 egl = (EGL10)EGLContext.getEGL();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   195
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   196
				EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   197
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   198
				int[] version = new int[2];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   199
				egl.eglInitialize(dpy, version);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   200
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   201
				int EGL_OPENGL_ES_BIT = 1;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   202
				int EGL_OPENGL_ES2_BIT = 4;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   203
				int renderableType = 0;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   204
				if (majorVersion == 2) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   205
					renderableType = EGL_OPENGL_ES2_BIT;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   206
				} else if (majorVersion == 1) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   207
					renderableType = EGL_OPENGL_ES_BIT;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   208
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   209
				int[] configSpec = {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   210
						//EGL10.EGL_DEPTH_SIZE,   16,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   211
						EGL10.EGL_RENDERABLE_TYPE, renderableType,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   212
						EGL10.EGL_NONE
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   213
				};
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   214
				EGLConfig[] configs = new EGLConfig[1];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   215
				int[] num_config = new int[1];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   216
				if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   217
					Log.e("SDL", "No EGL config available");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   218
					return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   219
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   220
				EGLConfig config = configs[0];
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   221
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   222
				/*int EGL_CONTEXT_CLIENT_VERSION=0x3098;
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   223
                int contextAttrs[] = new int[] { EGL_CONTEXT_CLIENT_VERSION, majorVersion, EGL10.EGL_NONE };
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   224
                EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, contextAttrs);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   225
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   226
                if (ctx == EGL10.EGL_NO_CONTEXT) {
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   227
                    Log.e("SDL", "Couldn't create context");
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   228
                    return false;
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   229
                }
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   230
                SDLActivity.mEGLContext = ctx;*/
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   231
				SDLActivity.mEGLDisplay = dpy;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   232
				SDLActivity.mEGLConfig = config;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   233
				SDLActivity.mGLMajor = majorVersion;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   234
				SDLActivity.mGLMinor = minorVersion;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   235
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   236
				SDLActivity.createEGLSurface();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   237
			} catch(Exception e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   238
				Log.v("SDL", e + "");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   239
				for (StackTraceElement s : e.getStackTrace()) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   240
					Log.v("SDL", s.toString());
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   241
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   242
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   243
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   244
		else SDLActivity.createEGLSurface();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   245
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   246
		return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   247
	}
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   248
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   249
	public static boolean createEGLContext() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   250
		EGL10 egl = (EGL10)EGLContext.getEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   251
		int EGL_CONTEXT_CLIENT_VERSION=0x3098;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   252
		int contextAttrs[] = new int[] { EGL_CONTEXT_CLIENT_VERSION, SDLActivity.mGLMajor, EGL10.EGL_NONE };
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   253
		SDLActivity.mEGLContext = egl.eglCreateContext(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, EGL10.EGL_NO_CONTEXT, contextAttrs);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   254
		if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   255
			Log.e("SDL", "Couldn't create context");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   256
			return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   257
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   258
		return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   259
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   260
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   261
	public static boolean createEGLSurface() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   262
		if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLConfig != null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   263
			EGL10 egl = (EGL10)EGLContext.getEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   264
			if (SDLActivity.mEGLContext == null) createEGLContext();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   265
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   266
			Log.v("SDL", "Creating new EGL Surface");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   267
			EGLSurface surface = egl.eglCreateWindowSurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, SDLActivity.mSurface, null);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   268
			if (surface == EGL10.EGL_NO_SURFACE) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   269
				Log.e("SDL", "Couldn't create surface");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   270
				return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   271
			}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   272
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   273
			if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, surface, surface, SDLActivity.mEGLContext)) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   274
				Log.e("SDL", "Old EGL Context doesnt work, trying with a new one");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   275
				createEGLContext();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   276
				if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, surface, surface, SDLActivity.mEGLContext)) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   277
					Log.e("SDL", "Failed making EGL Context current");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   278
					return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   279
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   280
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   281
			SDLActivity.mEGLSurface = surface;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   282
			return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   283
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   284
		return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   285
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   286
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   287
	// EGL buffer flip
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   288
	public static void flipEGL() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   289
		try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   290
			EGL10 egl = (EGL10)EGLContext.getEGL();
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   291
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   292
			egl.eglWaitNative(EGL10.EGL_CORE_NATIVE_ENGINE, null);
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   293
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   294
			// drawing here
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   295
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   296
			egl.eglWaitGL();
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   297
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   298
			egl.eglSwapBuffers(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface);
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   299
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   300
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   301
		} catch(Exception e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   302
			Log.v("SDL", "flipEGL(): " + e);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   303
			for (StackTraceElement s : e.getStackTrace()) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   304
				Log.v("SDL", s.toString());
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   305
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   306
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   307
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   308
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   309
	// Audio
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   310
	private static Object buf;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   311
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   312
	public static Object audioInit(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   313
		int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   314
		int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   315
		int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   316
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   317
		Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + ((float)sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   318
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   319
		// Let the user pick a larger buffer if they really want -- but ye
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   320
		// gods they probably shouldn't, the minimums are horrifyingly high
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   321
		// latency already
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   322
		desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   323
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   324
		mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   325
				channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   326
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   327
		audioStartThread();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   328
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   329
		Log.v("SDL", "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + ((float)mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   330
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   331
		if (is16Bit) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   332
			buf = new short[desiredFrames * (isStereo ? 2 : 1)];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   333
		} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   334
			buf = new byte[desiredFrames * (isStereo ? 2 : 1)]; 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   335
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   336
		return buf;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   337
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   338
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   339
	public static void audioStartThread() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   340
		mAudioThread = new Thread(new Runnable() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   341
			public void run() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   342
				mAudioTrack.play();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   343
				nativeRunAudioThread();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   344
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   345
		});
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   346
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   347
		// I'd take REALTIME if I could get it!
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   348
		mAudioThread.setPriority(Thread.MAX_PRIORITY);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   349
		mAudioThread.start();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   350
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   351
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   352
	public static void audioWriteShortBuffer(short[] buffer) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   353
		for (int i = 0; i < buffer.length; ) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   354
			int result = mAudioTrack.write(buffer, i, buffer.length - i);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   355
			if (result > 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   356
				i += result;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   357
			} else if (result == 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   358
				try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   359
					Thread.sleep(1);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   360
				} catch(InterruptedException e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   361
					// Nom nom
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   362
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   363
			} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   364
				Log.w("SDL", "SDL audio: error return from write(short)");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   365
				return;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   366
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   367
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   368
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   369
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   370
	public static void audioWriteByteBuffer(byte[] buffer) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   371
		for (int i = 0; i < buffer.length; ) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   372
			int result = mAudioTrack.write(buffer, i, buffer.length - i);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   373
			if (result > 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   374
				i += result;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   375
			} else if (result == 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   376
				try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   377
					Thread.sleep(1);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   378
				} catch(InterruptedException e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   379
					// Nom nom
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   380
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   381
			} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   382
				Log.w("SDL", "SDL audio: error return from write(short)");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   383
				return;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   384
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   385
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   386
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   387
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   388
	public static void audioQuit() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   389
		if (mAudioThread != null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   390
			try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   391
				mAudioThread.join();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   392
			} catch(Exception e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   393
				Log.v("SDL", "Problem stopping audio thread: " + e);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   394
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   395
			mAudioThread = null;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   396
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   397
			//Log.v("SDL", "Finished waiting for audio thread");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   398
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   399
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   400
		if (mAudioTrack != null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   401
			mAudioTrack.stop();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   402
			mAudioTrack = null;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   403
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   404
	}
7167
0b3b306f129a Android: added a callback to java to determine dpi/dip how much we should scale the ui
Xeli
parents: 7083
diff changeset
   405
	
7185
92a045156255 Android: first attempt at using the density call from java
Xeli
parents: 7167
diff changeset
   406
	public static int getDensity(){
7167
0b3b306f129a Android: added a callback to java to determine dpi/dip how much we should scale the ui
Xeli
parents: 7083
diff changeset
   407
		DisplayMetrics dm = SDLActivity.getContext().getResources().getDisplayMetrics();
7185
92a045156255 Android: first attempt at using the density call from java
Xeli
parents: 7167
diff changeset
   408
		return dm.densityDpi;
7167
0b3b306f129a Android: added a callback to java to determine dpi/dip how much we should scale the ui
Xeli
parents: 7083
diff changeset
   409
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   410
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   411
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   412
/**
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   413
    Simple nativeInit() runnable
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   414
 */
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   415
class SDLMain implements Runnable {
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   416
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   417
	private int surfaceWidth, surfaceHeight;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   418
	private GameConfig config;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   419
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   420
	public SDLMain(int width, int height, GameConfig _config) {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   421
		config = _config;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   422
		surfaceWidth = width;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   423
		surfaceHeight = height;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   424
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   425
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   426
	public void run() {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   427
		//Set up the IPC socket server to communicate with the engine
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   428
		EngineProtocolNetwork ipc = new EngineProtocolNetwork(config);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   429
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6332
diff changeset
   430
		String path = Utils.getDataPath(SDLActivity.mSingleton);//This represents the data directory
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   431
		path = path.substring(0, path.length()-1);//remove the trailing '/'
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   432
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   433
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   434
		// Runs SDL_main() with added parameters
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   435
		SDLActivity.nativeInit(new String[] { String.valueOf(ipc.port),
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   436
				String.valueOf(surfaceWidth), String.valueOf(surfaceHeight),
7083
5339aba29571 unbreak android build
Xeli
parents: 6901
diff changeset
   437
				"0", "en.txt", "xeli", "1", "1", "1", path, ""  });
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   438
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   439
		try {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   440
			ipc.quitIPC();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   441
			ipc.join();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   442
		} catch (InterruptedException e) {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   443
			e.printStackTrace();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   444
		}
6892
c02710a8bac4 android: prevent the app from a semi-crash when exiting a game
Xeli
parents: 6661
diff changeset
   445
		Log.v("SDL", "SDL thread terminated");
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   446
		//Log.v("SDL", "SDL thread terminated");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   447
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   448
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   449
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   450
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   451
/**
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   452
    SDLSurface. This is what we draw on, so we need to know when it's created
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   453
    in order to do anything useful. 
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   454
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   455
    Because of this, that's where we set up the SDL thread
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   456
 */
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   457
class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, 
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   458
View.OnKeyListener, View.OnTouchListener, SensorEventListener  {
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   459
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   460
	private GameConfig config;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   461
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   462
	// Sensors
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   463
	private static SensorManager mSensorManager;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   464
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   465
	// Startup    
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   466
	public SDLSurface(Context context, GameConfig _config) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   467
		super(context);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   468
		getHolder().addCallback(this); 
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   469
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   470
		setFocusable(true);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   471
		setFocusableInTouchMode(true);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   472
		requestFocus();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   473
		setOnKeyListener(this); 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   474
		setOnTouchListener(this);   
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   475
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   476
		mSensorManager = (SensorManager)context.getSystemService("sensor");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   477
		config = _config;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   478
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   479
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   480
	// Called when we have a valid drawing surface
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   481
	public void surfaceCreated(SurfaceHolder holder) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   482
		Log.v("SDL", "surfaceCreated()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   483
		holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   484
		SDLActivity.createEGLSurface();
6901
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   485
		//		enableSensor(Sensor.TYPE_ACCELEROMETER, true);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   486
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   487
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   488
	// Called when we lose the surface
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   489
	public void surfaceDestroyed(SurfaceHolder holder) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   490
		Log.v("SDL", "surfaceDestroyed()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   491
		SDLActivity.nativePause();
6901
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   492
		//		enableSensor(Sensor.TYPE_ACCELEROMETER, false);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   493
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   494
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   495
	// Called when the surface is resized
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   496
	public void surfaceChanged(SurfaceHolder holder,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   497
			int format, int width, int height) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   498
		Log.v("SDL", "surfaceChanged()");
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   499
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   500
		int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   501
		switch (format) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   502
		case PixelFormat.A_8:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   503
			Log.v("SDL", "pixel format A_8");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   504
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   505
		case PixelFormat.LA_88:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   506
			Log.v("SDL", "pixel format LA_88");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   507
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   508
		case PixelFormat.L_8:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   509
			Log.v("SDL", "pixel format L_8");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   510
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   511
		case PixelFormat.RGBA_4444:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   512
			Log.v("SDL", "pixel format RGBA_4444");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   513
			sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   514
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   515
		case PixelFormat.RGBA_5551:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   516
			Log.v("SDL", "pixel format RGBA_5551");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   517
			sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   518
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   519
		case PixelFormat.RGBA_8888:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   520
			Log.v("SDL", "pixel format RGBA_8888");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   521
			sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   522
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   523
		case PixelFormat.RGBX_8888:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   524
			Log.v("SDL", "pixel format RGBX_8888");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   525
			sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   526
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   527
		case PixelFormat.RGB_332:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   528
			Log.v("SDL", "pixel format RGB_332");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   529
			sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   530
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   531
		case PixelFormat.RGB_565:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   532
			Log.v("SDL", "pixel format RGB_565");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   533
			sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   534
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   535
		case PixelFormat.RGB_888:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   536
			Log.v("SDL", "pixel format RGB_888");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   537
			// Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   538
			sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   539
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   540
		default:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   541
			Log.v("SDL", "pixel format unknown " + format);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   542
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   543
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   544
		SDLActivity.onNativeResize(width, height, sdlFormat);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   545
		Log.v("SDL", "Window size:" + width + "x"+height);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   546
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   547
		SDLActivity.startApp(width, height, config);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   548
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   549
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   550
	// unused
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   551
	public void onDraw(Canvas canvas) {}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   552
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   553
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   554
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   555
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   556
	// Key events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   557
	public boolean onKey(View  v, int keyCode, KeyEvent event) {
6901
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   558
		switch(keyCode){
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   559
		case KeyEvent.KEYCODE_BACK:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   560
		        PascalExports.HWterminate(true);
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   561
                        return true;
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   562
		case KeyEvent.KEYCODE_VOLUME_DOWN:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   563
		case KeyEvent.KEYCODE_VOLUME_UP:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   564
		case KeyEvent.KEYCODE_VOLUME_MUTE:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   565
			return false;
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   566
		}
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   567
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   568
			//Log.v("SDL", "key down: " + keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   569
			SDLActivity.onNativeKeyDown(keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   570
			return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   571
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   572
		else if (event.getAction() == KeyEvent.ACTION_UP) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   573
			//Log.v("SDL", "key up: " + keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   574
			SDLActivity.onNativeKeyUp(keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   575
			return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   576
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   577
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   578
		return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   579
	}
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   580
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   581
	// Touch events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   582
	public boolean onTouch(View v, MotionEvent event) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   583
		{
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   584
			final int touchDevId = event.getDeviceId();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   585
			final int pointerCount = event.getPointerCount();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   586
			// touchId, pointerId, action, x, y, pressure
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   587
			int actionPointerIndex = event.getActionIndex();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   588
			int pointerFingerId = event.getPointerId(actionPointerIndex);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   589
			int action = event.getActionMasked();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   590
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   591
			float x = event.getX(actionPointerIndex);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   592
			float y = event.getY(actionPointerIndex);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   593
			float p = event.getPressure(actionPointerIndex);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   594
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   595
			if (action == MotionEvent.ACTION_MOVE && pointerCount > 1) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   596
				// TODO send motion to every pointer if its position has
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   597
				// changed since prev event.
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   598
				for (int i = 0; i < pointerCount; i++) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   599
					pointerFingerId = event.getPointerId(i);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   600
					x = event.getX(i);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   601
					y = event.getY(i);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   602
					p = event.getPressure(i);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   603
					SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   604
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   605
			} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   606
				SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   607
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   608
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   609
		return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   610
	} 
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   611
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   612
	// Sensor events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   613
	public void enableSensor(int sensortype, boolean enabled) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   614
		// TODO: This uses getDefaultSensor - what if we have >1 accels?
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   615
		if (enabled) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   616
			mSensorManager.registerListener(this, 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   617
					mSensorManager.getDefaultSensor(sensortype), 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   618
					SensorManager.SENSOR_DELAY_GAME, null);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   619
		} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   620
			mSensorManager.unregisterListener(this, 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   621
					mSensorManager.getDefaultSensor(sensortype));
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   622
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   623
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   624
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   625
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   626
		// TODO
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   627
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   628
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   629
	public void onSensorChanged(SensorEvent event) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   630
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   631
			SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   632
					event.values[1] / SensorManager.GRAVITY_EARTH,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   633
					event.values[2] / SensorManager.GRAVITY_EARTH);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   634
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   635
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   636
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   637
}
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   638