-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (95 loc) · 3.09 KB
/
Copy pathProgram.cs
File metadata and controls
118 lines (95 loc) · 3.09 KB
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
using Raylib_cs;
namespace Chip8Sharp;
public static class Program
{
private static readonly Dictionary<KeyboardKey, ConsoleKey> inputMap =
new Dictionary<KeyboardKey, ConsoleKey>()
{
{KeyboardKey.One, ConsoleKey.NumPad1},
{KeyboardKey.Two, ConsoleKey.NumPad2},
{KeyboardKey.Three, ConsoleKey.NumPad3},
{KeyboardKey.Four, ConsoleKey.NumPad4},
{KeyboardKey.Q, ConsoleKey.Q},
{KeyboardKey.W, ConsoleKey.W},
{KeyboardKey.E, ConsoleKey.E},
{KeyboardKey.R, ConsoleKey.R},
{KeyboardKey.A, ConsoleKey.A},
{KeyboardKey.S, ConsoleKey.S},
{KeyboardKey.D, ConsoleKey.D},
{KeyboardKey.F, ConsoleKey.F},
{KeyboardKey.Z, ConsoleKey.Z},
{KeyboardKey.X, ConsoleKey.X},
{KeyboardKey.C, ConsoleKey.C},
{KeyboardKey.V, ConsoleKey.V},
};
public static void Main()
{
var chip8 = new Chip8();
if (RomLoader.TryGetRom(out var rom) && rom != null)
{
chip8.LoadRom(rom);
if (chip8.RomLoaded)
{
Start(chip8);
}
}
}
public static void Start(Chip8 chip8)
{
int windowWidth = Constants.DisplayWidth * Constants.WindowScale;
int windowHeight = Constants.DisplayHeight * Constants.WindowScale;
Raylib.InitWindow(
windowWidth,
windowHeight,
"Chip8Sharp"
);
Raylib.SetTargetFPS(60);
var lastCycleTime = DateTime.UtcNow;
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.Black);
var currentTime = DateTime.UtcNow;
float deltaTime =
(float)(currentTime - lastCycleTime).TotalMilliseconds;
if (deltaTime > Constants.CycleDelay)
{
lastCycleTime = currentTime;
DetectInput(chip8);
chip8.Cycle();
DrawGraphics(chip8.Display);
}
Raylib.DrawFPS(12, 12);
Raylib.EndDrawing();
}
Raylib.CloseWindow();
}
private static void DrawGraphics(ReadOnlySpan<bool> display)
{
int windowScale = Constants.WindowScale;
int displayWidth = Constants.DisplayWidth;
for (int i = 0; i < display.Length; i++)
{
if (!display[i]) continue;
int xPos = (i % displayWidth) * windowScale;
int yPos = (i / displayWidth) * windowScale;
Raylib.DrawRectangle(
xPos, yPos, windowScale, windowScale, Color.White);
}
}
private static void DetectInput(Chip8 chip8)
{
foreach(var key in inputMap)
{
if (Raylib.IsKeyDown(key.Key))
{
chip8.SetKeyPress(key.Value, true);
continue;
}
if (Raylib.IsKeyReleased(key.Key))
{
chip8.SetKeyPress(key.Value, false);
}
}
}
}