Change to binding element
This commit is contained in:
parent
39bc524922
commit
f238c1a720
3 changed files with 58 additions and 51 deletions
|
@ -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}">
|
||||
<Grid>
|
||||
<Image x:Name="ImageControl" />
|
||||
<Image Source="{Binding Source}" />
|
||||
</Grid>
|
||||
</Window>
|
|
@ -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")]
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Reference in a new issue