Skip to content

Working with sound and input

MWstudios edited this page Jan 28, 2022 · 6 revisions

There are four pillars of what makes a game: Graphics, sound, input and logic. Starting with Ensoftener 2.0 and 3.0, the library isn't just about graphics, it's about sound and input as well. And since all three are solved, you may now focus only on game logic. Input and sound can be found under the Ensoftener.Input and Ensoftener.Sound namespaces, respectively.

Input

To get some info about your mouse cursor, simply use Input.MouseX, Input.MouseY and Input.[ButtonName].Held.

To get keyboard input, use Input.IsKeyPressed() or Input.IsKeyEnabled(). You might not know this, but Caps Lock, Num Lock and Scroll Lock aren't the only keys that have an on/off state. In fact, every key on the keyboard has an on/off state.

If you have an Xbox controller (like me), you can try getting inputs via XboxInput.Controller[n].IsButtonPressed(), as well as their joystick locations. You can also vibrate a controller via XboxInput.Controller[n].Vibrate().

Starting with Ensoftener 5.0, input supports fake inputs. Fake inputs can be enabled via Input.UseFakeInputs. After that, you can use methods such as Input.PressKey() or Input.LeftButton.Click().

Sound

There are currently three sound implementations - WindowsMediaPlayer, WPF MediaPlayer and CSCore's node-like wave and sample system. It's usually tedious to set them up, but once they're set up, they work well. For example, here's how you make a looping MP3:

using Ensoftener.Sound;

WMPSound sfx = new WMPSound("sound.mp3");
sfx.Loop = true;
sfx.Volume = 100;
sfx.Play();

Both classes share the exact same properties, so you may simply switch between those by changing the class name. However, not all properties are supported by all classes. Some properties take a longer time to process on some classes, so choose wisely.

Property WMPSound WPFSound CSCSound
Volume
Speed ✔ (Slower)
Position
Balance
Loop ✔ (Slower) ✔ (Fast) ✔ (Instant)
Length
Metadata

Advanced uses of CSCSound

CSCSound goes beyond just playing sound effects. Being an internal node system (like custom effects), it contains a Modifier.Modifier and a Modifier.ModifierAdvanced property, which can modify the sound signal and create custom effects.

Here is a good example of ModifierAdvanced. It accepts delegates with the sample source and sound data as parameters.

sfx.Modifier.ModifierAdvanced += (source, data) =>
{
	for (int i = data.Offset; i < data.Offset + data.Count; ++i) //every sample that's sent to be changed
		data.Buffer[i] = MathF.Min(data.Buffer[i] * 4, 1); //clipping the sound
};

How does it work?

Each feature described here was set up in a different way:

  • Mouse input is recieved via Global.Form.MouseMove and Global.Form.MouseWheel events. You can also set the mouse position, which is done via Cursor.Position. But before the mouse position is set, the coordinates need to be converted to screen coordinates.
  • Keyboard input is recieved via GetKeyboardState from user32.dll. I'm aware that there's a hundred other ways to do it, but this was, is and always will be the fastest way.
  • All controller inputs are done via XInputGetState and XInputSetState from xinput9_1_0.dll. This library was introduced with Windows Vista, so compatibility is not a problem.
  • WMPSound is a simpler variation of the WindowsMediaPlayer class from wmp.dll. All the methods and properties were already there, but you had to know where they are, because they were quite hidden in the sea of settings and subclasses.
  • WPFSound is a variation of MediaPlayer from PresentationCore.dll and WindowsBase.dll.
  • DirectSound is (or would be) a variation of SecondaryBuffer from Microsoft.DirectX.DirectSound. Unfortunately, as stated above, this library doesn't work in .NET 5 (but theoretically, if you were to get the assembly to work, it would run as intended).

Now you may argue that you can set this up yourself. Of course you can set this up yourself, just like everything else in here. But the point of Ensoftener is to simplify everything, to make it more achievable. And all the stuff that was done for SharpDX was really calling for this, because anyone could get easily lost.

In this wiki you can find out all the information on how to use the Ensoftener library. For more information on how to add Ensoftener to your project, see "Installing and running". The rest is dedicated to the library's features.

Notice: This wiki shows information for Ensoftener 5.0 and is currently outdated.

Clone this wiki locally