-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cs
More file actions
211 lines (174 loc) · 7 KB
/
Copy pathWindow.cs
File metadata and controls
211 lines (174 loc) · 7 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
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
using BlackBox.Machine;
using FontStashSharp;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Panel = System.Panel;
using Terminal = System.Terminal;
using Bitmap = System.Peripherals.Bitmap;
namespace BlackBox;
public class Window : Game
{
private const string Title = "Black Box";
private const string FontPath = "./Fonts/IBM-VGA.ttf";
private const int FontSize = 32;
public const int CharsPerLine = 87; //87 for 32px font
private const int WindowWidth = 1920;
private const int WindowHeight = 1000;
private const int BitmapSize = 256;
private const double TargetFps = 60.0;
public static int Gap { get; set; } = 6;
public static int CharSpacingH { get; set; } = 0;
public static int CharSpacingV { get; set; } = 3;
private readonly GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch = null!;
private Texture2D pixelTexture = null!;
private Texture2D bitmapTexture = null!;
private FontSystem fontSystem = null!;
private DynamicSpriteFont font = null!;
private int cellWidth;
private int cellHeight;
private int CharWidth => cellWidth - CharSpacingH;
private int CharHeight => cellHeight - CharSpacingV;
public static Rectangle PanelRectangle { get; private set; }
public static Rectangle TerminalRectangle { get; private set; }
public static Rectangle BitmapPanelRectangle { get; private set; }
public static Rectangle FilePanelRectangle { get; private set; }
public static Rectangle BackgroundRectangle { get; private set; }
public static RenderTarget2D PanelRenderTarget { get; private set; } = null!;
public static RenderTarget2D TerminalRenderTarget { get; private set; } = null!;
public static RenderTarget2D BitmapPanel { get; private set; } = null!;
public static RenderTarget2D FilePanel { get; private set; } = null!;
public static RenderTarget2D Background { get; private set; } = null!;
public Window()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
IsFixedTimeStep = true;
TargetElapsedTime = TimeSpan.FromSeconds(1.0 / TargetFps);
Window.Title = Title;
}
protected override void Initialize()
{
Input.Initialize(Window);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
if (!File.Exists(FontPath))
{
Console.WriteLine("Could not find font file: " + FontPath);
Exit();
return;
}
fontSystem = new FontSystem();
fontSystem.AddFont(File.ReadAllBytes(FontPath));
graphics.PreferredBackBufferWidth = WindowWidth;
graphics.PreferredBackBufferHeight = WindowHeight;
graphics.ApplyChanges();
CalculateLayout();
PanelRenderTarget = new RenderTarget2D(GraphicsDevice, Panel.Width * cellWidth, Panel.Height * cellHeight);
TerminalRenderTarget = new RenderTarget2D(GraphicsDevice, Terminal.Width * cellWidth, Terminal.Height * cellHeight);
BitmapPanel = new RenderTarget2D(GraphicsDevice, BitmapSize, BitmapSize);
FilePanel = new RenderTarget2D(GraphicsDevice, BitmapSize, BitmapSize);
Background = new RenderTarget2D(GraphicsDevice, WindowWidth, WindowHeight);
pixelTexture = new Texture2D(GraphicsDevice, 1, 1);
pixelTexture.SetData(new[] { Color.White });
bitmapTexture = new Texture2D(GraphicsDevice, Bitmap.Width, Bitmap.Height);
Bitmap.Clear();
}
private void CalculateLayout()
{
int sideW = (int)(WindowWidth * 0.3f);
int miniSize = Math.Min(sideW, (WindowHeight - Gap) / 2);
sideW = Math.Min(sideW, miniSize);
int mainW = WindowWidth - sideW - Gap;
BackgroundRectangle = new Rectangle(0, 0, WindowWidth, WindowHeight);
BitmapPanelRectangle = new Rectangle(mainW + Gap, 0, sideW, miniSize);
FilePanelRectangle = new Rectangle(mainW + Gap, WindowHeight - miniSize, sideW, miniSize);
font = fontSystem.GetFont(FontSize);
cellWidth = (int)font.MeasureString("M").X + CharSpacingH;
cellHeight = (int)font.MeasureString("M").Y + CharSpacingV;
int totalRows = WindowHeight / cellHeight;
int panelRows = (int)(totalRows * 0.6f);
int terminalRows = totalRows - panelRows;
Panel.Height = panelRows;
Terminal.Height = terminalRows;
int panelH = panelRows * cellHeight;
int terminalH = terminalRows * cellHeight;
int gridW = cellWidth * Terminal.Width;
PanelRectangle = new Rectangle(0, 0, gridW, panelH);
TerminalRectangle = new Rectangle(0, panelH, gridW, terminalH);
}
protected override void Update(GameTime gameTime)
{
Input.Update(gameTime);
Host.Loop();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(Background);
GraphicsDevice.Clear(Color.White);
GraphicsDevice.SetRenderTarget(PanelRenderTarget);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
for (int y = 0; y < Panel.Height; y++)
for (int x = 0; x < Panel.Width; x++)
{
var (bgR, bgG, bgB) = Panel.GetBackgroundColor(x, y);
var (fgR, fgG, fgB) = Panel.GetForegroundColor(x, y);
var ch = Panel.GetChar(x, y);
int posX = x * cellWidth, posY = y * cellHeight;
spriteBatch.Draw(pixelTexture, new Rectangle(posX, posY, cellWidth, cellHeight), new Color(bgR, bgG, bgB));
if (ch != ' ')
spriteBatch.DrawString(font, ch.ToString(), new Vector2(posX, posY), new Color(fgR, fgG, fgB));
}
spriteBatch.End();
GraphicsDevice.SetRenderTarget(TerminalRenderTarget);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
for (int y = 0; y < Terminal.Height; y++)
for (int x = 0; x < Terminal.Width; x++)
{
var (bgR, bgG, bgB) = Terminal.GetBackgroundColor(x, y);
var (fgR, fgG, fgB) = Terminal.GetForegroundColor(x, y);
var ch = Terminal.GetChar(x, y);
int posX = x * cellWidth, posY = y * cellHeight;
spriteBatch.Draw(pixelTexture, new Rectangle(posX, posY, cellWidth, cellHeight), new Color(bgR, bgG, bgB));
if (ch != ' ')
spriteBatch.DrawString(font, ch.ToString(), new Vector2(posX, posY), new Color(fgR, fgG, fgB));
}
spriteBatch.End();
GraphicsDevice.SetRenderTarget(BitmapPanel);
bitmapTexture.SetData(Bitmap.Buffer);
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
spriteBatch.Draw(bitmapTexture, new Rectangle(0, 0, BitmapSize, BitmapSize), Color.White);
spriteBatch.End();
GraphicsDevice.SetRenderTarget(FilePanel);
GraphicsDevice.Clear(Color.Gray);
GraphicsDevice.SetRenderTarget(null);
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
spriteBatch.Draw(Background, BackgroundRectangle, Color.White);
spriteBatch.Draw(PanelRenderTarget, PanelRectangle, Color.White);
spriteBatch.Draw(TerminalRenderTarget, TerminalRectangle, Color.White);
spriteBatch.Draw(FilePanel, FilePanelRectangle, Color.White);
spriteBatch.End();
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
spriteBatch.Draw(BitmapPanel, BitmapPanelRectangle, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
protected override void UnloadContent()
{
pixelTexture.Dispose();
base.UnloadContent();
}
public static void Main()
{
Window game = new();
game.Run();
}
}