<commit_before>﻿using System;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing;

using Duality;
using Duality.Resources;

using OpenTK;
using OpenTK.Input;
using OpenTK.Graphics;
using OpenTK.Platform.Windows;

namespace Duality.Launcher
{
	public class DualityLauncher : GameWindow
	{
		private	static bool	isDebugging			= false;
		private	static bool	isProfiling			= false;
		private	static bool	isRunFromEditor		= false;
		private Stopwatch	frameLimiterWatch	= new Stopwatch();

		public DualityLauncher(int w, int h, GraphicsMode mode, string title, GameWindowFlags flags) : base(w, h, mode, title, flags) {}

		protected override void OnResize(EventArgs e)
		{
			DualityApp.TargetResolution = new Vector2(ClientSize.Width, ClientSize.Height);
		}
		protected override void OnUpdateFrame(FrameEventArgs e)
		{
			if (DualityApp.ExecContext == DualityApp.ExecutionContext.Terminated)
			{
				this.Close();
				return;
			}
			
			if (!isDebugging && !isProfiling) // Don't limit frame rate when debugging or profiling.
			{
				//// Assure we'll at least wait 16 ms until updating again.
				//if (this.frameLimiterWatch.IsRunning)
				//{
				//    while (this.frameLimiterWatch.Elapsed.TotalSeconds < 0.016d) 
				//    {
				//        // Go to sleep if we'd have to wait too long
				//        if (this.frameLimiterWatch.Elapsed.TotalSeconds < 0.01d)
				//            System.Threading.Thread.Sleep(1);
				//    }
				//}

				// Give the processor a rest if we have the time, don't use 100% CPU even without VSync
				if (this.frameLimiterWatch.IsRunning && DualityApp.UserData.RefreshMode == RefreshMode.ManualSync)
				{
					while (this.frameLimiterWatch.Elapsed.TotalMilliseconds < Time.MsPFMult)
					{
						// Enough leftover time? Risk a millisecond sleep.
						if (this.frameLimiterWatch.Elapsed.TotalMilliseconds < Time.MsPFMult * 0.75f)
							System.Threading.Thread.Sleep(1);
					}
				}
				this.frameLimiterWatch.Restart();
			}
			DualityApp.Update();
		}
		protected override void OnRenderFrame(FrameEventArgs e)
		{
			if (DualityApp.ExecContext == DualityApp.ExecutionContext.Terminated) return;

			DualityApp.Render(new Rect(this.ClientSize.Width, this.ClientSize.Height));
			Profile.TimeRender.BeginMeasure();
			Profile.TimeSwapBuffers.BeginMeasure();
			this.SwapBuffers();
			Profile.TimeSwapBuffers.EndMeasure();
			Profile.TimeRender.EndMeasure();
		}
		
		private void OnUserDataChanged(object sender, EventArgs eventArgs)
		{
			switch (DualityApp.UserData.GfxMode)
			{
				case ScreenMode.Window:
				case ScreenMode.FixedWindow:
					this.WindowState = WindowState.Normal;
					this.WindowBorder = WindowBorder.Fixed;
					this.ClientSize = new Size(DualityApp.UserData.GfxWidth, DualityApp.UserData.GfxHeight);
					DisplayDevice.Default.RestoreResolution();
					break;

				case ScreenMode.Native:
				case ScreenMode.FullWindow:<commit_msg>Bugfixes #FIX: Fixed FullWindow screen resolution changes not using the displays native size.<commit_after>﻿using System;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing;

using Duality;
using Duality.Resources;

using OpenTK;
using OpenTK.Input;
using OpenTK.Graphics;
using OpenTK.Platform.Windows;

namespace Duality.Launcher
{
	public class DualityLauncher : GameWindow
	{
		private	static bool	isDebugging			= false;
		private	static bool	isProfiling			= false;
		private	static bool	isRunFromEditor		= false;
		private Stopwatch	frameLimiterWatch	= new Stopwatch();

		public DualityLauncher(int w, int h, GraphicsMode mode, string title, GameWindowFlags flags) : base(w, h, mode, title, flags) {}

		protected override void OnResize(EventArgs e)
		{
			DualityApp.TargetResolution = new Vector2(ClientSize.Width, ClientSize.Height);
		}
		protected override void OnUpdateFrame(FrameEventArgs e)
		{
			if (DualityApp.ExecContext == DualityApp.ExecutionContext.Terminated)
			{
				this.Close();
				return;
			}
			
			if (!isDebugging && !isProfiling) // Don't limit frame rate when debugging or profiling.
			{
				//// Assure we'll at least wait 16 ms until updating again.
				//if (this.frameLimiterWatch.IsRunning)
				//{
				//    while (this.frameLimiterWatch.Elapsed.TotalSeconds < 0.016d) 
				//    {
				//        // Go to sleep if we'd have to wait too long
				//        if (this.frameLimiterWatch.Elapsed.TotalSeconds < 0.01d)
				//            System.Threading.Thread.Sleep(1);
				//    }
				//}

				// Give the processor a rest if we have the time, don't use 100% CPU even without VSync
				if (this.frameLimiterWatch.IsRunning && DualityApp.UserData.RefreshMode == RefreshMode.ManualSync)
				{
					while (this.frameLimiterWatch.Elapsed.TotalMilliseconds < Time.MsPFMult)
					{
						// Enough leftover time? Risk a millisecond sleep.
						if (this.frameLimiterWatch.Elapsed.TotalMilliseconds < Time.MsPFMult * 0.75f)
							System.Threading.Thread.Sleep(1);
					}
				}
				this.frameLimiterWatch.Restart();
			}
			DualityApp.Update();
		}
		protected override void OnRenderFrame(FrameEventArgs e)
		{
			if (DualityApp.ExecContext == DualityApp.ExecutionContext.Terminated) return;

			DualityApp.Render(new Rect(this.ClientSize.Width, this.ClientSize.Height));
			Profile.TimeRender.BeginMeasure();
			Profile.TimeSwapBuffers.BeginMeasure();
			this.SwapBuffers();
			Profile.TimeSwapBuffers.EndMeasure();
			Profile.TimeRender.EndMeasure();
		}
		
		private void OnUserDataChanged(object sender, EventArgs eventArgs)
		{
			switch (DualityApp.UserData.GfxMode)
			{
				case ScreenMode.Window:
				case ScreenMode.FixedWindow:
					this.WindowState = WindowState.Normal;
					this.WindowBorder = WindowBorder.Fixed;
					this.ClientSize = new Size(DualityApp.UserData.GfxWidth, DualityApp.UserData.GfxHeight);
					DisplayDevice.Default.RestoreResolution();
					break;

				case ScreenMode.FullWindow:
					this.ClientSize = new Size(DisplayDevice.Default.Width, DisplayDevice.Default.Height);
					this.WindowState = WindowState.Fullscreen;
					this.WindowBorder = WindowBorder.Hidden;
					DisplayDevice.Default.RestoreResolution();
					break;

				case ScreenMode.Native:
{'fraction_non_alphanumeric': 0.09895414320193081, 'fraction_numerical': 0.005792437650844731, 'mean_word_length': 3.5043478260869567, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6687436', 'n_tokens_mistral': 2338, 'n_tokens_neox': 1888, 'n_words': 429}