diff --git a/Views/GameWindow.xaml b/Views/GameWindow.xaml index 66e917f..6c1731d 100644 --- a/Views/GameWindow.xaml +++ b/Views/GameWindow.xaml @@ -4,8 +4,8 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" - Title="GameWindow" Height="450" Width="800"> + Title="GameWindow" Width="{Binding Width}" Height="{Binding Height}"> - + \ No newline at end of file diff --git a/Views/GameWindow.xaml.cs b/Views/GameWindow.xaml.cs index 2d3ddcf..616691a 100644 --- a/Views/GameWindow.xaml.cs +++ b/Views/GameWindow.xaml.cs @@ -10,7 +10,7 @@ public partial class GameWindow : Window { AttachConsole(-1); InitializeComponent(); - new GameModel((int)Width, (int)Height, ImageControl); + DataContext = new GameViewModel(); } [DllImport("kernel32.dll")] diff --git a/ViewsModels/GameModel.cs b/ViewsModels/GameViewModel.cs similarity index 70% rename from ViewsModels/GameModel.cs rename to ViewsModels/GameViewModel.cs index 6e8771e..3069105 100644 --- a/ViewsModels/GameModel.cs +++ b/ViewsModels/GameViewModel.cs @@ -1,49 +1,56 @@ -using System; -using System.Windows.Controls; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Threading; - -namespace Tetris.ViewsModels; - -public class GameModel -{ - private const int RendererHertz = 5; - private const int GameRendererHertz = (1 / RendererHertz) * 1000; - private readonly WriteableBitmap _writeableBitmap; - - public GameModel(int width, int height, Image image) - { - _writeableBitmap = BitmapFactory.New(width, height); - image.Source = _writeableBitmap; - - var dispatcherRenderTimer = new DispatcherTimer - { - Interval = new TimeSpan(0, 0, 0, 0, GameRendererHertz) - }; - - dispatcherRenderTimer.Tick += Render; - dispatcherRenderTimer.Start(); - - var dispatcherUpdateTimer = new DispatcherTimer - { - Interval = new TimeSpan(0, 0, 0, 0, 25) - }; - - dispatcherUpdateTimer.Tick += Update; - dispatcherUpdateTimer.Start(); - } - - private void Render(object? sender, EventArgs eventArgs) - { - _writeableBitmap.Lock(); - _writeableBitmap.Clear(Colors.Black); - - _writeableBitmap.Unlock(); - } - - private void Update(object? sender, EventArgs eventArgs) - { - - } +using System; +using System.ComponentModel; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Threading; + +namespace Tetris.ViewsModels; + +public class GameViewModel : INotifyPropertyChanged +{ + public event PropertyChangedEventHandler? PropertyChanged; + + private const int RendererHertz = 5; + private const int GameRendererHertz = (1 / RendererHertz) * 1000; + private readonly int _width = 480, _height = 640; + private readonly WriteableBitmap _writeableBitmap; + + public GameViewModel() + { + _writeableBitmap = BitmapFactory.New(_width, _height); + // image.Source = _writeableBitmap; + + var dispatcherRenderTimer = new DispatcherTimer + { + Interval = new TimeSpan(0, 0, 0, 0, GameRendererHertz) + }; + + dispatcherRenderTimer.Tick += Render; + dispatcherRenderTimer.Start(); + + var dispatcherUpdateTimer = new DispatcherTimer + { + Interval = new TimeSpan(0, 0, 0, 0, 25) + }; + + dispatcherUpdateTimer.Tick += Update; + dispatcherUpdateTimer.Start(); + } + + public int Width => _width; + public int Height => _height; + public ImageSource Source => _writeableBitmap; + + private void Render(object? sender, EventArgs eventArgs) + { + _writeableBitmap.Lock(); + _writeableBitmap.Clear(Colors.Black); + + _writeableBitmap.Unlock(); + } + + private void Update(object? sender, EventArgs eventArgs) + { + + } } \ No newline at end of file