Dithering, audio volume, and fix shutdown bug

This commit is contained in:
Michael 2017-05-13 10:22:51 -04:00
parent 94e1603b85
commit c0f0e99f9d
8 changed files with 177 additions and 74 deletions

View file

@ -35,6 +35,9 @@ namespace ShiftOS.Objects
public class Save
{
public int MusicVolume { get; set; }
public int SfxVolume { get; set; }
[Obsolete("This save variable is no longer used in Beta 2.4 and above of ShiftOS. Please use ShiftOS.Engine.SaveSystem.CurrentUser.Username to access the current user's username.")]
public string Username { get; set; }

View file

@ -491,9 +491,9 @@ namespace ShiftOS.WinForms.Applications
TerminalBackend.InStory = false;
TerminalBackend.PrintPrompt();
bool help_entered = false;
TerminalBackend.TextSent += (text) =>
TerminalBackend.CommandProcessed += (text, args) =>
{
if (text == "sos.help" && help_entered == false)
if (text.EndsWith("sos.help") && help_entered == false)
help_entered = true;
};
while (help_entered == false)
@ -523,10 +523,10 @@ namespace ShiftOS.WinForms.Applications
TerminalBackend.InStory = false;
bool winopenEntered = false;
TerminalBackend.PrintPrompt();
TerminalBackend.TextSent += (text) =>
TerminalBackend.CommandProcessed += (text, args) =>
{
if (help_entered == true)
if (text == "win.open" && winopenEntered == false)
if (text.EndsWith("win.open") && winopenEntered == false)
winopenEntered = true;
};
while (winopenEntered == false)

View file

@ -74,7 +74,19 @@ namespace ShiftOS.WinForms
MemoryStream str = null;
NAudio.Wave.Mp3FileReader mp3 = null;
NAudio.Wave.WaveOut o = null;
while (!Engine.SaveSystem.ShuttingDown)
bool shuttingDown = false;
Engine.AppearanceManager.OnExit += () =>
{
shuttingDown = true;
o?.Stop();
o?.Dispose();
mp3?.Close();
mp3?.Dispose();
str?.Close();
str?.Dispose();
};
while (shuttingDown == false)
{
str = new MemoryStream(GetRandomSong());
mp3 = new NAudio.Wave.Mp3FileReader(str);
@ -87,14 +99,15 @@ namespace ShiftOS.WinForms
c = true;
};
while (!c)
{
try
{
o.Volume = (float)Engine.SaveSystem.CurrentSave.MusicVolume / 100;
}
catch { }
Thread.Sleep(10);
str.Dispose();
o.Dispose();
mp3.Dispose();
}
}
str?.Dispose();
o?.Dispose();
mp3?.Dispose();
});
athread.IsBackground = true;
athread.Start();

View file

@ -68,11 +68,7 @@ namespace ShiftOS.WinForms
SkinEngine.SetIconProber(new ShiftOSIconProvider());
ShiftOS.Engine.AudioManager.Init(new ShiftOSAudioProvider());
Localization.RegisterProvider(new WFLanguageProvider());
AppearanceManager.OnExit += () =>
{
Environment.Exit(0);
};
TutorialManager.RegisterTutorial(new Oobe());
TerminalBackend.TerminalRequested += () =>

View file

@ -226,6 +226,53 @@ namespace ShiftOS.WinForms.Tools
}
#endif
public static int GetClosestColor(int gray, bool eightBits, bool sixBits, bool fourBits, bool twoBits)
{
int newgray = gray;
if (!eightBits)
{
if (sixBits)
{
newgray = gray >> 2;
}
else
{
if (fourBits)
{
newgray = (int)linear(gray, 0, 255, 0, 15) * 4;
}
else
{
if (twoBits)
{
if (gray > 127 + 63)
{
newgray = 255;
}
else if (gray > 127)
newgray = 127 + 63;
else if (gray > 63)
{
newgray = 127;
}
else if (gray > 0)
newgray = 63;
else
newgray = 0;
}
else
{
if (gray > 127)
newgray = 255;
else
newgray = 0;
}
}
}
}
return newgray;
}
#if FLOYDSTEINBERG
public static Image DitherImage(Image source)
{
@ -260,75 +307,78 @@ namespace ShiftOS.WinForms.Tools
bool eightBits = Shiftorium.UpgradeInstalled("color_depth_8_bits");
bool color_depth_floydsteinberg = Shiftorium.UpgradeInstalled("color_depth_floyd-steinberg_dithering");
bool dithering = Shiftorium.UpgradeInstalled("color_depth_dithering");
if (!sixteenBits)
bool twentyfourbits = Shiftorium.UpgradeInstalled("color_depth_24_bits");
if (twentyfourbits)
{
sourceArr.CopyTo(destArr, 0);
}
else
{
if (dithering == true)
{
if (false == true)
{
if (!sixteenBits)
{
if (dithering == true)
{
if (false == true)
{
}
else
{
int error = 0;
for (int i = 0; i < destArr.Length; i += 3)
{
byte r = sourceArr[i];
byte g = sourceArr[i + 1];
byte b = sourceArr[i + 2];
if (SkinEngine.LoadedSkin.SystemKey == Color.FromArgb(r, g, b))
{
destArr[i] = r;
destArr[i + 1] = g;
destArr[i + 2] = b;
continue;
}
int gray = (((r + g + b) / 3) + error);
int newgray = gray;
newgray = GetClosestColor(gray, eightBits, sixBits, fourBits, twoBits);
if (newgray > 255)
newgray = 255;
if (newgray < 0)
newgray = 0;
error = gray - newgray;
destArr[i] = (byte)newgray;
destArr[i + 1] = (byte)newgray;
destArr[i + 2] = (byte)newgray;
}
}
}
else
{
int error = 0;
for (int i = 0; i < destArr.Length; i += 3)
for (int i = 0; i < sourceArr.Length; i += 3)
{
byte r = sourceArr[i];
byte g = sourceArr[i + 1];
byte b = sourceArr[i + 2];
int gray = (((r + g + b) / 3) + error);
int newgray = gray;
if (!eightBits)
if (SkinEngine.LoadedSkin.SystemKey == Color.FromArgb(r, g, b))
{
if (sixBits)
{
newgray = gray >> 2;
}
else
{
if (fourBits)
{
newgray = (int)linear(gray, 0, 255, 0, 63) * 4;
}
else
{
if (twoBits)
{
if (gray > 127 + 63)
{
newgray = 255;
}
else if (gray > 127)
newgray = 127 + 63;
else if (gray > 63)
{
newgray = 127;
}
else if (gray > 0)
newgray = 63;
else
newgray = 0;
}
else
{
if (gray > 127)
newgray = 255;
else
newgray = 0;
}
}
}
destArr[i] = r;
destArr[i + 1] = g;
destArr[i + 2] = b;
continue;
}
if (newgray > 255)
newgray = 255;
if (newgray < 0)
newgray = 0;
error = gray - newgray;
int gray = (r + g + b) / 3;
int newgray = GetClosestColor(gray, eightBits, sixBits, fourBits, twoBits);
destArr[i] = (byte)newgray;
destArr[i+1] = (byte)newgray;
destArr[i+2] = (byte)newgray;
destArr[i + 1] = (byte)newgray;
destArr[i + 2] = (byte)newgray;
}
}

View file

@ -223,6 +223,7 @@ namespace ShiftOS.Engine
/// </summary>
internal static void Exit()
{
OnExit?.Invoke();
//disconnect from MUD
ServerManager.Disconnect();
Environment.Exit(0);

View file

@ -88,7 +88,13 @@ namespace ShiftOS.Engine
_reader = new AudioFileReader(file);
_out = new WaveOut();
_out.Init(_reader);
_out.Volume = _provider.Volume;
try
{
_out.Volume = (float)SaveSystem.CurrentSave.SfxVolume / 100;
}
catch
{
}
_out.Play();
_out.PlaybackStopped += (o, a) => { PlayCompleted?.Invoke(); };
}

View file

@ -378,6 +378,40 @@ namespace ShiftOS.Engine
[Namespace("sos")]
public static class ShiftOSCommands
{
[Command("setsfxvolume", description = "Set the volume of various sound effects to a value between 1 and 100.")]
[RequiresArgument("value")]
public static bool SetSfxVolume(Dictionary<string, object> args)
{
int value = int.Parse(args["value"].ToString());
if (value >= 0 && value <= 100)
{
SaveSystem.CurrentSave.SfxVolume = value;
SaveSystem.SaveGame();
}
else
{
Console.WriteLine("Volume must be between 0 and 100!");
}
return true;
}
[Command("setmusicvolume", description ="Set the music volume to a value between 1 and 100.")]
[RequiresArgument("value")]
public static bool SetMusicVolume(Dictionary<string, object> args)
{
int value = int.Parse(args["value"].ToString());
if(value >= 0 && value <= 100)
{
SaveSystem.CurrentSave.MusicVolume = value;
SaveSystem.SaveGame();
}
else
{
Console.WriteLine("Volume must be between 0 and 100!");
}
return true;
}
[RemoteLock]
[Command("shutdown")]
public static bool Shutdown()