v0.7.4b: Initial LibGDX commit! more details below:

Large sections of game logic are now working through libgdx instead of
android libraries. There is still work to do but this is the first
major step. Big changes include:
- Graphics code is now through LibGDX (except for text rendering)
- Initialization and screen-handling logic is now mostly through LibGDX
- Audio is now through LibGDX
- Input handling is now through LibGDX
- Most misc functions are now through LibGDX
This commit is contained in:
Evan Debenham
2019-07-30 16:50:40 -04:00
parent f10be84a10
commit 2a523f2ea2
42 changed files with 828 additions and 972 deletions
@@ -22,20 +22,18 @@
package com.watabou.noosa.audio;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import com.badlogic.gdx.Gdx;
import com.watabou.noosa.Game;
public enum Music {
INSTANCE;
private MediaPlayer player;
private com.badlogic.gdx.audio.Music player;
private String lastPlayed;
private boolean looping;
@@ -58,25 +56,11 @@ public enum Music {
return;
}
try {
AssetFileDescriptor afd = Game.instance.getAssets().openFd( assetName );
MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType( AudioManager.STREAM_MUSIC );
mp.setDataSource( afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength() );
mp.prepare();
player = mp;
player.start();
player.setLooping(looping);
player.setVolume(volume, volume);
} catch (Exception e) {
Game.reportException(e);
player = null;
}
player = Gdx.audio.newMusic(Gdx.files.internal(assetName));
player.setLooping(looping);
player.setVolume(volume);
player.play();
}
public void mute() {
@@ -92,19 +76,15 @@ public enum Music {
public void resume() {
if (player != null) {
player.start();
player.play();
player.setLooping(looping);
}
}
public void stop() {
if (player != null) {
try {
player.stop();
player.release();
} catch ( Exception e ){
Game.reportException(e);
}
player.stop();
player.dispose();
player = null;
}
}
@@ -112,7 +92,7 @@ public enum Music {
public void volume( float value ) {
volume = value;
if (player != null) {
player.setVolume( value, value );
player.setVolume( value );
}
}
@@ -134,30 +114,29 @@ public enum Music {
return enabled;
}
public static final PhoneStateListener callMute = new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
if( state == TelephonyManager.CALL_STATE_RINGING ) {
INSTANCE.pause();
} else if( state == TelephonyManager.CALL_STATE_IDLE ) {
if (!Game.instance.isPaused()) {
INSTANCE.resume();
}
}
super.onCallStateChanged(state, incomingNumber);
}
};
//FIXME android-specific code, that is also broken by being part of this class.
public static void setMuteListener(){
//versions lower than this require READ_PHONE_STATE permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
TelephonyManager mgr =
(TelephonyManager) Game.instance.getSystemService(Activity.TELEPHONY_SERVICE);
mgr.listen(Music.callMute, PhoneStateListener.LISTEN_CALL_STATE);
mgr.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
if( state == TelephonyManager.CALL_STATE_RINGING ) {
INSTANCE.pause();
} else if( state == TelephonyManager.CALL_STATE_IDLE ) {
if (!Game.instance.isPaused()) {
INSTANCE.resume();
}
}
super.onCallStateChanged(state, incomingNumber);
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
@@ -21,113 +21,79 @@
package com.watabou.noosa.audio;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.watabou.noosa.Game;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
public enum Sample implements SoundPool.OnLoadCompleteListener {
public enum Sample {
INSTANCE;
public static final int MAX_STREAMS = 8;
protected SoundPool pool =
new SoundPool( MAX_STREAMS, AudioManager.STREAM_MUSIC, 0 );
protected HashMap<Object, Integer> ids =
new HashMap<>();
protected HashMap<Object, Sound> ids = new HashMap<>();
private boolean enabled = true;
private float volume = 1f;
private LinkedList<String> loadingQueue = new LinkedList<>();
private float globalVolume = 1f;
public void reset() {
for (Sound sound : ids.values()){
sound.dispose();
}
ids.clear();
loadingQueue = new LinkedList<>();
pool.release();
pool = new SoundPool( MAX_STREAMS, AudioManager.STREAM_MUSIC, 0 );
pool.setOnLoadCompleteListener( this );
}
public void pause() {
if (pool != null) {
pool.autoPause();
for (Sound sound : ids.values()) {
sound.pause();
}
}
public void resume() {
if (pool != null) {
pool.autoResume();
for (Sound sound : ids.values()) {
sound.resume();
}
}
public void load( String... assets ) {
for (String asset : assets) {
loadingQueue.add( asset );
}
loadNext();
}
private void loadNext() {
final String asset = loadingQueue.poll();
if (asset != null) {
if (!ids.containsKey( asset )) {
try {
pool.setOnLoadCompleteListener( new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loadNext();
}
} );
AssetManager manager = Game.instance.getAssets();
AssetFileDescriptor fd = manager.openFd( asset );
int streamID = pool.load( fd, 1 ) ;
ids.put( asset, streamID );
fd.close();
} catch (IOException e) {
loadNext();
} catch (NullPointerException e) {
// Do nothing (stop loading sounds)
}
} else {
loadNext();
//FIXME there used to be a queue here so that assets were loaded async.
//This was to prevent hanging on specific android versions (implement in vanilla v1.7.5)
//Maybe LibGDX already handles this?
for (String asset : assets){
if (!ids.containsKey(asset)){
ids.put(asset, Gdx.audio.newSound(Gdx.files.internal(asset)));
}
}
}
public void unload( Object src ) {
if (ids.containsKey( src )) {
pool.unload( ids.get( src ) );
ids.get( src ).dispose();
ids.remove( src );
}
}
public int play( Object id ) {
public long play( Object id ) {
return play( id, 1 );
}
public int play( Object id, float volume ) {
public long play( Object id, float volume ) {
return play( id, volume, volume, 1 );
}
public int play( Object id, float leftVolume, float rightVolume, float rate ) {
public long play( Object id, float volume, float pitch ) {
return play( id, volume, volume, pitch );
}
public long play( Object id, float leftVolume, float rightVolume, float pitch ) {
float volume = Math.max(leftVolume, rightVolume);
float pan = rightVolume - leftVolume;
if (enabled && ids.containsKey( id )) {
return pool.play( ids.get( id ), leftVolume*volume, rightVolume*volume, 0, 0, rate );
return ids.get(id).play( globalVolume*volume, pitch, pan );
} else {
return -1;
}
@@ -138,14 +104,11 @@ public enum Sample implements SoundPool.OnLoadCompleteListener {
}
public void volume( float value ) {
this.volume = value;
globalVolume = value;
}
public boolean isEnabled() {
return enabled;
}
@Override
public void onLoadComplete( SoundPool soundPool, int sampleId, int status ) {
}
}