project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/SDLActivity.java
author sheepluva
Sat, 07 Dec 2019 16:18:20 +0100
branchhedgeroid
changeset 15512 45d6806548e9
parent 11046 47a8c19ecb60
permissions -rw-r--r--
hedgeroid: add debug logging to library loading
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     3
 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     4
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10017
diff changeset
     5
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     6
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     7
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     8
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     9
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    10
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    11
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    12
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    15
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    16
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    17
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    18
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    20
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    21
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    22
package org.hedgewars.hedgeroid;
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    23
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
    24
import java.io.IOException;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    25
import java.io.UnsupportedEncodingException;
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
    26
import java.net.ConnectException;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    27
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    28
import javax.microedition.khronos.egl.EGL10;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    29
import javax.microedition.khronos.egl.EGLConfig;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    30
import javax.microedition.khronos.egl.EGLContext;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    31
import javax.microedition.khronos.egl.EGLDisplay;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    32
import javax.microedition.khronos.egl.EGLSurface;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    33
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    34
import org.hedgewars.hedgeroid.Datastructures.GameConfig;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    35
import org.hedgewars.hedgeroid.EngineProtocol.PascalExports;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    36
import org.hedgewars.hedgeroid.netplay.Netplay;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    37
import org.hedgewars.hedgeroid.util.FileUtils;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    38
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    39
import android.app.Activity;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    40
import android.content.Context;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    41
import android.graphics.Canvas;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    42
import android.graphics.PixelFormat;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    43
import android.hardware.Sensor;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    44
import android.hardware.SensorEvent;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    45
import android.hardware.SensorEventListener;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    46
import android.hardware.SensorManager;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    47
import android.media.AudioFormat;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    48
import android.media.AudioManager;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    49
import android.media.AudioTrack;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    50
import android.os.Bundle;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    51
import android.util.Base64;
7167
0b3b306f129a Android: added a callback to java to determine dpi/dip how much we should scale the ui
Xeli
parents: 7083
diff changeset
    52
import android.util.DisplayMetrics;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    53
import android.util.Log;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    54
import android.view.KeyEvent;
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
    55
import android.view.MotionEvent;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    56
import android.view.SurfaceHolder;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    57
import android.view.SurfaceView;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    58
import android.view.View;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    59
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
    60
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    61
/**
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
    62
    SDL Activity
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    63
 */
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    64
public class SDLActivity extends Activity {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    65
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    66
     * Set startConfig to the desired config when starting this activity. This avoids having to parcel all
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    67
     * the config objects into the Intent. Not particularly elegant, but it's actually a recommended
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    68
     * way to do this (http://developer.android.com/guide/faq/framework.html#3)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    69
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    70
    public static volatile GameConfig startConfig;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    71
    public static volatile boolean startNetgame;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
    72
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    73
    // Main components
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    74
    public static SDLActivity mSingleton;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    75
    private static SDLSurface mSurface;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    76
    private static Thread mSDLThread;
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
    77
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    78
    // Audio
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    79
    private static Thread mAudioThread;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    80
    private static AudioTrack mAudioTrack;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    81
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    82
    // EGL private objects
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    83
    private static EGLContext  mEGLContext;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    84
    private static EGLSurface  mEGLSurface;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    85
    private static EGLDisplay  mEGLDisplay;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    86
    private static EGLConfig   mEGLConfig;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    87
    private static int mGLMajor, mGLMinor;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    88
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
    89
    // Load the .so
15512
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
    90
    protected final static String[] libs = {
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
    91
        "SDL2",
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
    92
        "main",
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
    93
    };
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
    94
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
    95
    static{
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
    96
        for (String l : libs) {
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
    97
            try {
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
    98
                android.util.Log.d("org.hedgewars.hedgeroid.SDLActivity", "Hedgewars (class SDLActivity) is attempting to load library '" + l + "' ...");
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
    99
                System.loadLibrary(l);
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
   100
                android.util.Log.d("org.hedgewars.hedgeroid.SDLActivity", "Hedgewars (class SDLActivity) successfully loaded library '" + l + "'.");
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
   101
            } catch (Exception ex) {
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
   102
                android.util.Log.e("org.hedgewars.hedgeroid.SDLActivity", "Hedgewars (class SDLActivity) failed to load library '" + l + "':" + ex.getMessage());
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
   103
                throw ex;
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
   104
            }
45d6806548e9 hedgeroid: add debug logging to library loading
sheepluva
parents: 11046
diff changeset
   105
        }
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   106
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   107
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   108
    // Setup
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   109
    protected void onCreate(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   110
        super.onCreate(savedInstanceState);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   111
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   112
        // So we can call stuff from static callbacks
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   113
        mSingleton = this;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   114
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   115
        // Set up the surface
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   116
        mSurface = new SDLSurface(getApplication(), startConfig, startNetgame);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   117
        startConfig = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   118
        setContentView(mSurface);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   119
    }
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   120
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   121
    // Events
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   122
    protected void onPause() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   123
        Log.v("SDL", "onPause()");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   124
        super.onPause();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   125
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   126
        if(mEGLDisplay != null && mEGLContext != null){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   127
            EGL10 egl = (EGL10)EGLContext.getEGL();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   128
            egl.eglDestroyContext(mEGLDisplay, mEGLContext);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   129
            mEGLDisplay = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   130
            mEGLContext = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   131
        }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   132
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   133
        SDLActivity.nativePause();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   134
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   135
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   136
    protected void onResume() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   137
        Log.v("SDL", "onResume()");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   138
        super.onResume();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   139
    }
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   140
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   141
    protected void onDestroy() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   142
        super.onDestroy();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   143
        Log.v("SDL", "onDestroy()");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   144
        // Send a quit message to the application
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   145
        SDLActivity.nativeQuit();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   146
        // Now wait for the SDL thread to quit
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   147
        if (mSDLThread != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   148
            try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   149
                mSDLThread.join();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   150
            } catch(Exception e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   151
                Log.w("SDL", "Problem stopping thread: " + e);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   152
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   153
            mSDLThread = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   154
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   155
        mSingleton = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   156
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   157
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   158
    public static void synchronizedNativeInit(String...args) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   159
        synchronized(PascalExports.engineMutex) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   160
            nativeInit(args);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   161
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   162
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   163
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   164
    // C functions we call
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   165
    private static native void nativeInit(String...args);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   166
    public static native void nativeQuit();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   167
    public static native void nativePause();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   168
    public static native void nativeResume();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   169
    public static native void onNativeResize(int x, int y, int format);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   170
    public static native void onNativeKeyDown(int keycode);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   171
    public static native void onNativeKeyUp(int keycode);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   172
    public static native void onNativeTouch(int touchDevId, int pointerFingerId,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   173
            int action, float x,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   174
            float y, float p);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   175
    public static native void onNativeAccel(float x, float y, float z);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   176
    public static native void nativeRunAudioThread();
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   177
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   178
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   179
    // Java functions called from C
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   180
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   181
    public static boolean createGLContext(int majorVersion, int minorVersion) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   182
        return initEGL(majorVersion, minorVersion);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   183
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   184
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   185
    public static void flipBuffers() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   186
        flipEGL();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   187
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   188
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   189
    public static void setActivityTitle(final String title) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   190
        // Called from SDLMain() thread and can't directly affect the view
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   191
        mSingleton.runOnUiThread(new Runnable() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   192
            public void run() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   193
                mSingleton.setTitle(title);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   194
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   195
        });
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   196
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   197
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   198
    public static Context getContext() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   199
        return mSingleton;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   200
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   201
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   202
    public static void startApp(final int width, final int height, GameConfig config, boolean netgame) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   203
        // Start up the C app thread
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   204
        if (mSDLThread == null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   205
            mSDLThread = new Thread(new SDLMain(width, height, config, netgame));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   206
            mSDLThread.start();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   207
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   208
            SDLActivity.nativeResume();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   209
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   210
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   211
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   212
    // EGL functions
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   213
    public static boolean initEGL(int majorVersion, int minorVersion) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   214
        if (SDLActivity.mEGLDisplay == null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   215
            try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   216
                EGL10 egl = (EGL10)EGLContext.getEGL();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   217
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   218
                EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   219
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   220
                int[] version = new int[2];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   221
                egl.eglInitialize(dpy, version);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   222
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   223
                int EGL_OPENGL_ES_BIT = 1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   224
                int EGL_OPENGL_ES2_BIT = 4;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   225
                int renderableType = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   226
                if (majorVersion == 2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   227
                    renderableType = EGL_OPENGL_ES2_BIT;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   228
                } else if (majorVersion == 1) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   229
                    renderableType = EGL_OPENGL_ES_BIT;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   230
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   231
                int[] configSpec = {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   232
                        EGL10.EGL_RENDERABLE_TYPE, renderableType,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   233
                        EGL10.EGL_NONE
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   234
                };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   235
                EGLConfig[] configs = new EGLConfig[1];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   236
                int[] num_config = new int[1];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   237
                if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   238
                    Log.e("SDL", "No EGL config available");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   239
                    return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   240
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   241
                EGLConfig config = configs[0];
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   242
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   243
                SDLActivity.mEGLDisplay = dpy;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   244
                SDLActivity.mEGLConfig = config;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   245
                SDLActivity.mGLMajor = majorVersion;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   246
                SDLActivity.mGLMinor = minorVersion;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   247
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   248
                SDLActivity.createEGLSurface();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   249
            } catch(Exception e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   250
                Log.v("SDL", e + "");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   251
                for (StackTraceElement s : e.getStackTrace()) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   252
                    Log.v("SDL", s.toString());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   253
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   254
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   255
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   256
        else SDLActivity.createEGLSurface();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   257
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   258
        return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   259
    }
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   260
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   261
    public static boolean createEGLContext() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   262
        EGL10 egl = (EGL10)EGLContext.getEGL();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   263
        int EGL_CONTEXT_CLIENT_VERSION=0x3098;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   264
        int contextAttrs[] = new int[] { EGL_CONTEXT_CLIENT_VERSION, SDLActivity.mGLMajor, EGL10.EGL_NONE };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   265
        SDLActivity.mEGLContext = egl.eglCreateContext(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, EGL10.EGL_NO_CONTEXT, contextAttrs);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   266
        if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   267
            Log.e("SDL", "Couldn't create context");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   268
            return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   269
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   270
        return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   271
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   272
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   273
    public static boolean createEGLSurface() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   274
        if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLConfig != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   275
            EGL10 egl = (EGL10)EGLContext.getEGL();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   276
            if (SDLActivity.mEGLContext == null) createEGLContext();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   277
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   278
            Log.v("SDL", "Creating new EGL Surface");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   279
            EGLSurface surface = egl.eglCreateWindowSurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, SDLActivity.mSurface, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   280
            if (surface == EGL10.EGL_NO_SURFACE) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   281
                Log.e("SDL", "Couldn't create surface");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   282
                return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   283
            }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   284
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   285
            if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, surface, surface, SDLActivity.mEGLContext)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   286
                Log.e("SDL", "Old EGL Context doesnt work, trying with a new one");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   287
                createEGLContext();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   288
                if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, surface, surface, SDLActivity.mEGLContext)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   289
                    Log.e("SDL", "Failed making EGL Context current");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   290
                    return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   291
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   292
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   293
            SDLActivity.mEGLSurface = surface;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   294
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   295
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   296
        return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   297
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   298
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   299
    // EGL buffer flip
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   300
    public static void flipEGL() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   301
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   302
            EGL10 egl = (EGL10)EGLContext.getEGL();
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   303
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   304
            egl.eglWaitNative(EGL10.EGL_CORE_NATIVE_ENGINE, null);
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   305
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   306
            // drawing here
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   307
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   308
            egl.eglWaitGL();
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   309
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   310
            egl.eglSwapBuffers(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface);
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   311
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   312
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   313
        } catch(Exception e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   314
            Log.v("SDL", "flipEGL(): " + e);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   315
            for (StackTraceElement s : e.getStackTrace()) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   316
                Log.v("SDL", s.toString());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   317
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   318
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   319
    }
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   320
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   321
    // Audio
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   322
    private static Object buf;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   323
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   324
    public static Object audioInit(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   325
        int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   326
        int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   327
        int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   328
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   329
        Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + ((float)sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   330
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   331
        // Let the user pick a larger buffer if they really want -- but ye
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   332
        // gods they probably shouldn't, the minimums are horrifyingly high
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   333
        // latency already
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   334
        desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   335
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   336
        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   337
                channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   338
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   339
        audioStartThread();
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   340
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   341
        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");
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   342
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   343
        if (is16Bit) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   344
            buf = new short[desiredFrames * (isStereo ? 2 : 1)];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   345
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   346
            buf = new byte[desiredFrames * (isStereo ? 2 : 1)];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   347
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   348
        return buf;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   349
    }
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   350
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   351
    public static void audioStartThread() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   352
        mAudioThread = new Thread(new Runnable() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   353
            public void run() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   354
                mAudioTrack.play();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   355
                nativeRunAudioThread();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   356
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   357
        });
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   358
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   359
        // I'd take REALTIME if I could get it!
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   360
        mAudioThread.setPriority(Thread.MAX_PRIORITY);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   361
        mAudioThread.start();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   362
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   363
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   364
    public static void audioWriteShortBuffer(short[] buffer) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   365
        for (int i = 0; i < buffer.length; ) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   366
            int result = mAudioTrack.write(buffer, i, buffer.length - i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   367
            if (result > 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   368
                i += result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   369
            } else if (result == 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   370
                try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   371
                    Thread.sleep(1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   372
                } catch(InterruptedException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   373
                    // Nom nom
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   374
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   375
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   376
                Log.w("SDL", "SDL audio: error return from write(short)");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   377
                return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   378
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   379
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   380
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   381
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   382
    public static void audioWriteByteBuffer(byte[] buffer) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   383
        for (int i = 0; i < buffer.length; ) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   384
            int result = mAudioTrack.write(buffer, i, buffer.length - i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   385
            if (result > 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   386
                i += result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   387
            } else if (result == 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   388
                try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   389
                    Thread.sleep(1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   390
                } catch(InterruptedException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   391
                    // Nom nom
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   392
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   393
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   394
                Log.w("SDL", "SDL audio: error return from write(short)");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   395
                return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   396
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   397
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   398
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   399
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   400
    public static void audioQuit() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   401
        if (mAudioThread != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   402
            try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   403
                mAudioThread.join();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   404
            } catch(Exception e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   405
                Log.v("SDL", "Problem stopping audio thread: " + e);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   406
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   407
            mAudioThread = null;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   408
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   409
            //Log.v("SDL", "Finished waiting for audio thread");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   410
        }
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   411
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   412
        if (mAudioTrack != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   413
            mAudioTrack.stop();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   414
            mAudioTrack = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   415
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   416
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   417
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   418
    public static int getDensity(){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   419
        DisplayMetrics dm = SDLActivity.getContext().getResources().getDisplayMetrics();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   420
        return dm.densityDpi;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   421
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   422
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   423
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   424
/**
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   425
    Simple nativeInit() runnable
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   426
 */
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   427
class SDLMain implements Runnable {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   428
    public static final String TAG = "SDLMain";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   429
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   430
    public static final int RQ_LOWRES            = 0x00000001; // use half land array
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   431
    public static final int RQ_BLURRY_LAND       = 0x00000002; // downscaled terrain
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   432
    public static final int RQ_NO_BACKGROUND     = 0x00000004; // don't draw background
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   433
    public static final int RQ_SIMPLE_ROPE       = 0x00000008; // avoid drawing rope
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   434
    public static final int RQ_2D_WATER          = 0x00000010; // disabe 3D water effect
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   435
    public static final int RQ_SIMPLE_EXPLOSIONS = 0x00000020; // no fancy explosion effects
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   436
    public static final int RQ_NO_FLAKES         = 0x00000040; // no flakes
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   437
    public static final int RQ_NO_MENU_ANIM      = 0x00000080; // ammomenu appears with no animation
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   438
    public static final int RQ_NO_DROPLETS       = 0x00000100; // no droplets
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   439
    public static final int RQ_NO_CLAMPING       = 0x00000200; // don't clamp textures
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   440
    public static final int RQ_NO_TOOLTIPS       = 0x00000400; // tooltips are not drawn
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   441
    public static final int RQ_NO_VSYNC          = 0x00000800; // don't sync on vblank
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   442
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   443
    private final int surfaceWidth, surfaceHeight;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   444
    private final String playerName;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   445
    private final GameConfig config;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   446
    private final boolean netgame;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   447
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   448
    public SDLMain(int width, int height, GameConfig config, boolean netgame) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   449
        surfaceWidth = width;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   450
        surfaceHeight = height;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   451
        if(netgame) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   452
            playerName = Netplay.getAppInstance(SDLActivity.getContext().getApplicationContext()).getPlayerName();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   453
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   454
            playerName = "Player";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   455
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   456
        this.config = config;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   457
        this.netgame = netgame;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   458
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   459
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   460
    public void run() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   461
        //Set up the IPC socket server to communicate with the engine
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   462
        GameConnection gameConn;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   463
        String path;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   464
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   465
            if(netgame) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   466
                Netplay netplay = Netplay.getAppInstance(SDLActivity.mSingleton.getApplicationContext());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   467
                gameConn = GameConnection.forNetgame(config, netplay);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   468
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   469
                gameConn = GameConnection.forLocalGame(config);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   470
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   471
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   472
            path = FileUtils.getDataPathFile(SDLActivity.mSingleton).getAbsolutePath();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   473
            Log.d(TAG, "Starting engine");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   474
            // Runs SDL_main() with added parameters
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   475
            try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   476
                String pPort = String.valueOf(gameConn.port);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   477
                String pWidth = String.valueOf(surfaceWidth);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   478
                String pHeight = String.valueOf(surfaceHeight);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   479
                String pQuality = Integer.toString(RQ_NO_FLAKES|RQ_NO_DROPLETS|RQ_SIMPLE_EXPLOSIONS);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   480
                String pPlayerName = Base64.encodeToString(playerName.getBytes("UTF-8"), 0);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   481
                SDLActivity.synchronizedNativeInit(new String[] { pPort, pWidth, pHeight, pQuality, "en.txt", pPlayerName, "1", "1", "1", path, ""  });
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   482
            } catch (UnsupportedEncodingException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   483
                throw new AssertionError(e); // never happens
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   484
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   485
            Log.d(TAG, "Engine stopped");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   486
        } catch(ConnectException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   487
            Log.e(TAG, "Error starting IPC connection");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   488
        } catch (IOException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   489
            Log.e(TAG, "Missing SDCard");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   490
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   491
        SDLActivity.mSingleton.runOnUiThread(new Runnable() { public void run() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   492
            if(SDLActivity.mSingleton != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   493
                SDLActivity.mSingleton.finish();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   494
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   495
        }});
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   496
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   497
}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   498
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   499
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   500
/**
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   501
    SDLSurface. This is what we draw on, so we need to know when it's created
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   502
    in order to do anything useful.
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   503
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   504
    Because of this, that's where we set up the SDL thread
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   505
 */
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   506
class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   507
View.OnKeyListener, View.OnTouchListener, SensorEventListener  {
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   508
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   509
    private GameConfig config;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   510
    private boolean netgame;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   511
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   512
    // Sensors
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   513
    private static SensorManager mSensorManager;
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   514
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   515
    // Startup
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   516
    public SDLSurface(Context context, GameConfig _config, boolean netgame) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   517
        super(context);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   518
        getHolder().addCallback(this);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   519
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   520
        setFocusable(true);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   521
        setFocusableInTouchMode(true);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   522
        requestFocus();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   523
        setOnKeyListener(this);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   524
        setOnTouchListener(this);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   525
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   526
        mSensorManager = (SensorManager)context.getSystemService("sensor");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   527
        config = _config;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   528
        this.netgame = netgame;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   529
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   530
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   531
    // Called when we have a valid drawing surface
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   532
    public void surfaceCreated(SurfaceHolder holder) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   533
        Log.v("SDL", "surfaceCreated()");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   534
        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   535
        SDLActivity.createEGLSurface();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   536
        //      enableSensor(Sensor.TYPE_ACCELEROMETER, true);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   537
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   538
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   539
    // Called when we lose the surface
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   540
    public void surfaceDestroyed(SurfaceHolder holder) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   541
        Log.v("SDL", "surfaceDestroyed()");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   542
        SDLActivity.nativePause();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   543
        //      enableSensor(Sensor.TYPE_ACCELEROMETER, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   544
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   545
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   546
    // Called when the surface is resized
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   547
    public void surfaceChanged(SurfaceHolder holder,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   548
            int format, int width, int height) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   549
        Log.v("SDL", "surfaceChanged()");
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   550
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   551
        int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   552
        switch (format) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   553
        case PixelFormat.A_8:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   554
            Log.v("SDL", "pixel format A_8");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   555
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   556
        case PixelFormat.LA_88:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   557
            Log.v("SDL", "pixel format LA_88");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   558
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   559
        case PixelFormat.L_8:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   560
            Log.v("SDL", "pixel format L_8");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   561
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   562
        case PixelFormat.RGBA_4444:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   563
            Log.v("SDL", "pixel format RGBA_4444");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   564
            sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   565
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   566
        case PixelFormat.RGBA_5551:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   567
            Log.v("SDL", "pixel format RGBA_5551");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   568
            sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   569
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   570
        case PixelFormat.RGBA_8888:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   571
            Log.v("SDL", "pixel format RGBA_8888");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   572
            sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   573
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   574
        case PixelFormat.RGBX_8888:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   575
            Log.v("SDL", "pixel format RGBX_8888");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   576
            sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   577
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   578
        case PixelFormat.RGB_332:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   579
            Log.v("SDL", "pixel format RGB_332");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   580
            sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   581
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   582
        case PixelFormat.RGB_565:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   583
            Log.v("SDL", "pixel format RGB_565");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   584
            sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   585
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   586
        case PixelFormat.RGB_888:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   587
            Log.v("SDL", "pixel format RGB_888");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   588
            // Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   589
            sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   590
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   591
        default:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   592
            Log.v("SDL", "pixel format unknown " + format);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   593
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   594
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   595
        SDLActivity.onNativeResize(width, height, sdlFormat);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   596
        Log.v("SDL", "Window size:" + width + "x"+height);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   597
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   598
        SDLActivity.startApp(width, height, config, netgame);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   599
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   600
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   601
    // unused
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   602
    public void onDraw(Canvas canvas) {}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   603
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   604
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   605
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   606
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   607
    // Key events
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   608
    public boolean onKey(View  v, int keyCode, KeyEvent event) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   609
        switch(keyCode){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   610
        case KeyEvent.KEYCODE_BACK:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   611
            Log.d("SDL", "KEYCODE_BACK");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   612
            SDLActivity.nativeQuit();
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7568
diff changeset
   613
            return true;
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   614
        case KeyEvent.KEYCODE_VOLUME_DOWN:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   615
        case KeyEvent.KEYCODE_VOLUME_UP:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   616
        case KeyEvent.KEYCODE_VOLUME_MUTE:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   617
            return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   618
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   619
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   620
            //Log.v("SDL", "key down: " + keyCode);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   621
            SDLActivity.onNativeKeyDown(keyCode);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   622
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   623
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   624
        else if (event.getAction() == KeyEvent.ACTION_UP) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   625
            //Log.v("SDL", "key up: " + keyCode);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   626
            SDLActivity.onNativeKeyUp(keyCode);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   627
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   628
        }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   629
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   630
        return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   631
    }
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   632
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   633
    // Touch events
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   634
    public boolean onTouch(View v, MotionEvent event) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   635
        final int action = event.getAction() & MotionEvent.ACTION_MASK;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   636
        final int actionPointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   637
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   638
        if (action == MotionEvent.ACTION_MOVE) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   639
            // TODO send motion to every pointer if its position has
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   640
            // changed since prev event.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   641
            for (int i = 0; i < event.getPointerCount(); i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   642
                sendNativeTouch(event, action, i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   643
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   644
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   645
            sendNativeTouch(event, action, actionPointerIndex);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   646
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   647
        return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   648
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   649
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   650
    private static void sendNativeTouch(MotionEvent event, int action, int pointerIndex) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   651
        int touchDevId = event.getDeviceId();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   652
        int pointerFingerId = event.getPointerId(pointerIndex);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   653
        float x = event.getX(pointerIndex);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   654
        float y = event.getY(pointerIndex);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   655
        float pressure = event.getPressure(pointerIndex);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   656
        SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, pressure);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   657
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   658
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   659
    // Sensor events
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   660
    public void enableSensor(int sensortype, boolean enabled) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   661
        // TODO: This uses getDefaultSensor - what if we have >1 accels?
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   662
        if (enabled) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   663
            mSensorManager.registerListener(this,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   664
                    mSensorManager.getDefaultSensor(sensortype),
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   665
                    SensorManager.SENSOR_DELAY_GAME, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   666
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   667
            mSensorManager.unregisterListener(this,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   668
                    mSensorManager.getDefaultSensor(sensortype));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   669
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   670
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   671
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   672
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   673
        // TODO
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   674
    }
6599
1d3746138113 enable pause/resume on the java side
Xeli
parents: 6597
diff changeset
   675
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   676
    public void onSensorChanged(SensorEvent event) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   677
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   678
            SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   679
                    event.values[1] / SensorManager.GRAVITY_EARTH,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   680
                    event.values[2] / SensorManager.GRAVITY_EARTH);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   681
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9998
diff changeset
   682
    }
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   683
}
6597
814683bbd230 added TARDIS to minimal data dir, changed a couple of org_libsdl_app to org_hedgewars_hedgeroid in SDL_android.cpp and updated SDLActivity.java
Xeli
parents: 6487
diff changeset
   684