Example #1
0
        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();
            }
        }