project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/SDLActivity.java
author sheepluva
Fri, 17 Jan 2014 01:06:54 +0100
changeset 9998 736015b847e3
parent 9080 9b42757d7e71
child 10017 de822cd3df3a
permissions -rw-r--r--
update copyright to 2014
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     3
 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     4
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
9998
736015b847e3 update copyright to 2014
sheepluva
parents: 9080
diff changeset
     5
 * Copyright (c) 2004-2014 Andrey Korotaev <unC0Rr@gmail.com>
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     6
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     7
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     8
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     9
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    10
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    11
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    12
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    15
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    16
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    17
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    18
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    20
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    21
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    22
package org.hedgewars.hedgeroid;
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    23
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
    24
import java.io.IOException;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    25
import java.io.UnsupportedEncodingException;
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
    26
import java.net.ConnectException;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    27
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    28
import 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
    29
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
    30
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
    31
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
    32
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
    33
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    34
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
    35
import org.hedgewars.hedgeroid.EngineProtocol.PascalExports;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    36
import org.hedgewars.hedgeroid.netplay.Netplay;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    37
import org.hedgewars.hedgeroid.util.FileUtils;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    38
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    39
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
    40
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
    41
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
    42
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
    43
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
    44
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
    45
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
    46
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
    47
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
    48
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
    49
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
    50
import android.os.Bundle;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    51
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
    52
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
    53
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
    54
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
    55
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
    56
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
    57
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
    58
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
    59
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
    60
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    61
/**
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
    62
    SDL Activity
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    63
 */
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    64
public class SDLActivity extends Activity {
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    65
	/**
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    66
	 * 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
    67
	 * 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
    68
	 * 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
    69
	 */
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    70
	public static volatile GameConfig startConfig;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    71
	public static volatile boolean startNetgame;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7449
diff changeset
    72
	
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    73
	// Main components
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    74
	public static SDLActivity mSingleton;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    75
	private static SDLSurface mSurface;
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
    76
	private static Thread mSDLThread;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    77
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    78
	// Audio
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    79
	private static Thread mAudioThread;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    80
	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
    81
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    82
	// EGL private objects
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    83
	private static EGLContext  mEGLContext;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    84
	private static EGLSurface  mEGLSurface;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    85
	private static EGLDisplay  mEGLDisplay;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    86
	private static EGLConfig   mEGLConfig;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    87
	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
    88
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    89
	// Load the .so
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    90
	static {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    91
		System.loadLibrary("SDL");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    92
		System.loadLibrary("main");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    93
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    94
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    95
	// Setup
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    96
	protected void onCreate(Bundle savedInstanceState) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    97
		super.onCreate(savedInstanceState);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    98
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    99
		// So we can call stuff from static callbacks
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   100
		mSingleton = this;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   101
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   102
		// Set up the surface
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   103
		mSurface = new SDLSurface(getApplication(), startConfig, startNetgame);
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   104
		startConfig = null;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   105
		setContentView(mSurface);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   106
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   107
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   108
	// Events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   109
	protected void onPause() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   110
		Log.v("SDL", "onPause()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   111
		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
   112
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   113
		if(mEGLDisplay != null && mEGLContext != null){
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   114
			EGL10 egl = (EGL10)EGLContext.getEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   115
			egl.eglDestroyContext(mEGLDisplay, mEGLContext);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   116
			mEGLDisplay = null;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   117
			mEGLContext = null;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   118
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   119
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   120
		SDLActivity.nativePause();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   121
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   122
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   123
	protected void onResume() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   124
		Log.v("SDL", "onResume()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   125
		super.onResume();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   126
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   127
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   128
	protected void onDestroy() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   129
		super.onDestroy();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   130
		Log.v("SDL", "onDestroy()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   131
		// Send a quit message to the application
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   132
		SDLActivity.nativeQuit();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   133
		// Now wait for the SDL thread to quit
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   134
		if (mSDLThread != null) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   135
			try {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   136
				mSDLThread.join();
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   137
			} catch(Exception e) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   138
				Log.w("SDL", "Problem stopping thread: " + e);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   139
			}
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   140
			mSDLThread = null;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   141
		}
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   142
		mSingleton = null;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   143
	}
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   144
	
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   145
	public static void synchronizedNativeInit(String...args) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   146
		synchronized(PascalExports.engineMutex) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   147
			nativeInit(args);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   148
		}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   149
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   150
	
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   151
	// C functions we call
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   152
	private static native void nativeInit(String...args);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   153
	public static native void nativeQuit();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   154
	public static native void nativePause();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   155
	public static native void nativeResume();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   156
	public static native void onNativeResize(int x, int y, int format);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   157
	public static native void onNativeKeyDown(int keycode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   158
	public static native void onNativeKeyUp(int keycode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   159
	public static native void onNativeTouch(int touchDevId, int pointerFingerId,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   160
			int action, float x, 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   161
			float y, float p);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   162
	public static native void onNativeAccel(float x, float y, float z);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   163
	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
   164
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   165
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   166
	// 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
   167
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   168
	public static boolean createGLContext(int majorVersion, int minorVersion) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   169
		return initEGL(majorVersion, minorVersion);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   170
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   171
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   172
	public static void flipBuffers() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   173
		flipEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   174
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   175
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   176
	public static void setActivityTitle(final String title) {
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   177
		// Called from SDLMain() thread and can't directly affect the view
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   178
		mSingleton.runOnUiThread(new Runnable() {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   179
			public void run() {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   180
				mSingleton.setTitle(title);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   181
			}
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   182
		});
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   183
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   184
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   185
	public static Context getContext() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   186
		return mSingleton;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   187
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   188
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   189
	public static void startApp(final int width, final int height, GameConfig config, boolean netgame) {
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   190
		// Start up the C app thread
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   191
		if (mSDLThread == null) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   192
			mSDLThread = new Thread(new SDLMain(width, height, config, netgame));
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   193
			mSDLThread.start();
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   194
		} else {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   195
			SDLActivity.nativeResume();
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   196
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   197
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   198
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   199
	// EGL functions
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   200
	public static boolean initEGL(int majorVersion, int minorVersion) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   201
		if (SDLActivity.mEGLDisplay == null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   202
			try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   203
				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
   204
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   205
				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
   206
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   207
				int[] version = new int[2];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   208
				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
   209
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   210
				int EGL_OPENGL_ES_BIT = 1;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   211
				int EGL_OPENGL_ES2_BIT = 4;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   212
				int renderableType = 0;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   213
				if (majorVersion == 2) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   214
					renderableType = EGL_OPENGL_ES2_BIT;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   215
				} else if (majorVersion == 1) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   216
					renderableType = EGL_OPENGL_ES_BIT;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   217
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   218
				int[] configSpec = {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   219
						EGL10.EGL_RENDERABLE_TYPE, renderableType,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   220
						EGL10.EGL_NONE
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   221
				};
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   222
				EGLConfig[] configs = new EGLConfig[1];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   223
				int[] num_config = new int[1];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   224
				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
   225
					Log.e("SDL", "No EGL config available");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   226
					return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   227
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   228
				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
   229
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   230
				SDLActivity.mEGLDisplay = dpy;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   231
				SDLActivity.mEGLConfig = config;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   232
				SDLActivity.mGLMajor = majorVersion;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   233
				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
   234
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   235
				SDLActivity.createEGLSurface();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   236
			} catch(Exception e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   237
				Log.v("SDL", e + "");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   238
				for (StackTraceElement s : e.getStackTrace()) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   239
					Log.v("SDL", s.toString());
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   240
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   241
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   242
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   243
		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
   244
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   245
		return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   246
	}
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
   247
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   248
	public static boolean createEGLContext() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   249
		EGL10 egl = (EGL10)EGLContext.getEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   250
		int EGL_CONTEXT_CLIENT_VERSION=0x3098;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   251
		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
   252
		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
   253
		if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   254
			Log.e("SDL", "Couldn't create context");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   255
			return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   256
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   257
		return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   258
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   259
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   260
	public static boolean createEGLSurface() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   261
		if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLConfig != null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   262
			EGL10 egl = (EGL10)EGLContext.getEGL();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   263
			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
   264
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   265
			Log.v("SDL", "Creating new EGL Surface");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   266
			EGLSurface surface = egl.eglCreateWindowSurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, SDLActivity.mSurface, null);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   267
			if (surface == EGL10.EGL_NO_SURFACE) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   268
				Log.e("SDL", "Couldn't create surface");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   269
				return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   270
			}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   271
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   272
			if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, surface, surface, SDLActivity.mEGLContext)) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   273
				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
   274
				createEGLContext();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   275
				if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, surface, surface, SDLActivity.mEGLContext)) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   276
					Log.e("SDL", "Failed making EGL Context current");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   277
					return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   278
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   279
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   280
			SDLActivity.mEGLSurface = surface;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   281
			return true;
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
		return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   284
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   285
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   286
	// EGL buffer flip
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   287
	public static void flipEGL() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   288
		try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   289
			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
   290
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   291
			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
   292
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   293
			// 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
   294
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   295
			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
   296
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   297
			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
   298
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   299
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   300
		} catch(Exception e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   301
			Log.v("SDL", "flipEGL(): " + e);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   302
			for (StackTraceElement s : e.getStackTrace()) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   303
				Log.v("SDL", s.toString());
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   304
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   305
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   306
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   307
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   308
	// Audio
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   309
	private static Object buf;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   310
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   311
	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
   312
		int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   313
		int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   314
		int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   315
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   316
		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
   317
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   318
		// 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
   319
		// gods they probably shouldn't, the minimums are horrifyingly high
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   320
		// latency already
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   321
		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
   322
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   323
		mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   324
				channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   325
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   326
		audioStartThread();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   327
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   328
		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
   329
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   330
		if (is16Bit) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   331
			buf = new short[desiredFrames * (isStereo ? 2 : 1)];
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   332
		} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   333
			buf = new byte[desiredFrames * (isStereo ? 2 : 1)]; 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   334
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   335
		return buf;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   336
	}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   337
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   338
	public static void audioStartThread() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   339
		mAudioThread = new Thread(new Runnable() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   340
			public void run() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   341
				mAudioTrack.play();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   342
				nativeRunAudioThread();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   343
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   344
		});
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   345
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   346
		// I'd take REALTIME if I could get it!
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   347
		mAudioThread.setPriority(Thread.MAX_PRIORITY);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   348
		mAudioThread.start();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   349
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   350
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   351
	public static void audioWriteShortBuffer(short[] buffer) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   352
		for (int i = 0; i < buffer.length; ) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   353
			int result = mAudioTrack.write(buffer, i, buffer.length - i);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   354
			if (result > 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   355
				i += result;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   356
			} else if (result == 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   357
				try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   358
					Thread.sleep(1);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   359
				} catch(InterruptedException e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   360
					// Nom nom
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   361
				}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   362
			} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   363
				Log.w("SDL", "SDL audio: error return from write(short)");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   364
				return;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   365
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   366
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   367
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   368
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   369
	public static void audioWriteByteBuffer(byte[] buffer) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   370
		for (int i = 0; i < buffer.length; ) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   371
			int result = mAudioTrack.write(buffer, i, buffer.length - i);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   372
			if (result > 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   373
				i += result;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   374
			} else if (result == 0) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   375
				try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   376
					Thread.sleep(1);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   377
				} catch(InterruptedException e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   378
					// Nom nom
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
			} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   381
				Log.w("SDL", "SDL audio: error return from write(short)");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   382
				return;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   383
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   384
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   385
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   386
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   387
	public static void audioQuit() {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   388
		if (mAudioThread != null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   389
			try {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   390
				mAudioThread.join();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   391
			} catch(Exception e) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   392
				Log.v("SDL", "Problem stopping audio thread: " + e);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   393
			}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   394
			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
   395
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   396
			//Log.v("SDL", "Finished waiting for audio thread");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   397
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   398
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   399
		if (mAudioTrack != null) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   400
			mAudioTrack.stop();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   401
			mAudioTrack = null;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   402
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   403
	}
7167
0b3b306f129a Android: added a callback to java to determine dpi/dip how much we should scale the ui
Xeli
parents: 7083
diff changeset
   404
	
7185
92a045156255 Android: first attempt at using the density call from java
Xeli
parents: 7167
diff changeset
   405
	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
   406
		DisplayMetrics dm = SDLActivity.getContext().getResources().getDisplayMetrics();
7185
92a045156255 Android: first attempt at using the density call from java
Xeli
parents: 7167
diff changeset
   407
		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
   408
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   409
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   410
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   411
/**
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
   412
    Simple nativeInit() runnable
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   413
 */
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   414
class SDLMain implements Runnable {
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   415
	public static final String TAG = "SDLMain";
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   416
	
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   417
    public static final int RQ_LOWRES            = 0x00000001; // use half land array
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   418
    public static final int RQ_BLURRY_LAND       = 0x00000002; // downscaled terrain
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   419
    public static final int RQ_NO_BACKGROUND     = 0x00000004; // don't draw background
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   420
    public static final int RQ_SIMPLE_ROPE       = 0x00000008; // avoid drawing rope
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   421
    public static final int RQ_2D_WATER          = 0x00000010; // disabe 3D water effect
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   422
    public static final int RQ_SIMPLE_EXPLOSIONS = 0x00000020; // no fancy explosion effects
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   423
    public static final int RQ_NO_FLAKES         = 0x00000040; // no flakes
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   424
    public static final int RQ_NO_MENU_ANIM      = 0x00000080; // ammomenu appears with no animation
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   425
    public static final int RQ_NO_DROPLETS       = 0x00000100; // no droplets
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   426
    public static final int RQ_NO_CLAMPING       = 0x00000200; // don't clamp textures
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   427
    public static final int RQ_NO_TOOLTIPS       = 0x00000400; // tooltips are not drawn
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   428
    public static final int RQ_NO_VSYNC          = 0x00000800; // don't sync on vblank
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   429
	
7449
2e63537b44f3 Hedgeroid: Room screen WIP
Medo <smaxein@googlemail.com>
parents: 7334
diff changeset
   430
	private final int surfaceWidth, surfaceHeight;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   431
	private final String playerName;
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   432
	private final GameConfig config;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   433
	private final boolean netgame;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
   434
	
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   435
	public SDLMain(int width, int height, GameConfig config, boolean netgame) {
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   436
		surfaceWidth = width;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   437
		surfaceHeight = height;
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   438
		if(netgame) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   439
			playerName = Netplay.getAppInstance(SDLActivity.getContext().getApplicationContext()).getPlayerName();
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   440
		} else {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   441
			playerName = "Player";
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   442
		}
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   443
		this.config = config;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   444
		this.netgame = netgame;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   445
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   446
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   447
	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
   448
		//Set up the IPC socket server to communicate with the engine
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   449
		GameConnection gameConn;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   450
		String path;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   451
		try {
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   452
			if(netgame) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   453
				Netplay netplay = Netplay.getAppInstance(SDLActivity.mSingleton.getApplicationContext());
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   454
				gameConn = GameConnection.forNetgame(config, netplay);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   455
			} else {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   456
				gameConn = GameConnection.forLocalGame(config);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   457
			}
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   458
			
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   459
			path = FileUtils.getDataPathFile(SDLActivity.mSingleton).getAbsolutePath();
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   460
			Log.d(TAG, "Starting engine");
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   461
			// Runs SDL_main() with added parameters
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   462
			try {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   463
				String pPort = String.valueOf(gameConn.port);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   464
				String pWidth = String.valueOf(surfaceWidth);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   465
				String pHeight = String.valueOf(surfaceHeight);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   466
				String pQuality = Integer.toString(RQ_NO_FLAKES|RQ_NO_DROPLETS|RQ_SIMPLE_EXPLOSIONS);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   467
				String pPlayerName = Base64.encodeToString(playerName.getBytes("UTF-8"), 0);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   468
				SDLActivity.synchronizedNativeInit(new String[] { pPort, pWidth, pHeight, pQuality, "en.txt", pPlayerName, "1", "1", "1", path, ""  });
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   469
			} catch (UnsupportedEncodingException e) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   470
				throw new AssertionError(e); // never happens
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   471
			}
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   472
			Log.d(TAG, "Engine stopped");
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   473
		} catch(ConnectException e) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   474
			Log.e(TAG, "Error starting IPC connection");
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   475
		} catch (IOException e) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   476
			Log.e(TAG, "Missing SDCard");
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   477
		}
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   478
		SDLActivity.mSingleton.runOnUiThread(new Runnable() { public void run() {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   479
			if(SDLActivity.mSingleton != null) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   480
				SDLActivity.mSingleton.finish();
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   481
			}
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   482
		}});
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   483
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   484
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   485
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
   486
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   487
/**
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
    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
   489
    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
   490
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
   491
    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
   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
class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, 
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   494
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
   495
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   496
	private GameConfig config;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   497
	private boolean netgame;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   498
	
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   499
	// Sensors
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   500
	private static SensorManager mSensorManager;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   501
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   502
	// Startup    
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   503
	public SDLSurface(Context context, GameConfig _config, boolean netgame) {
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   504
		super(context);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   505
		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
   506
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   507
		setFocusable(true);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   508
		setFocusableInTouchMode(true);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   509
		requestFocus();
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   510
		setOnKeyListener(this); 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   511
		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
   512
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   513
		mSensorManager = (SensorManager)context.getSystemService("sensor");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   514
		config = _config;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   515
		this.netgame = netgame;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   516
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   517
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   518
	// Called when we have a valid drawing surface
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   519
	public void surfaceCreated(SurfaceHolder holder) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   520
		Log.v("SDL", "surfaceCreated()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   521
		holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   522
		SDLActivity.createEGLSurface();
6901
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   523
		//		enableSensor(Sensor.TYPE_ACCELEROMETER, true);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   524
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   525
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   526
	// Called when we lose the surface
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   527
	public void surfaceDestroyed(SurfaceHolder holder) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   528
		Log.v("SDL", "surfaceDestroyed()");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   529
		SDLActivity.nativePause();
6901
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   530
		//		enableSensor(Sensor.TYPE_ACCELEROMETER, false);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   531
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   532
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   533
	// Called when the surface is resized
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   534
	public void surfaceChanged(SurfaceHolder holder,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   535
			int format, int width, int height) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   536
		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
   537
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   538
		int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   539
		switch (format) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   540
		case PixelFormat.A_8:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   541
			Log.v("SDL", "pixel format A_8");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   542
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   543
		case PixelFormat.LA_88:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   544
			Log.v("SDL", "pixel format LA_88");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   545
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   546
		case PixelFormat.L_8:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   547
			Log.v("SDL", "pixel format L_8");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   548
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   549
		case PixelFormat.RGBA_4444:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   550
			Log.v("SDL", "pixel format RGBA_4444");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   551
			sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   552
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   553
		case PixelFormat.RGBA_5551:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   554
			Log.v("SDL", "pixel format RGBA_5551");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   555
			sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   556
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   557
		case PixelFormat.RGBA_8888:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   558
			Log.v("SDL", "pixel format RGBA_8888");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   559
			sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   560
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   561
		case PixelFormat.RGBX_8888:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   562
			Log.v("SDL", "pixel format RGBX_8888");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   563
			sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   564
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   565
		case PixelFormat.RGB_332:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   566
			Log.v("SDL", "pixel format RGB_332");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   567
			sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   568
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   569
		case PixelFormat.RGB_565:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   570
			Log.v("SDL", "pixel format RGB_565");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   571
			sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   572
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   573
		case PixelFormat.RGB_888:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   574
			Log.v("SDL", "pixel format RGB_888");
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   575
			// Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   576
			sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   577
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   578
		default:
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   579
			Log.v("SDL", "pixel format unknown " + format);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   580
			break;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   581
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   582
		SDLActivity.onNativeResize(width, height, sdlFormat);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   583
		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
   584
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   585
		SDLActivity.startApp(width, height, config, netgame);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   586
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   587
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   588
	// unused
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   589
	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
   590
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   591
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
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   594
	// Key events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   595
	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
   596
		switch(keyCode){
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   597
		case KeyEvent.KEYCODE_BACK:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   598
			Log.d("SDL", "KEYCODE_BACK");
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   599
			SDLActivity.nativeQuit();
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   600
            return true;
6901
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   601
		case KeyEvent.KEYCODE_VOLUME_DOWN:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   602
		case KeyEvent.KEYCODE_VOLUME_UP:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   603
		case KeyEvent.KEYCODE_VOLUME_MUTE:
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   604
			return false;
c021699c33dc - Don't crash on back press, but shutdown hedgewars
Xeli
parents: 6892
diff changeset
   605
		}
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   606
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   607
			//Log.v("SDL", "key down: " + keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   608
			SDLActivity.onNativeKeyDown(keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   609
			return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   610
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   611
		else if (event.getAction() == KeyEvent.ACTION_UP) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   612
			//Log.v("SDL", "key up: " + keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   613
			SDLActivity.onNativeKeyUp(keyCode);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   614
			return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   615
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   616
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   617
		return false;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   618
	}
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
   619
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   620
	// Touch events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   621
	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
   622
		final int action = event.getAction() & MotionEvent.ACTION_MASK;
7334
66a10ae88457 Hedgeroid: Fixed multitouch breakage
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
   623
		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
   624
6839
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   625
		if (action == MotionEvent.ACTION_MOVE) {
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   626
			// 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
   627
			// changed since prev event.
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   628
			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
   629
				sendNativeTouch(event, action, i);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   630
			}
6839
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   631
		} else {
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   632
			sendNativeTouch(event, action, actionPointerIndex);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   633
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   634
		return true;
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   635
	} 
6839
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   636
	
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   637
	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
   638
		int touchDevId = event.getDeviceId();
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   639
		int pointerFingerId = event.getPointerId(pointerIndex);
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   640
		float x = event.getX(pointerIndex);
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   641
		float y = event.getY(pointerIndex);
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   642
		float pressure = event.getPressure(pointerIndex);
2dd2c0f2c9d0 Remove API Level 8 dependency found by Android Lint
Medo <smaxein@googlemail.com>
parents: 6661
diff changeset
   643
		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
   644
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   645
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   646
	// Sensor events
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   647
	public void enableSensor(int sensortype, boolean enabled) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   648
		// TODO: This uses getDefaultSensor - what if we have >1 accels?
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   649
		if (enabled) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   650
			mSensorManager.registerListener(this, 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   651
					mSensorManager.getDefaultSensor(sensortype), 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   652
					SensorManager.SENSOR_DELAY_GAME, null);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   653
		} else {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   654
			mSensorManager.unregisterListener(this, 
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   655
					mSensorManager.getDefaultSensor(sensortype));
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   656
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   657
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   658
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   659
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   660
		// TODO
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   661
	}
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
	public void onSensorChanged(SensorEvent event) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   664
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   665
			SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   666
					event.values[1] / SensorManager.GRAVITY_EARTH,
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   667
					event.values[2] / SensorManager.GRAVITY_EARTH);
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   668
		}
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   669
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   670
}
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
   671