forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetrisApp.cs
123 lines (103 loc) · 4.56 KB
/
TetrisApp.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
//-----------------------------------------------------------------------------
//
// Tetris game for .NET nanoFramework
//
// Original source from http://bansky.net/blog (no longer available).
//
// This code was written by Pavel Bansky. It is released under the terms of
// the Creative Commons "Attribution NonCommercial ShareAlike 2.5" license.
// http://creativecommons.org/licenses/by-nc-sa/2.5/
//-----------------------------------------------------------------------------
// !!!----------- SAMPLE - ENSURE YOU CHOOSE THE CORRECT TARGET HERE --------------!!!
//#define STM32F769I_DISCO // Comment this in if for the target!
//#define ESP32 // Comment this in if for the target platform!
// !!!-----------------------------------------------------------------------------!!!
using System;
using nanoFramework.UI;
using nanoFramework.UI.Input;
using Tetris.GameLogic;
using Tetris.Presentation;
namespace Tetris
{
/// <summary>
/// Tetris Application
/// </summary>
public class TetrisApp : Application
{
/// <summary>
/// Game HighScoreTable
/// </summary>
public HighScoreTable HighScore;
private static ExtendedWeakReference highScoreEWD;
private TetrisApp()
{
// Create the object that configures the GPIO pins to buttons.
GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
// Assign GPIO / Key functions to GPIOButtonInputProvider
#if ESP32 // This is an example mapping, work them out for your needs!
inputProvider.AddButton(12, Button.VK_LEFT, true);
inputProvider.AddButton(13, Button.VK_RIGHT, true);
inputProvider.AddButton(34, Button.VK_UP, true);
inputProvider.AddButton(35, Button.VK_SELECT, true);
inputProvider.AddButton(36, Button.VK_DOWN, true);
DisplayControl.Initialize(new SpiConfiguration(1, 22, 21, 18, 5), new ScreenConfiguration(0, 0, 320, 240));
#elif STM32F769I_DISCO // This is an example (working) button map, work the actual pins out for your need!
//WARNING: Invalid pin mappings will never be returned, and may need you to reflash the device!
inputProvider.AddButton(PinNumber('J', 0), Button.VK_LEFT, true);
inputProvider.AddButton(PinNumber('J', 1), Button.VK_RIGHT, true);
inputProvider.AddButton(PinNumber('J', 3), Button.VK_UP, true);
inputProvider.AddButton(PinNumber('J', 4), Button.VK_DOWN, true);
inputProvider.AddButton(PinNumber('A', 6), Button.VK_SELECT, true);
DisplayControl.Initialize(new SpiConfiguration(), new ScreenConfiguration()); //TODO: surely this should "actually" be I2C?!
#else
throw new System.Exception("Unknown button and/or display mapping!");
#endif
// Create ExtendedWeakReference for high score table
highScoreEWD = ExtendedWeakReference.RecoverOrCreate(
typeof(TetrisApp),
0,
ExtendedWeakReference.c_SurvivePowerdown);
// Set persistence priority
highScoreEWD.Priority = (int)ExtendedWeakReference.PriorityLevel.Important;
// Try to recover previously saved HighScore
HighScore = (HighScoreTable)highScoreEWD.Target;
// If nothing was recovered - create new
if (HighScore == null)
HighScore = new HighScoreTable();
}
/// <summary>
/// OnStartUp event handler
/// </summary>
protected override void OnStartup(EventArgs e)
{
MainWindow = new MainMenuWindow(this);
base.OnStartup(e);
}
/// <summary>
/// Sets focus to MainWindow
/// </summary>
public void SetFocus()
{
Buttons.Focus(MainWindow);
}
/// <summary>
/// Persists high score to the FLASH memory
/// </summary>
public void PersistHighScore()
{
// Persist HighScore by setting the Target property
// of ExtendedWeakReference
highScoreEWD.Target = HighScore;
}
public static void Main()
{
new TetrisApp().Run();
}
private static int PinNumber(char port, byte pin)
{
if (port < 'A' || port > 'J')
throw new ArgumentException();
return ((port - 'A') * 16) + pin;
}
}
}