From 468ec1ef2a54dd2777c99a8afeaedf9f27603cc7 Mon Sep 17 00:00:00 2001 From: Tim Sarbin Date: Sat, 8 Dec 2018 20:29:10 -0500 Subject: [PATCH] Added ability to stop sfx. Minor map engine updates. --- .../Interfaces/Data/IMusicProvider.cs | 3 +- OpenDiablo2.Core/Map Engine/MapEngine.cs | 2 +- OpenDiablo2.SDL2/SDL2MusicProvider.cs | 9 +++- OpenDiablo2.SDL2/SDL2RenderWindow.cs | 18 +++----- OpenDiablo2.Scenes/SelectHeroClass.cs | 44 +++++++++++++------ 5 files changed, 46 insertions(+), 30 deletions(-) diff --git a/OpenDiablo2.Common/Interfaces/Data/IMusicProvider.cs b/OpenDiablo2.Common/Interfaces/Data/IMusicProvider.cs index 3fc246ba..3a2a3328 100644 --- a/OpenDiablo2.Common/Interfaces/Data/IMusicProvider.cs +++ b/OpenDiablo2.Common/Interfaces/Data/IMusicProvider.cs @@ -8,6 +8,7 @@ namespace OpenDiablo2.Common.Interfaces void LoadSong(Stream data); void PlaySong(); void StopSong(); - void PlaySfx(byte[] data); + void StopSfx(int channel); + int PlaySfx(byte[] data); } } diff --git a/OpenDiablo2.Core/Map Engine/MapEngine.cs b/OpenDiablo2.Core/Map Engine/MapEngine.cs index dfb2a2a1..cddbfab4 100644 --- a/OpenDiablo2.Core/Map Engine/MapEngine.cs +++ b/OpenDiablo2.Core/Map Engine/MapEngine.cs @@ -125,7 +125,7 @@ namespace OpenDiablo2.Core.Map_Engine foreach (var cellInfo in gameState.GetMapCellInfo((int)ax, (int)ay, eRenderCellType.WallLower)) - renderWindow.DrawMapCell(cellInfo, 320 + px + (int)ox + xOffset, 210 + py + (int)oy); + renderWindow.DrawMapCell(cellInfo, 320 + px + (int)ox + xOffset, 210 + py + (int)oy + 80); foreach (var cellInfo in gameState.GetMapCellInfo((int)ax, (int)ay, eRenderCellType.WallUpper)) renderWindow.DrawMapCell(cellInfo, 320 + px + (int)ox + xOffset, 210 + py + (int)oy); diff --git a/OpenDiablo2.SDL2/SDL2MusicProvider.cs b/OpenDiablo2.SDL2/SDL2MusicProvider.cs index 3f385302..5605a444 100644 --- a/OpenDiablo2.SDL2/SDL2MusicProvider.cs +++ b/OpenDiablo2.SDL2/SDL2MusicProvider.cs @@ -73,10 +73,15 @@ namespace OpenDiablo2.SDL2_ SDL_mixer.Mix_CloseAudio(); } - public void PlaySfx(byte[] data) + public int PlaySfx(byte[] data) { var sound = SDL_mixer.Mix_QuickLoad_WAV(data); - SDL_mixer.Mix_PlayChannel(-1, sound, 0); + return SDL_mixer.Mix_PlayChannel(-1, sound, 0); + } + + public void StopSfx(int channel) + { + SDL_mixer.Mix_HaltChannel(channel); } } } diff --git a/OpenDiablo2.SDL2/SDL2RenderWindow.cs b/OpenDiablo2.SDL2/SDL2RenderWindow.cs index 136fc624..dcc69be4 100644 --- a/OpenDiablo2.SDL2/SDL2RenderWindow.cs +++ b/OpenDiablo2.SDL2/SDL2RenderWindow.cs @@ -354,11 +354,9 @@ namespace OpenDiablo2.SDL2_ var offX = -minX; var offY = -minY; - var frameSize = new Size(diffX, Math.Abs(diffY)); - - var srcRect = new SDL.SDL_Rect { x = 0, y = 0, w = frameSize.Width, h = Math.Abs(frameSize.Height) }; - //var frameSizeMax = diffX * Math.Abs(diffY); + var frameSize = new Size(Math.Abs(diffX), Math.Abs(diffY)); + var srcRect = new SDL.SDL_Rect { x = minX, y = minY, w = frameSize.Width, h = frameSize.Height }; var texId = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_ARGB8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING, frameSize.Width, frameSize.Height); SDL.SDL_SetTextureBlendMode(texId, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND); @@ -371,10 +369,7 @@ namespace OpenDiablo2.SDL2_ UInt32* data = (UInt32*)pixels; var pitchChange = pitch / 4; - - for (var i = 0; i < frameSize.Height * pitchChange; i++) - data[i] = 0x0; - + var colors = getGameState().CurrentPalette.Colors; foreach (var block in mapCell.Blocks) { @@ -387,7 +382,7 @@ namespace OpenDiablo2.SDL2_ { if (colorIndex == 0) continue; - var color = getGameState().CurrentPalette.Colors[colorIndex]; + var color = colors[colorIndex]; if (color > 0) data[index] = color; @@ -399,8 +394,7 @@ namespace OpenDiablo2.SDL2_ xx++; if (xx == 32) { - index -= 32; - index += pitchChange; + index += pitchChange - 32; xx = 0; yy++; } @@ -428,7 +422,7 @@ namespace OpenDiablo2.SDL2_ public void DrawMapCell(MapCellInfo mapCellInfo, int xPixel, int yPixel) { var srcRect = new SDL.SDL_Rect { x = 0, y = 0, w = mapCellInfo.FrameWidth, h = Math.Abs(mapCellInfo.FrameHeight) }; - var destRect = new SDL.SDL_Rect { x = xPixel - mapCellInfo.OffX, y = yPixel - mapCellInfo.OffY, w = mapCellInfo.FrameWidth, h = mapCellInfo.FrameHeight }; + var destRect = new SDL.SDL_Rect { x = xPixel + mapCellInfo.Rect.X, y = yPixel + mapCellInfo.Rect.Y, w = mapCellInfo.FrameWidth, h = mapCellInfo.FrameHeight }; SDL.SDL_RenderCopy(renderer, (mapCellInfo.Texture as SDL2Texture).Pointer, ref srcRect, ref destRect); } diff --git a/OpenDiablo2.Scenes/SelectHeroClass.cs b/OpenDiablo2.Scenes/SelectHeroClass.cs index 301dae69..33a6319b 100644 --- a/OpenDiablo2.Scenes/SelectHeroClass.cs +++ b/OpenDiablo2.Scenes/SelectHeroClass.cs @@ -43,6 +43,8 @@ namespace OpenDiablo2.Scenes private bool showEntryUi = false; private eHero? selectedHero = null; private float secondTimer; + private int sfxChannel = -1; + private int sfxChannel2 = -1; private readonly ISprite backgroundSprite, campfireSprite; private readonly IFont headingFont; private readonly IFont heroDescFont; @@ -242,14 +244,27 @@ namespace OpenDiablo2.Scenes }, (path => sfxDictionary.Add(path, mpqProvider.GetBytes(path)))); } + private void StopSfx() + { + if (sfxChannel > -1) + soundProvider.StopSfx(sfxChannel); + + if (sfxChannel2 > -1) + soundProvider.StopSfx(sfxChannel); + } + private void OnOkclicked() { + StopSfx(); + // TODO: Support other session types gameState.Initialize(characterNameTextBox.Text, selectedHero.Value, eSessionType.Local); } private void OnExitClicked() { + StopSfx(); + var heros = Enum.GetValues(typeof(eHero)).Cast(); foreach(var hero in heros) { @@ -449,6 +464,7 @@ namespace OpenDiablo2.Scenes if (ri.Value.Stance != eHeroStance.Selected) continue; + PlayHeroDeselected(ri.Key); ri.Value.Stance = eHeroStance.Retreating; ri.Value.SpecialFrameTime = 0; break; @@ -476,25 +492,25 @@ namespace OpenDiablo2.Scenes switch (hero) { case eHero.Barbarian: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXBarbarianSelect]); + sfxChannel = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXBarbarianSelect]); break; case eHero.Necromancer: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXNecromancerSelect]); + sfxChannel = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXNecromancerSelect]); break; case eHero.Paladin: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXPaladinSelect]); + sfxChannel = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXPaladinSelect]); break; case eHero.Assassin: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAssassinSelect]); + sfxChannel = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAssassinSelect]); break; case eHero.Sorceress: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXSorceressSelect]); + sfxChannel = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXSorceressSelect]); break; case eHero.Amazon: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAmazonSelect]); + sfxChannel = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAmazonSelect]); break; case eHero.Druid: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXDruidSelect]); + sfxChannel = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXDruidSelect]); break; default: break; @@ -506,25 +522,25 @@ namespace OpenDiablo2.Scenes switch (hero) { case eHero.Barbarian: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXBarbarianDeselect]); + sfxChannel2 = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXBarbarianDeselect]); break; case eHero.Necromancer: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXNecromancerDeselect]); + sfxChannel2 = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXNecromancerDeselect]); break; case eHero.Paladin: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXPaladinDeselect]); + sfxChannel2 = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXPaladinDeselect]); break; case eHero.Assassin: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAssassinDeselect]); + sfxChannel2 = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAssassinDeselect]); break; case eHero.Sorceress: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXSorceressDeselect]); + sfxChannel2 = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXSorceressDeselect]); break; case eHero.Amazon: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAmazonDeselect]); + sfxChannel2 = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAmazonDeselect]); break; case eHero.Druid: - soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXDruidDeselect]); + sfxChannel2 = soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXDruidDeselect]); break; default: break;