public MainWindow() { InitializeComponent(); AppCoreMethods.ulEnableDefaultLogger("./ullog.txt"); AppCoreMethods.ulEnablePlatformFontLoader(); renderer = new(new ULConfig() { ResourcePath = "./resources/" }); ulBrowser.Init(renderer); ulBrowser.view.URL = "https://github.com/SupinePandora43"; bool loaded = false; ulBrowser.view.SetFinishLoadingCallback((user_data, caller, frame_id, is_main_frame, url) => { loaded = true; }); while (!loaded) { renderer.Update(); Thread.Sleep(10); } }
static void Main() { // Set Logger ULPlatform.SetLogger(new ULLogger() { LogMessage = (level, message) => { Console.WriteLine($"({level}): {message}"); } }); // Set Font Loader AppCoreMethods.ulEnablePlatformFontLoader(); // Set filesystem (Ultralight requires "resources/icudt67l.dat", and probably cacert.pem too) AppCoreMethods.ulEnablePlatformFileSystem(Path.GetDirectoryName(typeof(Program).Assembly.Location)); // Create config, used for specifying resources folder (used for URL loading) ULConfig config = new(); // Create Renderer Renderer renderer = new(config); // Create View View view = new(renderer, 512, 512); // Load URL bool loaded = false; view.SetFinishLoadingCallback((user_data, caller, frame_id, is_main_frame, url) => { loaded = true; }); view.URL = "https://github.com"; // Requires "UltralightNet.Resources" // Update Renderer until page is loaded while (!loaded) { renderer.Update(); // sleep | give ultralight time to process network etc. Thread.Sleep(10); } // Render renderer.Render(); // Get Surface ULSurface surface = view.Surface; // Get Bitmap ULBitmap bitmap = surface.Bitmap; // Swap Red and Blue channels bitmap.SwapRedBlueChannels(); // save bitmap to png file bitmap.WritePng("./github.png"); }
static void Main() { AppCoreMethods.ulEnableDefaultLogger("./log.txt"); AppCoreMethods.ulEnablePlatformFileSystem(Path.GetDirectoryName(typeof(Program).Assembly.Location)); ULApp app = new(new ULSettings(), new ULConfig()); ULWindow window = new(app.MainMonitor, 512, 512, false, ULWindowFlags.Titled | ULWindowFlags.Resizable); window.Title = "test title"; ULOverlay overlay = new(window, 512, 512, 0, 0); View view = overlay.View; //view.URL = "https://github.com/SupinePandora43/UltralightNet"; view.SetFailLoadingCallback( (user_data, caller, frame_id, is_main_frame, url, description, error_domain, error_code) => throw new Exception("Failed loading") ); view.SetFinishLoadingCallback((user_data, caller, frame_id, is_main_frame, url) => Console.WriteLine("loaded")); view.HTML = "<html><body><p>123</p></body></html>"; app.Run(); }
public void SetUpdateCallback(ULUpdateCallback callback, IntPtr userData = default) { if (callback is not null) { if (updateHandle.IsAllocated) { updateHandle.Free(); } updateHandle = GCHandle.Alloc(callback, GCHandleType.Normal); AppCoreMethods.ulAppSetUpdateCallback(Ptr, callback, userData); } else { if (updateHandle.IsAllocated) { updateHandle.Free(); } AppCoreMethods.ulAppSetUpdateCallback(Ptr, null, userData); } }
public ULApp(ULSettings settings, ULConfig config) { Ptr = AppCoreMethods.ulCreateApp(settings.Ptr, config.Ptr); }
public static void Main() { Stopwatch framerateStopwatch = new(); WindowCreateInfo windowCI = new() { WindowWidth = (int)Width, WindowHeight = (int)Height, WindowTitle = "UltralightNet.Veldrid.TestApp", X = 100, Y = 100 }; Sdl2Window window = VeldridStartup.CreateWindow(ref windowCI); GraphicsDeviceOptions options = new() { PreferStandardClipSpaceYDirection = true, PreferDepthRangeZeroToOne = true, SwapchainSrgbFormat = false }; GraphicsDevice graphicsDevice = VeldridStartup.CreateGraphicsDevice( window, options, BACKEND); ResourceFactory factory = graphicsDevice.ResourceFactory; CommandList commandList = factory.CreateCommandList(); ResourceLayout basicQuadResourceLayout = factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("_texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment )//, /*new ResourceLayoutElementDescription("_texture", * ResourceKind.TextureReadOnly, * ShaderStages.Fragment * )*/ ) ); GraphicsPipelineDescription mainPipelineDescription = new( BlendStateDescription.SingleAlphaBlend, new DepthStencilStateDescription( depthTestEnabled: false, depthWriteEnabled: false, comparisonKind: ComparisonKind.Never), new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false), PrimitiveTopology.TriangleStrip, new ShaderSetDescription( new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription( "vPos", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2 ), new VertexElementDescription( "fUv", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2 ) ) }, factory.CreateFromSpirv(new(ShaderStages.Vertex, Encoding.UTF8.GetBytes(@" #version 450 precision highp float; layout(location = 0) in vec2 in_pos; layout(location = 1) in vec2 in_uv; layout(location = 0) out vec2 out_uv; void main() { //gl_Position = vec4(vPos.x, vPos.y, vPos.z, 1.0); gl_Position = vec4(in_pos, 0, 1); //fUv = 0.5 * vPos.xy + vec2(0.5,0.5); out_uv = in_uv; } "), "main"), new ShaderDescription(ShaderStages.Fragment, Encoding.UTF8.GetBytes(@" #version 450 precision highp float; layout(set=0, binding = 0) uniform sampler2D _texture; //layout(binding = 0) uniform sampler _sampler; //layout(binding = 1) uniform texture2D _texture; layout(location = 0) in vec2 out_uv; layout(location = 0) out vec4 out_Color; void main() { //out_Color = texture(sampler2D(_texture, _sampler), out_uv); out_Color = texture(_texture, out_uv); } "), "main")) ), new ResourceLayout[] { basicQuadResourceLayout }, graphicsDevice.SwapchainFramebuffer.OutputDescription ); Pipeline pipeline = factory.CreateGraphicsPipeline(ref mainPipelineDescription); VeldridGPUDriver gpuDriver = new(graphicsDevice); gpuDriver.CommandList = commandList; ULPlatform.SetLogger(new ULLogger() { LogMessage = (lvl, msg) => Console.WriteLine(msg) });; AppCoreMethods.ulEnablePlatformFontLoader(); ULPlatform.SetGPUDriver(gpuDriver.GetGPUDriver()); var c = new ULConfig() { ForceRepaint = false, CachePath = "./cache/" }; Console.WriteLine(c.CachePath); Renderer renderer = new(c); View view = new(renderer, Width, Height, new ULViewConfig() { IsAccelerated = true, IsTransparent = true }); //View cpuView = new(renderer, Width, Height, TRANSPARENT, Session.DefaultSession(renderer), true); const string url = "https://en.key-test.ru/"; //"https://github.com/SupinePandora43"; view.URL = url; //cpuView.URL = url; WebRequest request = WebRequest.CreateHttp("https://raw.githubusercontent.com/SupinePandora43/UltralightNet/ulPath_pipelines/SilkNetSandbox/assets/index.html"); var response = request.GetResponse(); var responseStream = response.GetResponseStream(); StreamReader reader = new(responseStream); string htmlText = reader.ReadToEnd(); //view.HTML = htmlText; //cpuView.HTML = htmlText; /*Texture cpuTexture = null; * ResourceSet cpuTextureResourceSet = null; * * void CreateCPUTexture() * { * cpuTexture = factory.CreateTexture(new TextureDescription(cpuView.Width, cpuView.Height, 1, 1, 1, PixelFormat.B8_G8_R8_A8_UNorm, TextureUsage.Sampled, TextureType.Texture2D)); * cpuTextureResourceSet = factory.CreateResourceSet(new ResourceSetDescription(basicQuadResourceLayout, cpuTexture)); * } * * CreateCPUTexture(); */ int x = 0; int y = 0; bool cpu = false; window.MouseMove += (mm) => { x = (int)mm.MousePosition.X; y = (int)mm.MousePosition.Y; ULMouseEvent mouseEvent = new( ULMouseEvent.ULMouseEventType.MouseMoved, x, y, ULMouseEvent.Button.None ); view.FireMouseEvent(mouseEvent); //cpuView.FireMouseEvent(mouseEvent); }; window.MouseDown += (md) => { Console.WriteLine($"Mouse Down {md.Down} {md.MouseButton}"); if (md.MouseButton is MouseButton.Right) { cpu = !cpu; } if (md.MouseButton is MouseButton.Button1) { view.GoBack(); //cpuView.GoBack(); } else if (md.MouseButton is MouseButton.Button2) { view.GoForward(); //cpuView.GoForward(); } ULMouseEvent mouseEvent = new( ULMouseEvent.ULMouseEventType.MouseDown, x, y, md.MouseButton switch { MouseButton.Left => ULMouseEvent.Button.Left, MouseButton.Right => ULMouseEvent.Button.Right, MouseButton.Middle => ULMouseEvent.Button.Middle, _ => ULMouseEvent.Button.None } ); view.FireMouseEvent(mouseEvent); //cpuView.FireMouseEvent(mouseEvent); }; window.MouseUp += (mu) => { Console.WriteLine($"Mouse up {mu.Down} {mu.MouseButton}"); ULMouseEvent mouseEvent = new( ULMouseEvent.ULMouseEventType.MouseUp, x, y, mu.MouseButton switch { MouseButton.Left => ULMouseEvent.Button.Left, MouseButton.Right => ULMouseEvent.Button.Right, MouseButton.Middle => ULMouseEvent.Button.Middle, _ => ULMouseEvent.Button.None } ); view.FireMouseEvent(mouseEvent); //cpuView.FireMouseEvent(mouseEvent); }; window.MouseWheel += (mw) => { ULScrollEvent scrollEvent = new() { type = ULScrollEvent.ScrollType.ByPixel, deltaY = (int)mw.WheelDelta * 100 }; view.FireScrollEvent(scrollEvent); //cpuView.FireScrollEvent(scrollEvent); }; // idk why view.Focus(); //cpuView.Focus(); window.KeyDown += (kd) => { view.FireKeyEvent(kd.ToULKeyEvent()); //cpuView.FireKeyEvent(kd.ToULKeyEvent()); }; window.KeyUp += (ku) => { view.FireKeyEvent(ku.ToULKeyEvent()); //cpuView.FireKeyEvent(ku.ToULKeyEvent()); }; window.Resized += () => { graphicsDevice.ResizeMainWindow((uint)window.Width, (uint)window.Height); view.Resize((uint)window.Width, (uint)window.Height); //cpuView.Resize((uint)window.Width, (uint)window.Height); //CreateCPUTexture(); }; DeviceBuffer quadV = factory.CreateBuffer(new(4 * 4 * 4, BufferUsage.VertexBuffer)); graphicsDevice.UpdateBuffer(quadV, 0, new Vector4[] { new(-1, 1f, 0, 0), new(1, 1, 1, 0), new(-1, -1, 0, 1), new(1, -1, 1, 1), }); DeviceBuffer quadI = factory.CreateBuffer(new(sizeof(short) * 4, BufferUsage.IndexBuffer)); graphicsDevice.UpdateBuffer(quadI, 0, new short[] { 0, 1, 2, 3 }); Stopwatch stopwatch = new(); stopwatch.Start(); view.SetDOMReadyCallback((user_data, caller, frame_id, is_main_frame, url) => { Console.WriteLine("Dom is ready"); // view.EvaluateScript("window.location = \"https://heeeeeeeey.com/\"", out string exception); view.SetDOMReadyCallback(null); }); IntPtr rendererPtr = renderer.Ptr; uint frame_of_second = 0; framerateStopwatch.Restart(); uint fps = 0; while (window.Exists) { if (framerateStopwatch.ElapsedMilliseconds / 1000 >= 1) { fps = frame_of_second; Console.WriteLine(fps); frame_of_second = 0; framerateStopwatch.Restart(); } commandList.Begin(); //Console.WriteLine("Update"); Methods.ulUpdate(rendererPtr); gpuDriver.time = stopwatch.ElapsedTicks / 1000f; //Console.WriteLine("Render"); Methods.ulRender(rendererPtr); renderer.Render(); //Console.WriteLine("Done"); /*ULSurface surface = cpuView.Surface; * ULBitmap bitmap = surface.Bitmap; * * ULIntRect dirty = surface.DirtyBounds; * * IntPtr pixels = bitmap.LockPixels(); * uint rowBytes = bitmap.RowBytes; * uint height = bitmap.Height; * uint width = bitmap.Width; * uint bpp = bitmap.Bpp; * if (rowBytes == width * bpp) * { * graphicsDevice.UpdateTexture(cpuTexture, pixels, (uint)bitmap.Size, 0, 0, 0, width, height, 1, 0, 0); * } * else * { * Console.WriteLine("stride"); * graphicsDevice.UpdateTexture(cpuTexture, Unstrider.Unstride(pixels, width, cpuView.Height, bpp, rowBytes - (width * bpp)), 0, 0, 0, width, height, 1, 0, 0); * } * surface.ClearDirtyBounds(); * bitmap.UnlockPixels(); */ //gpuDriver.Render(); commandList.SetPipeline(pipeline); commandList.SetFramebuffer(graphicsDevice.SwapchainFramebuffer); commandList.SetFullViewports(); //commandList.ClearColorTarget(0, new RgbaFloat(MathF.Sin(stopwatch.Elapsed.Milliseconds / 100f), 255, 0, 255)); //commandList.ClearColorTarget(0, RgbaFloat.Blue); if (TRANSPARENT) { commandList.ClearColorTarget(0, RgbaFloat.Clear); } commandList.SetVertexBuffer(0, quadV); commandList.SetIndexBuffer(quadI, IndexFormat.UInt16); //if (cpu) //{ //commandList.SetGraphicsResourceSet(0, cpuTextureResourceSet); //} //else commandList.SetGraphicsResourceSet(0, gpuDriver.GetRenderTarget(view)); commandList.DrawIndexed( 4, 1, 0, 0, 0 ); commandList.End(); graphicsDevice.SubmitCommands(commandList); graphicsDevice.SwapBuffers(); window.PumpEvents(); frame_of_second++; // Thread.Sleep(1000 / 60 / 10); // ~600 fps } renderer.Dispose(); }
static void Main() { unsafe { ULPlatform.SetLogger(new _ULLogger() { LogMessage = &Log }); } // AppCoreMethods.ulEnableDefaultLogger("./ullog.txt"); ULFileSystem fs = new(); Dictionary <int, FileStream> files = new(); int last = 0; string dir = Path.GetDirectoryName(typeof(Program).Assembly.Location); Console.WriteLine(dir); fs.FileExists = (path) => { Console.WriteLine($"FileExists {path}"); return(File.Exists(Path.Combine(dir, path))); }; fs.OpenFile = (path, for_writing) => { Console.WriteLine($"OpenFile {path}"); FileStream f = File.Open(Path.Combine(dir, path), FileMode.Open, for_writing ? FileAccess.ReadWrite : FileAccess.Read); int id = last++; files[id] = f; return(id); }; fs.GetFileSize = (int id, out long result) => { Console.WriteLine($"GetFileSize {id}"); result = files[id].Length; return(true); }; fs.CloseFile = (id) => { Console.WriteLine($"CloseFile {id}"); files[id].Close(); files[id] = null; }; fs.GetFileMimeType = (string path, out string result) => { Console.WriteLine($"GetFileMimeType {path}"); if (path.EndsWith("js")) { result = "application/javascript"; } else if (path.EndsWith("css")) { result = "text/css"; } else if (path.EndsWith("dat")) { result = "application/octet-stream"; } else { result = "application/octet-stream"; } return(true); }; fs.ReadFromFile = (id, data, length) => { Console.WriteLine($"ReadFromFile {id} {data.Length} {length}"); return((long)files[id].Read(data));; }; //AppCoreMethods.ulEnablePlatformFileSystem(Path.GetDirectoryName(typeof(Program).Assembly.Location)); ULPlatform.SetFileSystem(fs); AppCoreMethods.ulEnablePlatformFontLoader(); ULConfig cfg = new(); Renderer renderer = new(cfg); View view = new(renderer, 512, 512); Console.WriteLine("done"); ULPlatform.Free(); }