Skip to content

Commit

Permalink
Added MediaPlayer/Song stubs.
Browse files Browse the repository at this point in the history
Added MessageBox for SDL.
Fixed platform Exit.
  • Loading branch information
tomspilman committed Sep 2, 2024
1 parent e56125c commit 90b8dc0
Show file tree
Hide file tree
Showing 15 changed files with 467 additions and 46 deletions.
6 changes: 3 additions & 3 deletions MonoGame.Framework/Media/MediaQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ internal void Clear()
for(; songs.Count > 0; )
{
song = songs[0];
#if !DIRECTX && !NATIVE
#if !DIRECTX
song.Stop();
#endif
songs.Remove(song);
}
}

#if !DIRECTX && !NATIVE
#if !DIRECTX
internal void SetVolume(float volume)
{
int count = songs.Count;
Expand All @@ -132,7 +132,7 @@ internal void Add(Song song)
songs.Add(song);
}

#if !DIRECTX && !NATIVE
#if !DIRECTX
internal void Stop()
{
int count = songs.Count;
Expand Down
2 changes: 1 addition & 1 deletion MonoGame.Framework/Media/Song.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public bool IsDisposed
get { return disposed; }
}

#if ANDROID || OPENAL || WEB || IOS
#if ANDROID || OPENAL || WEB || IOS || NATIVE
internal delegate void FinishedPlayingHandler(object sender, EventArgs args);
#if !DESKTOPGL
event FinishedPlayingHandler DonePlaying;
Expand Down
1 change: 1 addition & 0 deletions MonoGame.Framework/MonoGame.Framework.Native.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<ItemGroup>
<Compile Include="Platform\Input\InputKeyEventArgs.cs" />
<Compile Include="Platform\Utilities\AssemblyHelper.cs" />
<Compile Include="Platform\Threading.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion MonoGame.Framework/Platform/Native/GamePlatform.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using MonoGame.Interop;
using System.Threading;

namespace Microsoft.Xna.Framework;

Expand Down Expand Up @@ -39,6 +40,7 @@ public unsafe NativeGamePlatform(Game game) : base(game)
Window = _window;

Mouse.WindowHandle = _window.Handle;
MessageBox._window = _window._handle;
}

internal static unsafe MGG_GraphicsSystem* GraphicsSystem
Expand All @@ -56,7 +58,7 @@ internal static unsafe MGG_GraphicsSystem* GraphicsSystem

public override unsafe void Exit()
{
MGP.Platform_Exit(Handle);
Interlocked.Increment(ref _isExiting);
}

public override unsafe void RunLoop()
Expand All @@ -69,6 +71,8 @@ public override unsafe void RunLoop()

Game.Tick();

Threading.Run();

if (_isExiting > 0 && ShouldExit())
break;
else
Expand Down
54 changes: 54 additions & 0 deletions MonoGame.Framework/Platform/Native/Media.Interop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using System;
using System.Runtime.InteropServices;

namespace MonoGame.Interop;


[MGHandle]
internal readonly struct MGM_Song { }

internal static unsafe partial class MGM
{
const string MonoGameNativeDLL = "monogame.native";

#region Song

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void NativeFinishedCallback(nint callbackData);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGM_Song_Create", StringMarshalling = StringMarshalling.Utf8)]
public static partial MGM_Song* Song_Create(string mediaFilePath);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGM_Song_GetDuration", StringMarshalling = StringMarshalling.Utf8)]
public static partial ulong Song_GetDuration(MGM_Song* song);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGM_Song_GetPosition", StringMarshalling = StringMarshalling.Utf8)]
public static partial ulong Song_GetPosition(MGM_Song* song);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGM_Song_GetVolume", StringMarshalling = StringMarshalling.Utf8)]
public static partial float Song_GetVolume(MGM_Song* song);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGM_Song_SetVolume", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Song_SetVolume(MGM_Song* song, float volume);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGM_Song_Pause", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Song_Pause(MGM_Song* song);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGM_Song_Play", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Song_Play(MGM_Song* song, ulong startPositionMs, [MarshalAs(UnmanagedType.FunctionPtr)] NativeFinishedCallback callback, nint callbackData);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGM_Song_Resume", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Song_Resume(MGM_Song* song);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGM_Song_Stop", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Song_Stop(MGM_Song* song);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGM_Song_Destroy", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Song_Destroy(MGM_Song* song);

#endregion
}
45 changes: 35 additions & 10 deletions MonoGame.Framework/Platform/Native/MediaPlayer.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,70 @@ public static partial class MediaPlayer
{
private static void PlatformInitialize()
{

}

private static bool PlatformGetIsMuted()
{
return false;
return _isMuted;
}

private static void PlatformSetIsMuted(bool muted)
{
_isMuted = muted;

if (_queue.Count == 0)
return;

var newVolume = _isMuted ? 0.0f : _volume;
_queue.SetVolume(newVolume);
}

private static bool PlatformGetIsRepeating()
{
return false;
return _isRepeating;
}

private static void PlatformSetIsRepeating(bool repeating)
{

_isRepeating = repeating;
}

private static bool PlatformGetIsShuffled()
{
return false;
return _isShuffled;
}

private static void PlatformSetIsShuffled(bool shuffled)
{

_isShuffled = shuffled;
}

private static TimeSpan PlatformGetPlayPosition()
{
return TimeSpan.Zero;
if (_queue.ActiveSong == null)
return TimeSpan.Zero;

return _queue.ActiveSong.Position;
}

private static MediaState PlatformGetState()
{
return MediaState.Stopped;
return _state;
}

private static float PlatformGetVolume()
{
return 0.0f;
return _volume;
}

private static void PlatformSetVolume(float volume)
{
_volume = volume;

if (_queue.ActiveSong == null)
return;

_queue.SetVolume(_isMuted ? 0.0f : _volume);
}

private static bool PlatformGetGameHasControl()
Expand All @@ -70,21 +83,33 @@ private static bool PlatformGetGameHasControl()

private static void PlatformPause()
{
if (_queue.ActiveSong == null)
return;

_queue.ActiveSong.Pause();
}

private static void PlatformPlaySong(Song song, TimeSpan? startPosition)
{
if (_queue.ActiveSong == null)
return;

song.Volume = _isMuted ? 0.0f : _volume;
song.Play(startPosition, OnSongFinishedPlaying);
}

private static void PlatformResume()
{
if (_queue.ActiveSong == null)
return;

_queue.ActiveSong.Resume();
}

private static void PlatformStop()
{

// TOOD: What is this loop doing?
foreach (var song in Queue.Songs)
_queue.ActiveSong.Stop();
}
}
9 changes: 7 additions & 2 deletions MonoGame.Framework/Platform/Native/MessageBox.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using MonoGame.Interop;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;

namespace Microsoft.Xna.Framework.Input;

public static partial class MessageBox
{
private static Task<int?> PlatformShow(string title, string description, List<string> buttons)
internal static unsafe MGP_Window* _window;

private static unsafe Task<int?> PlatformShow(string title, string description, List<string> buttons)
{
return Task.FromResult<int?>(0);
var result = MGP.Window_ShowMessageBox(_window, title, description, buttons.ToArray(), buttons.Count);
return Task.FromResult<int?>(result);
}

private static void PlatformCancel(int? result)
Expand Down
12 changes: 9 additions & 3 deletions MonoGame.Framework/Platform/Native/Platform.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace MonoGame.Interop;
Expand Down Expand Up @@ -224,9 +225,6 @@ internal static unsafe partial class MGP
[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGP_Platform_ExitFullScreen", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Platform_ExitFullScreen(MGP_Platform* platform);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGP_Platform_Exit", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Platform_Exit(MGP_Platform* platform);

#endregion

#region Window
Expand Down Expand Up @@ -270,6 +268,14 @@ internal static unsafe partial class MGP
[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGP_Window_SetPosition", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Window_SetPosition(MGP_Window* window, int x, int y);

[LibraryImport(MonoGameNativeDLL, EntryPoint = "MGP_Window_ShowMessageBox", StringMarshalling = StringMarshalling.Utf8)]
public static partial int Window_ShowMessageBox(
MGP_Window* window,
string title,
string description,
string[] buttons,
int count);

#endregion

#region Mouse
Expand Down
Loading

0 comments on commit 90b8dc0

Please sign in to comment.