Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
Clean up iOS and Mac Effects.
Browse files Browse the repository at this point in the history
* Use MonoGame for processing effects.  This will allow the user to play multiple instances of Effects (Repeated Sounds see issue #273).  Warning: This will not only support those sound format allowed by MonoGame's Effects implementation which interfaces to the OpenAL library.

* Add functionality such as PauseAllEffects, ResumeEffect and ResumeAllEffects.

* Update CocosDehension audio interface.

* Add new AudioSample to CocosSharp samples.  This will only work with current source base and not with current release.  View link here: https://github.com/mono/cocos-sharp-samples/tree/master/AudioSample

* Update PCL CocosSharpAPI to include this new functionality interface.

See issues : #157 , #273
  • Loading branch information
kjpou1 committed Jul 26, 2015
1 parent e888b10 commit 65c001d
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 73 deletions.
45 changes: 21 additions & 24 deletions PCL/CocosSharpPCLShared/CocosSharpAPI.cs
Expand Up @@ -7,7 +7,7 @@ public partial class CCEffectPlayer : CocosDenshion.CCEffectPlayerCore, System.I
public void Dispose() { }
protected virtual void Dispose(bool disposing) { }
protected override void DisposeManagedResources() { }
public override void Open(string fileName, int soundId) { }
public override void Open(string filename, int uid) { }
public override void Pause() { }
public override void Play(bool loop=false) { }
public override void Resume() { }
Expand Down Expand Up @@ -70,6 +70,7 @@ public partial class CCSimpleAudioEngine {
public static CocosDenshion.CCSimpleAudioEngine SharedEngine { get { return default(CocosDenshion.CCSimpleAudioEngine); } }
public void End() { }
public static string FullPath(string path) { return default(string); }
public void PauseAllEffects() { }
public void PauseBackgroundMusic() { }
public void PauseEffect(int fxid) { }
public void PlayBackgroundMusic(string filename, bool loop=false) { }
Expand All @@ -79,7 +80,9 @@ public partial class CCSimpleAudioEngine {
public void PreloadBackgroundMusic(string filename) { }
public void PreloadEffect(string filename) { }
public void RestoreMediaState() { }
public void ResumeAllEffects() { }
public void ResumeBackgroundMusic() { }
public void ResumeEffect(int fxid) { }
public void RewindBackgroundMusic() { }
public void SaveMediaState() { }
public void StopAllEffects() { }
Expand Down Expand Up @@ -837,12 +840,13 @@ public partial class CCDrawNode : CocosSharp.CCNode {
public void DrawCircle(CocosSharp.CCPoint center, float radius, CocosSharp.CCColor4B color) { }
public void DrawCircle(CocosSharp.CCPoint center, float radius, int segments, CocosSharp.CCColor4B color) { }
public void DrawCubicBezier(CocosSharp.CCPoint origin, CocosSharp.CCPoint control1, CocosSharp.CCPoint control2, CocosSharp.CCPoint destination, int segments, float lineWidth, CocosSharp.CCColor4B color) { }
[System.ObsoleteAttribute("DrawDot is obsolete: Use DrawSolidCircle")]
public void DrawDot(CocosSharp.CCPoint pos, float radius, CocosSharp.CCColor4B color) { }
public void DrawEllipse(CocosSharp.CCRect rect, float lineWidth, CocosSharp.CCColor4B color) { }
public void DrawEllipse(int x, int y, int width, int height, float lineWidth, CocosSharp.CCColor4B color) { }
public void DrawLine(CocosSharp.CCPoint from, CocosSharp.CCPoint to, CocosSharp.CCColor4B color) { }
public void DrawLine(CocosSharp.CCPoint from, CocosSharp.CCPoint to, float lineWidth=1f) { }
public void DrawLine(CocosSharp.CCPoint from, CocosSharp.CCPoint to, float lineWidth, CocosSharp.CCColor4B color) { }
public void DrawLine(CocosSharp.CCPoint from, CocosSharp.CCPoint to, CocosSharp.CCColor4B color, CocosSharp.CCLineCap lineCap=(CocosSharp.CCLineCap)(0)) { }
public void DrawLine(CocosSharp.CCPoint from, CocosSharp.CCPoint to, float lineWidth, CocosSharp.CCColor4B color, CocosSharp.CCLineCap lineCap=(CocosSharp.CCLineCap)(0)) { }
public void DrawLine(CocosSharp.CCPoint from, CocosSharp.CCPoint to, float lineWidth=1f, CocosSharp.CCLineCap lineCap=(CocosSharp.CCLineCap)(0)) { }
public void DrawLineList(CocosSharp.CCV3F_C4B[] verts) { }
public void DrawPolygon(CocosSharp.CCPoint[] verts, int count, CocosSharp.CCColor4B fillColor, float borderWidth, CocosSharp.CCColor4B borderColor, bool closePolygon=true) { }
public void DrawPolygon(CocosSharp.CCPoint[] verts, int count, CocosSharp.CCColor4F fillColor, float borderWidth, CocosSharp.CCColor4F borderColor, bool closePolygon=true) { }
Expand All @@ -852,7 +856,10 @@ public partial class CCDrawNode : CocosSharp.CCNode {
public void DrawRect(CocosSharp.CCRect rect) { }
public void DrawRect(CocosSharp.CCRect rect, CocosSharp.CCColor4B fillColor) { }
public void DrawRect(CocosSharp.CCRect rect, CocosSharp.CCColor4B fillColor, float borderWidth, CocosSharp.CCColor4B borderColor) { }
public void DrawSegment(CocosSharp.CCPoint from, CocosSharp.CCPoint to, float radius, CocosSharp.CCColor4F color) { }
[System.ObsoleteAttribute("DrawSegment is obsolete: Use DrawLine")]
public void DrawSegment(CocosSharp.CCPoint from, CocosSharp.CCPoint to, float radius, CocosSharp.CCColor4B color, CocosSharp.CCLineCap lineCap=(CocosSharp.CCLineCap)(1)) { }
[System.ObsoleteAttribute("DrawSegment is obsolete: Use DrawLine")]
public void DrawSegment(CocosSharp.CCPoint from, CocosSharp.CCPoint to, float radius, CocosSharp.CCColor4F color, CocosSharp.CCLineCap lineCap=(CocosSharp.CCLineCap)(1)) { }
public void DrawSolidArc(CocosSharp.CCPoint pos, float radius, float startAngle, float sweepAngle, CocosSharp.CCColor4B color) { }
public void DrawSolidCircle(CocosSharp.CCPoint pos, float radius, CocosSharp.CCColor4B color) { }
public void DrawString(int x, int y, string format, params System.Object[] objects) { }
Expand Down Expand Up @@ -1417,6 +1424,8 @@ public partial class CCFont {
public virtual void SetCurrentGlyphCollection(CocosSharp.GlyphCollection glyphs, string customGlyphs) { }
}
public partial class CCFontFNT : CocosSharp.CCFont {
public CCFontFNT(string configInfo, CocosSharp.CCTexture2D atlasTexture, System.Nullable<CocosSharp.CCVector2> imageOffset=null) { }
public CCFontFNT(string configInfo, System.IO.Stream atlasStream, System.Nullable<CocosSharp.CCVector2> imageOffset=null) { }
public CCFontFNT(string fntFilePath, System.Nullable<CocosSharp.CCVector2> imageOffset=null) { }
public bool IsFontConfigValid { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { return default(bool); } }
public override System.Int32[] HorizontalKerningForText(string text, out int numLetters) { numLetters = default(int); return default(System.Int32[]); }
Expand Down Expand Up @@ -1835,6 +1844,7 @@ public partial class CCLabel : CocosSharp.CCNode, CocosSharp.ICCTextContainer {
protected CocosSharp.CCLabelLineBreak lineBreak;
protected CocosSharp.CCVerticalTextAlignment vertAlignment;
public CCLabel() { }
public CCLabel(CocosSharp.CCFontFNT fntFontConfig, string str, CocosSharp.CCSize dimensions, CocosSharp.CCLabelFormat labelFormat) { }
public CCLabel(string str, string fntFile) { }
public CCLabel(string str, string fntFile, CocosSharp.CCSize dimensions) { }
public CCLabel(string str, string fntFile, CocosSharp.CCSize dimensions, CocosSharp.CCLabelFormat labelFormat) { }
Expand Down Expand Up @@ -2088,6 +2098,11 @@ public partial class CCLens3DState : CocosSharp.CCGrid3DActionState {
public float Radius { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { return default(float); } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public override void Update(float time) { }
}
public enum CCLineCap {
Butt = 0,
Round = 1,
Square = 2,
}
public partial class CCLiquid : CocosSharp.CCGrid3DAction {
public CCLiquid(float duration, CocosSharp.CCGridSize gridSize, int waves=0, float amplitude=0f) : base (default(float)) { }
public int Waves { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { return default(int); } }
Expand Down Expand Up @@ -2210,24 +2225,6 @@ public abstract partial class CCMenuItemLabelBase : CocosSharp.CCMenuItem {
public override void Activate() { }
protected void LabelWillChange(CocosSharp.CCNode oldValue, CocosSharp.CCNode newValue) { }
}
[System.ObsoleteAttribute("Use CCMenuItemLabel instead.")]
public partial class CCMenuItemLabelBMFont : CocosSharp.CCMenuItemLabelBase {
[System.ObsoleteAttribute("Use CCMenuItemLabel instead.")]
public CCMenuItemLabelBMFont(CocosSharp.CCLabelBMFont labelBMFont, System.Action<System.Object> target=null) : base (default(System.Action<System.Object>)) { }
public override bool Enabled { get { return default(bool); } set { } }
public CocosSharp.CCLabelBMFont LabelBMFont { get { return default(CocosSharp.CCLabelBMFont); } set { } }
public override CocosSharp.CCScene Scene { get { return default(CocosSharp.CCScene); } }
}
[System.ObsoleteAttribute("Use CCMenuItemLabel instead.")]
public partial class CCMenuItemLabelTTF : CocosSharp.CCMenuItemLabelBase {
[System.ObsoleteAttribute("Use CCMenuItemLabel instead.")]
public CCMenuItemLabelTTF(CocosSharp.CCLabelTtf labelTTF, System.Action<System.Object> target=null) : base (default(System.Action<System.Object>)) { }
[System.ObsoleteAttribute("Use CCMenuItemLabel instead.")]
public CCMenuItemLabelTTF(System.Action<System.Object> target=null) : base (default(System.Action<System.Object>)) { }
public override bool Enabled { get { return default(bool); } set { } }
public CocosSharp.CCLabelTtf LabelTTF { get { return default(CocosSharp.CCLabelTtf); } set { } }
public override CocosSharp.CCScene Scene { get { return default(CocosSharp.CCScene); } }
}
public partial class CCMenuItemToggle : CocosSharp.CCMenuItem {
public CCMenuItemToggle(params CocosSharp.CCMenuItem[] items) { }
public CCMenuItemToggle(System.Action<System.Object> target, params CocosSharp.CCMenuItem[] items) { }
Expand Down Expand Up @@ -3679,7 +3676,7 @@ public enum CCTextAlignment {
Left = 0,
Right = 2,
}
public partial class CCTextFieldTTF : CocosSharp.CCLabelTtf {
public partial class CCTextFieldTTF : CocosSharp.CCLabel {
public CCTextFieldTTF(string text, string fontName, float fontSize) { }
public CCTextFieldTTF(string text, string fontName, float fontSize, CocosSharp.CCSize dimensions, CocosSharp.CCTextAlignment hAlignment) { }
public CCTextFieldTTF(string text, string fontName, float fontSize, CocosSharp.CCSize dimensions, CocosSharp.CCTextAlignment hAlignment, CocosSharp.CCVerticalTextAlignment vAlignment) { }
Expand Down
2 changes: 1 addition & 1 deletion src/CocosSharp.MacOS.csproj
Expand Up @@ -391,7 +391,6 @@
<Compile Include="particle_nodes\CCParticleSystemConfig.cs" />
<Compile Include="particle_nodes\CCParticleSystemCache.cs" />
<Compile Include="denshion\CCMusicPlayer.AVFoundation.cs" />
<Compile Include="denshion\CCEffectPlayer.AVFoundation.cs" />
<Compile Include="CCWindow.cs" />
<Compile Include="CCViewport.cs" />
<Compile Include="Actions\action_intervals\CCExtraAction.cs" />
Expand All @@ -418,6 +417,7 @@
<Compile Include="Renderer\RenderCommands\CCQuadCommand.cs" />
<Compile Include="Renderer\RenderCommands\CCCustomCommand.cs" />
<Compile Include="misc_nodes\CCGeometryNode.cs" />
<Compile Include="denshion\CCEffectPlayer.MonoGame.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="support\Converters\" />
Expand Down
2 changes: 1 addition & 1 deletion src/CocosSharp.iOS.csproj
Expand Up @@ -376,7 +376,6 @@
<Compile Include="events\CCEventListenerCustom.cs" />
<Compile Include="particle_nodes\CCParticleSystemCache.cs" />
<Compile Include="denshion\CCMusicPlayer.AVFoundation.cs" />
<Compile Include="denshion\CCEffectPlayer.AVFoundation.cs" />
<Compile Include="tileMap_parallax_nodes\CCTileLayerInfo.cs" />
<Compile Include="tileMap_parallax_nodes\CCTileMap.cs" />
<Compile Include="tileMap_parallax_nodes\CCTileMapInfo.cs" />
Expand All @@ -397,6 +396,7 @@
<Compile Include="Renderer\RenderCommands\CCQuadCommand.cs" />
<Compile Include="Renderer\RenderCommands\CCRenderCommand.cs" />
<Compile Include="misc_nodes\CCGeometryNode.cs" />
<Compile Include="denshion\CCEffectPlayer.MonoGame.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup />
Expand Down
1 change: 1 addition & 0 deletions src/denshion/CCEffectPlayer.AVFoundation.cs
Expand Up @@ -60,6 +60,7 @@ public override void Play(bool loop=false)
if (soundEffect !=null)
{
soundEffect.NumberOfLoops = loop ? -1 : 0;

soundEffect.Play();
}
}
Expand Down
75 changes: 59 additions & 16 deletions src/denshion/CCEffectPlayer.MonoGame.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Audio;
using CocosSharp;

Expand All @@ -9,6 +10,8 @@ public partial class CCEffectPlayer
SoundEffect effect;
SoundEffectInstance sfxInstance;

List<SoundEffectInstance> sfxInstances = new List<SoundEffectInstance>();


#region Properties

Expand Down Expand Up @@ -44,6 +47,7 @@ protected override void DisposeManagedResources()
if (effect != null)
effect.Dispose();


if (sfxInstance != null)
sfxInstance.Dispose();
}
Expand All @@ -59,13 +63,14 @@ public override void Play(bool loop=false)
{
return;
}
if (loop)
{
// if (loop)
// {
// If looping, then get an instance of this sound effect so that it can be
// stopped.
sfxInstance = effect.CreateInstance();
sfxInstance.IsLooped = true;
}
sfxInstance.IsLooped = loop;
sfxInstances.Add(sfxInstance);
// }
if (sfxInstance != null)
{
sfxInstance.Play();
Expand All @@ -78,26 +83,64 @@ public override void Play(bool loop=false)

public override void Pause()
{
if (sfxInstance != null && !sfxInstance.IsDisposed && sfxInstance.State == SoundState.Playing)
{
sfxInstance.Pause();
}
if (sfxInstances.Count > 0)
{
for (var x = 0; x < sfxInstances.Count; x++)
{
var instance = sfxInstances[x];

if (instance.IsDisposed)
sfxInstances.RemoveAt(x);

if (instance.State == SoundState.Playing)
instance.Pause();
}
}
}

public override void Resume()
{
if (sfxInstance != null && !sfxInstance.IsDisposed && sfxInstance.State == SoundState.Paused)
{
sfxInstance.Play();
}
// if (sfxInstance != null && !sfxInstance.IsDisposed && sfxInstance.State == SoundState.Paused)
// {
// sfxInstance.Resume();
// }

if (sfxInstances.Count > 0)
{
for (var x = 0; x < sfxInstances.Count; x++)
{
var instance = sfxInstances[x];

if (instance.IsDisposed)
sfxInstances.RemoveAt(x);

if (instance.State == SoundState.Paused)
instance.Resume();
}
}

}

public override void Stop()
{
if (sfxInstance != null && !sfxInstance.IsDisposed && sfxInstance.State == SoundState.Playing)
{
sfxInstance.Stop();
}
// if (sfxInstance != null && !sfxInstance.IsDisposed && sfxInstance.State == SoundState.Playing)
// {
// sfxInstance.Stop();
// }

if (sfxInstances.Count > 0)
{
for (var x = 0; x < sfxInstances.Count; x++)
{
var instance = sfxInstances[x];

if (instance.IsDisposed)
sfxInstances.RemoveAt(x);

if (instance.State == SoundState.Playing)
instance.Stop();
}
}
}

public override void Rewind()
Expand Down
72 changes: 68 additions & 4 deletions src/denshion/CCSimpleAudioEngine.cs
Expand Up @@ -157,7 +157,7 @@ public void PauseEffect(int fxid)
}
}

public void StopAllEffects()
public void PauseAllEffects()
{
List<CCEffectPlayer> l = new List<CCEffectPlayer>();

Expand All @@ -166,17 +166,81 @@ public void StopAllEffects()
try
{
l.AddRange(list.Values);
list.Clear();
}
catch (Exception ex)
{
CCLog.Log("Unexpected exception while stopping all effects.");
CCLog.Log("Unexpected exception while pausing all effects.");
CCLog.Log(ex.ToString());
}
}
foreach (CCEffectPlayer p in l)
{
p.Stop();
PauseEffect(p.SoundID);
}
}

public void ResumeEffect(int fxid)
{
try
{
if (list.ContainsKey(fxid))
{
list[fxid].Resume();
}
}
catch (Exception ex)
{
CCLog.Log("Unexpected exception while resuming a SoundEffect: {0}", fxid);
CCLog.Log(ex.ToString());
}
}

public void ResumeAllEffects()
{

List<CCEffectPlayer> l = new List<CCEffectPlayer>();

lock (list)
{
try
{
l.AddRange(list.Values);
}
catch (Exception ex)
{
CCLog.Log("Unexpected exception while resuming all effects.");
CCLog.Log(ex.ToString());
}
}
foreach (CCEffectPlayer p in l)
{
p.Resume();
}

}

public void StopAllEffects()
{
List<CCEffectPlayer> l = new List<CCEffectPlayer>();

lock (list)
{
try
{
l.AddRange(list.Values);
}
catch (Exception ex)
{
CCLog.Log("Unexpected exception while stopping all effects.");
CCLog.Log(ex.ToString());
}

foreach (CCEffectPlayer p in l)
{
StopEffect(p.SoundID);
}

list.Clear();
}

}
Expand Down
20 changes: 10 additions & 10 deletions tests/tests/Resources/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 65c001d

Please sign in to comment.