-
Notifications
You must be signed in to change notification settings - Fork 558
/
Copy pathVGACanvas.cs
222 lines (200 loc) · 6.86 KB
/
VGACanvas.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
using System;
using System.Collections.Generic;
using System.Text;
using Cosmos.HAL;
using System.Drawing;
using static Cosmos.HAL.VGADriver;
namespace Cosmos.System.Graphics
{
/// <summary>
/// VGACanvas class. Used to control screen.
/// </summary>
public class VGACanvas : Canvas
{
/// <summary>
/// Private boolean whether VGA graphics mode is enabled or not
/// </summary>
bool _Enabled;
private Mode _Mode;
/// <summary>
/// The HAL VGA driver
/// </summary>
private readonly VGADriver _VGADriver;
/// <summary>
/// VGA graphics mode Canvas constructor - see Canvas.cs
/// </summary>
/// <param name="aMode"></param>
public VGACanvas(Mode aMode) : base()
{
Global.mDebugger.Send("Creating VGACanvas with mode");
_VGADriver = new VGADriver();
_VGADriver.SetGraphicsMode(ModeToScreenSize(aMode), (VGADriver.ColorDepth)(int)aMode.ColorDepth);
Mode = aMode;
Enabled = true;
}
/// <summary>
/// Creates a VGA graphics mode with the default mode
/// </summary>
public VGACanvas() : base()
{
Enabled = true;
Mode = DefaultGraphicMode;
Global.mDebugger.Send("Creating VGACanvas with standard mode");
_VGADriver = new VGADriver();
_VGADriver.SetGraphicsMode(ModeToScreenSize(DefaultGraphicMode), (VGADriver.ColorDepth)(int)DefaultGraphicMode.ColorDepth);
}
/// <summary>
/// Name of the backend
/// </summary>
public override string Name() => "VGACanvas";
/// <summary>
/// Gets or sets the VGA graphics mode
/// </summary>
public override Mode Mode
{
get => _Mode; set
{
_VGADriver.SetGraphicsMode(ModeToScreenSize(_Mode), (VGADriver.ColorDepth)(int)_Mode.ColorDepth);
_Mode = value;
}
}
/// <summary>
/// Clears the screen of all pixels
/// </summary>
/// <param name="aColor"></param>
public override void Clear(int aColor)
{
_VGADriver.DrawFilledRectangle(0, 0, _VGADriver.PixelWidth, _VGADriver.PixelHeight, (uint)aColor);
}
/// <summary>
/// Clears the screen of all pixels
/// </summary>
/// <param name="aColor"></param>
public override void Clear(Color aColor)
{
var paletteIndex = _VGADriver.GetClosestColorInPalette(aColor);
_VGADriver.DrawFilledRectangle(0, 0, _VGADriver.PixelWidth, _VGADriver.PixelHeight, paletteIndex);
}
/// <summary>
/// Disables VGA graphics mode, parent method returns to 80x25 text mode
/// </summary>
public override void Disable()
{
if (Enabled)
{
VGAScreen.SetTextMode(TextSize.Size80x25);
Enabled = false;
}
}
/// <summary>
/// Draws an array of colors, specifiying X and Y coords
/// </summary>
/// <param name="aColors"></param>
/// <param name="aX"></param>
/// <param name="aY"></param>
/// <param name="aWidth"></param>
/// <param name="aHeight"></param>
public override void DrawArray(Color[] aColors, int aX, int aY, int aWidth, int aHeight)
{
throw new NotImplementedException();
}
/// <summary>
/// Draws a filled rectangle
/// </summary>
/// <param name="aColor"></param>
/// <param name="aXStart"></param>
/// <param name="aYStart"></param>
/// <param name="aWidth"></param>
/// <param name="aHeight"></param>
public override void DrawFilledRectangle(Color aColor, int aXStart, int aYStart, int aWidth, int aHeight)
{
_VGADriver.DrawFilledRectangle(aXStart, aYStart, aWidth, aHeight, _VGADriver.GetClosestColorInPalette(aColor));
}
/// <summary>
/// Draws a point
/// </summary>
/// <param name="aColor"></param>
/// <param name="aX"></param>
/// <param name="aY"></param>
public override void DrawPoint(Color aColor, int aX, int aY)
{
_VGADriver.SetPixel((uint)aX, (uint)aY, aColor);
}
/// <summary>
/// Draws a point
/// </summary>
/// <param name="aColor"></param>
/// <param name="aX"></param>
/// <param name="aY"></param>
public void DrawPoint(uint aColor, int aX, int aY)
{
_VGADriver.SetPixel((uint)aX, (uint)aY, aColor);
}
/// <summary>
/// Draws a point
/// </summary>
/// <param name="aColor"></param>
/// <param name="aX"></param>
/// <param name="aY"></param>
public override void DrawPoint(Color aColor, float aX, float aY)
{
throw new NotImplementedException();
}
/// <summary>
/// List of available resolutions
/// </summary>
private static readonly List<Mode> _AvailableModes = new List<Mode>
{
new Mode(640, 480, ColorDepth.ColorDepth4),
new Mode(720, 480, ColorDepth.ColorDepth4),
new Mode(320, 200, ColorDepth.ColorDepth8)
};
public override List<Mode> AvailableModes
{
get
{
return _AvailableModes;
}
}
/// <summary>
/// Retrieves the RGB value of a specified pixel
/// </summary>
/// <param name="aX"></param>
/// <param name="aY"></param>
/// <returns></returns>
public override Color GetPointColor(int aX, int aY)
{
return Color.FromArgb((int)_VGADriver.GetPixel((uint)aX, (uint)aY));
}
/// <summary>
/// The default graphics mode
/// </summary>
public override Mode DefaultGraphicMode => new Mode(640, 480, ColorDepth.ColorDepth4);
/// <summary>
/// Boolean value whether VGA is in graphics mode or not
/// </summary>
public bool Enabled { get => _Enabled; private set => _Enabled = value; }
private ScreenSize ModeToScreenSize(Mode aMode)
{
if (aMode.Width == 320 && aMode.Height == 200)
{
return ScreenSize.Size320x200;
}
else if (aMode.Width == 640 && aMode.Height == 480)
{
return ScreenSize.Size640x480;
}
else if (aMode.Width == 720 && aMode.Height == 480)
{
return ScreenSize.Size720x480;
}
else
{
throw new NotImplementedException();
}
}
public override void Display()
{
}
}
}