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