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