-
Notifications
You must be signed in to change notification settings - Fork 1
/
BASeTris.cs
569 lines (455 loc) · 19.2 KB
/
BASeTris.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using BaseTris;
using BASeCamp.BASeScores;
using BASeTris.AI;
using BASeTris.AssetManager;
using BASeTris.Choosers;
using BASeTris.Choosers.AIChoosers;
using BASeTris.FieldInitializers;
using BASeTris.GameStates;
using BASeTris.Rendering;
using BASeTris.Rendering.GDIPlus;
using BASeTris.Rendering.RenderElements;
using BASeTris.Rendering.Skia;
using BASeTris.Blocks;
using BASeTris.Tetrominoes;
using OpenTK.Platform;
using SkiaSharp;
using XInput.Wrapper;
using BASeTris.GameStates.Menu;
using BASeTris.BackgroundDrawers;
using BASeTris.Rendering.Adapters;
using BASeTris.Theme.Audio;
using BASeTris.GameStates.GameHandlers;
using BASeTris.Settings;
namespace BASeTris
{
//IStateOwner in the form itself.
//This is necessary so we can also have an implementation via the OpenTK GameWindow.
public partial class BASeTris : Form, IStateOwner,IGamePresenter
{
private GamePresenter _Present;
//delegate the BeforeGameStateChange event...
public GamePresenter GetPresenter() => _Present;
public event EventHandler<GameClosingEventArgs> GameClosing;
public event EventHandler<BeforeGameStateChangeEventArgs> BeforeGameStateChange
{
add => _Present.Game.BeforeGameStateChange += value;
remove => _Present.Game.BeforeGameStateChange -= value;
}
public SettingsManager Settings
{
get
{
return _Present.GameSettings;
}
}
public AudioThemeManager AudioThemeMan { get { return _Present.AudioThemeMan; } set { _Present.AudioThemeMan = value; } }
public Stopwatch GameTime
{
get => _Present.Game.GameTime;
set => _Present.Game.GameTime = value;
}
public TimeSpan FinalGameTime
{
get => _Present.Game.FinalGameTime;
set => _Present.Game.FinalGameTime = value;
}
public TimeSpan GetElapsedTime()
{
return _Present.Game.GetElapsedTime();
}
public double FrameTime { get { return lastFrame.TotalSeconds; } }
HashSet<Keys> PressedKeys = new HashSet<Keys>();
public BASeTris()
{
InitializeComponent();
}
public Type GetCanvasType()
{
return typeof(Graphics);
}
public double ScaleFactor
{
get { return current_factor; }
}
public Rectangle GameArea
{
get { return picTetrisField.ClientRectangle; }
}
public bool ForceSkiaUsage = true;
public static readonly double DefaultWidth = 643d;
public static readonly double DefaultHeight = 734d;
public void SetDisplayMode(GameState.DisplayMode pMode)
{
if (InvokeRequired) Invoke((MethodInvoker)(() => SetDisplayMode(pMode)));
if (ActiveRenderMode == RendererMode.Renderer_GDIPlus)
{
if (picFullSize.Visible == (ForceSkiaUsage || pMode == GameState.DisplayMode.Full)) return; //if full size visibility matches the passed state being full, we are already in that state.
picFullSize.Visible = ForceSkiaUsage || pMode == GameState.DisplayMode.Full;
picTetrisField.Visible = !ForceSkiaUsage && pMode == GameState.DisplayMode.Partitioned;
picStatistics.Visible = !ForceSkiaUsage && pMode == GameState.DisplayMode.Partitioned;
}
else
{
picFullSize.Visible = picTetrisField.Visible = picStatistics.Visible = false;
}
}
private double current_factor = 1.3;
public void SetScale(double factor)
{
current_factor = factor;
Invoke((MethodInvoker)(()=>
{
mnuScale_Tiny.Checked = mnuScale_Small.Checked = mnuScale_Large.Checked = mnuScale_Biggliest.Checked = false;
Size = new Size((int) (DefaultWidth * factor), (int) (DefaultHeight * factor));
}));
}
private void BASeTris_Load(object sender, EventArgs e)
{
_Present = new GamePresenter(this);
//XMLHighScores<NoSpecialInfo> TestScores = new XMLHighScores<NoSpecialInfo>(35000,(r)=>new NoSpecialInfo());
//int Position1 = TestScores.IsEligible(12000);
//int Position2 = TestScores.IsEligible(3000);
menuStrip1.Font = new Font(menuStrip1.Font.FontFamily, 14, FontStyle.Regular);
Win10MenuRenderer buildrender = new Win10MenuRenderer(null, true);
menuStrip1.Renderer = buildrender;
menuStrip1.BackColor = SystemColors.Control;
TetrisGame.InitState();
/*SkiaBitmap = new SKBitmap(new SKImageInfo(picTetrisField.Width, picTetrisField.Height, SKColorType.Rgba8888));
SKCanvas SkiaCanvas = new SKCanvas(SkiaBitmap);
StandardColouredBlock scb = new StandardColouredBlock() { DisplayStyle = StandardColouredBlock.BlockStyle.Style_Mottled,BlockColor = Color.Yellow };
TetrisBlockDrawSkiaParameters tt = new TetrisBlockDrawSkiaParameters(SkiaCanvas, new SKRect(0, 0, 64, 64), null, Settings);
scb.Rotation = 0;
scb.DoRotateTransform = true;
RenderingProvider.Static.DrawElement(this, SkiaCanvas, scb, tt);
SKTypeface st = TetrisGame.RetroFontSK;
TetrisGame.DrawTextSK(SkiaCanvas, "Testing 1 2 3", new SKPoint(15, 15), st, SkiaSharp.Views.Desktop.Extensions.ToSKColor(Color.Blue), 18, 1d);
*/
StartGame();
GenericMenuState TitleMenu = new GenericMenuState(StandardImageBackgroundGDI.GetStandardBackgroundDrawer(), this, new TitleMenuPopulator());
CurrentState = TitleMenu;
SetScale(1.33333d);
}
SKBitmap SkiaBitmap = null;
bool XPolling = false;
Nomino testBG = null;
public void StartGame()
{
_Present.StartGame();
}
TimeSpan lastFrame = TimeSpan.MinValue;
public void Present()
{
Invoke
((MethodInvoker)(() =>
{
Stopwatch FrameTimer = new Stopwatch();
FrameTimer.Start();
if (ActiveRenderMode == RendererMode.Renderer_GDIPlus)
{
if (!ForceSkiaUsage && _Present.Game.CurrentState.SupportedDisplayMode == GameState.DisplayMode.Partitioned)
{
picTetrisField.Invalidate();
picTetrisField.Refresh();
picStatistics.Invalidate();
picStatistics.Refresh();
}
else if (ForceSkiaUsage || _Present.Game.CurrentState.SupportedDisplayMode == GameState.DisplayMode.Full)
{
picFullSize.Invalidate();
picFullSize.Refresh();
}
}
FrameTimer.Stop();
lastFrame = FrameTimer.Elapsed;
}));
}
private Object SkiaLock = new object();
private SKBitmap SkiaCanvasBitmap = null;
private SKCanvas SkiaCanvas = null;
private Size LastSize;
private void RecreateSkiaObjects()
{
lock (SkiaLock)
{
SkiaBitmap.Dispose();
SkiaCanvas.Dispose();
SkiaCanvasBitmap = new SKBitmap(picFullSize.ClientSize.Width, picFullSize.ClientSize.Width);
SkiaCanvas = new SKCanvas(SkiaCanvasBitmap);
}
}
private SkiaRenderAssistant SkiaAssist = new SkiaRenderAssistant();
static Random rgen = new Random();
private void picFullSize_Paint(object sender, PaintEventArgs e)
{
if (_Present.Game == null) return;
if ( CurrentState.SupportedDisplayMode == GameState.DisplayMode.Full)
{
e.Graphics.CompositingQuality = CompositingQuality.HighSpeed;
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
if (ForceSkiaUsage)
{
if (LastSize != picFullSize.Size)
{
RecreateSkiaObjects();
}
SkiaCanvas.Clear();
SkiaAssist.PaintStateSkia(this, CurrentState, new OpenTK.Mathematics.Vector2i((int)picFullSize.Width, (int)picFullSize.Height), SkiaCanvas);
using (var skiaconverted = SkiaSharp.Views.Desktop.Extensions.ToBitmap(SkiaBitmap))
{
e.Graphics.DrawImageUnscaled(skiaconverted, new Point(0, 0));
}
}
else
{
_Present.Game.DrawProc(e.Graphics, new RectangleF(picFullSize.ClientRectangle.Left, picFullSize.ClientRectangle.Top, picFullSize.ClientRectangle.Width, picFullSize.ClientRectangle.Height));
}
}
}
private void picTetrisField_Paint(object sender, PaintEventArgs e)
{
/*e.Graphics.DrawImage(SkiaSharp.Views.Desktop.Extensions.ToBitmap(SkiaBitmap), Point.Empty);
return;*/
if (_Present.Game == null) return;
if (picTetrisField.Visible == false) return;
e.Graphics.CompositingQuality = CompositingQuality.HighSpeed;
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
if(_Present.Game.CurrentState is FieldActionGameState)
{
;
}
RectangleF useBounds;
var renderer = RenderingProvider.Static.GetHandler(typeof(Graphics), _Present.Game.CurrentState.GetType(), typeof(BaseDrawParameters));
if(renderer!=null)
{
if(renderer is IStateRenderingHandler staterender)
{
useBounds = new RectangleF(picTetrisField.ClientRectangle.Left, picTetrisField.ClientRectangle.Top, picTetrisField.ClientRectangle.Width, picTetrisField.ClientRectangle.Height);
staterender.Render(this, e.Graphics, _Present.Game.CurrentState,
new BaseDrawParameters(useBounds));
return;
}
}
//if the above doesn't go through....
useBounds = new RectangleF(picTetrisField.ClientRectangle.Left, picTetrisField.ClientRectangle.Top, picTetrisField.ClientRectangle.Width, picTetrisField.ClientRectangle.Height);
_Present.Game.DrawProc(e.Graphics, useBounds);
_LastDrawBounds = useBounds;
}
private void picStatistics_Paint(object sender, PaintEventArgs e)
{
if (CurrentState != null)
{
if (picStatistics.Visible == false) return;
e.Graphics.CompositingQuality = CompositingQuality.HighSpeed;
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
var renderer = RenderingProvider.Static.GetHandler(typeof(Graphics), _Present.Game.CurrentState.GetType(), typeof(BaseDrawParameters));
if (renderer != null)
{
if (renderer is IStateRenderingHandler staterender)
{
staterender.RenderStats(this, e.Graphics, _Present.Game.CurrentState,
new BaseDrawParameters(picStatistics.ClientRectangle));
return;
}
}
}
}
private void picTetrisField_Resize(object sender, EventArgs e)
{
picTetrisField.Invalidate();
picTetrisField.Refresh();
}
private void picTetrisField_Click(object sender, EventArgs e)
{
//testBG.Rotate(false);
picTetrisField.Invalidate();
picTetrisField.Refresh();
}
private void BASeTris_KeyDown(object sender, KeyEventArgs e)
{
_Present.IgnoreController = true;
if (e.KeyCode == Keys.G)
{
if (_Present.Game.CurrentState is GameplayGameState)
{
GameplayGameState gs = _Present.Game.CurrentState as GameplayGameState;
NominoBlock[][] inserts = new NominoBlock[4][];
for (int i = 0; i < inserts.Length; i++)
{
inserts[i] = new NominoBlock[gs.PlayField.ColCount];
for (int c = 1; c < inserts[i].Length; c++)
{
inserts[i][c] = new StandardColouredBlock() {BlockColor = Color.Red, DisplayStyle = StandardColouredBlock.BlockStyle.Style_CloudBevel};
}
}
InsertBlockRowsActionGameState irs = new InsertBlockRowsActionGameState(gs, 0, inserts, Enumerable.Empty<Action>());
CurrentState = irs;
}
}
else if (e.KeyCode == Keys.C)
{
if (e.Shift && e.Control)
{
EnterCheatState cheatstate = new EnterCheatState(CurrentState, _Present.Game, 64);
CurrentState = cheatstate;
}
}
Debug.Print("Button pressed:" + e.KeyCode);
var translated = _Present.TranslateKey(e.KeyCode);
if (translated != null)
{
_Present.Game.HandleGameKey(this, translated.Value, TetrisGame.KeyInputSource.Input_HID);
_Present.GameKeyDown(translated.Value);
}
}
private void BASeTris_FormClosing(object sender, FormClosingEventArgs e)
{
if (_Present.GameThread != null)
_Present.GameThread.Abort();
if (_Present.InputThread != null)
{
_Present.InputThread.Abort();
}
if (X.IsAvailable)
{
X.StopPolling();
}
if (_Present.ai != null) _Present.ai.AbortAI();
TetrisGame.Soundman.StopMusic();
Application.Exit();
}
public GameState CurrentState
{
get { return _Present.Game?.CurrentState; }
set { _Present.Game.CurrentState = value; }
}
private BCRect _LastDrawBounds;
public BCRect LastDrawBounds => _LastDrawBounds;
public void EnqueueAction(Action pAction)
{
_Present.EnqueueAction(pAction);
}
private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
StartGame();
}
public enum RendererMode
{
Renderer_GDIPlus,
}
//public RendererMode ActiveRenderMode = RendererMode.Renderer_GDIPlus;
public RendererMode ActiveRenderMode = RendererMode.Renderer_GDIPlus;
private void BASeTris_KeyUp(object sender, KeyEventArgs e)
{
Debug.Print("Button released:" + e.KeyCode);
var translated = _Present.TranslateKey(e.KeyCode);
if (translated != null)
{
_Present.GameKeyUp(translated.Value);
}
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
SetScale(0.5f);
((ToolStripMenuItem) sender).Checked = true;
}
private void xToolStripMenuItem_Click(object sender, EventArgs e)
{
SetScale(1);
((ToolStripMenuItem) sender).Checked = true;
}
private void xToolStripMenuItem1_Click(object sender, EventArgs e)
{
SetScale(1.3f);
((ToolStripMenuItem) sender).Checked = true;
}
private void xToolStripMenuItem2_Click(object sender, EventArgs e)
{
SetScale(1.6f);
((ToolStripMenuItem) sender).Checked = true;
}
private void BASeTris_ResizeEnd(object sender, EventArgs e)
{
}
private void GetHorizontalSizeData(float WindowHeight,float WindowWidth,out float FieldSize,out float StatSize)
{
FieldSize = WindowHeight * (332f / 641f);
StatSize = WindowWidth - FieldSize;
}
private void BASeTris_SizeChanged(object sender, EventArgs e)
{
picTetrisField.Width = (int) (picTetrisField.Height * (332f / 641f));
int statright = picStatistics.Right;
picStatistics.Left = picTetrisField.Right + 6;
picStatistics.Width = statright - picStatistics.Left;
}
private void aIToolStripMenuItem_Click(object sender, EventArgs e)
{
aIToolStripMenuItem.Checked = !aIToolStripMenuItem.Checked;
if (_Present.ai == null)
{
_Present.ai = new StandardNominoAI(this);
}
else
{
_Present.ai.AbortAI();
_Present.ai = null;
}
}
public void Feedback(float Strength, int Length)
{
_Present.Feedback(Strength, Length);
}
private void BASeTris_KeyPress(object sender, KeyPressEventArgs e)
{
/*
if (_Present.Game != null && _Present.Game.CurrentState is IDirectKeyboardInputState)
{
var Casted = (IDirectKeyboardInputState) _Present.Game.CurrentState;
Casted.KeyPressed(this, (int) e.KeyChar);
}*/
}
private void debugToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void picTetrisField_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Right)
{
ContextMenuStrip cms = new ContextMenuStrip();
for(int i=0;i<10;i++)
{
ToolStripMenuItem tsm = new ToolStripMenuItem("Item " + i);
cms.Items.Add(tsm);
}
cms.Show();
}
}
private void BASeTris_Enter(object sender, EventArgs e)
{
_Present.GameThreadPaused = false;
}
private void BASeTris_Leave(object sender, EventArgs e)
{
_Present.GameThreadPaused = true;
}
}
}