-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRender_Form.cs
259 lines (219 loc) · 9.07 KB
/
Render_Form.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Drawing.Imaging;
using NDI_SubTitle.Properties;
namespace NDI_SubTitle
{
class Render_Form : GameWindow
{
public Render_Form(DisplayDevice dd, int _width, int _height, Font font, RenderConfig config)
: base(_width, _height, new GraphicsMode(new ColorFormat(8, 8, 8, 8), 24, 8, 8),
"SubTitle - FullScreen", GameWindowFlags.Fullscreen, dd)
{
Title += ": OpenGL Version: " + GL.GetString(StringName.Version);
height = _height;
width = _width;
Config = config;
// Render Initial
program = SubTitle.Empty;
fading = SubTitle.Empty;
// Style
defaultBrush = new SolidBrush(config.Default_Color);
this.font = font;
bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
graphics = Graphics.FromImage(bmp);
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// Fade Setting
isFading = false;
// For delta_X, We plus delta_X into alpha
// Fade Effect will last for 500ms
// So every frame it will change 255/( ) = 10
delta_X = Alpha_Max / ((config.frameRateNumerator / config.frameRateDenominator) * config.Fade_Time / 1000);
// If Fading_X < Alpha_Max / 2 ,it's the former part (from program to empty)
// If Fading_X >= Alpha_Max / 2 ,it's the latter part (from empty to fading )
// If Fading_X >= Alpha_Max ,it means fade ends (set program as fading)
Fading_X = 0;
}
private int height;
private int width;
//Render
private RenderConfig Config;
public const int Alpha_Max = 255;
public const int Alpha_Min = 0;
private static readonly object syncLock = new object();
private Bitmap bmp;
private Graphics graphics;
private int textTexture = -1;
private Font font;
private SolidBrush defaultBrush;
private SubTitle program;
private SubTitle fading;
private bool isFading;
private int Fading_X;
private int delta_X;
private bool isChange = false;
public void Cut(SubTitle sub)
{
lock (syncLock)
{
if (isChange)
return;
program = sub;
isChange = true;
}
}
public void Fade(SubTitle sub)
{
lock (syncLock)
{
if (isChange)
return;
fading = sub.Clone();
fading.alpha = Alpha_Min;
isFading = true;
isChange = true;
Fading_X = 0; //Reset Fading_X
}
}
public void ChangeFont(FontFamily font, FontStyle fontStyle = FontStyle.Regular)
{
lock (syncLock)
this.font = new Font(font, Config.fontSize, fontStyle);
isChange = true;
}
public void onChanged(RenderConfig config)
{
lock (syncLock)
font = new Font(font.FontFamily, config.fontSize);
delta_X = Alpha_Max / ((Config.frameRateNumerator / Config.frameRateDenominator) * config.Fade_Time / 1000);
isChange = true;
}
private void GenerateBmp()
{
if (textTexture != -1)
GL.DeleteTextures(1, ref textTexture);
graphics.Clear(Color.Transparent);
if (!isFading)
{
graphics.DrawString(program.First_Sub, font, defaultBrush, Config.Point_Sub1);
graphics.DrawString(program.Second_Sub, font, defaultBrush, Config.Point_Sub2);
}
else
{
//SolidBrush current = solidBrushes[fadeBrushIndex];
//SolidBrush fadeOut = solidBrushes[solidBrushes.Count -1 - fadeBrushIndex];
//Console.WriteLine(current.Color.A);
//graphics.DrawString(fading.First_Sub, font, current, Point_Sub1);
//graphics.DrawString(fading.Second_Sub, font, current, Point_Sub2);
//graphics.DrawString(program.First_Sub, font, fadeOut, Point_Sub1);
//graphics.DrawString(program.Second_Sub, font, fadeOut, Point_Sub2);
//fadeBrushIndex++;
//if (fadeBrushIndex == solidBrushes.Count)
//{
// fadeBrushIndex = 0;
// isFading = false;
// program = fading;
//}
// BUG !!!!
// FADING
if (Fading_X >= Alpha_Max)
{
Fading_X = 255;
isFading = false; //end fade
program = fading;
}
if (Fading_X < Alpha_Max / 2) //Alpha_Max/2 = 127.5
{
// from program to empty
// It's 0 -> 127 , but alpha should be 255 -> 0
// So we use 255 - Fading_X * 2
int alpha = Alpha_Max - Fading_X * 2;
if (alpha < 0)
alpha = 0;
var brush = new SolidBrush(Color.FromArgb(alpha, Config.Default_Color));
graphics.DrawString(program.First_Sub, font, brush, Config.Point_Sub1);
graphics.DrawString(program.Second_Sub, font, brush, Config.Point_Sub2);
}
else if (Fading_X >= Alpha_Max / 2)
{
// from empty to fading
// It's 128 -> 255,but alpha should be 0 -> 255
// So we use Fading_X * 2 - 255
int alpha = Fading_X * 2 - Alpha_Max;
if (alpha > 255)
alpha = 255;
var brush = new SolidBrush(Color.FromArgb(alpha, Config.Default_Color));
graphics.DrawString(fading.First_Sub, font, brush, Config.Point_Sub1);
graphics.DrawString(fading.Second_Sub, font, brush, Config.Point_Sub2);
}
Fading_X += delta_X;
}
//bmp.Save("D:\\test.png");
GL.ClearColor(Color.AntiqueWhite);
GL.Enable(EnableCap.Texture2D);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.GenTextures(1, out textTexture);
GL.BindTexture(TextureTarget.Texture2D, textTexture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Rgb, PixelType.UnsignedByte, data.Scan0);
bmp.UnlockBits(data);
}
protected override void OnLoad(EventArgs e)
{
CursorVisible = false;
}
protected override void OnUnload(EventArgs e)
{
GL.DeleteTextures(1, ref textTexture);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
if (isChange || isFading)
{
lock (syncLock)
{
GenerateBmp();
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.BindTexture(TextureTarget.Texture2D, textTexture);
GL.Begin(BeginMode.Quads);
GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(1.0f, 1.0f);
GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-1.0f, 1.0f);
GL.End();
ErrorCode errorCode = GL.GetError();
if (errorCode != ErrorCode.NoError)
Console.WriteLine($"OpenTK ERROR!!: {errorCode.ToString()}");
}
isChange = false;
}
SwapBuffers();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
Console.WriteLine("On Key Press in openTK");
base.OnKeyPress(e);
}
}
}