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 = _window.PumpEvents(); InputTracker.UpdateFrameInput(snapshot); Update((float)deltaSeconds); if (!_window.Exists) { break; } Draw(); } DestroyAllObjects(); _gd.Dispose(); }
public void Update(float deltaSeconds) { float sprintFactor = InputTracker.GetKey(Key.ControlLeft) ? 0.1f : InputTracker.GetKey(Key.ShiftLeft) ? 2.5f : 1f; Vector3 motionDir = Vector3.Zero; if (InputTracker.GetKey(Key.A)) { motionDir += -Vector3.UnitX; } if (InputTracker.GetKey(Key.D)) { motionDir += Vector3.UnitX; } if (InputTracker.GetKey(Key.W)) { motionDir += -Vector3.UnitZ; } if (InputTracker.GetKey(Key.S)) { motionDir += Vector3.UnitZ; } if (InputTracker.GetKey(Key.Q)) { motionDir += -Vector3.UnitY; } if (InputTracker.GetKey(Key.E)) { motionDir += Vector3.UnitY; } if (motionDir != Vector3.Zero) { Quaternion lookRotation = Quaternion.CreateFromYawPitchRoll(_yaw, _pitch, 0f); motionDir = Vector3.Transform(motionDir, lookRotation); _position += motionDir * _moveSpeed * sprintFactor * deltaSeconds; UpdateViewMatrix(); } Vector2 mouseDelta = InputTracker.MousePosition - _previousMousePos; _previousMousePos = InputTracker.MousePosition; if (!ImGui.IsMouseHoveringAnyWindow() && (InputTracker.GetMouseButton(MouseButton.Left) || InputTracker.GetMouseButton(MouseButton.Right))) { _yaw += -mouseDelta.X * 0.01f; _pitch += -mouseDelta.Y * 0.01f; _pitch = Math.Clamp(_pitch, -1.55f, 1.55f); Quaternion lookRotation = Quaternion.CreateFromYawPitchRoll(_yaw, _pitch, 0f); Vector3 lookDir = Vector3.Transform(-Vector3.UnitZ, lookRotation); _lookDirection = lookDir; UpdateViewMatrix(); } }
public void Run() { while (_window.Exists) { InputTracker.UpdateFrameInput(_window.PumpEvents()); Update(1f / 60f); Draw(); } }
private void Update(float deltaSeconds) { _fta.AddTime(deltaSeconds); _scene.Update(deltaSeconds); if (ImGui.BeginMainMenuBar()) { if (ImGui.BeginMenu("Settings")) { if (ImGui.BeginMenu("Graphics Backend")) { if (ImGui.MenuItem("Vulkan", string.Empty, _gd.BackendType == GraphicsBackend.Vulkan, GraphicsDevice.IsBackendSupported(GraphicsBackend.Vulkan))) { ChangeBackend(GraphicsBackend.Vulkan); } if (ImGui.MenuItem("OpenGL", string.Empty, _gd.BackendType == GraphicsBackend.OpenGL, GraphicsDevice.IsBackendSupported(GraphicsBackend.OpenGL))) { ChangeBackend(GraphicsBackend.OpenGL); } if (ImGui.MenuItem("OpenGL ES", string.Empty, _gd.BackendType == GraphicsBackend.OpenGLES, GraphicsDevice.IsBackendSupported(GraphicsBackend.OpenGLES))) { ChangeBackend(GraphicsBackend.OpenGLES); } if (ImGui.MenuItem("Direct3D 11", string.Empty, _gd.BackendType == GraphicsBackend.Direct3D11, GraphicsDevice.IsBackendSupported(GraphicsBackend.Direct3D11))) { ChangeBackend(GraphicsBackend.Direct3D11); } if (ImGui.MenuItem("Metal", string.Empty, _gd.BackendType == GraphicsBackend.Metal, GraphicsDevice.IsBackendSupported(GraphicsBackend.Metal))) { ChangeBackend(GraphicsBackend.Metal); } ImGui.EndMenu(); } if (ImGui.BeginMenu("MSAA")) { if (ImGui.Combo("MSAA", ref _msaaOption, _msaaOptions, _msaaOptions.Length)) { ChangeMsaa(_msaaOption); } ImGui.EndMenu(); } bool threadedRendering = _scene.ThreadedRendering; if (ImGui.MenuItem("Render with multiple threads", string.Empty, threadedRendering, true)) { _scene.ThreadedRendering = !_scene.ThreadedRendering; } bool tinted = _fsq.UseTintedTexture; if (ImGui.MenuItem("Tinted output", string.Empty, tinted, true)) { _fsq.UseTintedTexture = !tinted; } ImGui.EndMenu(); } if (ImGui.BeginMenu("Window")) { bool isFullscreen = _window.WindowState == WindowState.BorderlessFullScreen; if (ImGui.MenuItem("Fullscreen", "F11", isFullscreen, true)) { ToggleFullscreenState(); } if (ImGui.MenuItem("Always Recreate Sdl2Window", string.Empty, _recreateWindow, true)) { _recreateWindow = !_recreateWindow; } if (ImGui.IsItemHovered()) { ImGui.SetTooltip( "Causes a new OS window to be created whenever the graphics backend is switched. This is much safer, and is the default."); } if (ImGui.MenuItem("sRGB Swapchain Format", string.Empty, _colorSrgb, true)) { _colorSrgb = !_colorSrgb; ChangeBackend(_gd.BackendType); } bool vsync = _gd.SyncToVerticalBlank; if (ImGui.MenuItem("VSync", string.Empty, vsync, true)) { _gd.SyncToVerticalBlank = !_gd.SyncToVerticalBlank; } bool resizable = _window.Resizable; if (ImGui.MenuItem("Resizable Window", string.Empty, resizable)) { _window.Resizable = !_window.Resizable; } bool bordered = _window.BorderVisible; if (ImGui.MenuItem("Visible Window Border", string.Empty, bordered)) { _window.BorderVisible = !_window.BorderVisible; } ImGui.EndMenu(); } if (ImGui.BeginMenu("Materials")) { if (ImGui.BeginMenu("Brick")) { DrawIndexedMaterialMenu(CommonMaterials.Brick); ImGui.EndMenu(); } if (ImGui.BeginMenu("Vase")) { DrawIndexedMaterialMenu(CommonMaterials.Vase); ImGui.EndMenu(); } if (ImGui.BeginMenu("Reflective")) { DrawIndexedMaterialMenu(CommonMaterials.Reflective); ImGui.EndMenu(); } ImGui.EndMenu(); } if (ImGui.BeginMenu("Debug")) { if (ImGui.MenuItem("Refresh Device Objects")) { RefreshDeviceObjects(1); } if (ImGui.MenuItem("Refresh Device Objects (10 times)")) { RefreshDeviceObjects(10); } if (ImGui.MenuItem("Refresh Device Objects (100 times)")) { RefreshDeviceObjects(100); } if (_controllerTracker != null) { if (ImGui.MenuItem("Controller State")) { _controllerDebugMenu = true; } } else { if (ImGui.MenuItem("Connect to Controller")) { Sdl2ControllerTracker.CreateDefault(out _controllerTracker); _scene.Camera.Controller = _controllerTracker; } } ImGui.EndMenu(); } if (ImGui.BeginMenu("RenderDoc")) { if (_renderDoc == null) { if (ImGui.MenuItem("Load")) { if (RenderDoc.Load(out _renderDoc)) { ChangeBackend(_gd.BackendType); } } } else { if (ImGui.MenuItem("Trigger Capture")) { _renderDoc.TriggerCapture(); } if (ImGui.BeginMenu("Options")) { bool allowVsync = _renderDoc.AllowVSync; if (ImGui.Checkbox("Allow VSync", ref allowVsync)) { _renderDoc.AllowVSync = allowVsync; } bool validation = _renderDoc.APIValidation; if (ImGui.Checkbox("API Validation", ref validation)) { _renderDoc.APIValidation = validation; } int delayForDebugger = (int)_renderDoc.DelayForDebugger; if (ImGui.InputInt("Debugger Delay", ref delayForDebugger)) { delayForDebugger = Math.Clamp(delayForDebugger, 0, int.MaxValue); _renderDoc.DelayForDebugger = (uint)delayForDebugger; } bool verifyBufferAccess = _renderDoc.VerifyBufferAccess; if (ImGui.Checkbox("Verify Buffer Access", ref verifyBufferAccess)) { _renderDoc.VerifyBufferAccess = verifyBufferAccess; } bool overlayEnabled = _renderDoc.OverlayEnabled; if (ImGui.Checkbox("Overlay Visible", ref overlayEnabled)) { _renderDoc.OverlayEnabled = overlayEnabled; } bool overlayFrameRate = _renderDoc.OverlayFrameRate; if (ImGui.Checkbox("Overlay Frame Rate", ref overlayFrameRate)) { _renderDoc.OverlayFrameRate = overlayFrameRate; } bool overlayFrameNumber = _renderDoc.OverlayFrameNumber; if (ImGui.Checkbox("Overlay Frame Number", ref overlayFrameNumber)) { _renderDoc.OverlayFrameNumber = overlayFrameNumber; } bool overlayCaptureList = _renderDoc.OverlayCaptureList; if (ImGui.Checkbox("Overlay Capture List", ref overlayCaptureList)) { _renderDoc.OverlayCaptureList = overlayCaptureList; } ImGui.EndMenu(); } if (ImGui.MenuItem("Launch Replay UI")) { _renderDoc.LaunchReplayUI(); } } ImGui.EndMenu(); } if (_controllerDebugMenu) { if (ImGui.Begin("Controller State", ref _controllerDebugMenu, ImGuiWindowFlags.NoCollapse)) { if (_controllerTracker != null) { ImGui.Columns(2); ImGui.Text($"Name: {_controllerTracker.ControllerName}"); foreach (SDL_GameControllerAxis axis in (SDL_GameControllerAxis[])Enum.GetValues(typeof(SDL_GameControllerAxis))) { ImGui.Text($"{axis}: {_controllerTracker.GetAxis(axis)}"); } ImGui.NextColumn(); foreach (SDL_GameControllerButton button in (SDL_GameControllerButton[])Enum.GetValues(typeof(SDL_GameControllerButton))) { ImGui.Text($"{button}: {_controllerTracker.IsPressed(button)}"); } } else { ImGui.Text("No controller detected."); } } ImGui.End(); } ImGui.Text(_fta.CurrentAverageFramesPerSecond.ToString("000.0 fps / ") + _fta.CurrentAverageFrameTimeMilliseconds.ToString("#00.00 ms")); ImGui.EndMainMenuBar(); } if (InputTracker.GetKeyDown(Key.F11)) { ToggleFullscreenState(); } if (InputTracker.GetKeyDown(Key.Keypad6)) { _window.X += 10; } if (InputTracker.GetKeyDown(Key.Keypad4)) { _window.X -= 10; } if (InputTracker.GetKeyDown(Key.Keypad8)) { _window.Y += 10; } if (InputTracker.GetKeyDown(Key.Keypad2)) { _window.Y -= 10; } _window.Title = _gd.BackendType.ToString(); }
private void Update(float deltaSeconds) { _fta.AddTime(deltaSeconds); _scene.Update(deltaSeconds); if (ImGui.BeginMainMenuBar()) { if (ImGui.BeginMenu("Settings")) { if (ImGui.BeginMenu("Graphics Backend")) { if (ImGui.MenuItem("Vulkan", GraphicsDevice.IsBackendSupported(GraphicsBackend.Vulkan))) { ChangeBackend(GraphicsBackend.Vulkan); } if (ImGui.MenuItem("OpenGL", GraphicsDevice.IsBackendSupported(GraphicsBackend.OpenGL))) { ChangeBackend(GraphicsBackend.OpenGL); } if (ImGui.MenuItem("OpenGL ES", GraphicsDevice.IsBackendSupported(GraphicsBackend.OpenGLES))) { ChangeBackend(GraphicsBackend.OpenGLES); } if (ImGui.MenuItem("Direct3D 11", GraphicsDevice.IsBackendSupported(GraphicsBackend.Direct3D11))) { ChangeBackend(GraphicsBackend.Direct3D11); } if (ImGui.MenuItem("Metal", GraphicsDevice.IsBackendSupported(GraphicsBackend.Metal))) { ChangeBackend(GraphicsBackend.Metal); } ImGui.EndMenu(); } if (ImGui.BeginMenu("MSAA")) { if (ImGui.Combo("MSAA", ref _msaaOption, _msaaOptions)) { ChangeMsaa(_msaaOption); } ImGui.EndMenu(); } bool isFullscreen = _window.WindowState == WindowState.BorderlessFullScreen; if (ImGui.MenuItem("Fullscreen", "F11", isFullscreen, true)) { ToggleFullscreenState(); } if (ImGui.MenuItem("Always Recreate Sdl2Window", string.Empty, _recreateWindow, true)) { _recreateWindow = !_recreateWindow; } if (ImGui.IsItemHovered(HoveredFlags.Default)) { ImGui.SetTooltip( "Causes a new OS window to be created whenever the graphics backend is switched. This is much safer, and is the default."); } bool threadedRendering = _scene.ThreadedRendering; if (ImGui.MenuItem("Render with multiple threads", string.Empty, threadedRendering, true)) { _scene.ThreadedRendering = !_scene.ThreadedRendering; } bool tinted = _fsq.UseTintedTexture; if (ImGui.MenuItem("Tinted output", string.Empty, tinted, true)) { _fsq.UseTintedTexture = !tinted; } bool vsync = _gd.SyncToVerticalBlank; if (ImGui.MenuItem("VSync", string.Empty, vsync, true)) { _gd.SyncToVerticalBlank = !_gd.SyncToVerticalBlank; } ImGui.EndMenu(); } if (ImGui.BeginMenu("Materials")) { if (ImGui.BeginMenu("Brick")) { DrawIndexedMaterialMenu(CommonMaterials.Brick); ImGui.EndMenu(); } if (ImGui.BeginMenu("Vase")) { DrawIndexedMaterialMenu(CommonMaterials.Vase); ImGui.EndMenu(); } if (ImGui.BeginMenu("Reflective")) { DrawIndexedMaterialMenu(CommonMaterials.Reflective); ImGui.EndMenu(); } ImGui.EndMenu(); } if (ImGui.BeginMenu("Debug")) { if (ImGui.MenuItem("Refresh Device Objects")) { RefreshDeviceObjects(1); } if (ImGui.MenuItem("Refresh Device Objects (10 times)")) { RefreshDeviceObjects(10); } if (ImGui.MenuItem("Refresh Device Objects (100 times)")) { RefreshDeviceObjects(100); } ImGui.EndMenu(); } ImGui.Text(_fta.CurrentAverageFramesPerSecond.ToString("000.0 fps / ") + _fta.CurrentAverageFrameTimeMilliseconds.ToString("#00.00 ms")); ImGui.EndMainMenuBar(); } if (InputTracker.GetKeyDown(Key.F11)) { ToggleFullscreenState(); } _window.Title = _gd.BackendType.ToString(); }
private void Update(float deltaSeconds) { _scene.Update(deltaSeconds); if (ImGui.BeginMainMenuBar()) { if (ImGui.BeginMenu("Settings")) { if (ImGui.BeginMenu("Graphics Backend")) { if (ImGui.MenuItem("Vulkan")) { ChangeRenderContext(GraphicsBackend.Vulkan); } if (ImGui.MenuItem("OpenGL")) { ChangeRenderContext(GraphicsBackend.OpenGL); } if (ImGui.MenuItem("OpenGL ES")) { ChangeRenderContext(GraphicsBackend.OpenGLES); } if (ImGui.MenuItem("Direct3D 11")) { ChangeRenderContext(GraphicsBackend.Direct3D11); } ImGui.EndMenu(); } bool isFullscreen = _window.WindowState == WindowState.BorderlessFullScreen; if (ImGui.MenuItem("Fullscreen", "F11", isFullscreen, true)) { ToggleFullscreenState(); } if (ImGui.MenuItem("Always Recreate Window", string.Empty, _recreateWindow, true)) { _recreateWindow = !_recreateWindow; } if (ImGui.IsLastItemHovered()) { ImGui.SetTooltip( "Causes a new OS window to be created whenever the RenderContext is switched. This is much safer, and is the default."); } ImGui.EndMenu(); } if (ImGui.BeginMenu("Materials")) { if (ImGui.BeginMenu("Brick")) { DrawMaterialMenu(CommonMaterials.Brick); ImGui.EndMenu(); } ImGui.EndMenu(); } ImGui.EndMainMenuBar(); } if (InputTracker.GetKeyDown(Key.F11)) { ToggleFullscreenState(); } _window.Title = _rc.BackendType.ToString(); }
private void Update(float deltaSeconds) { _fta.AddTime(deltaSeconds); _scene.Update(deltaSeconds); if (ImGui.BeginMainMenuBar()) { if (ImGui.BeginMenu("Settings")) { if (ImGui.BeginMenu("Graphics Backend")) { if (ImGui.MenuItem("Vulkan")) { ChangeBackend(GraphicsBackend.Vulkan); } if (ImGui.MenuItem("OpenGL")) { ChangeBackend(GraphicsBackend.OpenGL); } if (ImGui.MenuItem("OpenGL ES")) { ChangeBackend(GraphicsBackend.OpenGLES); } if (ImGui.MenuItem("Direct3D 11")) { ChangeBackend(GraphicsBackend.Direct3D11); } ImGui.EndMenu(); } bool isFullscreen = _window.WindowState == WindowState.BorderlessFullScreen; if (ImGui.MenuItem("Fullscreen", "F11", isFullscreen, true)) { ToggleFullscreenState(); } if (ImGui.MenuItem("Always Recreate Sdl2Window", string.Empty, _recreateWindow, true)) { _recreateWindow = !_recreateWindow; } bool threadedRendering = _scene.ThreadedRendering; if (ImGui.MenuItem("Render with multiple threads", string.Empty, threadedRendering, true)) { _scene.ThreadedRendering = !_scene.ThreadedRendering; } if (ImGui.IsLastItemHovered()) { ImGui.SetTooltip( "Causes a new OS Sdl2Window to be created whenever the graphics backend is switched. This is much safer, and is the default."); } ImGui.EndMenu(); } if (ImGui.BeginMenu("Materials")) { if (ImGui.BeginMenu("Brick")) { DrawMaterialMenu(CommonMaterials.Brick); ImGui.EndMenu(); } if (ImGui.BeginMenu("Vase")) { DrawMaterialMenu(CommonMaterials.Vase); ImGui.EndMenu(); } ImGui.EndMenu(); } if (ImGui.BeginMenu("Debug")) { if (ImGui.MenuItem("Refresh Device Objects")) { RefreshDeviceObjects(1); } if (ImGui.MenuItem("Refresh Device Objects (10 times)")) { RefreshDeviceObjects(10); } if (ImGui.MenuItem("Refresh Device Objects (100 times)")) { RefreshDeviceObjects(100); } ImGui.EndMenu(); } ImGui.Text(_fta.CurrentAverageFramesPerSecond.ToString("000.0 fps / ") + _fta.CurrentAverageFrameTimeMilliseconds.ToString("#00.00 ms")); ImGui.EndMainMenuBar(); } if (InputTracker.GetKeyDown(Key.F11)) { ToggleFullscreenState(); } _window.Title = _gd.BackendType.ToString(); }