Added support for Android Studio

This commit is contained in:
vladislav.khorev committed 2017-01-10 07:10:36 +03:00
1 parent e4a7822385
commit 99a6b3728d
45 files changed
+1834 -548

No files matched your search

@@ -0,0 +1,26 @@
package fishrungames.salmonengineandroid;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("fishrungames.salmonengineandroid", appContext.getPackageName());
}
}
+17
View File
@@ -0,0 +1,17 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fishrungames.salmonengineandroid">
<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:screenOrientation="landscape"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden"
android:noHistory="true">
</application>
<uses-feature android:glEsVersion="0x00020000"/>
</manifest>
+3
View File
@@ -0,0 +1,3 @@
#include <jni.h>
#include <string>
@@ -0,0 +1,448 @@
package fishrungames.salmonengineandroid;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.opengl.GLSurfaceView;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
//import android.net.Uri;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
public class EngineWrapper {
static class CustomGestureListener extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
EngineWrapper.OnFling(velocityX, velocityY, e2.getEventTime());
return true;
}
}
static class JniDestroyRunnable implements Runnable
{
public void run()
{
EngineWrapper.Destroy();
}
}
static ContextWrapper Instance = null;
static Map<String, Integer> FileMap = new HashMap<String, Integer>();
static Map<String, Integer> SoundMap = new HashMap<String, Integer>();
static Context context;
static SoundPool soundPool;
static MediaPlayer mp;
static GLSurfaceView mView;
static GestureDetector customGestureDetector = new GestureDetector(new CustomGestureListener());
public static void SetActivityInstance(ContextWrapper inst)
{
Instance = inst;
context = Instance.getBaseContext();
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mp = new MediaPlayer();
}
public static void SetView(GLSurfaceView view)
{
mView = view;
}
public static String GetApplicationDir()
{
return Instance.getFilesDir().getPath();
}
public static void AddToFileMap(String s, Integer i)
{
FileMap.put(s, i);
}
public synchronized static int OpenFile(String fileName)
{
String newFileName;
int i = fileName.lastIndexOf(".");
if (i == -1)
{
newFileName = fileName;
}
else
{
newFileName = fileName.substring(0, i);
}
if (!FileMap.containsKey(newFileName))
{
return 0;
}
Integer id = FileMap.get(newFileName);
try
{
InputStream is = Instance.getResources().openRawResource(id);
byte[] buffer = new byte[256];
int bytesRead;
CreateFile(is.available());
while ((bytesRead = is.read(buffer, 0, 256)) != -1)
{
WriteToFile(buffer, bytesRead);
}
} catch (Throwable t) {
ConsoleOut("Exception in FileWrapper::OpenFile!\n");
return 0;
}
return 1;
}
public static native void SetupEnviroment();
public static native void SetupApkFilePath(String s);
public static native void ConsoleOut(String s);
public static native void CreateFile(int fileSize);
public static native void WriteToFile(byte [] buffer, int bufferSize);
public static void LoadSound(String soundFileName)
{
if (SoundMap.containsKey(soundFileName))
{
return;
}
/*
String newFileName;
int i = soundFileName.lastIndexOf(".");
if (i == -1)
{
newFileName = soundFileName;
}
else
{
newFileName = soundFileName.substring(0, i);
}*/
//int soundId = soundPool.load(context, FileMap.get(newFileName), 1);
AssetFileDescriptor afd = null;
try
{
afd = Instance.getAssets().openFd(soundFileName);
} catch (IOException e)
{
e.printStackTrace();
}
int soundId = soundPool.load(afd, 1);
SoundMap.put(soundFileName, soundId);
}
public static void ReleaseAllSoundsAndMusic()
{
mp.stop();
mp.reset();
//soundPool.autoPause();
Enumeration<Integer> i = Collections.enumeration(SoundMap.values());
while(i.hasMoreElements())
{
soundPool.stop(i.nextElement());
}
SoundMap.clear();
}
public static void PauseAllSoundsAndMusic()
{
//NIU right now
soundPool.autoPause();
mp.pause();
}
public static void ResumeAllSoundsAndMusic()
{
//NIU right now
soundPool.autoResume();
mp.start();
}
public static void StopMusic()
{
mp.stop();
mp.reset();
}
public static void PlayMusic(String musicFileName)
{
InnerPlayMusic(musicFileName, false);
}
public static void PlayMusicLooped(String musicFileName)
{
InnerPlayMusic(musicFileName, true);
}
private static void InnerPlayMusic(String musicFileName, boolean looped)
{
/*
String newFileName;
int i = musicFileName.lastIndexOf(".");
if (i == -1)
{
newFileName = musicFileName;
}
else
{
newFileName = musicFileName.substring(0, i);
}
*/
try
{
//Uri path = Uri.parse("android.resource://"+Instance.getPackageName()+"/assets/"+newFileName);
AssetFileDescriptor afd = null;
try
{
afd = Instance.getAssets().openFd(musicFileName);
} catch (IOException e)
{
e.printStackTrace();
}
//mp.setDataSource(context, path);
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.setLooping(looped);
mp.start();
} catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void PlaySound(String soundFileName)
{
if (!SoundMap.containsKey(soundFileName))
{
return;
}
int soundId = SoundMap.get(soundFileName);
soundPool.play(soundId, 1,1,1,0,1);
}
public static void LoadSalmonEngineLibrary()
{
System.loadLibrary("engine");
}
static
{
//System.loadLibrary("gnustl_shared");
}
//Application methods
public static native void Update(long dt);
public static native void Destroy();
public static native int IsInited();
public static native void OnTapDown(float x, float y, long time);
public static native void OnTapUp(float x, float y, long time);
public static native void OnTapUpAfterMove(float x, float y, long time);
public static native void OnTapMove(float x, float y, long time);
public static native void OnFling(float velocityX, float velocityY, long time);
public static native void OnScroll(float distanceX, float distanceY, long time);
public static native void OnKeyPress(int keyCode);
public static void CallDestroy()
{
mView.queueEvent(new JniDestroyRunnable());
}
public static void ProcessKeyDown(int keyCode, KeyEvent event)
{
int ascii_keycode = keyCode;
if (keyCode == KeyEvent.KEYCODE_DEL)
{
ascii_keycode = 8; //Hack - getUnicodeChar does not recognize backspace
}
else
{
ascii_keycode = event.getUnicodeChar();
}
OnKeyPress(ascii_keycode);
}
static boolean IsScrolling = false;
static float PrevMovePosX = 0;
static float PrevMovePosY = 0;
static boolean EnableFlingGesture = false;
public static void ProcessTouchEvent(MotionEvent event)
{
if (EnableFlingGesture)
{
if (customGestureDetector.onTouchEvent(event))
{
return;
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
float x = event.getX();
float y = (float) mView.getHeight() - event.getY();
float oldX = PrevMovePosX;
float oldY = (float) mView.getHeight() - PrevMovePosY;
float shiftX = x - oldX;
float shiftY = y - oldY;
if (Math.abs(shiftX) > 0.001f || Math.abs(shiftY) > 0.001f)
{
OnScroll(-shiftX, -shiftY, event.getEventTime());
}
PrevMovePosX = event.getX();
PrevMovePosY = event.getY();
IsScrolling = true;
}
if (event.getAction() == MotionEvent.ACTION_UP)
{
float x = event.getX();
float y = (float) mView.getHeight() - event.getY();
if (IsScrolling)
{
IsScrolling = false;
OnTapUpAfterMove(x, y, event.getEventTime());
}
else
{
OnTapUp(x, y, event.getEventTime());
}
}
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
float x = event.getX();
float y = (float) mView.getHeight() - event.getY();
PrevMovePosX = event.getX();
PrevMovePosY = event.getY();
OnTapDown(x, y, event.getEventTime());
}
}
}
@@ -0,0 +1,289 @@
package fishrungames.salmonengineandroid;
import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.util.Log;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
/*
This is a OpenGL View template. Do not change this code - derive your own class instead!!
*/
public class GLViewAncestor extends GLSurfaceView
{
private static String TAG = "GL2JNIView";
private static final boolean DEBUG = false;
public GLViewAncestor(Context context)
{
super(context);
}
public void init(boolean translucent, int depth, int stencil)
{
/*
* By default, GLSurfaceView() creates a RGB_565 opaque surface. If we
* want a translucent one, we should change the surface's format here,
* using PixelFormat.TRANSLUCENT for GL Surfaces is interpreted as any
* 32-bit surface with alpha by SurfaceFlinger.
*/
if (translucent)
{
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
/*
* Setup the context factory for 2.0 rendering. See ContextFactory class
* definition below
*/
setEGLContextFactory(new ContextFactory());
/*
* We need to choose an EGLConfig that matches the format of our surface
* exactly. This is going to be done in our custom config chooser. See
* ConfigChooser class definition below.
*/
setEGLConfigChooser(translucent ? new ConfigChooser(8, 8, 8, 8, depth,
stencil) : new ConfigChooser(5, 6, 5, 0, depth, stencil));
}
private static class ContextFactory implements
GLSurfaceView.EGLContextFactory
{
private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
@Override
public EGLContext createContext(EGL10 egl, EGLDisplay display,
EGLConfig eglConfig)
{
Log.w(TAG, "creating OpenGL ES 2.0 context");
checkEglError("Before eglCreateContext", egl);
int[] attrib_list =
{ EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
EGLContext context = egl.eglCreateContext(display, eglConfig,
EGL10.EGL_NO_CONTEXT, attrib_list);
checkEglError("After eglCreateContext", egl);
return context;
}
@Override
public void destroyContext(EGL10 egl, EGLDisplay display,
EGLContext context)
{
egl.eglDestroyContext(display, context);
}
}
private static void checkEglError(String prompt, EGL10 egl)
{
int error;
while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS)
{
Log.e(TAG, String.format("%s: EGL error: 0x%x", prompt, error));
}
}
private static class ConfigChooser implements
GLSurfaceView.EGLConfigChooser
{
public ConfigChooser(int r, int g, int b, int a, int depth, int stencil)
{
mRedSize = r;
mGreenSize = g;
mBlueSize = b;
mAlphaSize = a;
mDepthSize = depth;
mStencilSize = stencil;
}
/*
* This EGL config specification is used to specify 2.0 rendering. We
* use a minimum size of 4 bits for red/green/blue, but will perform
* actual matching in chooseConfig() below.
*/
private static int EGL_OPENGL_ES2_BIT = 4;
private static int[] s_configAttribs2 =
{ EGL10.EGL_RED_SIZE, 4, EGL10.EGL_GREEN_SIZE, 4,
EGL10.EGL_BLUE_SIZE, 4, EGL10.EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT, EGL10.EGL_NONE };
@Override
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display)
{
/*
* Get the number of minimally matching EGL configurations
*/
int[] num_config = new int[1];
egl.eglChooseConfig(display, s_configAttribs2, null, 0, num_config);
int numConfigs = num_config[0];
if (numConfigs <= 0)
{
throw new IllegalArgumentException(
"No configs match configSpec");
}
/*
* Allocate then read the array of minimally matching EGL configs
*/
EGLConfig[] configs = new EGLConfig[numConfigs];
egl.eglChooseConfig(display, s_configAttribs2, configs, numConfigs,
num_config);
if (DEBUG)
{
printConfigs(egl, display, configs);
}
/*
* Now return the "best" one
*/
return chooseConfig(egl, display, configs);
}
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
EGLConfig[] configs)
{
for (EGLConfig config : configs)
{
int d = findConfigAttrib(egl, display, config,
EGL10.EGL_DEPTH_SIZE, 0);
int s = findConfigAttrib(egl, display, config,
EGL10.EGL_STENCIL_SIZE, 0);
// We need at least mDepthSize and mStencilSize bits
if (d < mDepthSize || s < mStencilSize)
continue;
// We want an *exact* match for red/green/blue/alpha
int r = findConfigAttrib(egl, display, config,
EGL10.EGL_RED_SIZE, 0);
int g = findConfigAttrib(egl, display, config,
EGL10.EGL_GREEN_SIZE, 0);
int b = findConfigAttrib(egl, display, config,
EGL10.EGL_BLUE_SIZE, 0);
int a = findConfigAttrib(egl, display, config,
EGL10.EGL_ALPHA_SIZE, 0);
if (r == mRedSize && g == mGreenSize && b == mBlueSize
&& a == mAlphaSize)
return config;
}
return null;
}
private int findConfigAttrib(EGL10 egl, EGLDisplay display,
EGLConfig config, int attribute, int defaultValue)
{
if (egl.eglGetConfigAttrib(display, config, attribute, mValue))
{
return mValue[0];
}
return defaultValue;
}
private void printConfigs(EGL10 egl, EGLDisplay display,
EGLConfig[] configs)
{
int numConfigs = configs.length;
Log.w(TAG, String.format("%d configurations", numConfigs));
for (int i = 0; i < numConfigs; i++)
{
Log.w(TAG, String.format("Configuration %d:\n", i));
printConfig(egl, display, configs[i]);
}
}
private void printConfig(EGL10 egl, EGLDisplay display, EGLConfig config)
{
int[] attributes =
{
EGL10.EGL_BUFFER_SIZE,
EGL10.EGL_ALPHA_SIZE,
EGL10.EGL_BLUE_SIZE,
EGL10.EGL_GREEN_SIZE,
EGL10.EGL_RED_SIZE,
EGL10.EGL_DEPTH_SIZE,
EGL10.EGL_STENCIL_SIZE,
EGL10.EGL_CONFIG_CAVEAT,
EGL10.EGL_CONFIG_ID,
EGL10.EGL_LEVEL,
EGL10.EGL_MAX_PBUFFER_HEIGHT,
EGL10.EGL_MAX_PBUFFER_PIXELS,
EGL10.EGL_MAX_PBUFFER_WIDTH,
EGL10.EGL_NATIVE_RENDERABLE,
EGL10.EGL_NATIVE_VISUAL_ID,
EGL10.EGL_NATIVE_VISUAL_TYPE,
0x3030, // EGL10.EGL_PRESERVED_RESOURCES,
EGL10.EGL_SAMPLES,
EGL10.EGL_SAMPLE_BUFFERS,
EGL10.EGL_SURFACE_TYPE,
EGL10.EGL_TRANSPARENT_TYPE,
EGL10.EGL_TRANSPARENT_RED_VALUE,
EGL10.EGL_TRANSPARENT_GREEN_VALUE,
EGL10.EGL_TRANSPARENT_BLUE_VALUE,
0x3039, // EGL10.EGL_BIND_TO_TEXTURE_RGB,
0x303A, // EGL10.EGL_BIND_TO_TEXTURE_RGBA,
0x303B, // EGL10.EGL_MIN_SWAP_INTERVAL,
0x303C, // EGL10.EGL_MAX_SWAP_INTERVAL,
EGL10.EGL_LUMINANCE_SIZE, EGL10.EGL_ALPHA_MASK_SIZE,
EGL10.EGL_COLOR_BUFFER_TYPE, EGL10.EGL_RENDERABLE_TYPE,
0x3042 // EGL10.EGL_CONFORMANT
};
String[] names =
{ "EGL_BUFFER_SIZE", "EGL_ALPHA_SIZE", "EGL_BLUE_SIZE",
"EGL_GREEN_SIZE", "EGL_RED_SIZE", "EGL_DEPTH_SIZE",
"EGL_STENCIL_SIZE", "EGL_CONFIG_CAVEAT",
"EGL_CONFIG_ID", "EGL_LEVEL", "EGL_MAX_PBUFFER_HEIGHT",
"EGL_MAX_PBUFFER_PIXELS", "EGL_MAX_PBUFFER_WIDTH",
"EGL_NATIVE_RENDERABLE", "EGL_NATIVE_VISUAL_ID",
"EGL_NATIVE_VISUAL_TYPE", "EGL_PRESERVED_RESOURCES",
"EGL_SAMPLES", "EGL_SAMPLE_BUFFERS",
"EGL_SURFACE_TYPE", "EGL_TRANSPARENT_TYPE",
"EGL_TRANSPARENT_RED_VALUE",
"EGL_TRANSPARENT_GREEN_VALUE",
"EGL_TRANSPARENT_BLUE_VALUE",
"EGL_BIND_TO_TEXTURE_RGB", "EGL_BIND_TO_TEXTURE_RGBA",
"EGL_MIN_SWAP_INTERVAL", "EGL_MAX_SWAP_INTERVAL",
"EGL_LUMINANCE_SIZE", "EGL_ALPHA_MASK_SIZE",
"EGL_COLOR_BUFFER_TYPE", "EGL_RENDERABLE_TYPE",
"EGL_CONFORMANT" };
int[] value = new int[1];
for (int i = 0; i < attributes.length; i++)
{
int attribute = attributes[i];
String name = names[i];
if (egl.eglGetConfigAttrib(display, config, attribute, value))
{
Log.w(TAG, String.format(" %s: %d\n", name, value[0]));
} else
{
// Log.w(TAG, String.format(" %s: failed\n", name));
while (egl.eglGetError() != EGL10.EGL_SUCCESS)
;
}
}
}
// Subclasses can adjust these values:
protected int mRedSize;
protected int mGreenSize;
protected int mBlueSize;
protected int mAlphaSize;
protected int mDepthSize;
protected int mStencilSize;
private int[] mValue = new int[1];
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
+3
View File
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">SalmonEngineAndroid</string>
</resources>
+11
View File
@@ -0,0 +1,11 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
</resources>
@@ -0,0 +1,17 @@
package fishrungames.salmonengineandroid;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}