annotate android-project/src/org/libsdl/app/SDLActivity.java @ 4993:3c9058f3ffe7

Implemented translation from Android keycodes to SDL scancodes
author Sam Lantinga <slouken@libsdl.org>
date Wed, 12 Jan 2011 21:21:21 -0800
parents dc9924fc5070
children 9f9bea41e88f
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
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 private static AudioTrack mAudioTrack;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
34 // Load the .so
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 static {
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 System.loadLibrary("SDL");
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 System.loadLibrary("main");
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
40 // Setup
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41 protected void onCreate(Bundle savedInstanceState) {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
42 //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
43 super.onCreate(savedInstanceState);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
45 // 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
46 mSingleton = this;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
48 // 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
49 mSurface = new SDLSurface(getApplication());
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 setContentView(mSurface);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 SurfaceHolder holder = mSurface.getHolder();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 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
53 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
55 // Events
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56 protected void onPause() {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
57 //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
58 super.onPause();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 protected void onResume() {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
62 //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
63 super.onResume();
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
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
67 // 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
68 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
69 public static native void nativeQuit();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 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
71 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
72 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
73 float y, float p);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 public static native void onNativeResize(int x, int y, int format);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 public static native void onNativeAccel(float x, float y, float z);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
78 // Java functions called from C
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
79 private static void createGLContext() {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80 mSurface.initEGL();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
83 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
84 mSurface.flipEGL();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
87 public static void updateAudio(byte [] buf) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
89 if(mAudioTrack == null) {
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
90 // Hardcoded things are bad. FIXME when we have more sound stuff
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
91 // working properly.
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
92 mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
93 11025,
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
94 AudioFormat.CHANNEL_CONFIGURATION_MONO,
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
95 AudioFormat.ENCODING_PCM_8BIT,
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
96 2048,
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
97 AudioTrack.MODE_STREAM);
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98 }
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
99
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 mAudioTrack.write(buf, 0, buf.length);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 mAudioTrack.play();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
103 Log.v("SDL", "Played some audio");
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105
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
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 /**
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 Simple nativeInit() runnable
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 */
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
111 class SDLMain implements Runnable {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
112 public void run() {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
113 // Runs SDL_main()
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 SDLActivity.nativeInit();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
116 //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
117 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121 /**
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 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
123 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
124
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125 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
126 */
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127 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
128 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
129
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
130 // 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
131 private Thread mSDLThread;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
133 // EGL private objects
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 private EGLContext mEGLContext;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 private EGLSurface mEGLSurface;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136 private EGLDisplay mEGLDisplay;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
138 // Sensors
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 private static SensorManager mSensorManager;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
141 // Startup
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142 public SDLSurface(Context context) {
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
143 super(context);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
144 getHolder().addCallback(this);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
145
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146 setFocusable(true);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
147 setFocusableInTouchMode(true);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
148 requestFocus();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
149 setOnKeyListener(this);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 setOnTouchListener(this);
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
151
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152 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
153 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
154
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
155 // 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
156 public void surfaceCreated(SurfaceHolder holder) {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
157 //Log.v("SDL", "surfaceCreated()");
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
158
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
159 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
160 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
161
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
162 // 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
163 public void surfaceDestroyed(SurfaceHolder holder) {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
164 //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
165
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
166 // 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
167 SDLActivity.nativeQuit();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
168
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
169 // 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
170 if (mSDLThread != null) {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
171 //synchronized (mSDLThread) {
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
172 try {
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
173 mSDLThread.join();
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
174 } catch(Exception e) {
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
175 Log.v("SDL", "Problem stopping thread: " + e);
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
176 }
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
177 //}
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
178 mSDLThread = null;
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
179
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
180 //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
181 }
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
182
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
183 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
184 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
185
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
186 // 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
187 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
188 int format, int width, int height) {
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
189 //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
190
4991
dc9924fc5070 Fixed video mode format for unknown surface format
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
191 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
192 switch (format) {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
193 case PixelFormat.A_8:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
194 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
195 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
196 case PixelFormat.LA_88:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
197 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
198 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
199 case PixelFormat.L_8:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
200 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
201 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
202 case PixelFormat.RGBA_4444:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
203 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
204 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
205 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
206 case PixelFormat.RGBA_5551:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
207 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
208 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
209 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
210 case PixelFormat.RGBA_8888:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
211 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
212 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
213 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
214 case PixelFormat.RGBX_8888:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
215 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
216 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
217 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
218 case PixelFormat.RGB_332:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
219 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
220 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
221 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
222 case PixelFormat.RGB_565:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
223 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
224 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
225 break;
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
226 case PixelFormat.RGB_888:
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
227 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
228 // 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
229 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
230 break;
4991
dc9924fc5070 Fixed video mode format for unknown surface format
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
231 default:
dc9924fc5070 Fixed video mode format for unknown surface format
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
232 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
233 break;
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
234 }
4988
f9af88a9c823 Whoops, need to pass that SDL format into native code
Sam Lantinga <slouken@libsdl.org>
parents: 4982
diff changeset
235 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
236
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
237 // 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
238 if (mSDLThread == null) {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
239 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
240 mSDLThread.start();
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
241 }
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
242 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
243
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
244 // unused
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
245 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
246
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 // EGL functions
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
249 public boolean initEGL() {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
250 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
251
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
252 try {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
253
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
254 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
255
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
256 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
257
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
258 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
259 egl.eglInitialize(dpy, version);
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 int[] configSpec = {
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
262 //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
263 EGL10.EGL_NONE
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
264 };
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
265 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
266 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
267 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
268 EGLConfig config = configs[0];
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
269
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
270 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
271
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
272 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
273
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
274 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
275
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
276 mEGLContext = ctx;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
277 mEGLDisplay = dpy;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
278 mEGLSurface = surface;
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
279
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
280 } catch(Exception e) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
281 Log.v("SDL", e + "");
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
282 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
283 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
284 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
285 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
286
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
287 return true;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
288 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
289
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
290 // EGL buffer flip
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
291 public void flipEGL() {
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
292 try {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
293 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
294
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
295 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
296
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
297 // drawing here
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
298
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
299 egl.eglWaitGL();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
300
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
301 egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
302
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
303
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
304 } catch(Exception e) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
305 Log.v("SDL", "flipEGL(): " + e);
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
306 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
307 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
308 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
309 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
310 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
311
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
312 // Key events
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4988
diff changeset
313 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
314
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
315 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
316 //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
317 SDLActivity.onNativeKeyDown(keyCode);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
318 return true;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
319 }
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
320 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
321 //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
322 SDLActivity.onNativeKeyUp(keyCode);
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
323 return true;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
324 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
325
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
326 return false;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
327 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
328
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
329 // Touch events
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
330 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
331
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
332 int action = event.getAction();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
333 float x = event.getX();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
334 float y = event.getY();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
335 float p = event.getPressure();
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
336
4982
660d3a432102 Added some missing pixel formats and SDL_GetPixelFormatName()
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
337 // 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
338 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
339 return true;
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
340 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
341
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
342 // Sensor events
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
343 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
344 // 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
345 if (enabled) {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
346 mSensorManager.registerListener(this,
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
347 mSensorManager.getDefaultSensor(sensortype),
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
348 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
349 } else {
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
350 mSensorManager.unregisterListener(this,
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
351 mSensorManager.getDefaultSensor(sensortype));
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
352 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
353 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
354
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
355 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
356 // TODO
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
357 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
358
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
359 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
360 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
361 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
362 event.values[1],
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
363 event.values[2]);
4965
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
364 }
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
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
367 }
91d0085b7560 Added the Android project and lots of info to README.android
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
368