project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/SDLActivity.java
author Medo <smaxein@googlemail.com>
Sat, 18 Aug 2012 18:11:04 +0200
changeset 7568 75ba91f14ed5
parent 7508 763d3961400b
child 7582 714310efad8f
permissions -rw-r--r--
Hedgeroid: - Updated assets - reworked team management in a room - lower engine quality (for now) - added focus/disable states for spinner and button graphics
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
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
     3
import java.io.UnsupportedEncodingException;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
     4
import java.util.concurrent.atomic.AtomicBoolean;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
     5
6047
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.EGL10;
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.EGLConfig;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     8
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
     9
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
    10
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
    11
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    12
import org.hedgewars.hedgeroid.Datastructures.GameConfig;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    13
import org.hedgewars.hedgeroid.EngineProtocol.PascalExports;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    14
import org.hedgewars.hedgeroid.frontlib.Flib;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    15
import org.hedgewars.hedgeroid.frontlib.Frontlib.GameSetupPtr;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    16
import org.hedgewars.hedgeroid.frontlib.Frontlib.GameconnPtr;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    17
import org.hedgewars.hedgeroid.frontlib.Frontlib.IntCallback;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    18
import org.hedgewars.hedgeroid.netplay.Netplay;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    19
import org.hedgewars.hedgeroid.util.FileUtils;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    20
import org.hedgewars.hedgeroid.util.TickHandler;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    21
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    22
import com.sun.jna.Pointer;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    23
6047
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.app.Activity;
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.content.Context;
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.graphics.Canvas;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    27
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
    28
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
    29
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
    30
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
    31
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
    32
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
    33
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
    34
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
    35
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
    36
import android.os.Handler;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    37
import android.os.HandlerThread;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    38
import android.os.Message;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    39
import android.util.Base64;
7167
0b3b306f129a Android: added a callback to java to determine dpi/dip how much we should scale the ui
Xeli
parents: 7083
diff changeset
    40
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
    41
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
    42
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
    43
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
    44
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
    45
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
    46
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
    47
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
    48
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    49
/**
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
    50
    SDL Activity
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    51
 */
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    52
public class SDLActivity extends Activity {
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    53
	/**
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    54
	 * Set startConfig to the desired config when starting this activity. This avoids having to parcel all
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    55
	 * the config objects into the Intent. Not particularly elegant, but it's actually a recommended
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    56
	 * way to do this (http://developer.android.com/guide/faq/framework.html#3)
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    57
	 */
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    58
	public static volatile GameConfig startConfig;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    59
	public static volatile boolean startNetgame;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    60
	
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    61
	// Main components
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    62
	public static SDLActivity mSingleton;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    63
	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
    64
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    65
	// This is what SDL runs in. It invokes SDL_main(), eventually
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    66
	private static Thread mSDLThread; // Guarded by SDLActivity.class
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    67
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    68
	// Audio
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    69
	private static Thread mAudioThread;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    70
	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
    71
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    72
	// EGL private objects
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    73
	private static EGLContext  mEGLContext;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    74
	private static EGLSurface  mEGLSurface;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    75
	private static EGLDisplay  mEGLDisplay;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    76
	private static EGLConfig   mEGLConfig;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    77
	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
    78
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    79
	// Load the .so
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    80
	static {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    81
		System.loadLibrary("SDL");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    82
		//System.loadLibrary("SDL_image");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    83
		//System.loadLibrary("SDL_mixer");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    84
		//System.loadLibrary("SDL_ttf");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    85
		System.loadLibrary("main");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    86
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    87
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    88
	// Setup
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    89
	protected void onCreate(Bundle savedInstanceState) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    90
		//Log.v("SDL", "onCreate()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    91
		super.onCreate(savedInstanceState);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    92
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    93
		// So we can call stuff from static callbacks
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    94
		mSingleton = this;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    95
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    96
		// Set up the surface
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    97
		mSurface = new SDLSurface(getApplication(), startConfig, startNetgame);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    98
		setContentView(mSurface);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    99
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   100
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   101
	// Events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   102
	protected void onPause() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   103
		Log.v("SDL", "onPause()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   104
		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
   105
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   106
		if(mEGLDisplay != null && mEGLContext != null){
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   107
			EGL10 egl = (EGL10)EGLContext.getEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   108
			egl.eglDestroyContext(mEGLDisplay, mEGLContext);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   109
			mEGLDisplay = null;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   110
			mEGLContext = null;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   111
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   112
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   113
		SDLActivity.nativePause();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   114
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   115
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   116
	protected void onResume() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   117
		Log.v("SDL", "onResume()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   118
		super.onResume();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   119
	}
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
	protected void onDestroy() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   122
		super.onDestroy();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   123
		Log.v("SDL", "onDestroy()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   124
		// Send a quit message to the application
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   125
		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
   126
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   127
		// Now wait for the SDL thread to quit
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   128
		synchronized(SDLActivity.class) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   129
			if (mSDLThread != null) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   130
				try {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   131
					mSDLThread.join();
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   132
				} catch(Exception e) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   133
					Log.w("SDL", "Problem stopping thread: " + e);
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   134
				}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   135
				mSDLThread = null;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   136
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   137
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   138
	}
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
   139
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   140
	// Messages from the SDLMain thread
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   141
	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
   142
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   143
	// Handler for the messages
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   144
	Handler commandHandler = new Handler() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   145
		public void handleMessage(Message msg) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   146
			if (msg.arg1 == COMMAND_CHANGE_TITLE) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   147
				setTitle((String)msg.obj);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   148
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   149
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   150
	};
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   151
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   152
	// Send a message from the SDLMain thread
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   153
	void sendCommand(int command, Object data) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   154
		Message msg = commandHandler.obtainMessage();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   155
		msg.arg1 = command;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   156
		msg.obj = data;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   157
		commandHandler.sendMessage(msg);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   158
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   159
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   160
	public static void synchronizedNativeInit(String...args) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   161
		synchronized(PascalExports.engineMutex) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   162
			nativeInit(args);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   163
		}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   164
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   165
	
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   166
	// C functions we call
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   167
	private static native void nativeInit(String...args);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   168
	public static native void nativeQuit();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   169
	public static native void nativePause();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   170
	public static native void nativeResume();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   171
	public static native void onNativeResize(int x, int y, int format);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   172
	public static native void onNativeKeyDown(int keycode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   173
	public static native void onNativeKeyUp(int keycode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   174
	public static native void onNativeTouch(int touchDevId, int pointerFingerId,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   175
			int action, float x, 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   176
			float y, float p);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   177
	public static native void onNativeAccel(float x, float y, float z);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   178
	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
   179
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   180
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   181
	// 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
   182
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   183
	public static boolean createGLContext(int majorVersion, int minorVersion) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   184
		return initEGL(majorVersion, minorVersion);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   185
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   186
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   187
	public static void flipBuffers() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   188
		flipEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   189
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   190
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   191
	public static void setActivityTitle(String title) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   192
		// Called from SDLMain() thread and can't directly affect the view
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   193
		mSingleton.sendCommand(COMMAND_CHANGE_TITLE, title);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   194
	}
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
	public static Context getContext() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   197
		return mSingleton;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   198
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   199
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   200
	public static void startApp(final int width, final int height, GameConfig config, boolean netgame) {
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   201
		synchronized(SDLActivity.class) {
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   202
			// Start up the C app thread TODO this is silly code
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   203
			if (mSDLThread == null) {
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   204
				final AtomicBoolean gameconnStartDone = new AtomicBoolean(false);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   205
				GameConnection.Listener listener = new GameConnection.Listener() {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   206
					public void gameConnectionReady(int port) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   207
						mSDLThread = new Thread(new SDLMain(width, height, port, "Medo"));
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   208
						mSDLThread.start();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   209
						gameconnStartDone.set(true);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   210
					}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   211
					
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   212
					public void gameConnectionDisconnected(int reason) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   213
						Log.e("startApp", "disconnected: "+reason);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   214
						gameconnStartDone.set(true);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   215
					}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   216
				};
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   217
				if(netgame) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   218
					Netplay netplay = Netplay.getAppInstance(mSingleton.getApplicationContext());
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   219
					GameConnection.forNetgame(config, netplay, listener);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   220
				} else {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   221
					GameConnection.forLocalGame(config, listener);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   222
				}
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   223
			} else {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   224
				SDLActivity.nativeResume();
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
   225
			}
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   226
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   227
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   228
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   229
	// EGL functions
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   230
	public static boolean initEGL(int majorVersion, int minorVersion) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   231
		if (SDLActivity.mEGLDisplay == null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   232
			//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
   233
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   234
			try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   235
				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
   236
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   237
				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
   238
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   239
				int[] version = new int[2];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   240
				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
   241
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   242
				int EGL_OPENGL_ES_BIT = 1;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   243
				int EGL_OPENGL_ES2_BIT = 4;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   244
				int renderableType = 0;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   245
				if (majorVersion == 2) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   246
					renderableType = EGL_OPENGL_ES2_BIT;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   247
				} else if (majorVersion == 1) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   248
					renderableType = EGL_OPENGL_ES_BIT;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   249
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   250
				int[] configSpec = {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   251
						//EGL10.EGL_DEPTH_SIZE,   16,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   252
						EGL10.EGL_RENDERABLE_TYPE, renderableType,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   253
						EGL10.EGL_NONE
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   254
				};
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   255
				EGLConfig[] configs = new EGLConfig[1];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   256
				int[] num_config = new int[1];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   257
				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
   258
					Log.e("SDL", "No EGL config available");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   259
					return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   260
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   261
				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
   262
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   263
				/*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
   264
                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
   265
                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
   266
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
   267
                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
   268
                    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
   269
                    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
   270
                }
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
   271
                SDLActivity.mEGLContext = ctx;*/
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   272
				SDLActivity.mEGLDisplay = dpy;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   273
				SDLActivity.mEGLConfig = config;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   274
				SDLActivity.mGLMajor = majorVersion;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   275
				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
   276
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   277
				SDLActivity.createEGLSurface();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   278
			} catch(Exception e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   279
				Log.v("SDL", e + "");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   280
				for (StackTraceElement s : e.getStackTrace()) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   281
					Log.v("SDL", s.toString());
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   282
				}
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
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   285
		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
   286
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   287
		return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   288
	}
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
   289
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   290
	public static boolean createEGLContext() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   291
		EGL10 egl = (EGL10)EGLContext.getEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   292
		int EGL_CONTEXT_CLIENT_VERSION=0x3098;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   293
		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
   294
		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
   295
		if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   296
			Log.e("SDL", "Couldn't create context");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   297
			return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   298
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   299
		return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   300
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   301
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   302
	public static boolean createEGLSurface() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   303
		if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLConfig != null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   304
			EGL10 egl = (EGL10)EGLContext.getEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   305
			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
   306
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   307
			Log.v("SDL", "Creating new EGL Surface");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   308
			EGLSurface surface = egl.eglCreateWindowSurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, SDLActivity.mSurface, null);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   309
			if (surface == EGL10.EGL_NO_SURFACE) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   310
				Log.e("SDL", "Couldn't create surface");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   311
				return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   312
			}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   313
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   314
			if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, surface, surface, SDLActivity.mEGLContext)) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   315
				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
   316
				createEGLContext();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   317
				if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, surface, surface, SDLActivity.mEGLContext)) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   318
					Log.e("SDL", "Failed making EGL Context current");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   319
					return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   320
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   321
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   322
			SDLActivity.mEGLSurface = surface;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   323
			return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   324
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   325
		return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   326
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   327
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   328
	// EGL buffer flip
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   329
	public static void flipEGL() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   330
		try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   331
			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
   332
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   333
			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
   334
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   335
			// 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
   336
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   337
			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
   338
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   339
			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
   340
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
   341
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   342
		} catch(Exception e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   343
			Log.v("SDL", "flipEGL(): " + e);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   344
			for (StackTraceElement s : e.getStackTrace()) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   345
				Log.v("SDL", s.toString());
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
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   348
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   349
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   350
	// Audio
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   351
	private static Object buf;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   352
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   353
	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
   354
		int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   355
		int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   356
		int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   357
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   358
		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
   359
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   360
		// 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
   361
		// gods they probably shouldn't, the minimums are horrifyingly high
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   362
		// latency already
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   363
		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
   364
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   365
		mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   366
				channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
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
		audioStartThread();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   369
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   370
		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
   371
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   372
		if (is16Bit) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   373
			buf = new short[desiredFrames * (isStereo ? 2 : 1)];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   374
		} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   375
			buf = new byte[desiredFrames * (isStereo ? 2 : 1)]; 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   376
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   377
		return buf;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   378
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   379
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   380
	public static void audioStartThread() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   381
		mAudioThread = new Thread(new Runnable() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   382
			public void run() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   383
				mAudioTrack.play();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   384
				nativeRunAudioThread();
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
		});
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   387
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   388
		// I'd take REALTIME if I could get it!
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   389
		mAudioThread.setPriority(Thread.MAX_PRIORITY);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   390
		mAudioThread.start();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   391
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   392
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   393
	public static void audioWriteShortBuffer(short[] buffer) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   394
		for (int i = 0; i < buffer.length; ) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   395
			int result = mAudioTrack.write(buffer, i, buffer.length - i);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   396
			if (result > 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   397
				i += result;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   398
			} else if (result == 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   399
				try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   400
					Thread.sleep(1);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   401
				} catch(InterruptedException e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   402
					// Nom nom
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
			} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   405
				Log.w("SDL", "SDL audio: error return from write(short)");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   406
				return;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   407
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   408
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
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
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   411
	public static void audioWriteByteBuffer(byte[] buffer) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   412
		for (int i = 0; i < buffer.length; ) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   413
			int result = mAudioTrack.write(buffer, i, buffer.length - i);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   414
			if (result > 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   415
				i += result;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   416
			} else if (result == 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   417
				try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   418
					Thread.sleep(1);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   419
				} catch(InterruptedException e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   420
					// Nom nom
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   421
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   422
			} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   423
				Log.w("SDL", "SDL audio: error return from write(short)");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   424
				return;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   425
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   426
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   427
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   428
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   429
	public static void audioQuit() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   430
		if (mAudioThread != null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   431
			try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   432
				mAudioThread.join();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   433
			} catch(Exception e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   434
				Log.v("SDL", "Problem stopping audio thread: " + e);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   435
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   436
			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
   437
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   438
			//Log.v("SDL", "Finished waiting for audio thread");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   439
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   440
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   441
		if (mAudioTrack != null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   442
			mAudioTrack.stop();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   443
			mAudioTrack = null;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   444
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   445
	}
7167
0b3b306f129a Android: added a callback to java to determine dpi/dip how much we should scale the ui
Xeli
parents: 7083
diff changeset
   446
	
7185
92a045156255 Android: first attempt at using the density call from java
Xeli
parents: 7167
diff changeset
   447
	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
   448
		DisplayMetrics dm = SDLActivity.getContext().getResources().getDisplayMetrics();
7185
92a045156255 Android: first attempt at using the density call from java
Xeli
parents: 7167
diff changeset
   449
		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
   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
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   452
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   453
/**
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
   454
    Simple nativeInit() runnable
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   455
 */
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   456
class SDLMain implements Runnable {
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   457
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents: 7334
diff changeset
   458
	private final int surfaceWidth, surfaceHeight;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   459
	private final int port;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   460
	private final String playerName;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   461
	HandlerThread thread = new HandlerThread("IPC thread");
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   462
	
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   463
	public SDLMain(int width, int height, int port, String playerName) {
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   464
		surfaceWidth = width;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   465
		surfaceHeight = height;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   466
		this.port = port;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   467
		this.playerName = playerName;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   468
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   469
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   470
	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
   471
		//Set up the IPC socket server to communicate with the engine
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   472
		String path = FileUtils.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
   473
		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
   474
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   475
		Log.d("SDLMain", "Starting engine");
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   476
		// Runs SDL_main() with added parameters
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   477
		try {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   478
			SDLActivity.synchronizedNativeInit(new String[] { String.valueOf(port),
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   479
					String.valueOf(surfaceWidth), String.valueOf(surfaceHeight),
7568
75ba91f14ed5 Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   480
					Integer.toString(0x40+0x10+0x100+0x2), "en.txt", Base64.encodeToString(playerName.getBytes("UTF-8"), 0), "1", "1", "1", path, ""  });
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   481
		} catch (UnsupportedEncodingException e) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   482
			throw new AssertionError(e); // never happens
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   483
		}
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   484
		Log.d("SDLMain", "Engine stopped");
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   485
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   486
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   487
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
   488
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   489
/**
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
   490
    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
   491
    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
   492
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
   493
    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
   494
 */
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
   495
class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, 
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   496
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
   497
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   498
	private GameConfig config;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   499
	private boolean netgame;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   500
	
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   501
	// Sensors
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   502
	private static SensorManager mSensorManager;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   503
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   504
	// Startup    
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   505
	public SDLSurface(Context context, GameConfig _config, boolean netgame) {
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   506
		super(context);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   507
		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
   508
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   509
		setFocusable(true);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   510
		setFocusableInTouchMode(true);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   511
		requestFocus();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   512
		setOnKeyListener(this); 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   513
		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
   514
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   515
		mSensorManager = (SensorManager)context.getSystemService("sensor");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   516
		config = _config;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   517
		this.netgame = netgame;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   518
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   519
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   520
	// Called when we have a valid drawing surface
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   521
	public void surfaceCreated(SurfaceHolder holder) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   522
		Log.v("SDL", "surfaceCreated()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   523
		holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   524
		SDLActivity.createEGLSurface();
6901
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   525
		//		enableSensor(Sensor.TYPE_ACCELEROMETER, true);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   526
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   527
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   528
	// Called when we lose the surface
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   529
	public void surfaceDestroyed(SurfaceHolder holder) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   530
		Log.v("SDL", "surfaceDestroyed()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   531
		SDLActivity.nativePause();
6901
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   532
		//		enableSensor(Sensor.TYPE_ACCELEROMETER, false);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   533
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   534
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   535
	// Called when the surface is resized
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   536
	public void surfaceChanged(SurfaceHolder holder,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   537
			int format, int width, int height) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   538
		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
   539
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   540
		int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   541
		switch (format) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   542
		case PixelFormat.A_8:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   543
			Log.v("SDL", "pixel format A_8");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   544
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   545
		case PixelFormat.LA_88:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   546
			Log.v("SDL", "pixel format LA_88");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   547
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   548
		case PixelFormat.L_8:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   549
			Log.v("SDL", "pixel format L_8");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   550
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   551
		case PixelFormat.RGBA_4444:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   552
			Log.v("SDL", "pixel format RGBA_4444");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   553
			sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   554
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   555
		case PixelFormat.RGBA_5551:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   556
			Log.v("SDL", "pixel format RGBA_5551");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   557
			sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   558
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   559
		case PixelFormat.RGBA_8888:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   560
			Log.v("SDL", "pixel format RGBA_8888");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   561
			sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   562
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   563
		case PixelFormat.RGBX_8888:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   564
			Log.v("SDL", "pixel format RGBX_8888");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   565
			sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   566
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   567
		case PixelFormat.RGB_332:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   568
			Log.v("SDL", "pixel format RGB_332");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   569
			sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   570
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   571
		case PixelFormat.RGB_565:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   572
			Log.v("SDL", "pixel format RGB_565");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   573
			sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   574
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   575
		case PixelFormat.RGB_888:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   576
			Log.v("SDL", "pixel format RGB_888");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   577
			// Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   578
			sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   579
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   580
		default:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   581
			Log.v("SDL", "pixel format unknown " + format);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   582
			break;
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
		SDLActivity.onNativeResize(width, height, sdlFormat);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   585
		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
   586
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   587
		SDLActivity.startApp(width, height, config, netgame);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   588
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   589
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   590
	// unused
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   591
	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
   592
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   593
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   594
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   595
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   596
	// Key events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   597
	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
   598
		switch(keyCode){
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   599
		case KeyEvent.KEYCODE_BACK:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   600
		        PascalExports.HWterminate(true);
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   601
                        return true;
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   602
		case KeyEvent.KEYCODE_VOLUME_DOWN:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   603
		case KeyEvent.KEYCODE_VOLUME_UP:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   604
		case KeyEvent.KEYCODE_VOLUME_MUTE:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   605
			return false;
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   606
		}
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   607
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   608
			//Log.v("SDL", "key down: " + keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   609
			SDLActivity.onNativeKeyDown(keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   610
			return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   611
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   612
		else if (event.getAction() == KeyEvent.ACTION_UP) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   613
			//Log.v("SDL", "key up: " + keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   614
			SDLActivity.onNativeKeyUp(keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   615
			return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   616
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   617
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   618
		return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   619
	}
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
   620
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   621
	// Touch events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   622
	public boolean onTouch(View v, MotionEvent event) {
6839
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   623
		final int action = event.getAction() & MotionEvent.ACTION_MASK;
7334
66a10ae88457 Hedgeroid: Fixed multitouch breakage
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
   624
		final int actionPointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;		
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   625
6839
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   626
		if (action == MotionEvent.ACTION_MOVE) {
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   627
			// TODO send motion to every pointer if its position has
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   628
			// changed since prev event.
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   629
			for (int i = 0; i < event.getPointerCount(); i++) {
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   630
				sendNativeTouch(event, action, i);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   631
			}
6839
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   632
		} else {
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   633
			sendNativeTouch(event, action, actionPointerIndex);
6599
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
		return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   636
	} 
6839
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   637
	
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   638
	private static void sendNativeTouch(MotionEvent event, int action, int pointerIndex) {
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   639
		int touchDevId = event.getDeviceId();
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   640
		int pointerFingerId = event.getPointerId(pointerIndex);
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   641
		float x = event.getX(pointerIndex);
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   642
		float y = event.getY(pointerIndex);
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   643
		float pressure = event.getPressure(pointerIndex);
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   644
		SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, pressure);
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   645
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   646
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   647
	// Sensor events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   648
	public void enableSensor(int sensortype, boolean enabled) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   649
		// TODO: This uses getDefaultSensor - what if we have >1 accels?
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   650
		if (enabled) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   651
			mSensorManager.registerListener(this, 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   652
					mSensorManager.getDefaultSensor(sensortype), 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   653
					SensorManager.SENSOR_DELAY_GAME, null);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   654
		} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   655
			mSensorManager.unregisterListener(this, 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   656
					mSensorManager.getDefaultSensor(sensortype));
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   657
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   658
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   659
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   660
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   661
		// TODO
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   662
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   663
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   664
	public void onSensorChanged(SensorEvent event) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   665
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   666
			SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   667
					event.values[1] / SensorManager.GRAVITY_EARTH,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   668
					event.values[2] / SensorManager.GRAVITY_EARTH);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   669
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   670
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   671
}
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
   672