Example #1
0
        public void Run()
        {
            long      previousFrameTicks = 0;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            while (_window.Exists)
            {
                long   currentFrameTicks = sw.ElapsedTicks;
                double deltaSeconds      = (currentFrameTicks - previousFrameTicks) / (double)Stopwatch.Frequency;

                while (_limitFrameRate && deltaSeconds < _desiredFrameLengthSeconds)
                {
                    currentFrameTicks = sw.ElapsedTicks;
                    deltaSeconds      = (currentFrameTicks - previousFrameTicks) / (double)Stopwatch.Frequency;
                }

                previousFrameTicks = currentFrameTicks;

                InputSnapshot snapshot = null;
                Sdl2Events.ProcessEvents();
                snapshot = _window.PumpEvents();
                InputTracker.UpdateFrameInput(snapshot, _window);
                Update((float)deltaSeconds);
                if (!_window.Exists)
                {
                    break;
                }

                Draw();
            }

            DestroyAllObjects();
            _gd.Dispose();
        }
Example #2
0
    public void PumpEvents(double deltaSeconds)
    {
        SetTitle();
        Sdl2Events.ProcessEvents();
        var snapshot = _window.PumpEvents();

        if (_window != null)
        {
            if (_pendingCursorUpdate.HasValue && _window.Focused)
            {
                using (PerfTracker.FrameEvent("3 Warping mouse"))
                {
                    Sdl2Native.SDL_WarpMouseInWindow(
                        _window.SdlWindowHandle,
                        (int)_pendingCursorUpdate.Value.X,
                        (int)_pendingCursorUpdate.Value.Y);

                    _pendingCursorUpdate = null;
                }
            }

            using (PerfTracker.FrameEvent("4 Raising input event"))
                Raise(new InputEvent(deltaSeconds, snapshot, _window.MouseDelta));
        }
    }
 public unsafe Sdl2ControllerTracker(int index)
 {
     _controllerIndex = index;
     _controller      = SDL_GameControllerOpen(index);
     ControllerName   = Marshal.PtrToStringUTF8((IntPtr)SDL_GameControllerName(_controller));
     Sdl2Events.Subscribe(ProcessEvent);
 }
Example #4
0
        public void Run()
        {
            SetupCSharpDefaults();
            SetupParamStudioConfig();
            //new StudioServer();
            long      previousFrameTicks = 0;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            while (_window.Exists)
            {
                bool focused = _window.Focused;
                if (!focused)
                {
                    _desiredFrameLengthSeconds = 1.0 / 20.0f;
                }
                else
                {
                    _desiredFrameLengthSeconds = 1.0 / 60.0f;
                }
                long   currentFrameTicks = sw.ElapsedTicks;
                double deltaSeconds      = (currentFrameTicks - previousFrameTicks) / (double)Stopwatch.Frequency;

                while (_limitFrameRate && deltaSeconds < _desiredFrameLengthSeconds)
                {
                    currentFrameTicks = sw.ElapsedTicks;
                    deltaSeconds      = (currentFrameTicks - previousFrameTicks) / (double)Stopwatch.Frequency;
                    System.Threading.Thread.Sleep(focused ? 0 : 1);
                }

                previousFrameTicks = currentFrameTicks;

                InputSnapshot snapshot = null;
                Sdl2Events.ProcessEvents();
                snapshot = _window.PumpEvents();
                InputTracker.UpdateFrameInput(snapshot, _window);
                Update((float)deltaSeconds);
                if (!_window.Exists)
                {
                    break;
                }

                if (_window.Focused)
                {
                    Draw();
                }
                else
                {
                    // Flush the background queues
                    Renderer.Frame(null, true);
                }
            }

            //DestroyAllObjects();
            _gd.Dispose();
            SaveParamStudioConfig();
            CFG.Save();
            System.Windows.Forms.Application.Exit();
        }
Example #5
0
        public unsafe Sdl2ControllerTracker(int index)
        {
            _controller = SDL_GameControllerOpen(index);
            SDL_Joystick joystick = SDL_GameControllerGetJoystick(_controller);

            _controllerIndex = SDL_JoystickInstanceID(joystick);
            ControllerName   = Marshal.PtrToStringUTF8((IntPtr)SDL_GameControllerName(_controller));
            Sdl2Events.Subscribe(ProcessEvent);
        }
Example #6
0
        public Sdl2EventProcessor(ICoreMessenger coreMessenger,
                                  IApplicationMessenger applicationMessenger,
                                  IInputGameController inputGameController,
                                  IInputMouseKeyboard inputMouseAndKeyboard)
        {
            _coreMessenger         = coreMessenger;
            _applicationMessenger  = applicationMessenger;
            _inputGameController   = inputGameController;
            _inputMouseAndKeyboard = inputMouseAndKeyboard;

            Sdl2Events.Subscribe(ProcessEvent);
        }
Example #7
0
        public void Run()
        {
            /*Task.Run(() =>
             * {
             *  while (true)
             *  {
             *      Thread.Sleep(5000);
             *      GC.Collect();
             *      GC.WaitForPendingFinalizers();
             *
             *      GC.Collect();
             *  }
             * });*/

            // Flush geometry megabuffers for editor geometry
            //Renderer.GeometryBufferAllocator.FlushStaging();

            long      previousFrameTicks = 0;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            while (_window.Exists)
            {
                bool focused = _window.Focused;
                if (!focused)
                {
                    _desiredFrameLengthSeconds = 1.0 / 20.0f;
                }
                else
                {
                    _desiredFrameLengthSeconds = 1.0 / 60.0f;
                }
                long   currentFrameTicks = sw.ElapsedTicks;
                double deltaSeconds      = (currentFrameTicks - previousFrameTicks) / (double)Stopwatch.Frequency;

                while (_limitFrameRate && deltaSeconds < _desiredFrameLengthSeconds)
                {
                    currentFrameTicks = sw.ElapsedTicks;
                    deltaSeconds      = (currentFrameTicks - previousFrameTicks) / (double)Stopwatch.Frequency;
                    System.Threading.Thread.Sleep(focused ? 0 : 1);
                }

                previousFrameTicks = currentFrameTicks;

                InputSnapshot snapshot = null;
                Sdl2Events.ProcessEvents();
                snapshot = _window.PumpEvents();
                InputTracker.UpdateFrameInput(snapshot, _window);
                Update((float)deltaSeconds);
                if (!_window.Exists)
                {
                    break;
                }

                if (_window.Focused)
                {
                    Draw();
                }
                else
                {
                    // Flush the background queues
                    Renderer.Frame(null, true);
                }
            }

            //DestroyAllObjects();
            Resource.ResourceManager.Shutdown();
            _gd.Dispose();
            CFG.Save();

            System.Windows.Forms.Application.Exit();
        }
Example #8
0
 public override void Dispose()
 {
     Sdl2Events.Unsubscribe(ProcessEvent);
     SDL_GameControllerClose(_controller);
 }
Example #9
0
 public void Shutdown()
 {
     Sdl2Events.Unsubscribe(ProcessEvent);
 }
Example #10
0
 public void ProcessEvents()
 {
     Sdl2Events.ProcessEvents();
 }
Example #11
0
        public void Run()
        {
            ChangeBackend();
            PerfTracker.StartupEvent("Set up backend");
            Sdl2Native.SDL_Init(SDLInitFlags.GameController);
            ImGui.StyleColorsClassic();
            Raise(new WindowResizedEvent(Window.Width, Window.Height));
            Raise(new BeginFrameEvent());

            var frameCounter = new FrameCounter();

            PerfTracker.StartupEvent("Startup done, rendering first frame");
            while (!_done)
            {
                ChangeBackend();

                PerfTracker.BeginFrame();
                double deltaSeconds = frameCounter.StartFrame();
                using (PerfTracker.FrameEvent("1 Raising begin frame"))
                    Raise(new BeginFrameEvent());

                InputSnapshot snapshot;
                using (PerfTracker.FrameEvent("2 Processing SDL events"))
                {
                    Sdl2Events.ProcessEvents();
                    snapshot = Window.PumpEvents();
                }

                if (!Window.Exists)
                {
                    break;
                }

                if (_pendingCursorUpdate.HasValue)
                {
                    using (PerfTracker.FrameEvent("3 Warping mouse"))
                    {
                        Sdl2Native.SDL_WarpMouseInWindow(
                            Window.SdlWindowHandle,
                            (int)_pendingCursorUpdate.Value.X,
                            (int)_pendingCursorUpdate.Value.Y);

                        _pendingCursorUpdate = null;
                    }
                }

                using (PerfTracker.FrameEvent("4 Raising input event"))
                    Raise(new InputEvent(deltaSeconds, snapshot, Window.MouseDelta));

                using (PerfTracker.FrameEvent("5 Performing update"))
                    Update((float)deltaSeconds);

                if (!Window.Exists)
                {
                    break;
                }

                using (PerfTracker.FrameEvent("6 Drawing"))
                    Draw();

                var flags = Resolve <IEngineSettings>().Flags;
                if (GraphicsDevice.SyncToVerticalBlank != flags.HasFlag(EngineFlags.VSync))
                {
                    GraphicsDevice.SyncToVerticalBlank = flags.HasFlag(EngineFlags.VSync);
                }

                using (PerfTracker.FrameEvent("7 Swap buffers"))
                {
                    CoreTrace.Log.Info("Engine", "Swapping buffers...");
                    GraphicsDevice.SwapBuffers();
                    CoreTrace.Log.Info("Engine", "Draw complete");
                }
            }

            DestroyAllObjects();
            GraphicsDevice.Dispose();
            Window.Close();
        }
Example #12
0
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("dotnet AgbSharp.Gui.dll <bootrom> <rom>");
                return;
            }

            // Create a window to render to using Veldrid
            WindowCreateInfo windowCreateInfo = new WindowCreateInfo()
            {
                X            = 100,
                Y            = 100,
                WindowWidth  = 240 * 2,
                WindowHeight = 160 * 2,
                WindowTitle  = "AgbSharp.Gui"
            };

            // Create a window to render to using Veldrid
            Sdl2Window     window;
            GraphicsDevice graphicsDevice;

            VeldridStartup.CreateWindowAndGraphicsDevice(windowCreateInfo, out window, out graphicsDevice);

            Renderer renderer = new Renderer(graphicsDevice);

#if DEBUG
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
#endif

            AgbDevice agbDevice = new AgbDevice();

            agbDevice.LoadBios(File.ReadAllBytes(args[0]));
            agbDevice.LoadRom(File.ReadAllBytes(args[1]));

            // Run emulation
            while (window.Exists)
            {
#if DEBUG
                double newElapsed = stopwatch.Elapsed.TotalMilliseconds;
#endif

                agbDevice.RunFrame();

#if DEBUG
                // Choppy audio, but we shouldn't need this for debugging anyway
                while (stopwatch.Elapsed.TotalMilliseconds - newElapsed < 16.7)
                {
                    ;
                }
#endif

                Sdl2Events.ProcessEvents();

                InputSnapshot snapshot = window.PumpEvents();
                foreach (KeyEvent keyEvent in snapshot.KeyEvents)
                {
                    switch (keyEvent.Key)
                    {
                    case Key.W:
                        agbDevice.Controller.UpdateKeyState(ControllerKey.Up, keyEvent.Down);
                        break;

                    case Key.S:
                        agbDevice.Controller.UpdateKeyState(ControllerKey.Down, keyEvent.Down);
                        break;

                    case Key.A:
                        agbDevice.Controller.UpdateKeyState(ControllerKey.Left, keyEvent.Down);
                        break;

                    case Key.D:
                        agbDevice.Controller.UpdateKeyState(ControllerKey.Right, keyEvent.Down);
                        break;

                    case Key.Minus:
                        agbDevice.Controller.UpdateKeyState(ControllerKey.Select, keyEvent.Down);
                        break;

                    case Key.Plus:
                        agbDevice.Controller.UpdateKeyState(ControllerKey.Start, keyEvent.Down);
                        break;

                    case Key.Comma:
                        agbDevice.Controller.UpdateKeyState(ControllerKey.B, keyEvent.Down);
                        break;

                    case Key.Period:
                        agbDevice.Controller.UpdateKeyState(ControllerKey.A, keyEvent.Down);
                        break;

                    case Key.K:
                        agbDevice.Controller.UpdateKeyState(ControllerKey.L, keyEvent.Down);
                        break;

                    case Key.L:
                        agbDevice.Controller.UpdateKeyState(ControllerKey.R, keyEvent.Down);
                        break;
                    }
                }

                renderer.Draw(agbDevice.Ppu.Framebuffer);

                // TODO
                // Update controllers
            }

#if DEBUG
            stopwatch.Stop();
#endif

            renderer.DisposeResources();
        }