Skip to content

Commit

Permalink
Immediately stop music instead of taking 3 seconds, when exiting game…
Browse files Browse the repository at this point in the history
… or turning off music
  • Loading branch information
UnknownShadow200 committed Sep 16, 2018
1 parent 65eee13 commit 44173bb
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 10 deletions.
3 changes: 3 additions & 0 deletions SharpWave/Backends/AL.cs
Expand Up @@ -32,8 +32,11 @@ public unsafe static class AL {
public static extern void alSourcef(uint sid, ALSourcef param, float value);
[DllImport(lib, CallingConvention = style)]
public static extern void alGetSourcei(uint sid, ALGetSourcei param, int* value);

[DllImport(lib, CallingConvention = style)]
public static extern void alSourcePlay(uint sid);
[DllImport(lib, CallingConvention = style)]
public static extern void alSourceStop(uint sid);

[DllImport(lib, CallingConvention = style)]
public static extern void alSourceQueueBuffers(uint sid, int numEntries, uint* bids);
Expand Down
3 changes: 3 additions & 0 deletions SharpWave/Backends/IAudioOutput.cs
Expand Up @@ -29,6 +29,8 @@ public abstract class IAudioOutput : IDisposable {
public abstract void SetFormat(AudioFormat format);
public abstract void BufferData(int index, AudioChunk chunk);
public abstract void Play();
public abstract void Stop();

public abstract bool IsCompleted(int index);
public abstract bool IsFinished();

Expand Down Expand Up @@ -90,6 +92,7 @@ public abstract class IAudioOutput : IDisposable {
end = BufferBlock(next, tmp, secondSize, chunks);
}

if (pendingStop) Stop();
while (!IsFinished()) { Thread.Sleep(10); }
}
}
Expand Down
5 changes: 5 additions & 0 deletions SharpWave/Backends/OpenALOut.cs
Expand Up @@ -89,6 +89,11 @@ public unsafe sealed class OpenALOut : IAudioOutput {
CheckError("SourcePlay");
}

public override void Stop() {
AL.alSourceStop(source);
CheckError("SourceStop");
}

void CheckError(string location) {
ALError error = AL.alGetError();
if (error == ALError.NoError) return;
Expand Down
7 changes: 5 additions & 2 deletions SharpWave/Backends/WinMM.cs
Expand Up @@ -7,14 +7,17 @@ namespace SharpWave {
[SuppressUnmanagedCodeSecurity]
internal static class WinMM {
const string lib = "winmm.dll";

[DllImport(lib, SetLastError = true)]
internal static extern uint waveOutGetNumDevs();

[DllImport(lib, SetLastError = true)]
internal static extern uint waveOutOpen(out IntPtr handle, IntPtr deviceID, ref WaveFormatEx format,
IntPtr callback, UIntPtr callbackInstance, uint flags);
[DllImport(lib, SetLastError = true)]
internal static extern uint waveOutClose(IntPtr handle);
internal static extern uint waveOutReset(IntPtr handle);
[DllImport(lib, SetLastError = true)]
internal static extern uint waveOutGetNumDevs();
internal static extern uint waveOutClose(IntPtr handle);

[DllImport(lib, SetLastError = true)]
internal static extern uint waveOutPrepareHeader(IntPtr handle, IntPtr header, int hdrSize);
Expand Down
5 changes: 5 additions & 0 deletions SharpWave/Backends/WinMmOut.cs
Expand Up @@ -99,6 +99,11 @@ public unsafe sealed class WinMmOut : IAudioOutput {

public override void Play() { }

public override void Stop() {
uint result = WinMM.waveOutReset(devHandle);
CheckError(result, "Reset");
}

void ApplyVolume(IntPtr handle, AudioChunk chunk) {
if (volumePercent == 100) return;

Expand Down
3 changes: 2 additions & 1 deletion src/Audio.c
Expand Up @@ -386,7 +386,8 @@ static ReturnCode Music_PlayOgg(struct Stream* source) {
if (res) break;
}

/* Wait until the buffers finished playing */
if (music_pendingStop) Audio_Stop(music_out);
/* Wait until the buffers finished playing */
while (!Audio_IsFinished(music_out)) { Thread_Sleep(10); }

Mem_Free(data);
Expand Down
9 changes: 5 additions & 4 deletions src/IsometricDrawer.c
Expand Up @@ -6,6 +6,7 @@
#include "ExtMath.h"
#include "Block.h"
#include "TerrainAtlas.h"
#include "Block.h"

Real32 iso_scale;
VertexP3fT2fC4b* iso_vertices;
Expand All @@ -14,10 +15,10 @@ GfxResourceID iso_vb;

bool iso_cacheInitalisesd;
PackedCol iso_colNormal, iso_colXSide, iso_colZSide, iso_colYBottom;
#define iso_cosX (0.86602540378443864f) /* Math_Cos(30.0 * MATH_DEG2RAD); */
#define iso_sinX (0.50000000000000000f) /* Math_Sin(30.0 * MATH_DEG2RAD); */
#define iso_cosY (0.70710678118654752f) /* Math_Cos(-45.0 * MATH_DEG2RAD); */
#define iso_sinY (-0.70710678118654752f) /* Math_Sin(-45.0 * MATH_DEG2RAD); */
#define iso_cosX (0.86602540378443864f) /* cos(30 * MATH_DEG2RAD) */
#define iso_sinX (0.50000000000000000f) /* sin(30 * MATH_DEG2RAD) */
#define iso_cosY (0.70710678118654752f) /* cos(-45 * MATH_DEG2RAD) */
#define iso_sinY (-0.70710678118654752f) /* sin(-45 * MATH_DEG2RAD) */

struct Matrix iso_transform;
Vector3 iso_pos;
Expand Down
1 change: 0 additions & 1 deletion src/IsometricDrawer.h
@@ -1,7 +1,6 @@
#ifndef CC_ISOMETRICDRAWER_H
#define CC_ISOMETRICDRAWER_H
#include "VertexStructs.h"
#include "Block.h"
/* Draws 2D isometric blocks for the hotbar and inventory UIs.
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Platform.c
Expand Up @@ -1215,6 +1215,12 @@ void Audio_BufferData(AudioHandle handle, Int32 idx, void* data, UInt32 dataSize

void Audio_Play(AudioHandle handle) { }

void Audio_Stop(AudioHandle handle) {
struct AudioContext* ctx = &Audio_Contexts[handle];
ReturnCode result = waveOutReset(ctx->Handle);
ErrorHandler_CheckOrFail(result, "Audio - resetting device");
}

bool Audio_IsCompleted(AudioHandle handle, Int32 idx) {
struct AudioContext* ctx = &Audio_Contexts[handle];
WAVEHDR* hdr = &ctx->Headers[idx];
Expand Down Expand Up @@ -1379,6 +1385,12 @@ void Audio_Play(AudioHandle handle) {
Audio_CheckError("SourcePlay");
}

void Audio_Stop(AudioHandle handle) {
struct AudioContext* ctx = &Audio_Contexts[handle];
alSourceStop(ctx->Source);
Audio_CheckError("SourceStop");
}

void Audio_Free(AudioHandle handle) {
struct AudioContext* ctx = &Audio_Contexts[handle];
if (!ctx->Count) return;
Expand Down
1 change: 1 addition & 0 deletions src/Platform.h
Expand Up @@ -119,6 +119,7 @@ struct AudioFormat* Audio_GetFormat(AudioHandle handle);
void Audio_SetFormat(AudioHandle handle, struct AudioFormat* format);
void Audio_BufferData(AudioHandle handle, Int32 idx, void* data, UInt32 dataSize);
void Audio_Play(AudioHandle handle);
void Audio_Stop(AudioHandle handle);
bool Audio_IsCompleted(AudioHandle handle, Int32 idx);
bool Audio_IsFinished(AudioHandle handle);
#endif
4 changes: 2 additions & 2 deletions src/Program.c
Expand Up @@ -64,8 +64,8 @@ int main(int argc, char** argv) {
String args[PROGRAM_MAX_CMDARGS];
Int32 argsCount = Platform_GetCommandLineArgs(argc, argv, args);
/* NOTE: Make sure to comment this out before pushing a commit */
String rawArgs = String_FromConst("UnknownShadow200 fff 127.0.0.1 25565");
argsCount = 4; String_UNSAFE_Split(&rawArgs, ' ', args, &argsCount);
// String rawArgs = String_FromConst("UnknownShadow200 fff 127.0.0.1 25565");
// argsCount = 4; String_UNSAFE_Split(&rawArgs, ' ', args, &argsCount);

if (argsCount == 0) {
String name = String_FromConst("Singleplayer");
Expand Down
1 change: 1 addition & 0 deletions src/Widgets.c
Expand Up @@ -17,6 +17,7 @@
#include "Game.h"
#include "ErrorHandler.h"
#include "Bitmap.h"
#include "Block.h"

#define WIDGET_UV(u1,v1, u2,v2) u1/256.0f,v1/256.0f, u2/256.0f,v2/256.0f
static void Widget_NullFunc(void* widget) { }
Expand Down

0 comments on commit 44173bb

Please sign in to comment.