added cpp functions for sounds

This commit is contained in:
Artem Budarin 2018-08-12 19:16:12 +05:00
parent 218ac42d6e
commit e7fc943114
7 changed files with 90 additions and 53 deletions

37
game/Sounds.cpp Executable file
View File

@ -0,0 +1,37 @@
#include "Sounds.h"
#include <jni.h>
static JNIEnv* env = NULL;
static jclass jSounds = NULL;
/** BackgroundSound */
void playBackgroundSound() {
jmethodID method = env->GetStaticMethodID(jSounds, "JniPlayBackgroundSound", "()V");
env->CallStaticVoidMethod(jSounds, method);
}
void stopBackgroundSound () {
jmethodID method = env->GetStaticMethodID(jSounds, "JniStopBackgroundSound", "()V");
env->CallStaticVoidMethod(jSounds, method);
}
/** GameSound - Gunshot */
void playGameSoundGunshot() {
jmethodID method = env->GetStaticMethodID(jSounds, "JniPlayGunshotSound", "()V");
env->CallStaticVoidMethod(jSounds, method);
}
void stopGameSoundGunshot() {
jmethodID method = env->GetStaticMethodID(jSounds, "JniStopGunshotSound", "()V");
env->CallStaticVoidMethod(jSounds, method);
}
JNIEXPORT void JNICALL Java_fishrungames_doublehitballs_sounds_JniSoundCalls_initJniSounds(JNIEnv *pEnv, jobject pThis) {
env = pEnv;
jSounds = env->FindClass("fishrungames/doublehitballs/sounds/JniSoundCalls");
playBackgroundSound();
playGameSoundGunshot();
stopBackgroundSound();
}

7
game/Sounds.h Executable file
View File

@ -0,0 +1,7 @@
#include <jni.h>
extern "C" {
JNIEXPORT void JNICALL Java_fishrungames_doublehitballs_sounds_JniSoundCalls_initJniSounds(JNIEnv *pEnv, jobject pThis);
}

View File

@ -42,6 +42,7 @@ add_library(DoubleHitBalls SHARED
${CMAKE_CURRENT_SOURCE_DIR}/../../game/gamecode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../game/gamecode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../game/loadingcode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../game/loadingcode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../game/menucode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../game/menucode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../game/Sounds.cpp
) )

View File

@ -9,6 +9,7 @@ import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL10;
import fishrungames.doublehitballs.sounds.JniSoundCalls;
import fishrungames.salmonengineandroid.GLViewAncestor; import fishrungames.salmonengineandroid.GLViewAncestor;
import fishrungames.salmonengineandroid.EngineWrapper; import fishrungames.salmonengineandroid.EngineWrapper;
@ -59,6 +60,7 @@ class GLView extends GLViewAncestor
public void onSurfaceChanged(GL10 gl, int width, int height) public void onSurfaceChanged(GL10 gl, int width, int height)
{ {
JniWrapper.Init(width,height); JniWrapper.Init(width,height);
JniSoundCalls.init();
} }
public void onSurfaceCreated(GL10 gl, EGLConfig config) public void onSurfaceCreated(GL10 gl, EGLConfig config)

View File

@ -6,7 +6,6 @@ public class JniWrapper
System.loadLibrary("DoubleHitBalls"); System.loadLibrary("DoubleHitBalls");
} }
public static native void Init(int width, int height); public static native void Init(int width, int height);
} }

View File

@ -1,11 +1,49 @@
package fishrungames.doublehitballs.sounds; package fishrungames.doublehitballs.sounds;
public interface JniSoundCalls { import android.media.MediaPlayer;
void playBackgroundSound(); import fishrungames.doublehitballs.GL2JNIActivity;
void stopBackgroundSound(); import fishrungames.doublehitballs.R;
void playGunshotSound(); /**
void stopGunshotSound(); * example sounds for JNI calls
* @author Artem Budarin
* Created by Artem Budarin on 06.08.2018.
*/
public class JniSoundCalls {
native public static void initJniSounds();
private static MediaPlayer backgroundPlayer = null;
private static MediaPlayer gunshotPlayer = null;
public static void init() {
initJniSounds();
}
public static void JniPlayBackgroundSound() {
backgroundPlayer = MediaPlayer.create(GL2JNIActivity.getInstance(), R.raw.background_sound);
backgroundPlayer.setLooping(true);
backgroundPlayer.start();
}
public static void JniStopBackgroundSound() {
if (backgroundPlayer != null) {
backgroundPlayer.stop();
}
}
public static void JniPlayGunshotSound() {
gunshotPlayer = MediaPlayer.create(GL2JNIActivity.getInstance(), R.raw.gunshot_sound);
gunshotPlayer.setLooping(false);
gunshotPlayer.start();
}
public static void JniStopGunshotSound() {
if (gunshotPlayer != null) {
gunshotPlayer.stop();
}
}
} }

View File

@ -1,47 +0,0 @@
package fishrungames.doublehitballs.sounds;
import android.media.MediaPlayer;
import fishrungames.doublehitballs.GL2JNIActivity;
import fishrungames.doublehitballs.R;
/**
* example sounds for JNI calls
* @author Artem Budarin
* Created by Artem Budarin on 06.08.2018.
*/
public class JniSoundCallsImpl implements JniSoundCalls {
private MediaPlayer backgroundPlayer = null;
private MediaPlayer gunshotPlayer = null;
@Override
public void playBackgroundSound() {
backgroundPlayer = MediaPlayer.create(GL2JNIActivity.getInstance(), R.raw.background_sound);
backgroundPlayer.setLooping(true);
backgroundPlayer.start();
}
@Override
public void stopBackgroundSound() {
if (backgroundPlayer != null) {
backgroundPlayer.stop();
}
}
@Override
public void playGunshotSound() {
gunshotPlayer = MediaPlayer.create(GL2JNIActivity.getInstance(), R.raw.gunshot_sound);
gunshotPlayer.setLooping(false);
gunshotPlayer.start();
}
@Override
public void stopGunshotSound() {
if (gunshotPlayer != null) {
gunshotPlayer.stop();
}
}
}