Skip to content
This repository has been archived by the owner. It is now read-only.
Browse files

Code cleanup

  • Loading branch information
bieh committed Jun 28, 2010
1 parent 877d51d commit 6f2641d19ef3f427a40b7758578f694a9557d0c0
@@ -4,7 +4,7 @@
<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name="TestActivity"
<activity android:name="SDLActivity"
<action android:name="android.intent.action.MAIN" />
@@ -2,7 +2,7 @@ LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := sanangeles
LOCAL_MODULE := sdltest

SDL := /home/paul/Projects/gsoc/SDL-gsoc2010_android/

@@ -41,7 +41,7 @@ extern "C" int SDL_main();
Functions called by JNI

extern "C" void Java_org_libsdl_android_TestActivity_nativeInit( JNIEnv* env, jobject obj )
extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv* env, jobject obj )
__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: NativeInit");

@@ -63,7 +63,7 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)

__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");

jclass cls = mEnv->FindClass ("org/libsdl/android/TestActivity");
jclass cls = mEnv->FindClass ("org/libsdl/android/SDLActivity");
mActivityInstance = cls;
midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
@@ -16,105 +16,146 @@
import java.lang.*;


In TestActivity::onResume() call SDL_Init
SDL_GL_CreateContext call SDLSurface::createSDLGLContext()
SDL_GL_FlipBuffers calls SDLSurface::flip()
SDL Activity
public class SDLActivity extends Activity {

//Main components
private static SDLActivity mSingleton;
private static SDLSurface mSurface;

//Load the .so
static {

public class TestActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

//So we can call stuff from static callbacks
mSingleton = this;

//Set up the surface
mSurface = new SDLSurface(getApplication());
SurfaceHolder holder = mSurface.getHolder();


protected void onPause() {

protected void onResume() {

//All set up. Start up SDL


private static SDLSurface mSurface;

static {

//C functions we call
public static native void nativeInit();

//Java functions called from C
private static void createGLContext(){

public static void flipBuffers(){

//EGL context creation


class SDLThread implements Runnable{
Simple nativeInit() runnable
class SDLRunner implements Runnable{
public void run(){
//Runs SDL_main()

SDLSurface. This is what we draw on, so we need to know when it's created
in order to do anything useful.
Because of this, that's where we set up the SDL thread
class SDLSurface extends SurfaceView implements SurfaceHolder.Callback{

//This is what SDL runs in. It invokes SDL_main(), eventually
private Thread mSDLThread;

//EGL private objects
private EGLContext mEGLContext;
private EGLSurface mEGLSurface;
private EGLDisplay mEGLDisplay;

public SDLSurface(Context context) {

//Called when we have a valid drawing surface
public void surfaceCreated(SurfaceHolder holder) {
Log.v("SDL","Surface created");

Thread runner = new Thread(new SDLThread(), "SDLThread"); // (1) Create a new thread.
runner.start(); // (2) Start the thread

mSDLThread = new Thread(new SDLRunner(), "SDLThread");

//Called when we lose the surface
public void surfaceDestroyed(SurfaceHolder holder) {
Log.v("SDL","Surface destroyed");

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

//Called when the surface is resized
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
Log.v("SDL","Surface resized");

public void onDraw(Canvas canvas) {}

boolean initEGL(){

//EGL functions
public boolean initEGL(){
Log.v("SDL","Starting up");


// Get an EGL instance
EGL10 egl = (EGL10)EGLContext.getEGL();

// Get to the default display.
EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

// We can now initialize EGL for that display
int[] version = new int[2];
egl.eglInitialize(dpy, version);

// Specify a configuration for our opengl session
// and grab the first configuration that matches is
int[] configSpec = {
@@ -124,44 +165,30 @@ boolean initEGL(){
egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
EGLConfig config = configs[0];

// Create an OpenGL ES context. This must be done only once
EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);

// Create an EGL surface we can render into.
EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);

// Before we can issue GL commands, we need to make sure
// the context is current and bound to a surface.
egl.eglMakeCurrent(dpy, surface, surface, ctx);

mEGLContext = ctx;
mEGLDisplay = dpy;
mEGLSurface = surface;

}catch(Exception e){
Log.v("SDL", e + "");
for(StackTraceElement s : e.getStackTrace()){
Log.v("SDL", s.toString());

Log.v("SDL","Done making!");

return true;

public SDLSurface(Context context) {



public void onDraw(Canvas canvas) {


public void flipBuffers(){

//EGL buffer flip
public void flipEGL(){

EGL10 egl = (EGL10)EGLContext.getEGL();
@@ -177,35 +204,13 @@ public void flipBuffers(){

}catch(Exception e){
Log.v("SDL", e + "");
Log.v("SDL", "flipEGL(): " + e);

for(StackTraceElement s : e.getStackTrace()){
Log.v("SDL", s.toString());


class TestRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
public void onSurfaceChanged(GL10 gl, int w, int h) {
//gl.glViewport(0, 0, w, h);
nativeResize(w, h);
public void onDrawFrame(GL10 gl) {
private static native void nativeInit();
private static native void nativeResize(int w, int h);
private static native void nativeRender();
private static native void nativeDone();

0 comments on commit 6f2641d

Please sign in to comment.