Skip to content

Commit

Permalink
version 4.5.0: more docblocks, and added CurrentMusic() to SoundManager
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMakesGames committed Mar 23, 2024
1 parent 6a953a7 commit ffd87ca
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Company>Ben Hendel-Doying</Company>
<Description>Some GraphicsManager extensions for PlayPlayMini.</Description>
<Copyright>2023-2024 Ben Hendel-Doying</Copyright>
<Version>4.4.0</Version>
<Version>4.5.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame playplaymini graphics extensions animations text</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Company>Ben Hendel-Doying</Company>
<Description>An extension for PlayPlayMini, adding a skinnable, object-oriented UI framework.</Description>
<Copyright>2021-2024 Ben Hendel-Doying</Copyright>
<Version>4.4.0</Version>
<Version>4.5.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame mouse ui framework oo</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Company>Ben Hendel-Doying</Company>
<Description>An opinionated framework for making smallish games with MonoGame.</Description>
<Copyright>2021-2024 Ben Hendel-Doying</Copyright>
<Version>4.4.0</Version>
<Version>4.5.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame game engine framework di state</PackageTags>
Expand Down
30 changes: 30 additions & 0 deletions BenMakesGames.PlayPlayMini/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@

namespace BenMakesGames.PlayPlayMini;

/// <summary>
/// Inherit this class to create your own game states.
///
/// Change the current game's state by calling one of `GameStateManager`'s `ChangeState` methods.
///
/// If a game state needs access to a service, such as the `GameStateManager`, `GraphicsManager`, `SoundManager`, etc.,
/// include the service in the game state's constructor arguments. The IoC container will automatically inject them.
///
/// Example:
///
/// public sealed class MyGameState: GameState
/// {
/// private GameStateManager GSM { get; }
/// private GraphicsManager Graphics { get; }
/// private KeyboardManager Keyboard { get; }
///
/// public MyGameState(GameStateManager gsm, GraphicsManager graphics, KeyboardManager keyboard)
/// {
/// GSM = gsm;
/// Graphics = graphics;
/// Keyboard = keyboard;
/// }
///
/// public override void Update(GameTime gameTime)
/// {
/// if(Keyboard.IsKeyDown(Keys.Escape))
/// GSM.ChangeState&lt;MyPauseMenu&gt;(); // or maybe GSM.Exit();
/// }
/// }
/// </summary>
public abstract class GameState
{
/// <summary>
Expand Down
19 changes: 19 additions & 0 deletions BenMakesGames.PlayPlayMini/Services/FrameCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@

namespace BenMakesGames.PlayPlayMini.Services;

/// <summary>
/// Counts the FPS of the game.
///
/// To draw the FPS on-screen, include the `FrameCounter` in a game state's constructor arguments, and perform a
/// `Graphics.DrawText(...)` call to draw the `FrameCounter`'s `AverageFramesPerSecond` and/or `CurrentFramesPerSecond`.
/// For example:
///
/// private FrameCounter FrameCounter { get; }
///
/// public MyGameState(FrameCounter frameCounter)
/// {
/// FrameCounter = frameCounter;
/// }
///
/// public override void Draw(GameTime gameTime)
/// {
/// Graphics.DrawText("Font", 4, 4, $"FPS: {FrameCounter.AverageFramesPerSecond}", Color.Red);
/// }
/// </summary>
[AutoRegister]
public sealed class FrameCounter: IServiceDraw
{
Expand Down
8 changes: 8 additions & 0 deletions BenMakesGames.PlayPlayMini/Services/SoundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public void PlaySound(string name, float volume = 1.0f, float pitch = 0.0f, floa
soundEffect.Play(v, pitch, pan);
}

public string? CurrentMusic()
{
if(MediaPlayer.State != MediaState.Playing || MediaPlayer.Queue.ActiveSong is not {} activeSong)
return null;

return Songs.Keys.FirstOrDefault(k => Songs[k] == activeSong);
}

public void PlayMusic(string name, bool repeat = false)
{
if (!Songs.TryGetValue(name, out var song))
Expand Down
2 changes: 1 addition & 1 deletion Docs/tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Since .NET 7, this advice is less relevant, and sometimes plain bad: not only ha

Language and compiler writers have been working on performance for many many years. Before you make a performance-based decision based on suggestions from internet randos, do some research; if/when you're having performance issues, do your own benchmarking!

* PlayPlayMini comes with a [FrameCounter service](/api/BenMakesGames.PlayPlayMini.Services.FrameCounter.html) you can use to help keep an eye on how your changes affect your game's FPS!
* PlayPlayMini comes with a [FrameCounter service](/PlayPlayMini/api/BenMakesGames.PlayPlayMini.Services.FrameCounter.html) you can use to help keep an eye on how your changes affect your game's FPS!
* [BenchmarkDotNet](https://benchmarkdotnet.org/) is a great tool to learn when you've got a free afternoon.

### Prefer Composition over Inheritance
Expand Down

0 comments on commit ffd87ca

Please sign in to comment.