public void RestartGraphics(bool full) { pngFont = IniFile.GetValue("misc", "font", "8x16-bold"); if (!Mix.FileExists("fonts\\" + pngFont + ".png")) { pngFont = "8x16-bold"; if (!Mix.FileExists("fonts\\" + pngFont + ".png")) { SystemMessageBox.Show(this, "Could not find font bitmaps. Please redownload the game.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); Close(); return; } } var fontBitmap = Mix.GetBitmap("fonts\\" + pngFont + ".png"); cellWidth = fontBitmap.Width / 32; cellHeight = fontBitmap.Height / 32; IsSquare = cellWidth == cellHeight; Program.Cols = IniFile.GetValue("misc", "screencols", Program.Cols); Program.Rows = IniFile.GetValue("misc", "screenrows", Program.Rows); if (full) { image = new Cell[Program.Cols, Program.Rows]; previousImage = new Cell[Program.Cols, Program.Rows]; } CachePNGFont(fontBitmap); fourThirtySeven = IniFile.GetValue("misc", "437", false); youtube = IniFile.GetValue("misc", "youtube", false); ClientSize = new Size(Program.Cols * cellWidth, Program.Rows * cellHeight); if (youtube) { //Find nearest YT size var eW = Program.Cols * cellWidth; var eH = Program.Rows * cellHeight; if (eW <= 854 || eH <= 480) { ClientSize = new Size(854, 480); } else if (eW <= 1280 || eH <= 720) { ClientSize = new Size(1280, 720); } else { ClientSize = new Size(1920, 1080); } var prime = Screen.FromRectangle(ClientRectangle).Bounds; if (ClientSize.Width == prime.Width && ClientSize.Height == prime.Height) { FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; Left = Top = 0; } else { FormBorderStyle = IniFile.GetValue("misc", "border", true) ? System.Windows.Forms.FormBorderStyle.FixedSingle : System.Windows.Forms.FormBorderStyle.None; } youtubeRect = new System.Drawing.Rectangle((ClientSize.Width / 2) - (eW / 2), (ClientSize.Height / 2) - (eH / 2), eW, eH); } else { FormBorderStyle = IniFile.GetValue("misc", "border", true) ? System.Windows.Forms.FormBorderStyle.FixedSingle : System.Windows.Forms.FormBorderStyle.None; } Show(); Refresh(); backBuffer = new Bitmap(Program.Cols * cellWidth, Program.Rows * cellHeight, PixelFormat.Format24bppRgb); scrollBuffer = new Bitmap(Program.Cols * cellWidth, Program.Rows * cellHeight, PixelFormat.Format24bppRgb); for (int row = 0; row < Program.Rows; row++) { for (int col = 0; col < Program.Cols; col++) { previousImage[col, row].Character = '\uFFFE'; } } }
public void PlaySound(string name) { if (!Enabled || musicVolume == 0) { return; } if (name.StartsWith("set://")) { name = name.Substring(6); var setNode = soundLibrary.FirstOrDefault(x => x.Name == name); if (setNode == null) { name = "-"; } else { name = "-"; var unmarked = new List <Token>(); foreach (var t in setNode.Tokens) { if (!t.HasToken("used")) { unmarked.Add(t); } } if (unmarked.Count == 0) { foreach (var t in setNode.Tokens) { t.RemoveToken("used"); unmarked.Add(t); } } while (unmarked.Count > 0) { var pick = Random.Next(unmarked.Count); var file = unmarked[pick].Name; if (!Mix.FileExists(file)) { unmarked.RemoveAt(pick); } else { name = file; unmarked[pick].AddToken("used"); break; } } } } if (sounds.ContainsKey(name) && sounds[name] == null) { return; } if (!sounds.ContainsKey(name)) { if (Mix.FileExists(name)) { sounds.Add(name, new Sound(name, system)); } else { sounds.Add(name, null); return; } } sounds[name].Play(); sounds[name].Channel.SetVolume(soundVolume); }
public MainForm() { #if !DEBUG try #endif { this.Text = "Noxico"; this.BackColor = System.Drawing.Color.Black; this.DoubleBuffered = true; this.FormBorderStyle = FormBorderStyle.FixedSingle; this.MaximizeBox = false; //it's about time, too! this.FormClosing += new FormClosingEventHandler(this.Form1_FormClosing); this.KeyDown += new KeyEventHandler(this.Form1_KeyDown); this.KeyPress += new KeyPressEventHandler(this.Form1_KeyPress); this.KeyUp += new KeyEventHandler(this.Form1_KeyUp); this.Icon = global::Noxico.Properties.Resources.app; this.ClientSize = new Size(Program.Cols * cellWidth, Program.Rows * cellHeight); this.Controls.Add(new Label() { Text = "Loading...", AutoSize = true, Font = new System.Drawing.Font("Arial", 24, FontStyle.Bold | FontStyle.Italic), ForeColor = System.Drawing.Color.White, Visible = true, Location = new System.Drawing.Point(16, 16) }); foreach (var reqDll in new[] { "Neo.Lua.dll" }) { if (!File.Exists(reqDll)) { throw new FileNotFoundException("Required DLL " + reqDll + " is missing."); } } try { Mix.Initialize("Noxico"); } catch (UnauthorizedAccessException) { if (!UacHelper.IsProcessElevated) { var proc = new System.Diagnostics.ProcessStartInfo(); proc.UseShellExecute = true; proc.WorkingDirectory = Environment.CurrentDirectory; proc.FileName = Application.ExecutablePath; proc.Verb = "runas"; try { System.Diagnostics.Process.Start(proc); } catch { } } Close(); return; } if (!Mix.FileExists("credits.txt")) { SystemMessageBox.Show(this, "Could not find game data. Please redownload the game.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); Close(); return; } var portable = false; IniPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "noxico.ini"); if (File.Exists("portable")) { portable = true; var oldIniPath = IniPath; IniPath = "noxico.ini"; Program.CanWrite(); /* * if (!Program.CanWrite()) * { * var response = SystemMessageBox.Show(this, "Trying to start in portable mode, but from a protected location. Use non-portable mode?" + Environment.NewLine + "Selecting \"no\" may cause errors.", Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); * if (response == DialogResult.Cancel) * { * Close(); * return; * } * else if (response == DialogResult.Yes) * { * IniPath = oldIniPath; * portable = false; * } * } */ } if (!File.Exists(IniPath)) { File.WriteAllText(IniPath, Mix.GetString("noxico.ini")); } IniFile.Load(IniPath); if (portable) { IniFile.SetValue("misc", "vistasaves", false); IniFile.SetValue("misc", "savepath", "./saves"); IniFile.SetValue("misc", "shotpath", "./screenshots"); } RestartGraphics(true); fourThirtySeven = IniFile.GetValue("misc", "437", false); Noxico = new NoxicoGame(); Noxico.Initialize(this); MouseUp += new MouseEventHandler(MainForm_MouseUp); MouseWheel += new MouseEventHandler(MainForm_MouseWheel); GotFocus += (s, e) => { Vista.GamepadFocused = true; }; LostFocus += (s, e) => { Vista.GamepadFocused = false; }; Vista.GamepadEnabled = IniFile.GetValue("misc", "xinput", true); Program.WriteLine("Environment: {0} {1}", Environment.OSVersion.Platform, Environment.OSVersion); Program.WriteLine("Application: {0}", Application.ProductVersion); if (Environment.OSVersion.Platform == PlatformID.Unix) { Program.WriteLine("*** You are running on a *nix system. ***"); Program.WriteLine("Key repeat delays exaggerated."); NoxicoGame.Mono = true; Vista.GamepadEnabled = false; } this.Controls.Clear(); starting = false; Running = true; Cursor = new Point(-1, -1); cursorPens = new Pen[3, 16]; cursorPens[0, 0] = cursorPens[1, 0] = cursorPens[2, 0] = Pens.Black; for (var i = 1; i < 9; i++) { cursorPens[0, i] = cursorPens[0, 16 - i] = new Pen(Color.FromArgb((i * 16) - 1, (i * 16) - 1, 0)); cursorPens[1, i] = cursorPens[1, 16 - i] = new Pen(Color.FromArgb(0, (i * 32) - 1, 0)); cursorPens[2, i] = cursorPens[2, 16 - i] = new Pen(Color.FromArgb((i * 32) - 1, (i * 32) - 1, (i * 32) - 1)); } fpsTimer = new Timer() { Interval = 1000, Enabled = true, }; fpsTimer.Tick += (s, e) => { this.Text = "Noxico - " + NoxicoGame.Updates + " updates, " + Frames + " frames"; NoxicoGame.Updates = 0; Frames = 0; }; #if GAMELOOP while (Running) { Noxico.Update(); Application.DoEvents(); } #else FormClosing += new FormClosingEventHandler(MainForm_FormClosing); var speed = IniFile.GetValue("misc", "speed", 15); if (speed <= 0) { speed = 15; } timer = new Timer() { Interval = speed, Enabled = true, }; timer.Tick += new EventHandler(timer_Tick); #endif } #if !DEBUG catch (Exception x) { new ErrorForm(x).ShowDialog(this); SystemMessageBox.Show(this, x.ToString(), Application.ProductName, MessageBoxButtons.OK); Running = false; fatal = true; Application.ExitThread(); } #endif if (!fatal) { Noxico.SaveGame(); } }
public void PlayMusic(string name, bool fade = true) { if (!Enabled || musicVolume == 0) { return; } var set = targetSet; if (name.StartsWith("set://")) { set = name.Substring(6); var setNode = musicLibrary.FirstOrDefault(x => x.Name == set); if (setNode == null) { name = "-"; } else { name = "-"; var unmarked = new List <Token>(); foreach (var t in setNode.Tokens) { if (!t.HasToken("used")) { unmarked.Add(t); } } if (unmarked.Count == 0) { foreach (var t in setNode.Tokens) { t.RemoveToken("used"); unmarked.Add(t); } } while (unmarked.Count > 0) { var pick = Random.Next(unmarked.Count); var file = unmarked[pick].Name; if (!Mix.FileExists(file)) { unmarked.RemoveAt(pick); } else { name = file; unmarked[pick].AddToken("used"); break; } } } } if (set == currentSet) { return; } if (fade && string.IsNullOrEmpty(FadeTarget) && musicChannel != null && name != musicPlaying) { FadeTarget = name; targetSet = set; fadeProcess = 300; return; } FadeTarget = null; if (name == "-") { if (musicChannel != null) { musicChannel.Stop(); } if (music != null) { music.Release(); } currentSet = string.Empty; musicPlaying = string.Empty; } else if (musicPlaying != name) { if (Mix.FileExists(name)) { if (musicChannel != null) { musicChannel.Stop(); } if (music != null) { music.Release(); } var data = Mix.GetBytes(name); var fCSex = new FMOD.CreateSoundExInfo() { Size = 216, Length = (uint)data.Length }; CheckError(system.CreateSound(data, FMOD.SoundMode.LoopNormal | FMOD.SoundMode.OpenMemory, ref fCSex, ref music)); system.PlaySound(music, false, ref musicChannel); musicPlaying = name; currentSet = set; musicChannel.SetVolume(musicVolume); //NoxicoGame.HostForm.Text = file; } else { Program.WriteLine("PlayMusic: couldn't load song \"{0}\".", name); } } }