Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
a0111086
Commit
a0111086
authored
Aug 13, 2015
by
Viktor Lidholt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #598 from vlidholt/master
Adds rough sound support for sprites
parents
602dbbee
b529445f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
184 additions
and
1 deletion
+184
-1
game_demo_world.dart
examples/game/lib/game_demo_world.dart
+9
-1
main.dart
examples/game/lib/main.dart
+8
-0
sound.dart
examples/game/lib/sound.dart
+162
-0
sprites.dart
examples/game/lib/sprites.dart
+5
-0
No files found.
examples/game/lib/game_demo_world.dart
View file @
a0111086
...
...
@@ -28,6 +28,10 @@ class GameDemoWorld extends NodeWithSize {
SpriteSheet
_spriteSheet
;
SpriteSheet
_spriteSheetUI
;
Map
<
String
,
SoundEffect
>
_sounds
;
SoundPool
_soundPool
=
SoundPool
.
sharedInstance
();
Navigator
_navigator
;
// Inputs
...
...
@@ -54,7 +58,7 @@ class GameDemoWorld extends NodeWithSize {
Function
_gameOverCallback
;
GameDemoWorld
(
App
app
,
this
.
_navigator
,
ImageMap
images
,
this
.
_spriteSheet
,
this
.
_spriteSheetUI
,
this
.
_gameOverCallback
)
:
super
(
new
Size
(
_gameSizeWidth
,
_gameSizeHeight
))
{
GameDemoWorld
(
App
app
,
this
.
_navigator
,
ImageMap
images
,
this
.
_spriteSheet
,
this
.
_spriteSheetUI
,
this
.
_
sounds
,
this
.
_
gameOverCallback
)
:
super
(
new
Size
(
_gameSizeWidth
,
_gameSizeHeight
))
{
// Fetch images
_imgNebula
=
images
[
"assets/nebula.png"
];
...
...
@@ -140,6 +144,8 @@ class GameDemoWorld extends NodeWithSize {
laser
.
constrainProportions
=
true
;
_lasers
.
add
(
laser
);
_gameLayer
.
addChild
(
laser
);
_soundPool
.
play
(
_sounds
[
"laser"
]);
}
void
addNebula
()
{
...
...
@@ -225,6 +231,8 @@ class GameDemoWorld extends NodeWithSize {
}
_gameLayer
.
addChild
(
explosionNode
);
_soundPool
.
play
(
_sounds
[
"explosion"
]);
}
void
update
(
double
dt
)
{
...
...
examples/game/lib/main.dart
View file @
a0111086
...
...
@@ -28,6 +28,7 @@ ImageMap _loader;
SpriteSheet
_spriteSheet
;
SpriteSheet
_spriteSheetUI
;
GameDemoApp
_app
;
Map
<
String
,
SoundEffect
>
_sounds
=
{};
main
()
async
{
_loader
=
new
ImageMap
(
_bundle
);
...
...
@@ -47,6 +48,12 @@ main() async {
_app
=
new
GameDemoApp
();
_sounds
[
"explosion"
]
=
new
SoundEffect
(
'https://github.com/slembcke/GalacticGuardian.spritebuilder/raw/GDC/Packages/SpriteBuilder%20Resources.sbpack/TempSounds/Explosion.wav'
);
_sounds
[
"laser"
]
=
new
SoundEffect
(
'https://github.com/slembcke/GalacticGuardian.spritebuilder/raw/GDC/Packages/SpriteBuilder%20Resources.sbpack/TempSounds/Laser.wav'
);
await
_sounds
[
"explosion"
].
load
();
await
_sounds
[
"laser"
].
load
();
runApp
(
_app
);
}
...
...
@@ -103,6 +110,7 @@ class GameDemoApp extends App {
_loader
,
_spriteSheet
,
_spriteSheetUI
,
_sounds
,
(
lastScore
)
{
setState
(()
{
_lastScore
=
lastScore
;});
}
...
...
examples/game/lib/sound.dart
0 → 100644
View file @
a0111086
part of
sprites
;
// TODO: The sound effects should probably use Android's SoundPool instead of
// MediaPlayer as it is more efficient and flexible for playing back sound effects
typedef
void
SoundCompleteCallback
(
);
class
SoundEffect
{
SoundEffect
(
this
.
_url
);
// TODO: Remove load method from SoundEffect
Future
load
()
async
{
UrlResponse
response
=
await
fetchUrl
(
_url
);
_data
=
response
.
body
;
}
String
_url
;
Object
_data
;
}
class
SoundStream
{
SoundStream
(
this
.
sound
,
this
.
tag
,
this
.
loop
,
this
.
time
,
this
.
volume
,
this
.
pitch
,
this
.
pan
,
this
.
callback
);
// TODO: Make these properties work
SoundEffect
sound
;
bool
playing
=
false
;
bool
loop
=
false
;
double
time
=
0.0
;
double
volume
=
1.0
;
double
pitch
=
1.0
;
double
pan
=
0.0
;
Object
tag
;
// TODO: Implement completion callback. On completion, sounds should
// also be removed from the list of playing sounds.
SoundCompleteCallback
callback
;
MediaPlayerProxy
_player
;
}
SoundPool
_sharedSoundPool
;
class
SoundPool
{
static
SoundPool
sharedInstance
()
{
if
(
_sharedSoundPool
==
null
)
{
_sharedSoundPool
=
new
SoundPool
();
}
return
_sharedSoundPool
;
}
SoundPool
()
{
_mediaService
=
new
MediaServiceProxy
.
unbound
();
shell
.
requestService
(
null
,
_mediaService
);
}
MediaServiceProxy
_mediaService
;
List
<
SoundStream
>
_playingSounds
=
[];
// TODO: This should no longer be needed when moving to SoundPool backing
Map
<
SoundEffect
,
MediaPlayerProxy
>
_mediaPlayers
=
{};
Future
_prepare
(
SoundStream
playingSound
)
async
{
await
playingSound
.
_player
.
ptr
.
prepare
(
playingSound
.
sound
.
_data
);
}
// TODO: Move sound loading here
// TODO: Support loading sounds from bundles
// Future<SoundEffect> load(url) async {
// ...
// }
// TODO: Add sound unloader
// unload(SoundEffect effect) {
// ...
// }
// TODO: Add paused property (should pause playback of all sounds)
bool
paused
;
SoundStream
play
(
SoundEffect
sound
,
[
Object
tag
,
bool
loop
=
false
,
double
volume
=
1.0
,
double
pitch
=
1.0
,
double
pan
=
0.0
,
double
startTime
=
0.0
,
SoundCompleteCallback
callback
=
null
])
{
// Create new PlayingSound object
SoundStream
playingSound
=
new
SoundStream
(
sound
,
tag
,
loop
,
startTime
,
volume
,
pitch
,
pan
,
callback
);
// TODO: Replace this with calls to SoundPool
if
(
_mediaPlayers
[
sound
]
==
null
)
{
// Create player
playingSound
.
_player
=
new
MediaPlayerProxy
.
unbound
();
_mediaService
.
ptr
.
createPlayer
(
playingSound
.
_player
);
// Prepare sound, then play it
_prepare
(
playingSound
).
then
((
_
)
{
playingSound
.
_player
.
ptr
.
seekTo
((
startTime
*
1000.0
).
toInt
());
playingSound
.
_player
.
ptr
.
start
();
});
_playingSounds
.
add
(
playingSound
);
_mediaPlayers
[
sound
]
=
playingSound
.
_player
;
}
else
{
// Reuse player
playingSound
.
_player
=
_mediaPlayers
[
sound
];
playingSound
.
_player
.
ptr
.
seekTo
((
startTime
*
1000.0
).
toInt
());
playingSound
.
_player
.
ptr
.
start
();
}
return
playingSound
;
}
void
stop
(
Object
tag
)
{
for
(
int
i
=
_playingSounds
.
length
;
i
>=
0
;
i
--)
{
SoundStream
playingSound
=
_playingSounds
[
i
];
if
(
playingSound
.
tag
==
tag
)
{
playingSound
.
_player
.
ptr
.
pause
();
_playingSounds
.
removeAt
(
i
);
}
}
}
List
<
SoundStream
>
playingSoundsForTag
(
Object
tag
)
{
List
<
SoundStream
>
list
=
[];
for
(
SoundStream
playingSound
in
_playingSounds
)
{
if
(
playingSound
.
tag
==
tag
)
{
list
.
add
(
playingSound
);
}
}
return
list
;
}
void
stopAll
()
{
for
(
SoundStream
playingSound
in
_playingSounds
)
{
playingSound
.
_player
.
ptr
.
pause
();
}
_playingSounds
=
[];
}
}
examples/game/lib/sprites.dart
View file @
a0111086
...
...
@@ -10,12 +10,16 @@ import 'dart:math' as math;
import
'dart:typed_data'
;
import
'dart:sky'
;
import
'package:mojo/mojo/url_response.mojom.dart'
;
import
'package:sky/animation/curves.dart'
;
import
'package:sky/base/scheduler.dart'
as
scheduler
;
import
'package:sky/mojo/asset_bundle.dart'
;
import
'package:sky/mojo/net/fetch.dart'
;
import
'package:sky/mojo/shell.dart'
as
shell
;
import
'package:sky/rendering/box.dart'
;
import
'package:sky/rendering/object.dart'
;
import
'package:sky/widgets/framework.dart'
;
import
'package:sky_services/media/media.mojom.dart'
;
import
'package:vector_math/vector_math.dart'
;
part
'action.dart'
;
...
...
@@ -26,6 +30,7 @@ part 'node.dart';
part
'node3d.dart'
;
part
'node_with_size.dart'
;
part
'particle_system.dart'
;
part
'sound.dart'
;
part
'sprite.dart'
;
part
'spritesheet.dart'
;
part
'sprite_box.dart'
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment