-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
133 lines (108 loc) · 5.69 KB
/
Program.cs
File metadata and controls
133 lines (108 loc) · 5.69 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
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.Numerics;
using Raylib_cs;
namespace SandSimulator;
internal static class Program
{
// STAThread is required if you deploy using NativeAOT on Windows - See https://github.com/raylib-cs/raylib-cs/issues/301
[System.STAThread]
public static void Main()
{
// Window settings
Vector2 resolution = new Vector2(1920, 1080);
int windowScale = 5;
int windowWidth = (int)(resolution.X / windowScale); //400;
int windowHeight = (int)(resolution.Y / windowScale); //250
int targetFps = 60;
// Initilize raylib window
Raylib.InitWindow(width: (int)(windowWidth * windowScale), height: (int)(windowHeight * windowScale), title: "Sand Simulator");
Raylib.SetTargetFPS(fps: targetFps);
// Initialize GUI components
float menuWidth = windowWidth * windowScale / 4; // Width of the control panel
float menuHeight = windowHeight * windowScale;
ControlMenu controlMenu = new ControlMenu(windowWidth * windowScale - menuWidth, 0, menuWidth, menuHeight);
Color[] selectedMaterialsColors = new Color[3];
// Debug stuff
bool debugEnabled = false;
Button debugButton = new Button(10, 77, 154, 47, "Debug");
// Initilize simulator instance
Simulator simulator = new Simulator(width: windowWidth, height: windowHeight, scale: windowScale);
SandType[] sandTypes = simulator.GetSandTypes;
// Benchmark state
string benchmarkResult = "";
// Run main simulation loop
while (!Raylib.WindowShouldClose())
{
// Debug functionality
if (Raylib.IsKeyPressed(KeyboardKey.D))
{
debugEnabled = !debugEnabled;
debugButton.IsEnabled = debugEnabled;
}
if (debugButton.IsClicked)
{
Console.WriteLine("Pressed debug button...");
}
// Control menu toggle
if (controlMenu.IsMenuButtonClicked) controlMenu.IsVisible = !controlMenu.IsVisible;
// Exit button
if (controlMenu.exitBtn.IsLeftClicked) break;
// Press B to run benchmark or click benchmark button
if (Raylib.IsKeyPressed(KeyboardKey.B) || controlMenu.benchmarkBtn.IsLeftClicked)
{
Raylib.SetTargetFPS(fps: 999); // Unlock FPS for benchmark
var (ms, frames) = simulator.RunBenchmark(sandCount: 50000, frames: 500);
benchmarkResult = $"{frames} frames: {ms:F0}ms ({ms / frames:F2}ms/frame)";
Console.WriteLine(benchmarkResult);
Raylib.SetTargetFPS(fps: targetFps); // Set it back to previous value
}
// Press C to clear the screen or click clear button
if (Raylib.IsKeyPressed(KeyboardKey.C) || controlMenu.clearBtn.IsLeftClicked)
{
Array.Fill(simulator.GetIdArray, 0);
benchmarkResult = "";
}
// Material selection
for (int i = 0; i < sandTypes.Length; i++)
{
controlMenu.CurrentMaterialSelection = [
controlMenu.materialButtons[i].IsLeftClicked ? i : controlMenu.CurrentMaterialSelection[0],
controlMenu.materialButtons[i].IsRightClicked ? i : controlMenu.CurrentMaterialSelection[1],
controlMenu.materialButtons[i].IsMiddleClicked ? i : controlMenu.CurrentMaterialSelection[2]
];
}
// Material painting
if (!controlMenu.IsHovered)
{
simulator.MousePaint(sandID: sandTypes[controlMenu.CurrentMaterialSelection[0]].GetID);
simulator.MousePaint(sandID: sandTypes[controlMenu.CurrentMaterialSelection[1]].GetID, triggerButton: MouseButton.Right);
simulator.MousePaint(sandID: sandTypes[controlMenu.CurrentMaterialSelection[2]].GetID, triggerButton: MouseButton.Middle);
}
// Main simulation logic
simulator.SimulateScene();
// Update the texture with the newest colorArray data
simulator.UpdateTexture();
// Simulation vizualization
Raylib.BeginDrawing();
Raylib.DrawTextureEx(texture: simulator.GetTexture, position: new Vector2(0, 0), rotation: 0.0f, scale: windowScale, tint: Color.White);
// Display GUI
simulator.GetSandColorFromIdDict.TryGetValue(sandTypes[controlMenu.CurrentMaterialSelection[0]].GetID, out selectedMaterialsColors[0]);
simulator.GetSandColorFromIdDict.TryGetValue(sandTypes[controlMenu.CurrentMaterialSelection[1]].GetID, out selectedMaterialsColors[1]);
simulator.GetSandColorFromIdDict.TryGetValue(sandTypes[controlMenu.CurrentMaterialSelection[2]].GetID, out selectedMaterialsColors[2]);
controlMenu.Draw(selectedMaterialsColors);
if (debugEnabled) debugButton.Draw();
// Display runtime info
Raylib.DrawRectangle(10, 10, 154, 47, Color.LightGray);
Raylib.DrawText("Sand Simulator", posX: 12, posY: 12, fontSize: 20, color: Color.Black);
Raylib.DrawText($"FPS: {Raylib.GetFPS():F0}", posX: 12, posY: 36, fontSize: 20, color: Color.Black);
if (!string.IsNullOrEmpty(benchmarkResult))
{
Raylib.DrawRectangle(200, 10, (int)(200 * windowScale), (int)(12 * windowScale), Color.LightGray);
Raylib.DrawText(benchmarkResult, posX: 203, posY: 20, fontSize: (int)(10 * windowScale), color: Color.DarkGray);
}
Raylib.EndDrawing();
}
simulator.Close();
controlMenu.Close();
Raylib.CloseWindow();
}
}