From d26fa699e5652fff223faec379aae134478e02c0 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Wed, 6 Apr 2022 11:30:44 +0200 Subject: [PATCH] Add game system --- Tetris.csproj | 6 ++++- Views/GameWindow.xaml.cs | 3 ++- ViewsModels/GameModel.cs | 51 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 ViewsModels/GameModel.cs diff --git a/Tetris.csproj b/Tetris.csproj index c70bc94..6fb7e41 100644 --- a/Tetris.csproj +++ b/Tetris.csproj @@ -9,10 +9,14 @@ - + Always + + + + diff --git a/Views/GameWindow.xaml.cs b/Views/GameWindow.xaml.cs index 236afea..2d3ddcf 100644 --- a/Views/GameWindow.xaml.cs +++ b/Views/GameWindow.xaml.cs @@ -1,5 +1,6 @@ using System.Runtime.InteropServices; using System.Windows; +using Tetris.ViewsModels; namespace Tetris.Views; @@ -9,7 +10,7 @@ public partial class GameWindow : Window { AttachConsole(-1); InitializeComponent(); - // new GameManager((int)Width, (int)Height, ImageControl); + new GameModel((int)Width, (int)Height, ImageControl); } [DllImport("kernel32.dll")] diff --git a/ViewsModels/GameModel.cs b/ViewsModels/GameModel.cs new file mode 100644 index 0000000..774d018 --- /dev/null +++ b/ViewsModels/GameModel.cs @@ -0,0 +1,51 @@ +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) + { + var random = new Random(); + + _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) + { + + } +} \ No newline at end of file