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