annotate android-project/src/org/libsdl/app/SDLActivity.java @ 5029:5d7d48157be7

Greg Jandl to SDL I did some digging, and I think I've found a better solution. By including some additional .m4 macros in the acinclude directory (http://www.gnu.org/software/autoconf-archive/ax_check_compiler_flags.html, http://www.gnu.org/software/autoconf-archive/ax_gcc_archflag.html and http://www.gnu.org/software/autoconf-archive/ax_gcc_x86_cpuid.html) then using the patch below (same as previous patch + one line change to configure.in), SDL will build correctly out of the box. The arch can then be overridden by --with-gcc-arch=<arch> (most will probably want to build with i686 there for general distribution) or --without-gcc-arch, to disable setting the arch all together. On my Atom netbook, I get -march=PentiumPro by default, which works great, as does --with-gcc-arch=i686.
author Sam Lantinga <slouken@libsdl.org>
date Tue, 18 Jan 2011 22:40:45 -0800
parents a514bfe6952a
children c66b2a778b7e
rev   line source
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 package org.libsdl.app;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 import javax.microedition.khronos.egl.EGLConfig;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4 import javax.microedition.khronos.opengles.GL10;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 import javax.microedition.khronos.egl.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 import android.app.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 import android.content.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9 import android.view.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 import android.os.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 import android.util.Log;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 import android.graphics.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 import android.text.method.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14 import android.text.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 import android.media.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 import android.hardware.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 import android.content.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 import java.lang.*;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 /**
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23 SDL Activity
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 */
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 public class SDLActivity extends Activity {
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
27 // Main components
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 private static SDLActivity mSingleton;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 private static SDLSurface mSurface;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
31 // Audio
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
32 private static Thread mAudioThread;
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 private static AudioTrack mAudioTrack;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
35 // Load the .so
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 static {
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 System.loadLibrary("SDL");
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
38 //System.loadLibrary("SDL_image");
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
39 //System.loadLibrary("SDL_mixer");
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
40 //System.loadLibrary("SDL_ttf");
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41 System.loadLibrary("main");
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
42 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
44 // Setup
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 protected void onCreate(Bundle savedInstanceState) {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
46 //Log.v("SDL", "onCreate()");
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 super.onCreate(savedInstanceState);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
49 // So we can call stuff from static callbacks
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 mSingleton = this;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
52 // Set up the surface
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 mSurface = new SDLSurface(getApplication());
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 setContentView(mSurface);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 SurfaceHolder holder = mSurface.getHolder();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56 holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
59 // Events
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60 protected void onPause() {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
61 //Log.v("SDL", "onPause()");
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62 super.onPause();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 protected void onResume() {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
66 //Log.v("SDL", "onResume()");
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 super.onResume();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
70 // Messages from the SDLMain thread
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
71 static int COMMAND_CHANGE_TITLE = 1;
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
72
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
73 // Handler for the messages
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
74 Handler commandHandler = new Handler() {
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
75 public void handleMessage(Message msg) {
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
76 if (msg.arg1 == COMMAND_CHANGE_TITLE) {
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
77 setTitle((String)msg.obj);
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
78 }
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
79 }
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
80 };
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
81
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
82 // Send a message from the SDLMain thread
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
83 void sendCommand(int command, Object data) {
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
84 Message msg = commandHandler.obtainMessage();
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
85 msg.arg1 = command;
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
86 msg.obj = data;
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
87 commandHandler.sendMessage(msg);
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
88 }
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
90 // C functions we call
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 public static native void nativeInit();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 public static native void nativeQuit();
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
93 public static native void onNativeResize(int x, int y, int format);
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 public static native void onNativeKeyDown(int keycode);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 public static native void onNativeKeyUp(int keycode);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 public static native void onNativeTouch(int action, float x,
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97 float y, float p);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98 public static native void onNativeAccel(float x, float y, float z);
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
99 public static native void nativeRunAudioThread();
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
102 // Java functions called from C
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
103
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
104 public static void createGLContext() {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105 mSurface.initEGL();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
108 public static void flipBuffers() {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 mSurface.flipEGL();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
112 public static void setActivityTitle(String title) {
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
113 // Called from SDLMain() thread and can't directly affect the view
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
114 mSingleton.sendCommand(COMMAND_CHANGE_TITLE, title);
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
115 }
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
116
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
117 // Audio
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
118 private static Object buf;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
119
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
120 public static Object audioInit(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
121 int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
122 int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
123 int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
124
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
125 Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + ((float)sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
126
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
127 // Let the user pick a larger buffer if they really want -- but ye
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
128 // gods they probably shouldn't, the minimums are horrifyingly high
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
129 // latency already
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
130 desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
131
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
132 mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
133 channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
134
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
135 audioStartThread();
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
136
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
137 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");
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
138
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
139 if (is16Bit) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
140 buf = new short[desiredFrames * (isStereo ? 2 : 1)];
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
141 } else {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
142 buf = new byte[desiredFrames * (isStereo ? 2 : 1)];
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
143 }
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
144 return buf;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
145 }
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
147 public static void audioStartThread() {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
148 mAudioThread = new Thread(new Runnable() {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
149 public void run() {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
150 mAudioTrack.play();
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
151 nativeRunAudioThread();
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
152 }
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
153 });
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
154
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
155 // I'd take REALTIME if I could get it!
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
156 mAudioThread.setPriority(Thread.MAX_PRIORITY);
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
157 mAudioThread.start();
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
158 }
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
159
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
160 public static void audioWriteShortBuffer(short[] buffer) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
161 for (int i = 0; i < buffer.length; ) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
162 int result = mAudioTrack.write(buffer, i, buffer.length - i);
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
163 if (result > 0) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
164 i += result;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
165 } else if (result == 0) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
166 try {
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
167 Thread.sleep(1);
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
168 } catch(InterruptedException e) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
169 // Nom nom
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
170 }
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
171 } else {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
172 Log.w("SDL", "SDL audio: error return from write(short)");
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
173 return;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
174 }
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
175 }
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
176 }
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
177
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
178 public static void audioWriteByteBuffer(byte[] buffer) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
179 for (int i = 0; i < buffer.length; ) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
180 int result = mAudioTrack.write(buffer, i, buffer.length - i);
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
181 if (result > 0) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
182 i += result;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
183 } else if (result == 0) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
184 try {
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
185 Thread.sleep(1);
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
186 } catch(InterruptedException e) {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
187 // Nom nom
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
188 }
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
189 } else {
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
190 Log.w("SDL", "SDL audio: error return from write(short)");
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
191 return;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
192 }
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
193 }
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
194 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
195
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
196 public static void audioQuit() {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
197 if (mAudioThread != null) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
198 try {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
199 mAudioThread.join();
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
200 } catch(Exception e) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
201 Log.v("SDL", "Problem stopping audio thread: " + e);
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
202 }
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
203 mAudioThread = null;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
204
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
205 //Log.v("SDL", "Finished waiting for audio thread");
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
206 }
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
207
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
208 if (mAudioTrack != null) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
209 mAudioTrack.stop();
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
210 mAudioTrack = null;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
211 }
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
212 }
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
213 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
214
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
215 /**
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
216 Simple nativeInit() runnable
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
217 */
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
218 class SDLMain implements Runnable {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
219 public void run() {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
220 // Runs SDL_main()
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
221 SDLActivity.nativeInit();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
222
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
223 //Log.v("SDL", "SDL thread terminated");
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
224 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
225 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
226
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
227
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
228 /**
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
229 SDLSurface. This is what we draw on, so we need to know when it's created
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
230 in order to do anything useful.
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
231
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
232 Because of this, that's where we set up the SDL thread
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
233 */
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
234 class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
235 View.OnKeyListener, View.OnTouchListener, SensorEventListener {
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
236
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
237 // This is what SDL runs in. It invokes SDL_main(), eventually
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
238 private Thread mSDLThread;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
239
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
240 // EGL private objects
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
241 private EGLContext mEGLContext;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
242 private EGLSurface mEGLSurface;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
243 private EGLDisplay mEGLDisplay;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
244
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
245 // Sensors
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
246 private static SensorManager mSensorManager;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
247
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
248 // Startup
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
249 public SDLSurface(Context context) {
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
250 super(context);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
251 getHolder().addCallback(this);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
252
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
253 setFocusable(true);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
254 setFocusableInTouchMode(true);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
255 requestFocus();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
256 setOnKeyListener(this);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
257 setOnTouchListener(this);
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
258
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
259 mSensorManager = (SensorManager)context.getSystemService("sensor");
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
260 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
261
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
262 // Called when we have a valid drawing surface
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
263 public void surfaceCreated(SurfaceHolder holder) {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
264 //Log.v("SDL", "surfaceCreated()");
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
265
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
266 enableSensor(Sensor.TYPE_ACCELEROMETER, true);
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
267 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
268
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
269 // Called when we lose the surface
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
270 public void surfaceDestroyed(SurfaceHolder holder) {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
271 //Log.v("SDL", "surfaceDestroyed()");
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
272
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
273 // Send a quit message to the application
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
274 SDLActivity.nativeQuit();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
275
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
276 // Now wait for the SDL thread to quit
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
277 if (mSDLThread != null) {
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
278 try {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
279 mSDLThread.join();
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
280 } catch(Exception e) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
281 Log.v("SDL", "Problem stopping thread: " + e);
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
282 }
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
283 mSDLThread = null;
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
284
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
285 //Log.v("SDL", "Finished waiting for SDL thread");
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
286 }
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
287
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
288 enableSensor(Sensor.TYPE_ACCELEROMETER, false);
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
289 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
290
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
291 // Called when the surface is resized
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
292 public void surfaceChanged(SurfaceHolder holder,
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
293 int format, int width, int height) {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
294 //Log.v("SDL", "surfaceChanged()");
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
295
4991
dc9924fc5070 Fixed video mode format for unknown surface format
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
296 int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
297 switch (format) {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
298 case PixelFormat.A_8:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
299 Log.v("SDL", "pixel format A_8");
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
300 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
301 case PixelFormat.LA_88:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
302 Log.v("SDL", "pixel format LA_88");
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
303 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
304 case PixelFormat.L_8:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
305 Log.v("SDL", "pixel format L_8");
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
306 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
307 case PixelFormat.RGBA_4444:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
308 Log.v("SDL", "pixel format RGBA_4444");
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
309 sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
310 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
311 case PixelFormat.RGBA_5551:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
312 Log.v("SDL", "pixel format RGBA_5551");
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
313 sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
314 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
315 case PixelFormat.RGBA_8888:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
316 Log.v("SDL", "pixel format RGBA_8888");
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
317 sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
318 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
319 case PixelFormat.RGBX_8888:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
320 Log.v("SDL", "pixel format RGBX_8888");
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
321 sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
322 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
323 case PixelFormat.RGB_332:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
324 Log.v("SDL", "pixel format RGB_332");
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
325 sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
326 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
327 case PixelFormat.RGB_565:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
328 Log.v("SDL", "pixel format RGB_565");
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
329 sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
330 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
331 case PixelFormat.RGB_888:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
332 Log.v("SDL", "pixel format RGB_888");
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
333 // Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
334 sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
335 break;
4991
dc9924fc5070 Fixed video mode format for unknown surface format
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
336 default:
dc9924fc5070 Fixed video mode format for unknown surface format
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
337 Log.v("SDL", "pixel format unknown " + format);
dc9924fc5070 Fixed video mode format for unknown surface format
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
338 break;
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
339 }
4988
f9af88a9c823 Whoops, need to pass that SDL format into native code
Sam Lantinga <slouken@libsdl.org>
parents: 4982
diff changeset
340 SDLActivity.onNativeResize(width, height, sdlFormat);
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
341
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
342 // Now start up the C app thread
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
343 if (mSDLThread == null) {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
344 mSDLThread = new Thread(new SDLMain(), "SDLThread");
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
345 mSDLThread.start();
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
346 }
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
347 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
348
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
349 // unused
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
350 public void onDraw(Canvas canvas) {}
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
351
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
352
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
353 // EGL functions
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
354 public boolean initEGL() {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
355 Log.v("SDL", "Starting up");
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
356
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
357 try {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
358
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
359 EGL10 egl = (EGL10)EGLContext.getEGL();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
360
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
361 EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
362
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
363 int[] version = new int[2];
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
364 egl.eglInitialize(dpy, version);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
365
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
366 int[] configSpec = {
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
367 //EGL10.EGL_DEPTH_SIZE, 16,
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
368 EGL10.EGL_NONE
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
369 };
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
370 EGLConfig[] configs = new EGLConfig[1];
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
371 int[] num_config = new int[1];
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
372 egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
373 EGLConfig config = configs[0];
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
374
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
375 EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
376
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
377 EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
378
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
379 egl.eglMakeCurrent(dpy, surface, surface, ctx);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
380
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
381 mEGLContext = ctx;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
382 mEGLDisplay = dpy;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
383 mEGLSurface = surface;
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
384
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
385 } catch(Exception e) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
386 Log.v("SDL", e + "");
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
387 for (StackTraceElement s : e.getStackTrace()) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
388 Log.v("SDL", s.toString());
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
389 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
390 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
391
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
392 return true;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
393 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
394
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
395 // EGL buffer flip
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
396 public void flipEGL() {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
397 try {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
398 EGL10 egl = (EGL10)EGLContext.getEGL();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
399
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
400 egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
401
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
402 // drawing here
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
403
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
404 egl.eglWaitGL();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
405
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
406 egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
407
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
408
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
409 } catch(Exception e) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
410 Log.v("SDL", "flipEGL(): " + e);
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4993
diff changeset
411 for (StackTraceElement s : e.getStackTrace()) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
412 Log.v("SDL", s.toString());
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
413 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
414 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
415 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
416
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
417 // Key events
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
418 public boolean onKey(View v, int keyCode, KeyEvent event) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
419
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
420 if (event.getAction() == KeyEvent.ACTION_DOWN) {
4993
3c9058f3ffe7 Implemented translation from Android keycodes to SDL scancodes
Sam Lantinga <slouken@libsdl.org>
parents: 4991
diff changeset
421 //Log.v("SDL", "key down: " + keyCode);
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
422 SDLActivity.onNativeKeyDown(keyCode);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
423 return true;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
424 }
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
425 else if (event.getAction() == KeyEvent.ACTION_UP) {
4993
3c9058f3ffe7 Implemented translation from Android keycodes to SDL scancodes
Sam Lantinga <slouken@libsdl.org>
parents: 4991
diff changeset
426 //Log.v("SDL", "key up: " + keyCode);
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
427 SDLActivity.onNativeKeyUp(keyCode);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
428 return true;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
429 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
430
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
431 return false;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
432 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
433
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
434 // Touch events
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
435 public boolean onTouch(View v, MotionEvent event) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
436
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
437 int action = event.getAction();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
438 float x = event.getX();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
439 float y = event.getY();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
440 float p = event.getPressure();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
441
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
442 // TODO: Anything else we need to pass?
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
443 SDLActivity.onNativeTouch(action, x, y, p);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
444 return true;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
445 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
446
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
447 // Sensor events
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
448 public void enableSensor(int sensortype, boolean enabled) {
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
449 // TODO: This uses getDefaultSensor - what if we have >1 accels?
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
450 if (enabled) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
451 mSensorManager.registerListener(this,
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
452 mSensorManager.getDefaultSensor(sensortype),
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
453 SensorManager.SENSOR_DELAY_GAME, null);
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
454 } else {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
455 mSensorManager.unregisterListener(this,
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
456 mSensorManager.getDefaultSensor(sensortype));
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
457 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
458 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
459
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
460 public void onAccuracyChanged(Sensor sensor, int accuracy) {
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
461 // TODO
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
462 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
463
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
464 public void onSensorChanged(SensorEvent event) {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
465 if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
466 SDLActivity.onNativeAccel(event.values[0],
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
467 event.values[1],
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
468 event.values[2]);
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
469 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
470 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
471
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
472 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
473