From db554a5757540566d2ab9833c6fdf0514315b76b Mon Sep 17 00:00:00 2001 From: luvletter2333 Date: Sun, 16 May 2021 02:45:59 +0800 Subject: [PATCH] Fix FullScreen Fade --- Form1.cs | 38 +++-- Form1.resx | 259 ++++++++++++++++++++------------ NDIRender.cs | 4 +- Ndi-SubTitle.csproj | 3 +- NDIConfig.cs => RenderConfig.cs | 26 ++-- Render_Form.cs | 101 +++++++++++-- 6 files changed, 289 insertions(+), 142 deletions(-) rename NDIConfig.cs => RenderConfig.cs (75%) diff --git a/Form1.cs b/Form1.cs index 91998df..cb3f199 100644 --- a/Form1.cs +++ b/Form1.cs @@ -38,7 +38,7 @@ enum RenderMode SubTitle Previewing = SubTitle.Empty; JObject Config; - NDIConfig NDI_Config; + RenderConfig NDI_Config; NDIRender Renderer; CancellationTokenSource cancelNDI; float Font_Size; @@ -70,9 +70,9 @@ private void Form1_Load(object sender, EventArgs e) { Config = JObject.Parse(File.ReadAllText(config_path)); if (Config.ContainsKey("NDI")) - NDI_Config = NDIConfig.ReadNDIConfig(Config["NDI"] as JObject); + NDI_Config = RenderConfig.ReadNDIConfig(Config["NDI"] as JObject); else - NDI_Config = new NDIConfig(true); + NDI_Config = new RenderConfig(true); if (Config.ContainsKey("Font-Size")) Font_Size = Convert.ToSingle(Config["Font-Size"].ToString()); else @@ -82,7 +82,7 @@ private void Form1_Load(object sender, EventArgs e) { Console.WriteLine("Reading Config File Failed"); Console.WriteLine(ex.ToString()); - NDI_Config = new NDIConfig(true); + NDI_Config = new RenderConfig(true); Font_Size = 50; } } @@ -309,21 +309,31 @@ private void btn_Clear_Fade_Click(object sender = null, EventArgs e = null) { if (render_form == null) return; - + render_form.Fade(SubTitle.Empty); + Program(SubTitle.Empty); } } private void btn_fade_Click(object sender = null, EventArgs e = null) //Fade { - if (Renderer == null) - return; if (subTitles.Count == 0) return; if (lst_SubTitle.SelectedIndex < 0) return; if (subTitles.Count > lst_SubTitle.SelectedIndex) { - Renderer.Fade(Previewing); + if (render_mode == RenderMode.FullScreen) + { + if (render_form == null) + return; + render_form.Fade(Previewing); + } + else + { + if (Renderer == null) + return; + Renderer.Fade(Previewing); + } Program(Previewing); run_printer = Previewing.id + 1; if (run_printer != subTitles.Count) @@ -342,21 +352,17 @@ private void btn_Hard_Click(object sender = null, EventArgs e = null)//Send Dire { if (render_form == null) return; render_form.Cut(Previewing); - Program(Previewing); - run_printer = Previewing.id + 1; - if (run_printer != subTitles.Count) - lst_SubTitle.SetSelected(run_printer, true); } else { if (Renderer == null) return; Renderer.Cut(Previewing); - Program(Previewing); - run_printer = Previewing.id + 1; - if (run_printer != subTitles.Count) - lst_SubTitle.SetSelected(run_printer, true); } + Program(Previewing); + run_printer = Previewing.id + 1; + if (run_printer != subTitles.Count) + lst_SubTitle.SetSelected(run_printer, true); } private void btn_Clear_Click(object sender = null, EventArgs e = null) diff --git a/Form1.resx b/Form1.resx index a9d43c4..b22f95b 100644 --- a/Form1.resx +++ b/Form1.resx @@ -126,10 +126,14 @@ 宋体, 10pt - 12, 12 + 16, 17 + + + + 4, 0, 4, 0 - 143, 17 + 159, 20 2 @@ -150,14 +154,13 @@ 25 - 564, 67 + 776, 94 - - 3, 2, 3, 2 + 4, 3, 4, 3 - 100, 26 + 138, 36 3 @@ -178,13 +181,13 @@ 24 - 670, 67 + 921, 94 - 3, 2, 3, 2 + 4, 3, 4, 3 - 100, 26 + 138, 36 4 @@ -211,10 +214,13 @@ 宋体, 14pt - 270, 100 + 371, 140 + + + 4, 0, 4, 0 - 106, 24 + 124, 28 5 @@ -241,10 +247,13 @@ 宋体, 10pt - 12, 39 + 16, 55 + + + 4, 0, 4, 0 - 134, 17 + 149, 20 6 @@ -265,13 +274,13 @@ 21 - 12, 94 + 16, 132 - 3, 2, 3, 2 + 4, 3, 4, 3 - 83, 36 + 114, 50 7 @@ -292,13 +301,13 @@ 20 - 181, 94 + 249, 132 - 3, 2, 3, 2 + 4, 3, 4, 3 - 83, 36 + 114, 50 8 @@ -325,10 +334,13 @@ 宋体, 15pt - 1118, 296 + 1537, 414 + + + 4, 0, 4, 0 - 103, 25 + 118, 30 9 @@ -355,10 +367,13 @@ 宋体, 15pt - 1119, 136 + 1539, 190 + + + 4, 0, 4, 0 - 103, 25 + 118, 30 10 @@ -385,10 +400,13 @@ 宋体, 12pt - 1119, 337 + 1539, 472 + + + 4, 0, 4, 0 - 179, 20 + 214, 24 11 @@ -415,10 +433,13 @@ 宋体, 12pt - 1119, 177 + 1539, 248 + + + 4, 0, 4, 0 - 179, 20 + 214, 24 12 @@ -439,13 +460,13 @@ 15 - 1280, 471 + 1760, 659 - 3, 2, 3, 2 + 4, 3, 4, 3 - 141, 65 + 194, 91 13 @@ -466,13 +487,13 @@ 14 - 1124, 471 + 1546, 659 - 3, 2, 3, 2 + 4, 3, 4, 3 - 141, 65 + 194, 91 14 @@ -493,13 +514,13 @@ 13 - 1124, 547 + 1546, 766 - 3, 2, 3, 2 + 4, 3, 4, 3 - 141, 70 + 194, 98 15 @@ -520,13 +541,13 @@ 12 - 101, 94 + 139, 132 - 3, 2, 3, 2 + 4, 3, 4, 3 - 75, 36 + 103, 50 16 @@ -547,16 +568,16 @@ 11 - 15 + 21 - 12, 136 + 16, 190 - 3, 2, 3, 2 + 4, 3, 4, 3 - 252, 484 + 345, 676 17 @@ -577,19 +598,19 @@ 宋体, 12pt - 20 + 24 - 269, 136 + 370, 190 - 3, 2, 3, 2 + 4, 3, 4, 3 True - 837, 484 + 1149, 676 18 @@ -610,10 +631,13 @@ True - 377, 107 + 518, 150 + + + 4, 0, 4, 0 - 63, 15 + 87, 21 20 @@ -640,10 +664,13 @@ 宋体, 10pt - 12, 64 + 16, 90 + + + 4, 0, 4, 0 - 134, 17 + 149, 20 22 @@ -664,13 +691,13 @@ 7 - 153, 64 + 210, 90 - 3, 2, 3, 2 + 4, 3, 4, 3 - 287, 23 + 393, 29 23 @@ -688,13 +715,13 @@ 6 - 446, 59 + 613, 83 - 3, 2, 3, 2 + 4, 3, 4, 3 - 100, 30 + 138, 42 24 @@ -715,13 +742,13 @@ 5 - 1280, 547 + 1760, 766 - 3, 2, 3, 2 + 4, 3, 4, 3 - 141, 70 + 194, 98 25 @@ -745,10 +772,13 @@ NoControl - 9, 47 + 12, 66 + + + 4, 4, 4, 4 - 116, 28 + 160, 39 26 @@ -769,10 +799,13 @@ 0 - 131, 18 + 180, 25 + + + 4, 4, 4, 4 - 123, 23 + 168, 29 27 @@ -793,10 +826,13 @@ True - 6, 22 + 8, 31 + + + 4, 0, 4, 0 - 119, 15 + 164, 21 28 @@ -820,10 +856,13 @@ NoControl - 131, 47 + 180, 66 + + + 4, 4, 4, 4 - 123, 28 + 169, 39 29 @@ -847,10 +886,13 @@ True - 6, 85 + 8, 119 + + + 4, 0, 4, 0 - 47, 15 + 65, 21 32 @@ -871,16 +913,19 @@ 5 - 59, 81 + 81, 113 + + + 4, 4, 4, 4 - 66, 25 + 89, 31 33 - 1680 + 1920 txt_screen_width @@ -895,16 +940,19 @@ 7 - 182, 81 + 250, 113 + + + 4, 4, 4, 4 - 72, 25 + 98, 31 35 - 1050 + 1080 txt_screen_height @@ -922,10 +970,13 @@ True - 131, 85 + 180, 119 + + + 4, 0, 4, 0 - 55, 15 + 76, 21 34 @@ -949,13 +1000,13 @@ NoControl - 670, 97 + 921, 136 - 3, 2, 3, 2 + 4, 3, 4, 3 - 100, 27 + 138, 38 37 @@ -979,13 +1030,13 @@ NoControl - 564, 100 + 776, 140 - 3, 2, 3, 2 + 4, 3, 4, 3 - 100, 24 + 138, 34 36 @@ -1009,10 +1060,13 @@ True - 92, 23 + 126, 32 + + + 4, 4, 4, 4 - 108, 19 + 145, 25 1 @@ -1036,10 +1090,13 @@ True - 6, 22 + 8, 31 + + + 4, 4, 4, 4 - 52, 19 + 68, 25 0 @@ -1060,10 +1117,16 @@ 1 - 564, 9 + 776, 13 + + + 4, 4, 4, 4 + + + 4, 4, 4, 4 - 206, 53 + 283, 74 38 @@ -1084,10 +1147,16 @@ 1 - 776, 12 + 1067, 17 + + + 4, 4, 4, 4 + + + 4, 4, 4, 4 - 269, 112 + 370, 157 39 @@ -1111,13 +1180,13 @@ True - 8, 15 + 11, 21 - 1439, 628 + 1979, 879 - 3, 2, 3, 2 + 4, 3, 4, 3 Form1 diff --git a/NDIRender.cs b/NDIRender.cs index 4aefc84..e6d7ee9 100644 --- a/NDIRender.cs +++ b/NDIRender.cs @@ -31,12 +31,12 @@ public class NDIRender private int Fading_X; private readonly int delta_X; - public NDIRender(CancellationToken cancellationToken, NDIConfig config, Font font) + public NDIRender(CancellationToken cancellationToken, RenderConfig config, Font font) { this.cancellationToken = cancellationToken; // We are going to create a 1920x180 frame at 50p, progressive (default). - this.videoFrame = new VideoFrame(config.NDI_width, config.NDI_height, config.aspectRatio, config.frameRateNumerator, config.frameRateDenominator); + this.videoFrame = new VideoFrame(config.Width, config.Height, config.aspectRatio, config.frameRateNumerator, config.frameRateDenominator); bmp = new Bitmap(videoFrame.Width, videoFrame.Height, videoFrame.Stride, System.Drawing.Imaging.PixelFormat.Format32bppArgb, videoFrame.BufferPtr); graphics = Graphics.FromImage(bmp); diff --git a/Ndi-SubTitle.csproj b/Ndi-SubTitle.csproj index 5883cfb..db4d1f0 100644 --- a/Ndi-SubTitle.csproj +++ b/Ndi-SubTitle.csproj @@ -91,7 +91,7 @@ - + Form @@ -106,6 +106,7 @@ Form1.cs + Designer ResXFileCodeGenerator diff --git a/NDIConfig.cs b/RenderConfig.cs similarity index 75% rename from NDIConfig.cs rename to RenderConfig.cs index bb73048..1001293 100644 --- a/NDIConfig.cs +++ b/RenderConfig.cs @@ -8,11 +8,11 @@ namespace NDI_SubTitle { - public class NDIConfig + public class RenderConfig { // NDI Output int width, int height, float aspectRatio, int frameRateNumerator, int frameRateDenominator - public int NDI_width; - public int NDI_height; + public int Width; + public int Height; public float aspectRatio; public int frameRateNumerator; public int frameRateDenominator; @@ -23,13 +23,13 @@ public class NDIConfig public Color Default_Color; public int Fade_Time; //ms - public NDIConfig(bool is_default = false) + public RenderConfig(bool is_default = false) { if (is_default) { - NDI_width = 1920; - NDI_height = 180; - aspectRatio = Convert.ToSingle(NDI_width) / Convert.ToSingle(NDI_height); + Width = 1920; + Height = 180; + aspectRatio = Convert.ToSingle(Width) / Convert.ToSingle(Height); frameRateNumerator = 50; frameRateDenominator = 1; @@ -40,14 +40,14 @@ public NDIConfig(bool is_default = false) } } - public static NDIConfig ReadNDIConfig(JObject jo) + public static RenderConfig ReadNDIConfig(JObject jo) { try { - NDIConfig config = new NDIConfig(); - config.NDI_width = Convert.ToInt32(jo["NDI_width"].ToString()); - config.NDI_height = Convert.ToInt32(jo["NDI_height"].ToString()); - config.aspectRatio = Convert.ToSingle(config.NDI_width) / Convert.ToSingle(config.NDI_height); + RenderConfig config = new RenderConfig(); + config.Width = Convert.ToInt32(jo["NDI_width"].ToString()); + config.Height = Convert.ToInt32(jo["NDI_height"].ToString()); + config.aspectRatio = Convert.ToSingle(config.Width) / Convert.ToSingle(config.Height); config.frameRateNumerator = Convert.ToInt32(jo["frameRateNumerator"].ToString()); config.frameRateDenominator = Convert.ToInt32(jo["frameRateDenominator"].ToString()); @@ -66,7 +66,7 @@ public static NDIConfig ReadNDIConfig(JObject jo) catch (Exception e) { Console.WriteLine(e.ToString()); - return new NDIConfig(true);//Return Default Setting + return new RenderConfig(true);//Return Default Setting } } } diff --git a/Render_Form.cs b/Render_Form.cs index 10561c8..e3d91d0 100644 --- a/Render_Form.cs +++ b/Render_Form.cs @@ -18,7 +18,7 @@ namespace NDI_SubTitle { class Render_Form : GameWindow { - public Render_Form(DisplayDevice dd, int _width, int _height, Font font, NDIConfig config) + 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) { @@ -26,18 +26,18 @@ public Render_Form(DisplayDevice dd, int _width, int _height, Font font, NDIConf height = _height; width = _width; - NDI_Config = config; + Config = config; // Render Initial program = SubTitle.Empty; fading = SubTitle.Empty; - Point_Sub1 = new Point(50, height / 2); - Point_Sub2 = new Point(50, height / 2 + 100); + Point_Sub1 = config.Point_Sub1; + Point_Sub2 = config.Point_Sub2; // Style defaultBrush = new SolidBrush(config.Default_Color); this.font = font; - bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); + 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; @@ -54,15 +54,27 @@ public Render_Form(DisplayDevice dd, int _width, int _height, Font font, NDIConf // If Fading_X >= Alpha_Max ,it means fade ends (set program as fading) Fading_X = 0; + + solidBrushes = new List(); + int alpha = Alpha_Min; + for (; alpha < Alpha_Max; alpha += delta_X) + { + solidBrushes.Add(new SolidBrush(Color.FromArgb(alpha, config.Default_Color))); + } + if (alpha > Alpha_Max) + solidBrushes.Add(new SolidBrush(Color.FromArgb(Alpha_Max, config.Default_Color))); + + + } private int height; private int width; //Render - private NDIConfig NDI_Config; + private RenderConfig Config; public const int Alpha_Max = 255; - public const int Alpha_Min = 255; + public const int Alpha_Min = 0; private static readonly object syncLock = new object(); private Bitmap bmp; @@ -79,6 +91,8 @@ public Render_Form(DisplayDevice dd, int _width, int _height, Font font, NDIConf private bool isFading; private int Fading_X; private readonly int delta_X; + private List solidBrushes; + private int fadeBrushIndex = 0; private bool isChange = false; @@ -118,9 +132,68 @@ private void GenerateBmp() if (textTexture != -1) GL.DeleteTextures(1, ref textTexture); graphics.Clear(Color.Transparent); - graphics.DrawString(program.First_Sub, font, defaultBrush, Point_Sub1); - graphics.DrawString(program.Second_Sub, font, defaultBrush, Point_Sub2); - bmp.Save("D:\\test.png"); + + if (!isFading) + { + graphics.DrawString(program.First_Sub, font, defaultBrush, Point_Sub1); + graphics.DrawString(program.Second_Sub, font, defaultBrush, 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, Color.White)); + graphics.DrawString(program.First_Sub, font, brush, Point_Sub1); + graphics.DrawString(program.Second_Sub, font, brush, 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, Color.White)); + graphics.DrawString(fading.First_Sub, font, brush, Point_Sub1); + graphics.DrawString(fading.Second_Sub, font, brush, Point_Sub2); + } + Fading_X += delta_X; + } + //bmp.Save("D:\\test.png"); GL.ClearColor(Color.AntiqueWhite); GL.Enable(EnableCap.Texture2D); @@ -132,10 +205,10 @@ private void GenerateBmp() 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.Format32bppArgb); + ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, - OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); + OpenTK.Graphics.OpenGL.PixelFormat.Rgb, PixelType.UnsignedByte, data.Scan0); bmp.UnlockBits(data); } @@ -154,7 +227,7 @@ protected override void OnUpdateFrame(FrameEventArgs e) } protected override void OnRenderFrame(FrameEventArgs e) { - if (isChange) + if (isChange || isFading) { lock (syncLock) { @@ -177,8 +250,6 @@ protected override void OnRenderFrame(FrameEventArgs e) ErrorCode errorCode = GL.GetError(); if (errorCode != ErrorCode.NoError) Console.WriteLine($"OpenTK ERROR!!: {errorCode.ToString()}"); - - SwapBuffers(); } isChange = false; }