4 import javax.microedition.khronos.egl.EGLConfig; |
4 import javax.microedition.khronos.egl.EGLConfig; |
5 import javax.microedition.khronos.egl.EGLContext; |
5 import javax.microedition.khronos.egl.EGLContext; |
6 import javax.microedition.khronos.egl.EGLDisplay; |
6 import javax.microedition.khronos.egl.EGLDisplay; |
7 import javax.microedition.khronos.egl.EGLSurface; |
7 import javax.microedition.khronos.egl.EGLSurface; |
8 |
8 |
9 import org.hedgewars.hedgeroid.EngineProtocol.GameConfig; |
9 import org.hedgewars.hedgeroid.Datastructures.GameConfig; |
10 import org.hedgewars.hedgeroid.EngineProtocol.PascalExports; |
10 import org.hedgewars.hedgeroid.EngineProtocol.PascalExports; |
11 import org.hedgewars.hedgeroid.frontlib.Flib; |
11 import org.hedgewars.hedgeroid.frontlib.Flib; |
12 import org.hedgewars.hedgeroid.frontlib.Frontlib.GameSetupPtr; |
12 import org.hedgewars.hedgeroid.frontlib.Frontlib.GameSetupPtr; |
13 import org.hedgewars.hedgeroid.frontlib.Frontlib.GameconnPtr; |
13 import org.hedgewars.hedgeroid.frontlib.Frontlib.GameconnPtr; |
|
14 import org.hedgewars.hedgeroid.frontlib.Frontlib.IntCallback; |
14 import org.hedgewars.hedgeroid.netplay.TickHandler; |
15 import org.hedgewars.hedgeroid.netplay.TickHandler; |
|
16 |
|
17 import com.sun.jna.Pointer; |
15 |
18 |
16 import android.app.Activity; |
19 import android.app.Activity; |
17 import android.content.Context; |
20 import android.content.Context; |
18 import android.graphics.Canvas; |
21 import android.graphics.Canvas; |
19 import android.graphics.PixelFormat; |
22 import android.graphics.PixelFormat; |
422 */ |
425 */ |
423 class SDLMain implements Runnable { |
426 class SDLMain implements Runnable { |
424 |
427 |
425 private final int surfaceWidth, surfaceHeight; |
428 private final int surfaceWidth, surfaceHeight; |
426 private final GameConfig config; |
429 private final GameConfig config; |
427 |
430 private GameconnPtr conn; |
|
431 HandlerThread thread = new HandlerThread("IPC thread"); |
|
432 |
|
433 private final IntCallback dccb = new IntCallback() { |
|
434 public void callback(Pointer context, int arg1) { |
|
435 Log.d("SDLMain", "Disconnected: "+arg1); |
|
436 Flib.INSTANCE.flib_gameconn_destroy(conn); |
|
437 conn = null; |
|
438 thread.quit(); |
|
439 } |
|
440 }; |
|
441 |
428 public SDLMain(int width, int height, GameConfig _config) { |
442 public SDLMain(int width, int height, GameConfig _config) { |
429 config = _config; |
443 config = _config; |
430 surfaceWidth = width; |
444 surfaceWidth = width; |
431 surfaceHeight = height; |
445 surfaceHeight = height; |
432 } |
446 } |
433 |
447 |
434 public void run() { |
448 public void run() { |
435 //Set up the IPC socket server to communicate with the engine |
449 //Set up the IPC socket server to communicate with the engine |
436 HandlerThread thread = new HandlerThread("IPC thread"); |
450 |
437 thread.start(); // TODO ensure it gets stopped |
|
438 final GameconnPtr conn = Flib.INSTANCE.flib_gameconn_create("Xeli", GameSetupPtr.createJavaOwned(config), false); |
451 final GameconnPtr conn = Flib.INSTANCE.flib_gameconn_create("Xeli", GameSetupPtr.createJavaOwned(config), false); |
439 if(conn == null) { |
452 if(conn == null) { |
440 throw new AssertionError(); |
453 throw new AssertionError(); |
441 } |
454 } |
|
455 Flib.INSTANCE.flib_gameconn_onDisconnect(conn, dccb, null); |
442 int port = Flib.INSTANCE.flib_gameconn_getport(conn); |
456 int port = Flib.INSTANCE.flib_gameconn_getport(conn); |
443 |
457 |
444 String path = Utils.getDataPath(SDLActivity.mSingleton);//This represents the data directory |
458 String path = Utils.getDataPath(SDLActivity.mSingleton);//This represents the data directory |
445 path = path.substring(0, path.length()-1);//remove the trailing '/' |
459 path = path.substring(0, path.length()-1);//remove the trailing '/' |
446 |
460 |
|
461 thread.start(); // TODO ensure it gets stopped |
|
462 new TickHandler(thread.getLooper(), 500, new Runnable() { |
|
463 public void run() { |
|
464 Log.d("SDLMain", "pre-tick"); |
|
465 Flib.INSTANCE.flib_gameconn_tick(conn); |
|
466 Log.d("SDLMain", "post-tick"); |
|
467 } |
|
468 }).start(); |
|
469 |
|
470 Log.d("SDLMain", "Starting engine"); |
447 // Runs SDL_main() with added parameters |
471 // Runs SDL_main() with added parameters |
448 SDLActivity.nativeInit(new String[] { String.valueOf(port), |
472 SDLActivity.nativeInit(new String[] { String.valueOf(port), |
449 String.valueOf(surfaceWidth), String.valueOf(surfaceHeight), |
473 String.valueOf(surfaceWidth), String.valueOf(surfaceHeight), |
450 "0", "en.txt", "xeli", "1", "1", "1", path, "" }); |
474 "0", "en.txt", "xeli", "1", "1", "1", path, "" }); |
451 |
475 Log.d("SDLMain", "Engine stopped"); |
452 new TickHandler(thread.getLooper(), 50, new Runnable() { |
|
453 public void run() { |
|
454 Flib.INSTANCE.flib_gameconn_tick(conn); |
|
455 } |
|
456 }); |
|
457 try { |
476 try { |
458 thread.quit(); |
|
459 thread.join(); |
477 thread.join(); |
460 } catch (InterruptedException e) { |
478 } catch (InterruptedException e) { |
461 throw new AssertionError(); |
479 throw new AssertionError(); |
462 } |
480 } |
|
481 Log.v("SDLMain", "thread joined"); |
463 Flib.INSTANCE.flib_gameconn_destroy(conn); |
482 Flib.INSTANCE.flib_gameconn_destroy(conn); |
464 Log.v("SDL", "SDL thread terminated"); |
483 Log.v("SDLMain", "SDL thread terminated"); |
465 } |
484 } |
466 } |
485 } |
467 |
486 |
468 |
487 |
469 /** |
488 /** |