<commit_before>﻿using System;
using DotSDL.Events;
using DotSDL.Graphics;
using Lyon.Presenters;
using Roton.Emulation.Data;
using Roton.Emulation.Data.Impl;
using Roton.Infrastructure.Impl;

namespace Lyon.App.Impl
{
    [ContextEngine(ContextEngine.Startup)]
    public class Window : SdlWindow, IWindow
    {
        private readonly Lazy<IKeyboardPresenter> _keyboardPresenter;
        private readonly Lazy<IScenePresenter> _scenePresenter;
        private bool _closed;

        public int Width => WindowWidth;
        public int Height => WindowHeight;
        
        private IKeyboardPresenter KeyboardPresenter => _keyboardPresenter.Value;
        private IScenePresenter ScenePresenter => _scenePresenter.Value;

        public Window(
            IConfig config,
            Lazy<IKeyboardPresenter> keyboardPresenter,
            Lazy<IScenePresenter> scenePresenter) : base("Lyon",
            new Point {X = WindowPosUndefined, Y = WindowPosUndefined},
            640 * config.VideoScale, 350 * config.VideoScale,
            640, 350)
        {
            _keyboardPresenter = keyboardPresenter;
            _scenePresenter = scenePresenter;
            KeyPressed += OnKeyDown;
            Closed += OnClosed;
        }

        private void OnClosed(object sender, WindowEvent e)
        {
            Close();
        }

        public void SetSize(int width, int height)
        {
            ScenePresenter.UpdateViewport();
        }

        public void Start(int updateRate)
        {
            base.Start((uint) updateRate);
        }

        public void Close()
        {
            if (_closed)
                return;
            
            _closed = true;
            Stop();
        }

        private void OnKeyDown(object obj, KeyboardEvent e)
        {
            KeyboardPresenter.Press(e);
        }

        public override IntPtr GetCanvasPointer()
        {
            var bitmap = ScenePresenter.Render();
            return bitmap.Bits.Length == 0 ? IntPtr.Zero : bitmap.BitsPointer;
        }

        protected override void OnUpdate()
        {
        }

        public void MakeCurrent()
        {
        }

        public void SwapBuffers()
        {
        }
    }
}<commit_msg>Implemented fix for Lyon hanging on quit in Windows.<commit_after>﻿using System;
using DotSDL.Events;
using DotSDL.Graphics;
using Lyon.Presenters;
using Roton.Emulation.Data;
using Roton.Emulation.Data.Impl;
using Roton.Infrastructure.Impl;

namespace Lyon.App.Impl
{
    [ContextEngine(ContextEngine.Startup)]
    public class Window : SdlWindow, IWindow
    {
        private readonly Lazy<IKeyboardPresenter> _keyboardPresenter;
        private readonly Lazy<IScenePresenter> _scenePresenter;
        private bool _closeWindow;

        public int Width => WindowWidth;
        public int Height => WindowHeight;
        
        private IKeyboardPresenter KeyboardPresenter => _keyboardPresenter.Value;
        private IScenePresenter ScenePresenter => _scenePresenter.Value;

        public Window(
            IConfig config,
            Lazy<IKeyboardPresenter> keyboardPresenter,
            Lazy<IScenePresenter> scenePresenter) : base("Lyon",
            new Point {X = WindowPosUndefined, Y = WindowPosUndefined},
            640 * config.VideoScale, 350 * config.VideoScale,
            640, 350)
        {
            _keyboardPresenter = keyboardPresenter;
            _scenePresenter = scenePresenter;
            KeyPressed += OnKeyDown;
            Closed += OnClosed;
        }

        private void OnClosed(object sender, WindowEvent e)
        {
            Close();
        }

        public void SetSize(int width, int height)
        {
            ScenePresenter.UpdateViewport();
        }

        public void Start(int updateRate)
        {
            base.Start((uint) updateRate);
        }

        public void Close()
        {
            _closeWindow = true;
        }

        private void OnKeyDown(object obj, KeyboardEvent e)
        {
            KeyboardPresenter.Press(e);
        }

        public override IntPtr GetCanvasPointer()
        {
            var bitmap = ScenePresenter.Render();
            return bitmap.Bits.Length == 0 ? IntPtr.Zero : bitmap.BitsPointer;
        }

        protected override void OnUpdate()
        {
            if(_closeWindow)
                Stop();
        }

        public void MakeCurrent()
        {
        }

        public void SwapBuffers()
        {
        }
    }
}
{'fraction_non_alphanumeric': 0.07647843485993774, 'fraction_numerical': 0.005780346820809248, 'mean_word_length': 1.8674314850223073, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '3913079', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1171, 'n_words': 278}