Example #1
0
        public void Run()
        {
            m_factory = new DisposeCollectorResourceFactory(m_gd.ResourceFactory);
            GraphicsDeviceCreated?.Invoke(m_gd, m_factory, m_gd.MainSwapchain);

            Stopwatch sw = Stopwatch.StartNew();
            double    previousElapsed = sw.Elapsed.TotalSeconds;

            while (m_window.Exists)
            {
                double newElapsed   = sw.Elapsed.TotalSeconds;
                float  deltaSeconds = (float)(newElapsed - previousElapsed);

                InputSnapshot inputSnapshot = m_window.PumpEvents();
                InputTracker.UpdateFrameInput(inputSnapshot);

                if (m_window.Exists)
                {
                    previousElapsed = newElapsed;
                    if (m_windowResized)
                    {
                        m_windowResized = false;
                        m_gd.ResizeMainWindow((uint)m_window.Width, (uint)m_window.Height);
                        Resized?.Invoke();
                    }

                    Rendering?.Invoke(deltaSeconds);
                }
            }

            m_gd.WaitForIdle();
            m_factory.DisposeCollector.DisposeAll();
            m_gd.Dispose();
            GraphicsDeviceDestroyed?.Invoke();
        }
Example #2
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            SwapchainSource      ss  = SwapchainSource.CreateUIView(this.View.Handle);
            SwapchainDescription scd = new SwapchainDescription(
                ss,
                (uint)View.Frame.Width,
                (uint)View.Frame.Height,
                PixelFormat.R32_Float,
                false);

            if (_backend == GraphicsBackend.Metal)
            {
                _gd = GraphicsDevice.CreateMetal(_options);
                _sc = _gd.ResourceFactory.CreateSwapchain(ref scd);
            }
            else if (_backend == GraphicsBackend.OpenGLES)
            {
                _gd = GraphicsDevice.CreateOpenGLES(_options, scd);
                _sc = _gd.MainSwapchain;
            }
            else if (_backend == GraphicsBackend.Vulkan)
            {
                throw new NotImplementedException();
            }

            GraphicsDeviceCreated?.Invoke(_gd, _gd.ResourceFactory, _sc);
            _viewLoaded = true;
        }
Example #3
0
        public void Run()
        {
            GraphicsDeviceOptions options = new GraphicsDeviceOptions(
                debug: false,
                swapchainDepthFormat: PixelFormat.R16_UNorm,
                syncToVerticalBlank: true,
                resourceBindingModel: ResourceBindingModel.Improved,
                preferDepthRangeZeroToOne: true,
                preferStandardClipSpaceYDirection: true);

#if DEBUG
            options.Debug = true;
#endif

            // construct a graphics device using the default backend.
            _gd = VeldridStartup.CreateGraphicsDevice(_window, options);

            // to determine what the default backend is you can run:
            //VeldridStartup.GetPlatformDefaultBackend()

            // to specify a specific backend use:
            //_gd = VeldridStartup.CreateGraphicsDevice(_window, options, GraphicsBackend.Metal);



            _factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory);
            GraphicsDeviceCreated?.Invoke(_gd, _factory, _gd.MainSwapchain);

            Stopwatch sw = Stopwatch.StartNew();
            double    previousElapsed = sw.Elapsed.TotalSeconds;
            while (IsWindowOpen())
            {
                double newElapsed   = sw.Elapsed.TotalSeconds;
                float  deltaSeconds = (float)(newElapsed - previousElapsed);

                InputSnapshot inputSnapshot = _window.PumpEvents();
                InputTracker.UpdateFrameInput(inputSnapshot);

                if (_window.Exists)
                {
                    previousElapsed = newElapsed;
                    if (_windowResized)
                    {
                        _windowResized = false;
                        _gd.ResizeMainWindow((uint)_window.Width, (uint)_window.Height);
                        Resized?.Invoke();
                    }

                    Rendering?.Invoke(deltaSeconds);
                }
            }

            Shutdown?.Invoke();

            _gd.WaitForIdle();
            _factory.DisposeCollector.DisposeAll();
            _gd.Dispose();
            GraphicsDeviceDestroyed?.Invoke();
        }
Example #4
0
        public void Run()
        {
            var options = new GraphicsDeviceOptions(
                false,
                PixelFormat.R16_UNorm,
                true,
                ResourceBindingModel.Improved,
                true,
                true);

#if DEBUG
            options.Debug = true;
#endif
            GraphicsDevice gd;
            if (_options.GraphicsBackend.HasValue)
            {
                gd = VeldridStartup.CreateGraphicsDevice(_window, options, _options.GraphicsBackend.Value);
            }
            else
            {
                gd = VeldridStartup.CreateGraphicsDevice(_window, options);
            }
            var factory = new DisposeCollectorResourceFactory(gd.ResourceFactory);

            _veldrid = new VeldridContext(gd, factory, gd.MainSwapchain);
            GraphicsDeviceCreated?.Invoke(_veldrid);

            var sw = Stopwatch.StartNew();
            var previousElapsed = sw.Elapsed.TotalSeconds;

            while (_window.Exists)
            {
                var newElapsed   = sw.Elapsed.TotalSeconds;
                var deltaSeconds = (float)(newElapsed - previousElapsed);

                var inputSnapshot = _window.PumpEvents();

                if (_window.Exists)
                {
                    previousElapsed = newElapsed;
                    if (_windowResized)
                    {
                        _windowResized = false;
                        gd.ResizeMainWindow((uint)_window.Width, (uint)_window.Height);
                        Resized?.Invoke();
                    }

                    Rendering?.Invoke(deltaSeconds);
                }
            }

            gd.WaitForIdle();
            factory.DisposeCollector.DisposeAll();
            gd.Dispose();
            GraphicsDeviceDestroyed?.Invoke();
        }
        private void CreateDevice()
        {
            var options      = new GraphicsDeviceOptions(true, PixelFormat.R32_Float, true, ResourceBindingModel.Improved);
            var logicalDpi   = 96.0f * CompositionScaleX;
            var renderWidth  = RenderSize.Width;
            var renderHeight = RenderSize.Height;

            _graphicsDevice = GraphicsDevice.CreateD3D11(options, this, renderWidth, renderHeight, logicalDpi);
            _resources      = new DisposeCollectorResourceFactory(_graphicsDevice.ResourceFactory);
            GraphicsDeviceCreated?.Invoke(_graphicsDevice, _resources, _graphicsDevice.MainSwapchain);
        }
Example #6
0
        private Game()
        {
            Content.RootDirectory = "Content";
            Window.Title          = "Hero6";
            var services = new MonoGameServiceLocator(Services);

            services.Add <IPlatformInfo, PlatformInfo>();
            services.Add <IServiceLocator>(services);
            services.Add <IFileWrapper, FileWrapper>();
            services.Add <IDirectoryWrapper, DirectoryWrapper>();
            ui = services.Make <MonoGameUserInterfaces>();
            services.Add <IUserInterfaces>(ui);
            gameSettings = new GameSettings(ui);
            services.Add <IGameSettings>(gameSettings);
            var userSettings = services.Make <UserSettings>(typeof(UserSettings));

            services.Add <IUserSettings>(userSettings);
            services.Add <ILoggerCore, LoggerCore>();
            logger = services.Make <Logger>(typeof(Logger));
            services.Add <IMouseCore, MouseCore>();
            services.Add <ILogger>(logger);
            services.Add <IControllerRepository, ControllerRepositoryProvider>();
            services.Add(Content);
            campaigns = services.Make <MonoGameCampaigns>();
            services.Add <ICampaigns>(campaigns);
            services.Add <IMouse, Mouse>();

            var graphics = new GraphicsDeviceManager(this)
            {
                PreferredBackBufferWidth  = userSettings.WindowWidth,
                PreferredBackBufferHeight = userSettings.WindowHeight,
                IsFullScreen    = userSettings.IsFullScreen,
                GraphicsProfile = GraphicsProfile.Reach,
#if ANDROID
                SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight
#endif
            };

            graphics.DeviceCreated += (s, a) =>
            {
                spriteBatch = new SpriteBatch(GraphicsDevice);
                services.Add(graphics);
                services.Add(spriteBatch);

                logger.Info("Graphics Device Created.");
                logger.Info($"Graphics Adapter Width {GraphicsDevice.Adapter.CurrentDisplayMode.Width}");
                logger.Info($"Graphics Adapter Height {GraphicsDevice.Adapter.CurrentDisplayMode.Height}");
                logger.Info($"Graphics Adapter Aspect Ratio {GraphicsDevice.Adapter.CurrentDisplayMode.AspectRatio}");
                GraphicsDeviceCreated?.Invoke(s, a);
            };

            logger.Info("Hero6 Game Instance Created.");
        }
        public void Run()
        {
            GraphicsDeviceOptions options = new GraphicsDeviceOptions(
                debug: false,
                swapchainDepthFormat: PixelFormat.R16_UNorm,
                syncToVerticalBlank: true,
                resourceBindingModel: ResourceBindingModel.Improved,
                preferDepthRangeZeroToOne: true,
                preferStandardClipSpaceYDirection: true);

#if DEBUG
            options.Debug = true;
#endif
            GD      = VeldridStartup.CreateGraphicsDevice(Window, options, GraphicsBackend.Direct3D11);
            Factory = new DisposeCollectorResourceFactory(GD.ResourceFactory);
            GraphicsDeviceCreated?.Invoke(GD, Factory, GD.MainSwapchain);

            Stopwatch sw = Stopwatch.StartNew();
            double    previousElapsed = sw.Elapsed.TotalSeconds;
            long      ticks           = 0;

            while (Window.Exists)
            {
                double newElapsed   = sw.Elapsed.TotalSeconds;
                float  deltaSeconds = (float)(newElapsed - previousElapsed);

                InputSnapshot inputSnapshot = Window.PumpEvents();
                InputTracker.UpdateFrameInput(inputSnapshot);

                if (Window.Exists)
                {
                    previousElapsed = newElapsed;
                    if (WindowResized)
                    {
                        WindowResized = false;
                        GD.ResizeMainWindow((uint)Window.Width, (uint)Window.Height);
                        Resized?.Invoke();
                    }

                    Rendering?.Invoke(deltaSeconds, ticks++);
                }
            }

            GD.WaitForIdle();
            Factory.DisposeCollector.DisposeAll();
            GD.Dispose();
            GraphicsDeviceDestroyed?.Invoke();
        }
Example #8
0
        public void Run()
        {
            GraphicsDeviceOptions options = new GraphicsDeviceOptions(
                debug: false,
                swapchainDepthFormat: PixelFormat.R16_UNorm,
                syncToVerticalBlank: true,
                resourceBindingModel: ResourceBindingModel.Improved);

#if DEBUG
            options.Debug = true;
#endif
            _gd      = VeldridStartup.CreateGraphicsDevice(_window, options);
            _factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory);
            GraphicsDeviceCreated?.Invoke(_gd, _factory, _gd.MainSwapchain);

            Stopwatch sw = Stopwatch.StartNew();
            double    previousElapsed = sw.Elapsed.TotalSeconds;

            while (_window.Exists)
            {
                double newElapsed   = sw.Elapsed.TotalSeconds;
                float  deltaSeconds = (float)(newElapsed - previousElapsed);

                InputSnapshot inputSnapshot = _window.PumpEvents();
                InputTracker.UpdateFrameInput(inputSnapshot);

                if (_window.Exists)
                {
                    previousElapsed = newElapsed;
                    if (_windowResized)
                    {
                        _windowResized = false;
                        _gd.ResizeMainWindow((uint)_window.Width, (uint)_window.Height);
                        Resized?.Invoke();
                    }

                    Rendering?.Invoke(deltaSeconds);
                }
            }

            _gd.WaitForIdle();
            _factory.DisposeCollector.DisposeAll();
            _gd.Dispose();
            GraphicsDeviceDestroyed?.Invoke();
        }
Example #9
0
 private void OnViewCreatedDevice()
 {
     _disposeFactory = _view.GraphicsDevice.ResourceFactory;
     GraphicsDeviceCreated?.Invoke(_view.GraphicsDevice, _disposeFactory, _view.MainSwapchain);
     Resized?.Invoke();
 }
Example #10
0
 private static void OnGraphicsDeviceCreated(object sender, EventArgs e)
 {
     GraphicsDeviceCreated?.Invoke(sender, e);
 }
Example #11
0
        public unsafe UserControl1()
        {
            try
            {
                InitializeComponent();
                if (this.DesignMode)
                {
                    return;
                }
                this.MouseWheel += UserControl1_MouseWheel;

                //释放组件
                Disposed += OnDispose;
                //this.MouseMove += UserControl1_MouseMove    ;
                GraphicsDeviceOptions options = new GraphicsDeviceOptions(
                    debug: false,
                    swapchainDepthFormat: PixelFormat.R16_UNorm,
                    syncToVerticalBlank: true,
                    resourceBindingModel: ResourceBindingModel.Improved);
#if DEBUG
                options.Debug = true;
#endif
                //获取当前窗体的Hwnd
                var hwnd = this.Handle;
                var p    = hwnd.ToPointer();
                //这里创建的sld windows消息捕获出现了问题,

                //获取运行进程的handle
                var instance = Process.GetCurrentProcess().Handle;
                _gd      = VeldridStartup.CreateVulkanGraphicsDeviceForWin32(options, hwnd, instance, this.Width, this.Height);
                _factory = new DisposeCollectorResourceFactory(_gd.ResourceFactory);

                _scene = new PongGlobe.Scene.Scene(this.Width, this.Height);
                var globeRender = new RayCastedGlobe(_scene);
                var shareRender = new ShareRender(_scene);
                this.renders.Add(shareRender);
                this.renders.Add(globeRender);

                //创建完相关对象后注册事件
                this.GraphicsDeviceCreated   += OnGraphicsDeviceCreated;
                this.GraphicsDeviceDestroyed += OnDeviceDestroyed;
                this.Rendering += PreDraw;
                this.Rendering += Draw;


                GraphicsDeviceCreated?.Invoke(_gd, _factory, _gd.MainSwapchain);
                _window = new Sdl2Window(hwnd, false);
                //创建一个计时器
                sw = Stopwatch.StartNew();
                previousElapsed = sw.Elapsed.TotalSeconds;
                //this.ParentForm.Shown += ParentForm_Shown;
                //开始运行
                //Run();
                //创建一个线程执行run,使用多线程之后便需要考虑不同线程变量同步的问题了,例如isrunning变量的修改需要lock之后再修改,有时可以使用线程安全的集合来处理不同线程的变量交换,
                //c#封装了很多,这些都不是问题_coomadList并不是线程安全的,在主线程创建,在子线程使用,这样问题并不大,当时两个子线程同时使用commandLits便可能出现问题
                //这里的渲染仍然是单线程的,每个不同的render都可以独开线程并提交渲染任务
                _renderTask = Task.Factory.StartNew(() => { Run(); });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }