2022-04-07 15:30:48 +02:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2022-04-06 11:30:44 +02:00
|
|
|
|
}
|