Commit 67c481b2 authored by Eric Seidel's avatar Eric Seidel

Teach Asteroids Sounds how to load from AssetBundle

@abarth
parent 4cae568f
...@@ -48,8 +48,8 @@ main() async { ...@@ -48,8 +48,8 @@ main() async {
_app = new GameDemoApp(); _app = new GameDemoApp();
_sounds["explosion"] = new SoundEffect('https://github.com/slembcke/GalacticGuardian.spritebuilder/raw/GDC/Packages/SpriteBuilder%20Resources.sbpack/TempSounds/Explosion.wav'); _sounds["explosion"] = new SoundEffect(_bundle.load('assets/explosion.wav'));
_sounds["laser"] = new SoundEffect('https://github.com/slembcke/GalacticGuardian.spritebuilder/raw/GDC/Packages/SpriteBuilder%20Resources.sbpack/TempSounds/Laser.wav'); _sounds["laser"] = new SoundEffect(_bundle.load('assets/laser.wav'));
await _sounds["explosion"].load(); await _sounds["explosion"].load();
await _sounds["laser"].load(); await _sounds["laser"].load();
......
...@@ -6,16 +6,15 @@ part of sprites; ...@@ -6,16 +6,15 @@ part of sprites;
typedef void SoundEffectStreamCallback(SoundEffectStream); typedef void SoundEffectStreamCallback(SoundEffectStream);
class SoundEffect { class SoundEffect {
SoundEffect(this._url); SoundEffect(this._pipeFuture);
// TODO: Remove load method from SoundEffect // TODO: Remove load method from SoundEffect
Future load() async { Future load() async {
UrlResponse response = await fetchUrl(_url); _data = await _pipeFuture;
_data = response.body;
} }
String _url; Future<MojoDataPipeConsumer> _pipeFuture;
Object _data; MojoDataPipeConsumer _data;
} }
class SoundEffectStream { class SoundEffectStream {
......
...@@ -54,7 +54,6 @@ class SoundManager { ...@@ -54,7 +54,6 @@ class SoundManager {
static void purgeSharedInstance() { static void purgeSharedInstance() {
if (_sharedSoundManager == null) return; if (_sharedSoundManager == null) return;
_sharedSoundManager._running = false;
_sharedSoundManager = null; _sharedSoundManager = null;
} }
...@@ -72,7 +71,6 @@ class SoundManager { ...@@ -72,7 +71,6 @@ class SoundManager {
bool enableBackgroundMusic; bool enableBackgroundMusic;
bool enableSoundEffects; bool enableSoundEffects;
bool _running = true;
int _lastTimeStamp; int _lastTimeStamp;
void playEvent(SoundEvent evt, [double volume = 1.0, double pitch = 1.0, double pan = 0.0]) { void playEvent(SoundEvent evt, [double volume = 1.0, double pitch = 1.0, double pan = 0.0]) {
......
...@@ -10,6 +10,7 @@ import 'dart:math' as math; ...@@ -10,6 +10,7 @@ import 'dart:math' as math;
import 'dart:typed_data'; import 'dart:typed_data';
import 'dart:sky'; import 'dart:sky';
import 'package:mojo/core.dart';
import 'package:mojo/mojo/url_response.mojom.dart'; import 'package:mojo/mojo/url_response.mojom.dart';
import 'package:sky/animation/curves.dart'; import 'package:sky/animation/curves.dart';
import 'package:sky/base/scheduler.dart' as scheduler; import 'package:sky/base/scheduler.dart' as scheduler;
......
...@@ -18,6 +18,7 @@ abstract class AssetBundle { ...@@ -18,6 +18,7 @@ abstract class AssetBundle {
void close(); void close();
ImageResource loadImage(String key); ImageResource loadImage(String key);
Future<String> loadString(String key); Future<String> loadString(String key);
Future<core.MojoDataPipeConsumer> load(String key);
} }
class NetworkAssetBundle extends AssetBundle { class NetworkAssetBundle extends AssetBundle {
...@@ -27,13 +28,15 @@ class NetworkAssetBundle extends AssetBundle { ...@@ -27,13 +28,15 @@ class NetworkAssetBundle extends AssetBundle {
void close() { } void close() { }
ImageResource loadImage(String key) { String _urlFromKey(String key) => _baseUrl.resolve(key).toString();
return image_cache.load(_baseUrl.resolve(key).toString());
}
Future<String> loadString(String key) { Future<core.MojoDataPipeConsumer> load(String key) async {
return fetchString(_baseUrl.resolve(key).toString()); return (await fetchUrl(_urlFromKey(key))).body;
} }
ImageResource loadImage(String key) => image_cache.load(_urlFromKey(key));
Future<String> loadString(String key) => fetchString(_urlFromKey(key));
} }
Future _fetchAndUnpackBundle(String relativeUrl, AssetBundleProxy bundle) async { Future _fetchAndUnpackBundle(String relativeUrl, AssetBundleProxy bundle) async {
...@@ -66,19 +69,23 @@ class MojoAssetBundle extends AssetBundle { ...@@ -66,19 +69,23 @@ class MojoAssetBundle extends AssetBundle {
ImageResource loadImage(String key) { ImageResource loadImage(String key) {
return _imageCache.putIfAbsent(key, () { return _imageCache.putIfAbsent(key, () {
Completer<sky.Image> completer = new Completer<sky.Image>(); Completer<sky.Image> completer = new Completer<sky.Image>();
_bundle.ptr.getAsStream(key).then((response) { load(key).then((assetData) {
new sky.ImageDecoder(response.assetData.handle.h, completer.complete); new sky.ImageDecoder(assetData.handle.h, completer.complete);
}); });
return new ImageResource(completer.future); return new ImageResource(completer.future);
}); });
} }
Future<String> _fetchString(String key) async { Future<String> _fetchString(String key) async {
core.MojoDataPipeConsumer pipe = (await _bundle.ptr.getAsStream(key)).assetData; core.MojoDataPipeConsumer pipe = await load(key);
ByteData data = await core.DataPipeDrainer.drainHandle(pipe); ByteData data = await core.DataPipeDrainer.drainHandle(pipe);
return new String.fromCharCodes(new Uint8List.view(data.buffer)); return new String.fromCharCodes(new Uint8List.view(data.buffer));
} }
Future<core.MojoDataPipeConsumer> load(String key) async {
return (await _bundle.ptr.getAsStream(key)).assetData;
}
Future<String> loadString(String key) { Future<String> loadString(String key) {
return _stringCache.putIfAbsent(key, () => _fetchString(key)); return _stringCache.putIfAbsent(key, () => _fetchString(key));
} }
......
...@@ -12,6 +12,8 @@ import 'package:mojo_services/mojo/network_service.mojom.dart'; ...@@ -12,6 +12,8 @@ import 'package:mojo_services/mojo/network_service.mojom.dart';
import 'package:mojo_services/mojo/url_loader.mojom.dart'; import 'package:mojo_services/mojo/url_loader.mojom.dart';
import 'package:sky/mojo/shell.dart' as shell; import 'package:sky/mojo/shell.dart' as shell;
export 'package:mojo/mojo/url_response.mojom.dart' show UrlResponse;
NetworkServiceProxy _initNetworkService() { NetworkServiceProxy _initNetworkService() {
NetworkServiceProxy networkService = new NetworkServiceProxy.unbound(); NetworkServiceProxy networkService = new NetworkServiceProxy.unbound();
shell.requestService("mojo:authenticated_network_service", networkService); shell.requestService("mojo:authenticated_network_service", networkService);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment